话不多说,直接上代码:
import numpy as np
import random
class Population:
def __init__(self, min_range, max_range, dim, factor, rounds, size, object_func, CR):
self.min_range = min_range
self.max_range = max_range
self.dimension = dim
self.factor = factor
self.rounds = rounds
self.size = size
self.cur_round = 1
self.CR = CR
self.get_object_function_value = object_func
# 初始化种群
self.individuality = [np.array([random.uniform(self.min_range, self.max_range) for s in range(self.dimension)]) for tmp in range(size)]
self.object_function_values = [self.get_object_function_value(v) for v in self.individuality]
self.mutant = None
def mutate1(self):
self.mutant = []
for i in range(self.size):
r0, r1, r2 = 0, 0, 0
while r0 == r1 or r1 == r2 or r0 == r2 or r0 == i:
r0 = random.randint(0, self.size-1)
r1 = random.randint(0, self.size-1)
r2 = random.randint(0, self.size-1)
tmp = self.individuality[r0] + (self.individuality[r1] - self.individuality[r2]) * self.factor
for t in range(self.dimension):
if tmp[t] > self.max_range or tmp[t] < self.min_range:
tmp[t] = random.uniform(self.min_range, self.max_range)
self.mutant.append(tmp)
def mutate2(self):
self.mutant = []
for i in range(self.size):
r0, r1, r2, r3, r4 = 0, 0, 0, 0, 0
while r0 == r1 or r1 == r2 or r0 == r2 or r0 == i or r0==r3 or r0==r4 or r2==r3 or r2==r4 or r3==r4:
r0 = random.randint(0, self.size-1)
r1 = random.randint(0, self.size-1)
r2 = random.randint(0, self.size-1)
r3 = random.randint(0, self.size-1)
r4 = random.randint(0, self.size-1)
tmp = self.individuality[r0] + (self.individuality[r1] - self.individuality[r2]) * self.factor + (self.individuality[r3] - self.individuality[r4]) * self.factor
for t in range(self.dimension):
if tmp[t] > self.max_range or tmp[t] < self.min_range:
tmp[t] = random.uniform(self.min_range, self.max_range)
self.mutant.append(tmp)
def mutate3(self):
self.mutant = []
for i in range(self.size):
r0, r1, r2 = 0, 0, 0
while r0 == r1 or r1 == r2 or r0 == r2 or r0 == i:
r0 = random.randint(0, self.size-1)
r1 = random.randint(0, self.size-1)
r2 = random.randint(0, self.size-1)
m = min(self.object_function_values)
i = self.object_function_values.index(m)
tmp = self.individuality[r0] + (self.individuality[r1] - self.individuality[r2]) * self.factor + (self.individuality[i] - self.individuality[r0]) * self.factor
for t in range(self.dimension):
if tmp[t] > self.max_range or tmp[t] < self.min_range:
tmp[t] = random.uniform(self.min_range, self.max_range)
self.mutant.append(tmp)
def crossover_and_select(self):
for i in range(self.size):
Jrand = random.randint(0, self.dimension)
for j in range(self.dimension):
if random.random() > self.CR and j != Jrand:
self.mutant[i][j] = self.individuality[i][j]
tmp = self.get_object_function_value(self.mutant[i])
if tmp < self.object_function_values[i]:
self.individuality[i] = self.mutant[i]
self.object_function_values[i] = tmp
def evolution1(self):
while self.cur_round < self.rounds:
self.mutate1()
self.crossover_and_select()
self.cur_round = self.cur_round + 1
m = min(self.object_function_values)
i = self.object_function_values.index(m)
print("最佳个体:" + str(self.individuality[i]))
print("目标函数值:" + str(m))
def evolution2(self):
while self.cur_round < self.rounds:
self.mutate2()
self.crossover_and_select()
self.cur_round = self.cur_round + 1
m = min(self.object_function_values)
i = self.object_function_values.index(m)
print("最佳个体:" + str(self.individuality[i]))
print("目标函数值:" + str(m))
def evolution3(self):
while self.cur_round < self.rounds:
self.mutate3()
self.crossover_and_select()
self.cur_round = self.cur_round + 1
m = min(self.object_function_values)
i = self.object_function_values.index(m)
print("最佳个体:" + str(self.individuality[i]))
print("目标函数值:" + str(m))
#测试部分
if __name__ == "__main__":
def f(v):
f=0
for i in v:
f+=i**2
return f
#三种参数选择策略(随机选择);p1,p2,p3的差异在于factor和CR
p1 = Population(min_range=-10, max_range=10, dim=30, factor=1, rounds=800, size=100, object_func=f,CR=0.1)
p2 = Population(min_range=-10, max_range=10, dim=30, factor=1, rounds=800, size=100, object_func=f,CR=0.9)
p3 = Population(min_range=-10, max_range=10, dim=30, factor=0.8, rounds=800, size=100, object_func=f,CR=0.2)
ps=[p1,p2,p3]
def random_(ps):
a=random.randint(0,2)
if a==0:
print('参数 F=1, CR=0.1')
elif a==1:
print('参数 F=1, CR=0.9')
else:
print('参数 F=0.8, CR=0.2')
p=ps[a]
return p
for i in range(3):
p=random_(ps)
if i==0:
print('变异策略1结果:')
p.evolution1()
elif i==1:
print('变异策略2结果:')
p.evolution2()
else:
print('变异策略3结果:')
p.evolution3()