2016-02-02 46 views
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 

請告訴我們,如果你可以讓上面的錯誤的東西,以及如何解決它。

此外,以何種形式返回多目標問題解決方案的輸出。我打算爲此產生一個帕累託最佳陣線。 謝謝大家。

回答

2

事實證明,這不是OpenMDAO錯誤,而是NSGA2的Pyopt稀疏包裝中的一個錯誤。修復並不可怕,我已經爲它提交了pull request到pyoptsparse回購。與此同時,修補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是無梯度優化器,根本不會使用它。因此,除非您還計劃測試某些基於漸變的方法,否則可以將該方法從組件中移除。

+0

感謝您的幫助。從這個結果我需要畫一個帕累托最優邊界。你能給出一些基本的步驟嗎? 此外,pyopt-sparse驅動程序不會採用任何優化器特定的選項,如人口大小,世代數等。是否有方法使用OpenMDAO設置這些參數。 –

+0

您可以在這裏查看可用的選項: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 –