jMetalPy
jMetalPy copied to clipboard
Does number of variables need to be the same as number of constraints in the NSGAII?
Hi,
I am trying to run a multi-objective optimization on my problem which looks like this:
class LoadOptCWT3(FloatProblem):
def __init__(self):
super(LoadOptCWT3, self).__init__()
self.number_of_variables = 23
self.number_of_objectives = 2
self.number_of_constraints = 2
self.obj_directions = [self.MINIMIZE, self.MINIMIZE]
self.obj_labels = ['f(x)', 'f(y)']
self.lower_bound = [0.0,0.0]
self.upped_bound = [1.0,1.0]
FloatSolution.lower_bound = self.lower_bound
FloatSolution.upper_bound = self.upper_bound
def evaluate(self, solution: FloatSolution) -> FloatSolution:
x = solution.variables
solution.objectives[0] = -((-0.002 * x[0]) + (-0.001*x[1])
+ (0.002*x[2]) + (0.0*x[3])
+ (0.002*x[4]) + (0.0*x[5])
+ (-0.177*x[6]) + (0.561*x[7])
+ (-0.002*x[8]) + (0.002*x[9])
+ (0.001*x[10]) + (0.0*x[11])
+ (-0.001*x[12]) + (0.003*x[13])
+ (0.005*x[14]) + (0.0*x[15])
+ (0.001*x[16]) + (0.001*x[17])
+ (0.0*x[18]) + (0.003*x[19])
+ (-0.001*x[20]) + (0.0*x[21])
+ (0.01*x[22]) + 0.145)
solution.objectives[1] = -((0.01 * x[0]) + (-0.007*x[1])
+ (-0.003*x[2]) + (0.0*x[3])
+ (0.0*x[4]) + (-0.001*x[5])
+ (-3.984*x[6]) + (5.937*x[7])
+ (0.006*x[8]) + (0.0*x[9])
+ (0.0*x[10]) + (0.012*x[11])
+ (0.0*x[12]) + (-0.07*x[13])
+ (0.102*x[14]) + (0.001*x[15])
+ (0.0*x[16]) + (0.0*x[17])
+ (0.0*x[18]) + (0.0*x[19])
+ (-0.001*x[20]) + (0.0*x[21])
+ (0.608*x[22]) + 0.716)
self.__evaluate_constraints(solution)
return solution
def __evaluate_constraints(self, solution: FloatSolution) -> None:
constraints = [0.0 for _ in range(self.number_of_constraints)]
x = solution.variables
constraints[0] = -((-0.002 * x[0]) + (-0.001*x[1])
+ (0.002*x[2]) + (0.0*x[3])
+ (0.002*x[4]) + (0.0*x[5])
+ (-0.177*x[6]) + (0.561*x[7])
+ (-0.002*x[8]) + (0.002*x[9])
+ (0.001*x[10]) + (0.0*x[11])
+ (-0.001*x[12]) + (0.003*x[13])
+ (0.005*x[14]) + (0.0*x[15])
+ (0.001*x[16]) + (0.001*x[17])
+ (0.0*x[18]) + (0.003*x[19])
+ (-0.001*x[20]) + (0.0*x[21])
+ (0.01*x[22]) + 0.145)
constraints[1] = -((0.01 * x[0]) + (-0.007*x[1])
+ (-0.003*x[2]) + (0.0*x[3])
+ (0.0*x[4]) + (-0.001*x[5])
+ (-3.984*x[6]) + (5.937*x[7])
+ (0.006*x[8]) + (0.0*x[9])
+ (0.0*x[10]) + (0.012*x[11])
+ (0.0*x[12]) + (-0.07*x[13])
+ (0.102*x[14]) + (0.001*x[15])
+ (0.0*x[16]) + (0.0*x[17])
+ (0.0*x[18]) + (0.0*x[19])
+ (-0.001*x[20]) + (0.0*x[21])
+ (0.608*x[22]) + 0.716)
solution.constraints = constraints
def get_name(self):
return 'LoadOptCWT3'
When i run NSGAII on this problem, I get an error: IndexError: list index out of range
This makes me think that NSGAII only works if number of variables is same as number of constraints (in my case they are not same, variables are 23 and constraints are 2).
Is this true? If so, what is the way to do multi objective optimization in this scenario using jMetalPy?
Appreciate your help.
Hello @ShreyaPriyanil ,
The number of variables and the number of constraints do not need to be the same. Nevertheless, the number of elements in the lower bound and upper bound arrays need to be the same as the number of variables:
- self.lower_bound = [0.0,0.0]
- self.upped_bound = [1.0,1.0]
+ self.lower_bound = self.number_of_variables * [0.0]
+ self.upper_bound = self.number_of_variables * [1.0]
Thank you.