使用python运行上述代码之后,提示'MyProblem' object has no attribute 'obj_directions',该怎么解决?
时间: 2023-08-06 07:03:22 浏览: 79
抱歉,这是我犯了一个错误。在Platypus库的较新版本中,`obj_directions`已经被重命名为`directions`。因此,你需要将`self.obj_directions[:]`替换为`self.directions[:]`。下面是修改后的代码:
```
from platypus import NSGAII, Problem, Real, Integer, Constraint
class MyProblem(Problem):
def __init__(self):
super(MyProblem, self).__init__(3, 2, 3)
self.types[:] = [Real(0, 1), Real(0, 1), Real(0, 1)]
self.constraints[:] = [Constraint(lambda x: x[0] + x[1] + x[2] - 1 <= 0)]
self.directions[:] = [self.MINIMIZE, self.MINIMIZE, self.MINIMIZE]
self.obj_labels[:] = ['f1', 'f2', 'f3']
def evaluate(self, solution):
x = solution.variables
solution.objectives[:] = [x[0]**2, x[1]**2, x[2]**2]
solution.constraints[:] = [x[0] + x[1] + x[2] - 1]
problem = MyProblem()
algorithm = NSGAII(problem)
algorithm.run(10000)
```
请尝试一下这个修改后的代码,并让我知道是否成功了!
阅读全文