2
我想解決一個多目標優化問題使用openmada的pyopt稀疏驅動程序與NSGA2算法。以下是代碼:'TypeError'使用NSGA2解決多目標問題。從python-sparse在OpenMDAO 1.x
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
請告訴我們,如果你可以讓上面的錯誤的東西,以及如何解決它。
此外,以何種形式返回多目標問題解決方案的輸出。我打算爲此產生一個帕累託最佳陣線。 謝謝大家。
感謝您的幫助。從這個結果我需要畫一個帕累托最優邊界。你能給出一些基本的步驟嗎? 此外,pyopt-sparse驅動程序不會採用任何優化器特定的選項,如人口大小,世代數等。是否有方法使用OpenMDAO設置這些參數。 –
您可以在這裏查看可用的選項:https://bitbucket.org/mdolab/pyoptsparse/src/8588944541cbee2dc14758ed405822a18f6685e5/pyoptsparse/pyNSGA2/pyNSGA2.py?at=default&fileviewer=file-view-default。至於繪製pareto邊界的步驟,我們沒有1.x中的任何特定內容來幫助您,但您可以查看OpenMDAO classic中的一些舊代碼,這些代碼在這方面會有一些用處:https:// github的.com/openMDAO/openMDAO-框架/團塊的/ dev/openmdao.lib/SRC/openmdao/LIB /組件/ pareto_filter.py –