pymoo
pymoo copied to clipboard
Equality constraints do not work
Hello, I was attempting to add an equality constraint to make sure two variables (x0 and x2) have the same value. But from the results, it seems the equality constraint does not work.
from pymoo.core.problem import Problem
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
class OptimProblem(Problem):
def __init__(self):
super().__init__(n_var=3, n_obj=2, n_eq_constr=1, xl=-2, xu=2)
def _evaluate(self, x, out, *args, **kwargs):
f_list, g_list = [], []
F1 = (x[:,0]-1) ** 2 + x[:,1] ** 2 + x[:,2] ** 2
F2 = x[:,0] ** 2 + (x[:,1]-1) ** 2 + x[:,2] ** 2
out["F"] = np.column_stack([F1, F2])
out["H"] = np.array(x[:,0] - x[:,2]).reshape(-1, 1)
algorithm = NSGA2()
problem = OptimProblem()
res = minimize(problem, algorithm, seed=42, save_history=True, verbose=True)
print("-> Best solution found: %s" % res.X)
print("-> Function value: %s" % res.F)
Part of the solutions are provided below, and we can find x0 and x2 do not have the same value.
-> Best solution found: [[ 9.99392935e-01 -2.40933039e-04 1.64089449e-03]
[ 1.00331634e-03 1.00001028e+00 3.09432886e-03]
[ 6.29561951e-01 3.58239706e-01 -4.29194570e-03]
[ 8.96458414e-01 1.86068670e-01 2.03431762e-03]
...
I am very grateful if someone can give some hints. Thank you!