torchmdo.optimize
Design Variables
- class torchmdo.optimize.DesignVariable(name, value=None, lower=None, upper=None, scale=1.0, fixed=False)[source]
Specifies a design variable, along with input bounds for that variable, if any.
- Parameters:
name (
str) – the name of an attribute in aModelthat will be used as a design variable for optimization.value (
Optional[Tensor]) – if specified, this value will be used as an initial value of this design variable for optimization. If not specified, then the value of the attribute stored in theModelwith the same name will be used as the initial value of this design variable for optimization.lower (
Optional[Tensor]) – if specified, this design variable will have a lower bound given by this value.upper (
Optional[Tensor]) – if specified, this design variable will have an upper bound given by this value.scale (
float) – the scale of the expected change in this design variable. Internally, all variables will be scaled such that unit variation is expected. This can help condition and be particularly useful when finite-differences are employed for gradient computations.fixed (
bool) – if True, this design variable should be fixed and it will therefore not be changed during optimization.
Objectives
- class torchmdo.optimize.Minimize(name, value=None, lower=None, upper=None, scale=1.0, linear=False)[source]
Specifies an objective to be minimized during optimization.
- Parameters:
name (
str) – the name of an attribute in aModelthat will be minimized during optimization.linear (
bool) – if True then this objective will be treated as linear in the design variables (as specified byDesignVariable). Default: False.
- class torchmdo.optimize.Maximize(name, value=None, lower=None, upper=None, scale=1.0, linear=False)[source]
Specifies an objective to be maximized during optimization.
- Parameters:
name (
str) – the name of an attribute in aModelthat will be maximized during optimization.linear (
bool) – if True then this objective will be treated as linear in the design variables (as specified byDesignVariable). Default: False.
Constraints
- class torchmdo.optimize.Constraint(name, value=None, lower=None, upper=None, scale=1.0, linear=False)[source]
Specifies an equality or inequality constraint to be used for optimization.
- Parameters:
name (
str) – the name of an attribute in aModelthat will be used as a constraint for optimization.lower (
Optional[Tensor]) – if specified, this output will be constrained as an inequality constraint such that the output value must be greater than or equal to lower. If upper is also specified and lower == upper then this will be an equality constraint.upper (
Optional[Tensor]) – if specified, this output will be constrained as an inequality constraint such that the output value must be less than or equal to upper. If lower is also specified and lower == upper then this will be an equality constraint.scale (
float) – the scale of the expected change in this output. Internally, the output will be scaled such that unit variation is expected. This can help conditioning.linear (
bool) – if True then this constraint will be treated as linear in the design variables (as specified byDesignVariable). Default: False.
Optimizer
- class torchmdo.optimize.Optimizer(initial_design_variables, objective, constraints, model, vectorize_constraint_jac=False)[source]
Object used to perform non-linear constrained optimization.
- Parameters:
initial_design_variables (
List[DesignVariable]) – list of design variables to be used in the optimization problem.objective (
Union[Minimize,Maximize,NearestFeasible]) – objective to be used in the optimization problem.constraints (
List[Constraint]) – list of constraints to be used in the optimization problem.model (
Model) – object that computes all outputs whenmodel.compute()is called.vectorize_constraint_jac (
bool) – if true then the computation of the constraint jacobian will be vectorized which may help a lot when many constraints are present. Note, however, that this is an experimental feature. Default: False.
- callback(xk, res)[source]
Callback function for scipy minimize.
- Parameters:
xk (
ndarray) – current design variable setting.res (
OptimizeResult) – current optimization result and details.
- Return type:
bool- Returns:
Flag indicating whether to terminate optimization.
- check_grad()[source]
verifies that the objective gradient and constraint jacobian are correct at the current setting of the design variables.
- Return type:
Tuple[Tensor,Tensor]- Returns:
A scalar tensor that gives the error in the objective gradient and a 1D tensor that gives the error in each row of the the constraint jacobian, respectively.
- optimize(maxiter=1000, display_step=50, keep_feasible=False, use_finite_diff=False, callback=None, **optimizer_options)[source]
Optimize the objective, subject to constraints
- Parameters:
maxiter (
int) – maximum number of optimization iterationsdisplay_step (
int) – number of optimization iterations between printing a logging message to monitor the procedure.keep_feasible (
bool) – whether the optimizer should attempt to keep the optimization trajectory in a feasible region. Default: False.use_finite_diff (
bool) – if True, will approximate gradients using finite differences rather than using pytorch’s automatic differentiation. This will result in less accurate gradients and slower computation but can be useful for debugging.callback (
Optional[Callable[[ndarray,OptimizeResult],bool]]) – callback function which must match the function signature ofOptimizer.callback()which is the default callback function. Note that if specified then display_step will be ignored unless the user provided callback makes a call toOptimizer.callback().optimizer_options – additional optimization options from scipy’s trust-constr implementation. See here.
- Return type:
Optional[OptimizeResult]