Reputation: 3
I'm trying to use pymoo for a NSGA 2 multiple optimization problem, but I get a Problem error for shape expect and what is provided. here is the code:
import numpy as np
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import ElementwiseProblem
from pymoo.core.problem import Problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
class TestProblem(ElementwiseProblem):
def __init__(self):
super().__init__(n_var=3,
n_obj=2,
n_ineq_const=2,
vtype=int)
def _evaluate(self, TA, out, *args, **kwargs):
f1 = 910 * TA[0] + 2280 * TA[1] + 5500 * TA[2]
f2 = (200-TA[0])/TA[0] + (500-TA[1])/TA[1] + (100-TA[2])/TA[2]
g1 = TA[2] + TA[0] - TA[1]
g2 = TA[0] + TA[2] - 61
out["F"] = [f1, -f2]
out["G"] = np.column_stack([g1, g2])
problem = TestProblem()
algorithm = NSGA2(pop_size=100)
res = minimize(problem,
algorithm,
("n_gen", 200),
save_history=False,
verbose=False,
seed=1)
and the error I get is:
Exception: ('Problem Error: G can not be set, expected shape (100, 0) but provided (100, 1, 2)', ValueError('cannot reshape array of size 200 into shape (100,0)')) I would be thankful if you would help me with it.
I tried "np.column_stack" for a solution but the error remains.
Upvotes: 0
Views: 376
Reputation: 540
n_ieq_constr=2
(Not n_ineq_const=2
)
xl=np.array([-100, -100, -100]),
xu=np.array([100, 100, 100]),
So your full code becomes:
import numpy as np
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import ElementwiseProblem
from pymoo.core.problem import Problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
class TestProblem(ElementwiseProblem):
def __init__(self):
super().__init__(n_var=3,
n_obj=2,
n_ieq_constr=2,
xl=np.array([-100, -100, -100]),
xu=np.array([100, 100, 100]),
vtype=int)
def _evaluate(self, TA, out, *args, **kwargs):
f1 = 910 * TA[0] + 2280 * TA[1] + 5500 * TA[2]
f2 = (200-TA[0])/TA[0] + (500-TA[1])/TA[1] + (100-TA[2])/TA[2]
g1 = TA[2] + TA[0] - TA[1]
g2 = TA[0] + TA[2] - 61
out["F"] = [f1, -f2]
out["G"] = [g1, g2]
problem = TestProblem()
algorithm = NSGA2(pop_size=100)
res = minimize(problem,
algorithm,
("n_gen", 200),
save_history=False,
verbose=True,
seed=1)
plot = Scatter()
plot.add(res.F, edgecolor="red", facecolor="none")
plot.show()
Upvotes: 0