n = 5 lb = -5.12 * np.ones((1, n)) ub = 5.12 * np.ones((1, n)) ranges = np.vstack([lb, ub]) # 生成自变量的范围矩阵 lbin = np.ones((1, n)) ubin = np.ones((1, n)) borders = np.vstack([lbin, ubin]) # 生成自变量的边界矩阵(1表示变量的区间是闭区间) precisions = [ 1 ] * n # 根据crtfld的函数特性,这里需要设置精度为任意正值,否则在生成区域描述器时会默认为整数编码,并对变量范围作出一定调整 FieldDR = ga.crtfld(ranges, borders, precisions) # 生成区域描述器 # 调用编程模板 [pop_trace, var_trace, times] = ga.sga_new_real_templet(AIM_M, 'aimfuc', None, None, FieldDR, problem='R', maxormin=1, MAXGEN=1000, NIND=100, SUBPOP=1, GGAP=0.9, selectStyle='sus', recombinStyle='xovdprs', recopt=None, pm=None, distribute=True, drawing=1)
调用算法模板时可以设置drawing=2,此时算法模板将在种群进化过程中绘制动画,但注意执行前要在Python控制台执行命令matplotlib qt5。 """ import numpy as np import geatpy as ga # 获取函数接口地址 AIM_M = __import__('aimfuc') # 变量设置 ranges = np.vstack([np.zeros((1, 4)), np.ones((1, 4))]) # 生成自变量的范围矩阵 borders = np.vstack([np.ones((1, 4)), np.ones((1, 4))]) # 生成自变量的边界矩阵 FieldDR = ga.crtfld(ranges, borders) # 生成区域描述器 # 调用编程模板(设置problem = 'I'处理离散型变量问题,详见该算法模板的源代码) [pop_trace, var_trace, times] = ga.sga_new_real_templet(AIM_M, 'aimfuc', None, None, FieldDR, problem='I', maxormin=-1, MAXGEN=50, NIND=10, SUBPOP=1, GGAP=0.9, selectStyle='sus', recombinStyle='xovdp', recopt=0.9, pm=0.1, distribute=True, drawing=1)
if __name__ == "__main__": AIM_M = __import__('main') # 获取函数接口所在文件的地址 # 变量设置 ranges = np.vstack( [32 * np.ones((1, len(words))), 122 * np.ones( (1, len(words)))]) # 生成自变量的范围矩阵 borders = np.vstack( [np.ones((1, len(words))), 122 * np.ones( (1, len(words)))]) # 生成自变量的边界矩阵 FieldDR = ga.crtfld(ranges, borders) # 生成区域描述器 # 调用编程模板 [pop_trace, var_trace, times] = ga.sga_new_real_templet(AIM_M, 'aim', None, None, FieldDR, problem='I', maxormin=1, MAXGEN=2000, NIND=50, SUBPOP=1, GGAP=0.9, selectStyle='sus', recombinStyle='xovdp', recopt=0.9, pm=0.1, drawing=1) # 输出结果 for num in var_trace[np.argmin(pop_trace[:, 1]), :]: print(chr(int(num)), end='')