使用 pymoo 优化函数时,如果我包含 if 语句,则会收到如下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我无法理解此错误的原因,它似乎与 numpy 有关
简化代码如下:
import numpy as np from pymoo.model.problem import Problem
#function with 2 variables to be maximised but not over a critical figure
def joist_h_des(b, h):
defCHK = 581818181/(b*h**3/12)/50
#ensures return value is as close to 1 as possible but does not exceed
if defCHK > 1:
defCHK = 0
else:
defCHK = defCHK
return defCHK
#multi objective optimisation using pymoo class MyProblem(Problem):
def __init__(self):
super().__init__(n_var=2,
n_obj=2,
n_constr=2,
xl=np.array([25,100]),
xu=np.array([100,300]))
def _evaluate(self, X, out, *args, **kwargs):
f1 = -joist_h_des(X[:, 0], X[:, 1])
f2 = X[:, 0] * X[:, 1]
g1 = ((X[:, 0] * X[:, 1])-200000)
g2 = - (X[:, 0] * X[:, 1])-60000
out["F"] = np.column_stack([f1, f2])
out["G"] = np.column_stack([g1, g2])
problem = MyProblem()
#3. Initialization of an Algorithm
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.factory import get_sampling, get_crossover, get_mutation
algorithm = NSGA2(
pop_size=40,
n_offsprings=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 )
#4. Definition of a Termination Criterion
from pymoo.factory import get_termination
termination = get_termination("n_gen", 40)
#5. Optimize from pymoo.optimize import minimize
res = minimize(problem,
algorithm,
termination,
seed=1,
save_history=True,
verbose=True)
这会产生:
Traceback (most recent call last):
File "C:/Users/richa/PycharmProjects/untitled3/basic.py", line 70, in <module>
verbose=True)
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\optimize.py", line 85, in minimize
res = algorithm.solve()
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 226, in solve
self._solve(self.problem)
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 321, in _solve
self.next()
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 243, in next
self.initialize()
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\algorithm.py", line 215, in initialize
self._initialize()
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\algorithms\genetic_algorithm.py", line 81, in _initialize
self.evaluator.eval(self.problem, pop, algorithm=self)
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\evaluator.py", line 78, in eval
self._eval(problem, pop[I], **kwargs)
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\evaluator.py", line 97, in _eval
**kwargs)
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\problem.py", line 286, in evaluate
out = self._evaluate_batch(X, calc_gradient, out, *args, **kwargs)
File "C:\Users\richa\anaconda3\lib\site-packages\pymoo\model\problem.py", line 385, in _evaluate_batch
self._evaluate(X, out, *args, **kwargs)
File "C:/Users/richa/PycharmProjects/untitled3/basic.py", line 28, in _evaluate
f1 = -joist_h_des(X[:, 0], X[:, 1])
File "C:/Users/richa/PycharmProjects/untitled3/basic.py", line 9, in joist_h_des
if defCHK > 1:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Process finished with exit code 1
任何建议将不胜感激