4

我想用 Platypus 执行多目标优化,只使用具有 2 个目标、3 个变量且没有约束的整数(不是浮点数),我需要最大化目标值。我是这样定义的:

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

但我不断得到这样的结果:

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]

请你帮助我好吗?

提前致谢。

4

1 回答 1

4

试试这个:

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))

基本上,在这种情况下,由于所有输入的范围都是相同的(int1、int2 和 int3 具有相同的范围),我们也可以完成“int1.decode(second_variable)”等,但我把它留在这里以防万一您想将每个整数的范围更改为不同的值。

于 2017-08-27T02:15:33.710 回答