Reputation: 452
I am trying to set up my optimization with Python's Pymoo library, I am using their 'getting started' guide but passing my own independent variables and also not using constraints. I get the same using the example functions from the guide (I have commented them out in the code below).
Here is the code:
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()
When I print out the kwargs in the _evaluate_elementwise method in the Problem class, indeed I get it is the algorithm object:
{'algorithm': <pymoo.algorithms.nsga2.NSGA2 object at 0x00000212D12413C8>}
I struggle to see how it might be taking the algorithm object as an argument to _evalute, which accepts (_x,_out,*args,**kwargs). If anyone is more familiar with this package I'd appreciate the help tremendously !
Here is the full trackeback:
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'
Upvotes: 1
Views: 1648
Reputation: 186
It seems like your error occurs because of the lack of *args, **kwargs
in _evaluate
function. I edited your code a bit, you may check it:
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)
Upvotes: 1