Platypus optimization using integers

1.7k views Asked by At

I want to perform multiobjective optimization with Platypus using just integers (not floats) with 2 objectives, 3 variables and no constraints, and I need to maximize the objectives values. I defined it like this:

problem = Problem(3, 2)
problem.directions[:] = Problem.MAXIMIZE
problem.types[:] = [Integer(-50, 50), Integer(-50, 50), Integer(-50, 50)]

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

for solution in algorithm.result:
    print solution

But I keep getting results like this:

Solution[[False, True, False, True, False, True, True],[False, True, False, True, False, True, False],[True, True, True, False, True, False, True]|-12.2,629.8|0]
Solution[[False, True, False, True, False, True, True],[True, True, False, True, False, True, False],[True, False, True, False, True, True, False]|-28.0,1240.0|0]

Could you please help me?

Thanks in advance.

1

There are 1 answers

0
Jesper - jtk.eth On BEST ANSWER

Try this:

from platypus import Problem, Integer, NSGAII

def my_function(x):
    """ Some objective function"""
    return -x[0] ** 2 - x[2] ** 2  # we expect the result x[0] = 0, x[1] = whatever, and x[2] = 0

problem = Problem(3, 1)  # define 3 inputs and 1 objective (and no constraints)
problem.directions[:] = Problem.MAXIMIZE
int1 = Integer(-50, 50)
int2 = Integer(-50, 50)
int3 = Integer(-50, 50)
problem.types[:] = [int1, int2, int3]
problem.function = my_function
algorithm = NSGAII(problem)
algorithm.run(10000)

# grab the variables (note: we are just taking the ones in the location result[0])
first_variable = algorithm.result[0].variables[0]
second_variable = algorithm.result[0].variables[1]
third_variable = algorithm.result[0].variables[2]

print(int1.decode(first_variable))
print(int2.decode(second_variable))
print(int3.decode(third_variable))

Basically, in this case since the ranges are the same for all the inputs (int1, int2, and int3 have same ranges) we could have also done "int1.decode(second_variable)" etc., but I left it general here in case you want to change the range of each integer to something different.