'TypeError' 同时使用 NSGA2 解决 Multi-objective 问题。来自 OpenMDAO 中的 pyopt-sparse 1.x

'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO 1.x

我正在尝试使用 openMDAO 的 pyopt-sparse 驱动程序和 NSGA2 算法来解决多 objective 优化问题。以下是代码:

from __future__ import print_function
from openmdao.api import IndepVarComp, Component, Problem, Group, pyOptSparseDriver

class Circ2(Component):

    def __init__(self):
        super(Circ2, self).__init__()

        self.add_param('x', val=10.0)
        self.add_param('y', val=10.0)

        self.add_output('f1', val=40.0)
        self.add_output('f2', val=40.0)

    def solve_nonlinear(self, params, unknowns, resids):

        x = params['x']
        y = params['y']

        unknowns['f1'] = (x - 0.0)**2 + (y - 0.0)**2
        unknowns['f2'] = (x - 1.0)**2 + (y - 1.0)**2

    def linearize(self, params, unknowns, resids):

        J = {}
        x = params['x']
        y = params['y']

        J['f1', 'x'] = 2*x
        J['f1', 'y'] = 2*y
        J['f2', 'x'] = 2*x-2
        J['f2', 'y'] = 2*y-2
        return J


if __name__ == "__main__":

    # Defining Problem & Root
    top = Problem()
    root = top.root = Group()

    # Adding in-present variable values and model
    startVal = 50.0
    root.add('p1', IndepVarComp('x', startVal))
    root.add('p2', IndepVarComp('y', startVal))

    root.add('p', Circ2())

    # Making Connections
    root.connect('p1.x', 'p.x')
    root.connect('p2.y', 'p.y')

    # Configuring Driver
    top.driver = pyOptSparseDriver()
    top.driver.options['optimizer'] = 'NSGA2'

    # Setting bounds for the optimizer
    top.driver.add_desvar('p1.x', lower=-600, upper=600)
    top.driver.add_desvar('p2.y', lower=-600, upper=600)

    # Setting Objective Function(s)
    top.driver.add_objective('p.f1')
    top.driver.add_objective('p.f2')

    # # Setting up constraints
    # top.driver.add_constraint('con.c', lower=1.0)

    top.setup()
    top.run()

我收到以下错误 -

Traceback (most recent call last):
  File "/home/prasad/DivyaManglam/Python Scripts/Pareto Testing/Basic_Sphere.py", line 89, in <module>
    top.run()
  File "/usr/local/lib/python2.7/dist-packages/openmdao/core/problem.py", line 1038, in run
    self.driver.run(self)
  File "/usr/local/lib/python2.7/dist-packages/openmdao/drivers/pyoptsparse_driver.py", line 280, in run
    sol = opt(opt_prob, sens=self._gradfunc, storeHistory=self.hist_file)
  File "/usr/local/lib/python2.7/dist-packages/pyoptsparse/pyNSGA2/pyNSGA2.py", line 193, in __call__
    self.optProb.comm.bcast(-1, root=0)
  File "MPI/Comm.pyx", line 1276, in mpi4py.MPI.Comm.bcast (src/mpi4py.MPI.c:108819)
  File "MPI/msgpickle.pxi", line 620, in mpi4py.MPI.PyMPI_bcast (src/mpi4py.MPI.c:47164)
  File "MPI/msgpickle.pxi", line 143, in mpi4py.MPI.Pickle.load (src/mpi4py.MPI.c:41248)
TypeError: only length-1 arrays can be converted to Python scalars

请告知您是否可以解决上述错误以及如何修复它。

另外,多objective问题的解决方案的输出将以什么形式返回。我打算为此生成帕累托最优前沿。 谢谢大家

事实证明这不是 OpenMDAO 错误,而是 NSGA2 的 Pyopt 稀疏包装器中的错误。修复并不可怕,我已经为此向 pyoptsparse 存储库提交了一个 pull request。同时,修补您的本地 pyoptsparse 副本将非常容易。

您询问了如何报告结果。当前的 NSGA2 包装器不对结果做任何事情。它只是让 NSGA2 将它们全部写入一系列文本文件,例如 nsga2_best_pop.out,如下所示:

# This file contains the data of final feasible population (if found)
# of objectives = 1, # of constraints = 0, # of real_var = 2, # of bits of bin_var = 0, constr_violation, rank, crowding_distance
-1.000000e+00   3.190310e+01    -2.413640e+02   0.000000e+00    1       1.000000e+14
-1.000000e+00   -5.309160e+02   2.449727e+02    0.000000e+00    1       0.000000e+00
-1.000000e+00   -3.995119e+02   -1.829071e+02   0.000000e+00    1       0.000000e+00

附带说明一下,在您的示例中,您为示例中的组件实现了线性化方法。当您需要计算梯度时会使用该方法,但 NSGA2 是无梯度优化器,根本不会使用它。因此,除非您还计划测试一些基于梯度的方法,否则您可以将该方法排除在组件之外。