2

我正在尝试使用鸭嘴兽和 NSGA2 算法解决多目标优化问题。以下是实现的示例代码:

将 [-10,10] 中的 x 最小化 ( x^2 , (x-1)^2 )

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)

但我的问题是,如果我想最大化 x^2 并最小化 (x-1)^2,我应该如何定义问题以及正确的方法是什么。

4

1 回答 1

2

在这些示例中,我们假设目标被最小化。Platypus 很灵活,允许通过设置方向属性来更改每个目标的优化方向。例如:

problem.directions[:] = Problem.MAXIMIZE

所以正确的方法如下:

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)
于 2018-04-08T12:51:15.497 回答