Modifying Design Variables, Constraints, and Objectives

Modifying Design Variables, Constraints, and Objectives

OpenMDAO provides 3 methods to let the user modify options for design variables, constraints, and objectives after they were set in their respective methods add_design_var, add_constraint, and add_objective. The methods are:

System.set_design_var_options(name, lower=UNDEFINED, upper=UNDEFINED, scaler=UNDEFINED, adder=UNDEFINED, ref=UNDEFINED, ref0=UNDEFINED)[source]

Set options for design vars in the model.

Can be used to set the options outside of setting them when calling add_design_var

Parameters
namestr

Name of the variable in this system’s namespace.

lowerfloat or ndarray, optional

Lower boundary for the input.

upperupper or ndarray, optional

Upper boundary for the input.

scalerfloat or ndarray, optional

Value to multiply the model value to get the scaled value for the driver. scaler is second in precedence. adder and scaler are an alterantive to using ref and ref0.

adderfloat or ndarray, optional

Value to add to the model value to get the scaled value for the driver. adder is first in precedence. adder and scaler are an alterantive to using ref and ref0.

reffloat or ndarray, optional

Value of design var that scales to 1.0 in the driver.

ref0float or ndarray, optional

Value of design var that scales to 0.0 in the driver.

System.set_constraint_options(name, ref=UNDEFINED, ref0=UNDEFINED, equals=UNDEFINED, lower=UNDEFINED, upper=UNDEFINED, adder=UNDEFINED, scaler=UNDEFINED, alias=UNDEFINED)[source]

Set options for objectives in the model.

Can be used to set options that were set using add_constraint.

Parameters
namestr

Name of the response variable in the system.

reffloat or ndarray, optional

Value of response variable that scales to 1.0 in the driver.

ref0float or ndarray, optional

Value of response variable that scales to 0.0 in the driver.

equalsfloat or ndarray, optional

Equality constraint value for the variable.

lowerfloat or ndarray, optional

Lower boundary for the variable.

upperfloat or ndarray, optional

Upper boundary for the variable.

adderfloat or ndarray, optional

Value to add to the model value to get the scaled value for the driver. adder is first in precedence. adder and scaler are an alterantive to using ref and ref0.

scalerfloat or ndarray, optional

Value to multiply the model value to get the scaled value for the driver. scaler is second in precedence. adder and scaler are an alterantive to using ref and ref0.

aliasstr, optional

Alias for this response. Necessary when adding multiple constraints on different indices or slices of a single variable.

System.set_objective_options(name, ref=UNDEFINED, ref0=UNDEFINED, adder=UNDEFINED, scaler=UNDEFINED, alias=UNDEFINED)[source]

Set options for objectives in the model.

Can be used to set options after they have been set by add_objective.

Parameters
namestr

Name of the response variable in the system.

reffloat or ndarray, optional

Value of response variable that scales to 1.0 in the driver.

ref0float or ndarray, optional

Value of response variable that scales to 0.0 in the driver.

adderfloat or ndarray, optional

Value to add to the model value to get the scaled value for the driver. adder is first in precedence. adder and scaler are an alterantive to using ref and ref0.

scalerfloat or ndarray, optional

Value to multiply the model value to get the scaled value for the driver. scaler is second in precedence. adder and scaler are an alterantive to using ref and ref0.

aliasstr

Alias for this response. Necessary when adding multiple objectives on different indices or slices of a single variable.

Example of Modifying Design Variable, Constraint, and Objective Options

Here is a simple examples of how these methods can be used for an optimizer example. Let’s say a user runs an optimization, and then based on the result, they want to change the bounds of the constraint, and then re-optimize.

# The initial optimization run

import numpy as np
import openmdao.api as om
from openmdao.test_suite.components.sellar import SellarDerivatives

prob = om.Problem(model=SellarDerivatives())
model = prob.model
model.nonlinear_solver = om.NonlinearBlockGS()

prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['tol'] = 1e-9

model.add_design_var('z', lower=np.array([-10.0, 0.0]), upper=np.array([10.0, 10.0]))
model.add_design_var('x', lower=0.0, upper=10.0)
model.add_objective('obj')
model.add_constraint('con1', upper=0.0)
model.add_constraint('con2', upper=0.0)

prob.setup()
prob.set_solver_print(level=0)
prob.run_driver()
print(f"con1 = {prob.get_val('con1')}")
# modify the constraint and run again

model.set_constraint_options(name='con1', upper=-1.0)
prob.setup()
prob.run_driver()
print(f"con1 = {prob.get_val('con1')}")
Optimization terminated successfully    (Exit mode 0)
            Current function value: [4.17385352]
            Iterations: 12
            Function evaluations: 12
            Gradient evaluations: 12
Optimization Complete
-----------------------------------
con1 = [-1.]