openmdao

How to fix values to component variables in OpenMDAO


In OpenMDAO, I created a bunch of modular components such that any combination of them can be created into a model that can be optimized. However, for a particular model instance, I want to fix the values of certain component variables. First I simply used

p.set_val('x1', 1.0)

My understanding is that this only sets the initial value for optimization so it is subject to change. I tried to fix the value by introducing a constraint such as

p.model.add_subsystem('con', om.ExecComp('c = x1 - 1.0', promotes=['*'])
p.model.add_constraint('c', lower=0.0, upper=0.0)

This worked...

However, is there a simpler way to fix variable values without adding additional constraints?


Solution

  • There isn't a lot of detail here, so I have to make a few guesses.

    Fundamentally, if you don't want an optimizer to change a variable... don't give it control of that design variable. Then it simply can't change it from whatever the value was initially.

    The nature of your question causes me to infer that your modular components all make their own calls to add_design_var ... the idea being that they have some inherent degree of freedom and whenever you add that block to the model you expect something to give a value for that. something could be either an optimizer, or instead a human that sets a fixed value.

    So I think the problem you are wrestling with is that you get a bunch of calls to add_design_var and then sometimes need to limit one of those values.

    There are a few options I can suggest:

    Its hard for me to be more specific with out some sample code to play with, but generally I would suggest that you remove the add_design_var calls from the component setups. Instead, write some kind of a helper function that pass an instance of problem into, along with some kind of configuration data, and have it interogate the components and decide which dv's to add.