1

我正在尝试使用 Python 的 Pymoo 库设置我的优化,我正在使用他们的“入门”指南,但传递了我自己的独立变量,也没有使用约束。我使用指南中的示例函数得到了相同的结果(我在下面的代码中将它们注释掉了)。

这是代码:

class MyProblem(Problem): 
    
    def __init__(self,total,G,t):                      
        super().__init__(n_var = 3,  # 2 in the case of the example from guide
                         n_obj = 2, 
                         n_constr = 0, 
                         #xl = np.array([-1.0,0.0]),    # for example from guide
                         #xu = np.array([1.0, 10.0]),
                         xl = np.array([-1.0,0.0, -1.0]), 
                         xu = np.array([1.0, 10.0, 1.0]),
                         elementwise_evaluation = True)
        self.total = total,         # my own independent variables 
        self.G = G,
        self.t = t
    
    def _evaluate(self, x, out):   
        f1 = 1/3*self.total*(1+2*((x[0]-x[2])*np.exp(-self.t/x[1]) + x[2]))
        f2 = 1/3*self.total*self.G*(1-((x[0]-x[2])*np.exp(-self.t/x[1]) + x[2]))
        #f1 = x[0]**2 + x[1]**2         # example from guide
        #f2 = (x[0]-1)**2 + x[1]**2
        
        out["F"] = np.column_stack([f1, f2])
        
elementwise_problem = MyProblem(total,G,t)

problem = elementwise_problem

algorithm = NSGA2(pop_size = 100,
                  n_offspring = 10, 
                  sampling = get_sampling('real_random'),
                  crossover = get_crossover('real_sbx', prob = 0.9, eta = 15),
                  mutation = get_mutation('real_pm',eta = 20),
                  eliminate_duplicates = True)

termination = get_termination("n_gen", 40)

# method 1
results = minimize(problem,
                   algorithm,
                   termination,
                   seed = 1, 
                   save_history = True,
                   verbose = True)

# method 2
obj = copy.deepcopy(algorithm)

obj.setup(problem, termination = termination, seed = 1)

# until the termination criterion has not been met
while obj.has_next():
    # perform an iteration of the algorithm
    obj.next()
    # access the algorithm to print some intermediate outputs
    print(f"gen: {obj.n_gen} n_nds: {len(obj.opt)} constr: {obj.opt.get('CV').min()} ideal: {obj.opt.get('F').min(axis=0)}")
    

result = obj.result()

当我在 Problem 类的 _evaluate_elementwise 方法中打印出kwargs时,确实我知道它是算法对象:

{'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at 0x00000212D12413C8>}

我很难看出它如何将算法对象作为 _evalute 的参数,它接受 (_x,_out,*args,**kwargs)。如果有人更熟悉这个包,我会非常感激帮助!

这是完整的引用:

关键字参数:{'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at 0x00000212D12413C8>} Traceback(最近一次调用最后一次):

文件“”,第 6 行,详细 = True)

文件“C:\Users\anaconda3\lib\site-packages\pymoo\optimize.py”,第 85 行,最小化 res = algorithm.solve()

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py”,第 226 行,在求解 self._solve(self.problem)

_solve self.next() 中的文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py”,第 321 行

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py”,第 243 行,在下一个 self.initialize()

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py”,第 215 行,初始化 self._initialize()

_initialize self.evaluator.eval(self.problem, pop, algorithm=self) 中的文件“C:\Users\anaconda3\lib\site-packages\pymoo\algorithms\genetic_algorithm.py”,第 81 行

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\evaluator.py”,第 78 行,在 eval self._eval(problem, pop[I], **kwargs)

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\evaluator.py”,第 97 行,在 _eval **kwargs 中)

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py”,第 284 行,in 评估 = self._evaluate_elementwise(X, calc_gradient, out, *args, **kwargs)

_evaluate_elementwise [ret.append(func(x)) for x in X] 中的文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py”,第 413 行

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py”,第 413 行,在 [ret.append(func(x)) for x in X]

文件“C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py”,第 400 行,在 func self._evaluate(_x, _out, *args, **kwargs)

TypeError:_evaluate() 得到了一个意外的关键字参数“算法”

4

1 回答 1

1

似乎您的错误是由于缺少功能而发生*args, **kwargs_evaluate。我稍微编辑了你的代码,你可以检查一下:

class MyProblem(Problem):
    
    total = 5.0        # my own independent variables 
    G = 6.0
    t = 7.0
        
    def __init__(self):                      
        super().__init__(n_var = 3,  # 2 in the case of the example from guide
                         n_obj = 2, 
                         n_constr = 0, 
                         #xl = np.array([-1.0,0.0]),    # for example from guide
                         #xu = np.array([1.0, 10.0]),
                         xl = np.array([-1.0,0.0, -1.0]), 
                         xu = np.array([1.0, 10.0, 1.0]),
                         elementwise_evaluation = True)

    
    def _evaluate(self, x, out, *args, **kwargs): # added *args, **kwargs   
        f1 = 1/3*self.total*(1+2*((x[0]-x[2])*np.exp(-self.t/x[1]) + x[2]))
        f2 = 1/3*self.total*self.G*(1-((x[0]-x[2])*np.exp(-self.t/x[1]) + x[2]))
        #f1 = x[0]**2 + x[1]**2         # example from guide
        #f2 = (x[0]-1)**2 + x[1]**2
        
        out["F"] = np.column_stack([f1, f2])
        
elementwise_problem = MyProblem()

#problem = elementwise_problem

algorithm = NSGA2(pop_size = 100,
                  n_offspring = 10, 
                  sampling = get_sampling('real_random'),
                  crossover = get_crossover('real_sbx', prob = 0.9, eta = 15),
                  mutation = get_mutation('real_pm',eta = 20),
                  eliminate_duplicates = True)

termination = get_termination("n_gen", 40)

# method 1
results = minimize(elementwise_problem,
                   algorithm,
                   termination,
                   seed = 1, 
                   save_history = True,
                   verbose = True)
于 2021-07-15T11:56:16.217 回答