0
votes

When running the following example code:

from openmdao.api import Problem, Group, IndepVarComp, ImplicitComponent, ScipyOptimizeDriver


class Test1Comp(ImplicitComponent):

    def setup(self):
        self.add_input('x', 0.5)
        self.add_input('design_x', 1.0)
        self.add_output('z', val=0.0)
        self.add_output('obj')

        self.declare_partials(of='*', wrt='*', method='fd', form='central', step=1.0e-4)

    def apply_nonlinear(self, inputs, outputs, resids):
        x = inputs['x']
        design_x = inputs['design_x']
        z = outputs['z']

        resids['z'] = x*z + z - 4
        resids['obj'] = (z/5.833333 - design_x)**2


if __name__ == "__main__":

    prob = Problem()
    model = prob.model = Group()

    model.add_subsystem('p1', IndepVarComp('x', 0.5))
    model.add_subsystem('d1', IndepVarComp('design_x', 1.0))
    model.add_subsystem('comp', Test1Comp())

    model.connect('p1.x', 'comp.x')
    model.connect('d1.design_x', 'comp.design_x')

    prob.driver = ScipyOptimizeDriver()
    prob.driver.options["optimizer"] = 'SLSQP'
    model.add_design_var("d1.design_x", lower=0.5, upper=1.5)
    model.add_objective('comp.obj')

    prob.setup()
    prob.run_model()
    print(prob['comp.z'])

I get:

Traceback (most recent call last):
  File "C:/Users/jonat/Desktop/mockup_component3.py", line 40, in <module>
    prob.setup()
  File "C:\Python\openmdao\core\problem.py", line 409, in setup
    model._setup(comm, 'full', mode)
  File "C:\Python\openmdao\core\system.py", line 710, in _setup
    self._setup_relevance(mode, self._relevant)
  File "C:\Python\openmdao\core\system.py", line 1067, in _setup_relevance
    self._relevant = relevant = self._init_relevance(mode)
  File "C:\Python\openmdao\core\group.py", line 693, in _init_relevance
    return get_relevant_vars(self._conn_global_abs_in2out, desvars, responses, mode)
  File "C:\Python\openmdao\core\group.py", line 1823, in get_relevant_vars
    if 'type_' in nodes[node]:
TypeError: 'instancemethod' object has no attribute '__getitem__'

Can someone explain why? I've succesfully run a similar component, but without optimization, so I'm suspicious the error comes from the optimization constructs. For example, do I have to define the objective in an ExplicitComponent?

2
Running your code I wasn't able to get the same error msg you got. Could you add the full stack trace to your question? - Justin Gray

2 Answers

0
votes

I get a more descriptive message when I run:

KeyError: 'Variable name "comp.y" not found.'

Which just means that component "comp" doesn't have a variable named "y" (or "z").

0
votes

The issue seems to have been caused by incorrect installation of OpenMDAO. I had previously tried to install by downloading a zip-file containing OpenMDAO. Now I instead installed using pip and the error disappeared.