Search code examples
openmdao

Is it possible to set initial values to use in optimisation?


I'm currently using SQSLP, and defining my design variables like so:

p.model.add_design_var('indeps.upperWeights', lower=np.array([1E-3, 1E-3, 1E-3]))
p.model.add_design_var('indeps.lowerWeights', upper=np.array([-1E-3, -1E-3, -1E-3]))
p.model.add_constraint('cl', equals=1)
p.model.add_objective('cd')

p.driver = om.ScipyOptimizeDriver()

However, it insists on trying [1, 1, 1] for both variables. I can't override with val=[...] in the component because of how the program is structured.

Is it possible to get the optimiser to accept some initial values instead of trying to set anything without a default value to 1?


Solution

  • By default, OpenMDAO initializes variables to a value of 1.0 (this tends to avoid unintentional divide-by-zero if things were initialized to zero).

    1. Specifying shape=... on input or output results in the variable values being populated by 1.0

    2. Specifying val=... uses the given value as the default value.

    But that's only the default values. Typically, when you run an optimization, you need to specify initial values of the variables for the given problem at hand. This is done after setup, through the problem object.

    The set_val and get_val methods on problem allow the user to convert units. (using Newtons here for example)

    p.set_val('indeps.upperWeights', np.array([1E-3, 1E-3, 1E-3]), units='N')
    p.set_val('indeps.upperWeights', np.array([-1E-3, -1E-3, -1E-3]), units='N')
    

    There's a corresponding get_val method to retrieve values in the desired units after optimization.

    You can also access the problem object as though it were a dictionary, although doing so removes the ability to specify units (you get the variable values in its native units).

    p['indeps.upperWeights'] = np.array([1E-3, 1E-3, 1E-3])
    p['indeps.upperWeights'] = np.array([-1E-3, -1E-3, -1E-3])