def recommendSinglePr(EXPScoreVector, RSPScoreVector, ACTScoreVector, recommendNum, candicateList): candicateNum = candicateList.__len__() # 候选者数量,也是编码的长度 problem = GAProblem(candicateNum, EXPScoreVector, RSPScoreVector, ACTScoreVector, recommendNum) """种群设置""" Encoding = 'RI' # 编码方式 NIND = 200 # 种群规模,论文上200 Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # 创建区域描述器 population = ea.Population(Encoding, Field, NIND) # 实例化种群对象 """算法参数设置""" myAlgorithm = ea.moea_NSGA2_templet(problem, population) # 实例化模板对象 myAlgorithm.MAXGEN = 100 # 最大迭代数量 myAlgorithm.drawing = 0 # 设置绘图方式(0:不绘图 1:结果图 2:过程动画) """调用模板种群进化 调用run执行算法模板,得到帕累托最优解集NDSet。NDSet是一个种群类Population的对象。 NDSet.ObjV为最优解个体的目标函数值;NDSet.Phen为对应的决策变量值。 详见Population.py中关于种群类的定义。 """ NDSet = myAlgorithm.run() # 执行算法模板,获取非支配种群 NDSet.save() # 结果保存文件 # # 输出 # print("用时:%f秒" % (myAlgorithm.passTime)) # print("评价次数:%d" % (myAlgorithm.evalsNum)) # print("非支配个体数:%d" % (NDSet.sizes)) # print('单位时间找到帕累托前沿点个数:%d' % (int(NDSet.sizes // # myAlgorithm.passTime))) # 计算指标 # PF = problem.getReferObjV() # 获取真实前沿 # if PF is not None and NDSet.sizes != 0: # GD = ea.indicator.GD(NDSet.ObjV, PF) # 计算GD指标 # IGD = ea.indicator.IGD(NDSet.ObjV, PF) # 计算IGD指标 # HV = ea.indicator.HV(NDSet.ObjV, PF) # 计算Spacing 指标 # Spacing = ea.indicator.Spacing(NDSet.ObjV) # print('GD', GD) # print('IGD', IGD) # print('HV', HV) # print('Spacing', Spacing) # # """进化过程指标追踪分析""" # if PF is not None: # metricName = [['IGD'], ['HV']] # [NDSet_trace, Metrics] = ea.indicator.moea_tracking(myAlgorithm.pop_trace, # PF, metricName, problem.maxormins) # ea.trcplot(Metrics, labels=metricName, titles=metricName) # 自己的做法 论文中没有 计算200个解每个人选的次数,按照次数依次推荐 candicateCount = np.dot(np.ones((candicateNum, NIND)), NDSet.Chrom) scores = {} for index, c in enumerate(candicateList): scores[c] = candicateCount[0][index] return [ x[0] for x in sorted(scores.items(), key=lambda d: d[1], reverse=True) [0:recommendNum] ]
def run_nsga(self): Field = ea.crtfld(self.Encoding, self.myproblem.varTypes, self.myproblem.ranges, self.myproblem.borders) population = ea.Population(self.Encoding, Field, self.NIND) myAlgorithm = ea.moea_NSGA2_templet(self.myproblem, population) myAlgorithm.MAXGEN = self.MAXGEN # 最大遗传代数 myAlgorithm.drawing = self.Drawing # 设置绘图方式 NDSet = myAlgorithm.run() NDSet.save() print('用时:%f 秒' % (myAlgorithm.passTime)) print('评价次数:%d 次' % (myAlgorithm.evalsNum)) print('非支配个体数:%d 个' % (NDSet.sizes))
# -*- coding: utf-8 -*- import geatpy as ea # import geatpy if __name__ == '__main__': """===============================实例化问题对象============================""" problemName = 'CON' # 问题名称 fileName = 'MyProblem' # 问题类所在的文件名 MyProblem = getattr(__import__(fileName), problemName) # 获得自定义问题类 problem = MyProblem() # 生成问题对象 """=================================种群设置===============================""" Encoding = 'RI' # 编码方式 NIND = 200 # 种群规模 Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # 创建区域描述器 population = ea.Population(Encoding, Field, NIND) # 实例化种群对象(此时种群还没被初始化,仅仅是完成种群对象的实例化) """===============================算法参数设置=============================""" myAlgorithm = ea.moea_NSGA2_templet(problem, population) # 实例化一个算法模板对象 myAlgorithm.MAXGEN = 50 # 最大进化代数 myAlgorithm.drawing = 1 # 设置绘图方式(0:不绘图;1:绘制结果图;2:绘制目标空间过程动画;3:绘制决策空间过程动画) """=========================调用算法模板进行种群进化========================""" NDSet = myAlgorithm.run() # 执行算法模板,得到非支配种群 NDSet.save() # 把结果保存到文件中 # 输出 print('用时:%f 秒' % (myAlgorithm.passTime)) print('评价次数:%d 次' % (myAlgorithm.evalsNum)) print('非支配个体数:%d 个' % (NDSet.sizes)) print('单位时间找到帕累托前沿点个数:%d 个' % (int(NDSet.sizes // myAlgorithm.passTime)))
if __name__ == '__main__': # 实例化问题对象 problem = MyProblem() # 构建算法 algorithm = ea.moea_awGA_templet( problem, ea.Population(Encoding='RI', NIND=50), MAXGEN=20, # 最大进化代数 logTras=0) # 表示每隔多少代记录一次日志信息 # 求解 res = ea.optimize(algorithm, verbose=False, drawing=0, outputMsg=False, drawLog=False, saveFlag=False) prophetPop = res['optPop'] algorithm = ea.moea_NSGA2_templet( problem, ea.Population(Encoding='RI', NIND=50), prophetPop=prophetPop, # 传入先验知识 MAXGEN=50, # 最大进化代数 logTras=0) # 表示每隔多少代记录一次日志信息,0表示不记录。 # 求解 res = ea.optimize(algorithm, verbose=False, drawing=1, outputMsg=True, drawLog=False, saveFlag=True) print(res)
# -*- coding: utf-8 -*- from MyProblem import MyProblem # 导入自定义问题接口 import geatpy as ea # import geatpy """ 该案例展示了一个离散决策变量的最小化目标的双目标优化问题的求解。问题的定义详见MyProblem.py。 """ if __name__ == '__main__': # 实例化问题对象 problem = MyProblem() # 构建算法 algorithm = ea.moea_NSGA2_templet( problem, ea.Population(Encoding='BG', NIND=50), MAXGEN=200, # 最大进化代数 logTras=0) # 表示每隔多少代记录一次日志信息,0表示不记录。 algorithm.mutOper.Pm = 0.2 # 修改变异算子的变异概率 algorithm.recOper.XOVR = 0.9 # 修改交叉算子的交叉概率 # 求解 res = ea.optimize(algorithm, verbose=False, drawing=1, outputMsg=True, drawLog=False, saveFlag=False) print(res)
def __init__(self, X, y, target, model, l, u, drawing=1, maxgen=100, moea=1): '''===============================实例化问题对象==================================''' self.problem = MyProblem(target=target, X=X, y=y, model=model, l=l, u=u) '''===========================种群设置==============================''' Encoding = 'RI' # 'RI':实整数编码,即实数和整数的混合编码; NIND = 100 # 种群规模 Field = ea.crtfld(Encoding, self.problem.varTypes, self.problem.ranges, self.problem.borders) # 创建区域描述器 self.population = ea.Population(Encoding, Field, NIND) '''实例化种群对象(此时种群还没被真正初始化,仅仅是生成一个种群对象)''' """=========================算法参数设置============================""" if moea == 1: self.myAlgorithm = ea.moea_NSGA2_templet( self.problem, self.population) # 实例化一个算法模板对象 elif moea == 2: self.myAlgorithm = ea.moea_NSGA2_DE_templet( self.problem, self.population) elif moea == 3: self.myAlgorithm = moea_NSGA2_toZero(self.problem, self.population) elif moea == 4: self.myAlgorithm = moea_NSGA2_DE_toZero(self.problem, self.population) elif moea == 5: self.myAlgorithm = ea.moea_NSGA3_templet(self.problem, self.population) elif moea == 6: self.myAlgorithm = ea.moea_NSGA3_DE_templet( self.problem, self.population) elif moea == 7: self.myAlgorithm = ea.moea_awGA_templet(self.problem, self.population) elif moea == 8: self.myAlgorithm = ea.moea_RVEA_templet(self.problem, self.population) elif moea == 9: self.myAlgorithm = moea_NSGA2_10p_toZero(self.problem, self.population) elif moea == 10: self.myAlgorithm = moea_NSGA2_20p_toZero(self.problem, self.population) elif moea == 11: self.myAlgorithm = moea_NSGA2_30p_toZero(self.problem, self.population) elif moea == 12: self.myAlgorithm = moea_NSGA2_random10p_toZero( self.problem, self.population) elif moea == 13: self.myAlgorithm = moea_NSGA2_random20p_toZero( self.problem, self.population) elif moea == 14: self.myAlgorithm = moea_NSGA2_random30p_toZero( self.problem, self.population) else: print('error moea method!!') self.myAlgorithm.MAXGEN = maxgen # 设置最大遗传代数 self.myAlgorithm.drawing = drawing