anyoptimization / pymoo

NSGA2, NSGA3, R-NSGA3, MOEAD, Genetic Algorithms (GA), Differential Evolution (DE), CMAES, PSO
https://pymoo.org
Apache License 2.0
2.28k stars 390 forks source link

'Problem Error: G can not be set, expected shape (100, 1) but provided (100, 2)' #364

Closed hangcyelk closed 1 year ago

hangcyelk commented 1 year ago

Hi, I'm just touching the pymoo lib and tried to fit my problem: a three variable two objectives optimization. I've attached my code herein, it basically stopped at the "seed=1" and returned the problem error. Appreciate your help!

import numpy as np

from pymoo.algorithms.moo.nsga2 import NSGA2 from pymoo.core.problem import ElementwiseProblem from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter

''' mathmatic problem min f1(x)=255x1+255x2-170 min f2(x)=255x2-85 min f3(x)=255x2+255x3-170 s.t. x1+x2+x3 - 1<=0 0.9-(x1+X2+x3)<=0 (I tried to make x1+x2+x3 ==1 but it seems like not doable) 0<=x1<=1 0<=x2<=1 0<=x3<=1 x∈R '''

class MyProblem(ElementwiseProblem):

def __init__(self):
    super().__init__(n_var=3,
                     n_obj=3,
                     n_ieq_constr=1,
                     xl=np.array([0,0,0]),
                     xn=np.array([1,1,1]))

def _evaluate(self, x, out, *args, **kwargs):
    f1 = 255 * x[0] + 255*x[2] -170
    f2 = 255* x[2] -85
    f3 = 255*x[1]+255*x[2]-170

    g1 = x[0] + x[1] +x[2] -1
    g2 = 0.9 - (x[0] + x[1] +x[2])

    out["F"] = [f1, f2, f3]
    out["G"] = [g1, g2]

problem = MyProblem()

algorithm = NSGA2(pop_size=100)

res = minimize(problem, algorithm, ("n_gen", 100), verbose=False, seed=1)

plot = Scatter() plot.add(res.F, edgecolor="red", facecolor="none") plot.show()

bruscalia commented 1 year ago

Hi! It seems you passed the wrong number on inequalities when instantiating the problem n_ieq_constr=1. Just fix it to 2 and your problem should work well :)

blankjul commented 1 year ago

@bruscalia thanks for replying!

Did this resolve the issue?

hangcyelk commented 1 year ago

Thanks for pointing that out @Bruno! However I think the 'res' turned out to be nonetype which gave me another error. "'NoneType' object is not subscriptable" when I run this program

Any idea of what's happening?

Thanks, eli

Julian Blank @.***> 于2023年1月12日周四 00:14写道:

@bruscalia https://github.com/bruscalia thanks for replying!

Did this resolve the issue?

— Reply to this email directly, view it on GitHub https://github.com/anyoptimization/pymoo/issues/364#issuecomment-1379824965, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGHEQ22SJC6B6KFLSDZXGQ3WR6HM3ANCNFSM6AAAAAATWDX5UU . You are receiving this because you authored the thread.Message ID: @.***>

bruscalia commented 1 year ago

Hi @hangcyelk! I'll try to run your code locally to investigate the error. For now, I believe you could try to handle your constraints with a Repair operator. Take a look at how @blankjul did it in this portfolio allocation problem https://pymoo.org/case_studies/portfolio_allocation.html