42759

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

I am trying to solve a multi-objective optimization problem using openMDAO's pyopt-sparse driver with NSGA2 algorithm. Following is the code:

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()

and I am getting the following error -

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

Kindly tell if you can make anything of the above error and how to fix it.

Also, in what form would the output of the multi-objective problem's solution be returned. I intend to generate a pareto-optimal front for this. Thank you all.

Answer1:

It turns out this is not an OpenMDAO bug, but rather a bug in the Pyopt sparse wrapper for NSGA2. The fix wasn't terrible, and I've submitted a pull request to the pyoptsparse repo for it. In the meantime it would be easy enough to patch your local copy of pyoptsparse.

You asked how the results will be reported. The current NSGA2 wrapper doesn't do anything with the results. It just lets NSGA2 write them all out to a series of text files such as nsga2_best_pop.out that look like this:

# 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

As a side note, in your example, you implemented a linearize method for the component in your example. That method is used when you need to compute gradients, but NSGA2 is a gradient free optimizer and won't use it at all. So unless you're also planning to test out some gradient based methods you can leave that method out of your components.

Recommend

  • Failure to import pyOptSparseDriver
  • How do I prevent loss: nan while I'm fitting my keras model?
  • How can I get a COUNT(col) … GROUP BY to use an index?
  • At what point is code reordering in C++ optimization stopped?
  • How to curve a Polyline in react-google-maps?
  • Multi-dimensional regression with Keras
  • Drawing a polygon over the entire map
  • JsonConvert.DeserializeObject, Index was outside the bounds of the array
  • How to use a decaying learning rate with an estimator in tensorflow?
  • Drag and reorder - UICollectionview with sections
  • Time taken for Hadoop job to execute
  • Cassandra 2.1: Recursion by nesting UDT's
  • Spark (Scala) Writing (and reading) to local file system from driver
  • pip in virtualenv gets ConnectTimeoutError
  • Ember.js model to be organised as a tree structure
  • pyodbc doesn't report sql server error
  • Thread safety of a fluent like class using clone() and non final fields
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Deleting and Updating values from a cusrsor adapter
  • Modifying destination and filename of gulp-svg-sprite
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to limit post in wp_query
  • Websockets service method fails during R startup
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to set the response of a form post action to a iframe source?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Linking SubReports Without LinkChild/LinkMaster
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?