Skip to content

pymoo/constraint-not-declared

Rule constraint-not-declared
Pack pymoo
Severity error
Fix none, edit by hand
Library versions pymoo 0.6 and later

Writing out["G"] or out["H"] without declaring the number of constraints raises on the first evaluation. Pass n_ieq_constr or n_eq_constr with the number of constraints.

class Bounded(Problem):
def __init__(self):
super().__init__(n_var=2, n_obj=1, xl=0.0, xu=1.0)
super().__init__(n_var=2, n_obj=1, n_ieq_constr=1, xl=0.0, xu=1.0)
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = (x**2).sum(axis=1)
out["G"] = 0.5 - x[:, 0]

The rule reports out["G"] written in a class that never passes n_ieq_constr, and out["H"] in a class that never passes n_eq_constr. Both counts default to 0, and the first evaluation raises Exception, "Problem Error: G can not be set, expected shape (2, 0) but provided (2,)". pymoo 0.5.0 ignored such a constraint without an error. The class must call super().__init__ with keywords only, so a class that passes **kwargs on is left alone.

xpd has no automatic fix, so make the change by hand.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This writes out["G"], and the class never passes n_ieq_constr, which defaults to 0. Evaluated on 2 solutions, it raised Exception, "Problem Error, G can not be set, expected shape (2, 0) but provided (2,)". Pass n_ieq_constr with the number of inequality constraints to super().__init__.

Browse the other rules of the pymoo pack.