mutation(POP)
return POP
if name ==‘main’:
pop=implement()
def func(x):
return x+16np.sin(5x)+10np.cos(4x)
x=np.linspace(-10,10,100000)
y=func(x)
scatter_x=np.array([ind.x for ind in pop])
scatter_y=np.array([ind.fitness for ind in pop])
best=sorted(pop,key=lambda pop: pop.fitness,reverse=True)[0]
print(‘best_y’,best.x)
print(‘best_y’,best.fitness)
plt.plot(x,y)
plt.scatter(best.x,best.fitness,c=‘g’,label=‘best point’)
plt.legend()
plt.show()
(2)结果
3、遗传算法带约束