Different types of objective definition in platypus

489 views Asked by At

I am trying to solve a multi-objective optimization problem using platypus and NSGA2 algorithm. Following is a example code of an implementation:

minimaize ( x^2 , (x-1)^2 ) for x in [-10,10]

from platypus import NSGAII, Problem, Real

def schaffer(x):
   return [x[0]**2, (x[0]-2)**2]

problem = Problem(1, 2)
problem.types[:] = Real(-10, 10)
problem.function = schaffer

algorithm = NSGAII(problem)
algorithm.run(10000)

But my problems is that, If I want to maximize x^2 and minimize (x-1)^2, how i should define problems and what is the correct way to do it.

1

There are 1 answers

0
samie On BEST ANSWER

In these examples, we have assumed that the objectives are being minimized. Platypus is flexible and allows the optimization direction to be changed per objective by setting the directions attribute. For example:

problem.directions[:] = Problem.MAXIMIZE

So the correct way is in following:

from platypus import NSGAII, Problem, Real

def schaffer(x):
    return [x[0]**2, (x[0]-2)**2]

problem = Problem(1, 2)
problem.directions[0] = Problem.MAXIMIZE
problem.directions[1] = Problem.MINIMIZE
problem.types[:] = Real(-10, 10)
problem.function = schaffer

algorithm = NSGAII(problem)
algorithm.run(10000)
for solution in algorithm.result:
    print(solution.objectives)