Exemplo n.º 1
0
# -*- coding: utf-8 -*-
import geatpy as ea # import geatpy

"""================================实例化问题对象=========================="""
problemName = 'WFG1'      # 问题名称
fileName = problemName    # 这里因为目标函数写在与之同名的文件里,所以文件名也是问题名称
MyProblem = getattr(__import__(fileName), problemName) # 获得自定义问题类
problem = MyProblem(3)    # 生成问题对象
"""==================================种群设置=============================="""
Encoding = 'RI'           # 编码方式
NIND = 100                # 种群规模
Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # 创建区域描述器
population = ea.Population(Encoding, Field, NIND) # 实例化种群对象(此时种群还没被初始化,仅仅是完成种群对象的实例化)
"""================================算法参数设置============================"""
myAlgorithm = ea.moea_NSGA3_templet(problem, population) # 实例化一个算法模板对象
myAlgorithm.MAXGEN = 500  # 最大进化代数
myAlgorithm.drawing = 1 # 设置绘图方式(0:不绘图;1:绘制结果图;2:绘制目标空间过程动画;3:绘制决策空间过程动画)
"""===========================调用算法模板进行种群进化=========================
调用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.getBest() # 获取真实前沿,详见Problem.py中关于Problem类的定义
Exemplo n.º 2
0
    def optimize(self):
        """===============================实例化问题对象==========================="""
        problem = MyProblem(self.M, self.maxormins, self.Dim, self.varTypes,
                            self.lb, self.ub, self.lbin, self.ubin,
                            self.f_list)  # 生成问题对象
        """=================================种群设置==============================="""
        Encoding = 'BG'  # 编码方式,使用二进制/格雷编码
        Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges,
                          problem.borders)  # 创建区域描述器
        population = ea.Population(
            Encoding, Field, self.NIND)  # 实例化种群对象(此时种群还没被初始化,仅仅是完成种群对象的实例化)
        """===============================算法参数设置============================="""
        if self.M == 1:
            myAlgorithm = ea.soea_SGA_templet(
                problem, population)  # 实例化一个算法模板对象, single objective
        else:
            myAlgorithm = ea.moea_NSGA3_templet(
                problem, population)  # 实例化一个算法模板对象, multi objective

        myAlgorithm.MAXGEN = self.MAXGEN  # 最大进化代数
        myAlgorithm.recOper.XOVR = 0.7  # 交叉概率
        myAlgorithm.mutOper.Pm = 1  # 变异概率(换算成标准变异概率为0.025)
        myAlgorithm.drawing = 0  # 设置绘图方式(0:不绘图;1:绘制结果图;2:绘制目标空间过程动画;3:绘制决策空间过程动画)
        """==========================调用算法模板进行种群进化======================="""
        if self.M == 1:  # single objective
            [population, obj_trace, var_trace] = myAlgorithm.run()  # 执行算法模板
            self.obj_trace = obj_trace  # save obj_trace for plot
            population.save()  # 把最后一代种群的信息保存到文件中
            # 输出结果
            best_gen = np.argmin(problem.maxormins *
                                 obj_trace[:, 1])  # 记录最优种群个体是在哪一代
            best_ObjV = np.array([obj_trace[best_gen, 1]])
            best_var = np.array([var_trace[best_gen]])

            if os.path.exists('best_ObjV.csv') == False:
                np.savetxt('best_ObjV.csv', best_ObjV, delimiter=',')
                np.savetxt('best_var.csv', best_var, delimiter=',')
            else:
                best_ObjV_old = self.read_csv_to_np('best_ObjV.csv')
                best_var_old = self.read_csv_to_np('best_var.csv')
                best_ObjV = np.vstack((best_ObjV_old, best_ObjV))
                best_var = np.vstack((best_var_old, best_var))
                np.savetxt('best_ObjV.csv', best_ObjV, delimiter=',')
                np.savetxt('best_var.csv', best_var, delimiter=',')

            # print('最优的目标函数值为:%s' % (best_ObjV))
            # print('最优的控制变量值为:')
            # for i in range(var_trace.shape[1]):
            #     print(var_trace[best_gen, i])
            # print('有效进化代数:%s' % (obj_trace.shape[0]))
            # print('最优的一代是第 %s 代' % (best_gen + 1))
            # print('评价次数:%s' % (myAlgorithm.evalsNum))
            # print('时间已过 %s 秒' % (myAlgorithm.passTime))
            return population
        else:  # multi objective
            NDSet = myAlgorithm.run()  # 执行算法模板,得到帕累托最优解集NDSet
            NDSet.save()  # 把结果保存到文件中
            # 输出
            # print('用时:%s 秒' % (myAlgorithm.passTime))
            # print('非支配个体数:%s 个' % (NDSet.sizes))
            # print('单位时间找到帕累托前沿点个数:%s 个' % (int(NDSet.sizes // myAlgorithm.passTime)))
            return NDSet
Exemplo n.º 3
0
import geatpy as ea  # Import geatpy
from myproblem import MyProblem  # Import MyProblem class
if __name__ == '__main__':
    """=========================Instantiate your problem=========================="""
    M = 3  # Set the number of objects.
    problem = MyProblem(M)  # Instantiate MyProblem class
    """===============================Population set=============================="""
    Encoding = 'RI'  # Encoding type.
    NIND = 100  # Set the number of individuals.
    Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges,
                      problem.borders)  # Create the field descriptor.
    population = ea.Population(
        Encoding, Field, NIND
    )  # Instantiate Population class(Just instantiate, not initialize the population yet.)
    """================================Algorithm set==============================="""
    myAlgorithm = ea.moea_NSGA3_templet(
        problem, population)  # Instantiate a algorithm class.
    myAlgorithm.MAXGEN = 500  # Set the max times of iteration.
    """===============================Start evolution=============================="""
    NDSet = myAlgorithm.run()  # Run the algorithm templet.
    """=============================Analyze the result============================="""
    PF = problem.getReferObjV()  # Get the global pareto front.
    GD = ea.indicator.GD(NDSet.ObjV, PF)  # Calculate GD
    IGD = ea.indicator.IGD(NDSet.ObjV, PF)  # Calculate IGD
    HV = ea.indicator.HV(NDSet.ObjV, PF)  # Calculate HV
    Space = ea.indicator.Spacing(NDSet.ObjV)  # Calculate Space
    print('The number of non-dominated result: %s' % (NDSet.sizes))
    print('GD: ', GD)
    print('IGD: ', IGD)
    print('HV: ', HV)
    print('Space: ', Space)
Exemplo n.º 4
0
    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