I need to write a complicated constraint in Pymoo for a scheduling problem.
for all i = 1 to 7, t = 1 to 14
sum (k in 1 to 3 ) y(i)(k)(t) <= 1
How the indices and summation could be incorporated in Pymoo constraints/ objective functions ?? please share any reference that shows an example of writing such constraint.
Here is the draft code I created using the genetic algorithm NSGA-II
import numpy as np
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import Problem
from pymoo.optimize import minimize
from pymoo.factory import get_sampling, get_crossover, get_mutation
class MyProblem(Problem):
def __init__(self):
super().__init__(n_var=294, # since y is 3 indexed variable (7*3*14)
n_obj=1,
n_constr=56, # Constraint is written for all i range(4 to 7) & all t range (1 to 14) Total (14*4)
xl=np.array([0, 0, 0]),
xu=np.array([v, 1, 1]),
ElementwiseProblem=True)
def _evaluate(self, x, out, *args, **kwargs):
#f1 = Here the Obj. Function
#g1 = Jere is the Constraint
out["F"] = [f1]
out["G"] = [g1]
Problem = MyProblem()
algorithm = NSGA2(pop_size=40,
n_offsprings=10,
sampling="real_random",
crossover="real_sbx", prob=0.9, eta=15,
#mutation="real_pm", eta=20,// eta repeated error
eliminate_duplicates=True)
stop_criteria = ('n_gen', 60)
results = minimize(
problem = problem,
algorithm = algorithm,
termination =stop_criteria)