0

Is it possible to optimize a single list entry in OpenMDAO 1.X? In this example, I desire to add/optimize the first entry of z. In my real problem, it is impossible for me to add the entire vector as a design variable (I'm using NREL's DAKOTA driver). I thought I could make dummy variables connected to z, but looks like 1.X does not support connecting variables to list entries.

ppb.py:

from __future__ import print_function
import numpy as np
from dakota_driver.driver import pydakdriver
from openmdao.api import ScipyOptimizer
from openmdao.api import IndepVarComp, Component, Problem, Group

class Paraboloid(Component):
    """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """

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

        self.add_param('x', val=6.0)
        self.add_param('y', val=-7.0)
        self.add_param('z', val=np.array([2., 2., 2.]))

        self.add_output('f_xy', val=0.0)

    def solve_nonlinear(self, params, unknowns, resids):
        """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
        """

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

        unknowns['f_xy'] = z[0]*(x-3.0)**2 + x*y + (y+4.0)**2 - 3.0 + abs(z[0]-4)

    def linearize(self, params, unknowns, resids):
        """ Jacobian for our paraboloid."""

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

        J['f_xy', 'x'] = 2.0*x - 6.0 + y
        J['f_xy', 'y'] = 2.0*y + 8.0 + x
        J['f_xy', 'z'] = (x-3.0)**2 + 1
        return J
top = Problem()

root = top.root = Group()

root.add('p1', IndepVarComp('x', 13.0))
root.add('p2', IndepVarComp('y', -14.0))
root.add('p3', IndepVarComp('z', np.array([0.0, 0., 0.])))
root.add('p', Paraboloid())

root.connect('p1.x', 'p.x')
root.connect('p2.y', 'p.y')
root.connect('p3.z', 'p.z')


top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'Powell'


top.driver.add_desvar('p3.z[0]', lower=-30., upper=30.) # <--- Is it possible to do this?

top.driver.add_objective('p.f_xy')

top.setup()

top['p1.x'] = 3.0
top['p2.y'] = -4.0
#top['p3.z'] = 2.0
top['p3.z'] = np.array([0., 0., 0.])

top.run()

Run Results:

$ python ppb.py 
Traceback (most recent call last):
  File "ppb.py", line 75, in <module>
    top.setup()
  File "/scratch/jquick/1.0/lib/python2.7/site-packages/openmdao/core/problem.py", line 586, in setup
    raise NameError("Can't find param of interest '%s'." % v)
NameError: Can't find param of interest 'p3.z[0]'.

1 Answer 1

3

Specify the index to be optimized using the indices keyword:

top.driver.add_desvar('p3.z', lower=-30., upper=30., indices=[0])
Sign up to request clarification or add additional context in comments.

1 Comment

you can see the arguments to this method in the src docs: openmdao.readthedocs.io/en/1.7.1/srcdocs/packages/core/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.