代码(aimfuc)
通过sga_real_templet的简单应用了解了实值编码。
现在使用sga_new_real_templet模板实现一个简单的应用。
geatpy库的new模板与非new模板的区别,就是增加了精英保留机制。
import numpy as np
def aimfuc(Phen, LegV):
x = Phen[:, [0]]
f = x + 10 * np.sin(5 * x) + 7 * np.cos(4 * x)
return [f, LegV]
代码(main)
import numpy as np
import geatpy as ga
AIM_M = __import__('aimfuc')
x = [0, 10]
b = [1, 1]
# codes = [1]
precisions = [6]
# scales = [0]
ranges = np.vstack([x]).T
borders = np.vstack([b]).T
fieldd = ga.crtfld(ranges, borders, precisions) # , codes, scales)
problem = 'R'
maxormin = -1
MAXGEN = 100
NIND = 50
SUBPOP = 1
GGAP = 0.65
pm = 0.05
selectStyle = 'rws'
recombinStyle = 'xovsp'
drawing = 1
[pop_trace, var_trcae, times] = ga.sga_new_real_templet(AIM_M,