Pymoo Python: TypeError: _evaluate() got an unexpected keyword argument 'algorithm'
Pymoo Python: TypeError: _evaluate() got an unexpected keyword argument 'algorithm'
我正在尝试使用 Python 的 Pymoo 库进行优化,我使用的是他们的 'getting started' 指南,但传递了我自己的自变量,也不使用约束。我使用指南中的示例函数得到了相同的结果(我在下面的代码中将它们注释掉了)。
代码如下:
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()
当我在问题 class 的 _evaluate_elementwise 方法中打印出 kwargs 时,实际上我得到的是算法对象:
{'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at
0x00000212D12413C8>}
我很难理解它如何将算法对象作为 _evalute 的参数,它接受 (_x,_out,*args,**kwargs)。如果有人更熟悉这个软件包,我将非常感谢您的帮助!
这是完整的通告:
Keyword args: {'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at
0x00000212D12413C8>} Traceback (most recent call last):
File "", line 6, in verbose = True)
File "C:\Users\anaconda3\lib\site-packages\pymoo\optimize.py", line
85, in minimize res = algorithm.solve()
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py",
line 226, in solve self._solve(self.problem)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py",
line 321, in _solve self.next()
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py",
line 243, in next self.initialize()
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py",
line 215, in initialize self._initialize()
File
"C:\Users\anaconda3\lib\site-packages\pymoo\algorithms\genetic_algorithm.py",
line 81, in _initialize self.evaluator.eval(self.problem, pop,
algorithm=self)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\evaluator.py",
line 78, in eval self._eval(problem, pop[I], **kwargs)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\evaluator.py",
line 97, in _eval
**kwargs)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py",
line 284, in evaluate out = self._evaluate_elementwise(X,
calc_gradient, out, *args, **kwargs)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py",
line 413, in _evaluate_elementwise [ret.append(func(x)) for x in X]
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py",
line 413, in [ret.append(func(x)) for x in X]
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py",
line 400, in func self._evaluate(_x, _out, *args, **kwargs)
TypeError: _evaluate() got an unexpected keyword argument 'algorithm'
您的错误似乎是因为 _evaluate
函数中缺少 *args, **kwargs
。我稍微编辑了你的代码,你可以检查一下:
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)
我正在尝试使用 Python 的 Pymoo 库进行优化,我使用的是他们的 'getting started' 指南,但传递了我自己的自变量,也不使用约束。我使用指南中的示例函数得到了相同的结果(我在下面的代码中将它们注释掉了)。
代码如下:
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()
当我在问题 class 的 _evaluate_elementwise 方法中打印出 kwargs 时,实际上我得到的是算法对象:
{'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at 0x00000212D12413C8>}
我很难理解它如何将算法对象作为 _evalute 的参数,它接受 (_x,_out,*args,**kwargs)。如果有人更熟悉这个软件包,我将非常感谢您的帮助!
这是完整的通告:
Keyword args: {'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at 0x00000212D12413C8>} Traceback (most recent call last):
File "", line 6, in verbose = True)
File "C:\Users\anaconda3\lib\site-packages\pymoo\optimize.py", line 85, in minimize res = algorithm.solve()
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 226, in solve self._solve(self.problem)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 321, in _solve self.next()
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 243, in next self.initialize()
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 215, in initialize self._initialize()
File "C:\Users\anaconda3\lib\site-packages\pymoo\algorithms\genetic_algorithm.py", line 81, in _initialize self.evaluator.eval(self.problem, pop, algorithm=self)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\evaluator.py", line 78, in eval self._eval(problem, pop[I], **kwargs)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\evaluator.py", line 97, in _eval **kwargs)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py", line 284, in evaluate out = self._evaluate_elementwise(X, calc_gradient, out, *args, **kwargs)
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py", line 413, in _evaluate_elementwise [ret.append(func(x)) for x in X]
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py", line 413, in [ret.append(func(x)) for x in X]
File "C:\Users\anaconda3\lib\site-packages\pymoo\model\problem.py", line 400, in func self._evaluate(_x, _out, *args, **kwargs)
TypeError: _evaluate() got an unexpected keyword argument 'algorithm'
您的错误似乎是因为 _evaluate
函数中缺少 *args, **kwargs
。我稍微编辑了你的代码,你可以检查一下:
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)