modopt_driver.py#

OpenMDAO Wrapper for the modOpt optimization library.

modOpt is a modular optimization framework providing interfaces to various gradient-based and gradient-free optimization algorithms.

Available Optimizers#

Gradient-Based:
  • SLSQP: Sequential Least Squares Programming (supports all constraint types)

  • PySLSQP: Pure Python implementation of SLSQP

  • BFGS: Broyden-Fletcher-Goldfarb-Shanno (unconstrained)

  • LBFGSB: Limited-memory BFGS with bounds

  • TrustConstr: Trust-region constrained algorithm

  • SNOPT: Sparse Nonlinear Optimizer (requires license)

  • IPOPT: Interior Point Optimizer (requires separate installation)

  • OpenSQP: A sequential quadratic programming optimizer built into modOpt

Gradient-Free:
  • COBYLA: Constrained Optimization BY Linear Approximation

  • COBYQA: Constrained Optimization BY Quadratic Approximation

  • NelderMead: Nelder-Mead simplex algorithm (unconstrained)

Notes#

  • SLSQP is the default optimizer and supports gradients, bounds, and all constraint types

  • SNOPT and IPOPT offer high performance but require separate installation/licenses

  • Linear constraints are handled efficiently by pre-computing their Jacobians

  • Gradient-free methods (COBYLA, COBYQA, NelderMead) are useful when derivatives are unavailable

See the modOpt documentation at https://modopt.readthedocs.io for detailed information on algorithm-specific options and capabilities.

class openmdao.drivers.modopt_driver.modOptDriver(**kwargs)[source]

Bases: Driver

Driver wrapper for the modOpt optimization library.

modOpt provides interfaces to various gradient-based and gradient-free optimization algorithms including SLSQP, IPOPT, SNOPT, COBYLA, and others.

Inequality and equality constraints are supported by several optimizers. Refer to the modOpt documentation for algorithm-specific capabilities.

modOptDriver supports the following:
  • equality_constraints

  • inequality_constraints

  • two_sided_constraints

  • linear_constraints (with pre-computed Jacobians for efficiency)

Parameters:
**kwargsdict of keyword arguments

Keyword arguments that will be mapped into the Driver options.

Attributes:
failbool

Flag that indicates failure of most recent optimization.

iter_countint

Counter for function evaluations.

opt_settingsdict

Dictionary of optimizer-specific options. See the modOpt documentation for algorithm-specific settings.

_check_obj_gradbool

Used internally to control when to perform singular checks on computed objective gradient.

_check_nl_jacbool

Used internally to control when to perform singular checks on computed nonlinear constraint jacobian.

_con_cachedict

Cached result of constraint evaluations.

_lincongrad_cachenp.ndarray or None

Pre-calculated gradients of linear constraints.

_model_ranbool

Flag indicating whether the model has been evaluated at least once, used to activate relevance filtering on subsequent iterations.

_total_jac_sparsitydict or None

User-specified total Jacobian sparsity pattern.

_mo_prob<modOpt Problem object>

The modOpt problem object that is built and fed to the Optimizer.

Methods

add_recorder(recorder)

Add a recorder to the driver.

check_relevance()

Check if there are constraints that don't depend on any design vars.

cleanup()

Clean up resources prior to exit.

compute_lagrange_multipliers([...])

Get the approximated Lagrange multipliers of one or more constraints.

declare_coloring([num_full_jacs, tol, ...])

Set options for total deriv coloring.

get_coloring_fname([mode])

Get the filename for the coloring file.

get_constraint_values([ctype, lintype, ...])

Return constraint values.

get_design_var_values([get_remote, ...])

Return the design variable values.

get_driver_derivative_calls()

Return number of derivative evaluations made during a driver run.

get_driver_objective_calls()

Return number of objective evaluations made during a driver run.

get_exit_status()

Return exit status of driver run.

get_objective_values([driver_scaling])

Return objective values.

get_reports_dir()

Get the path to the directory where the report files should go.

record_derivatives()

Record the current total jacobian.

record_iteration()

Record an iteration of the current Driver.

run()

Optimize the problem using the selected modOpt optimizer.

scaling_report([outfile, title, ...])

Generate a self-contained html file containing a detailed connection viewer.

set_design_var(name, value[, set_remote, units])

Redirect to _set_design_var, as this public method is deprecated.

use_fixed_coloring([coloring])

Tell the driver to use a precomputed coloring.

__init__(**kwargs)[source]

Initialize the modOptDriver.

Parameters:
**kwargsdict of keyword arguments

Keyword arguments that will be mapped into the Driver options.

run()[source]

Optimize the problem using the selected modOpt optimizer.

The optimization process performs the following steps: 1. Initial model evaluation 2. Extract and format design variables, bounds, and constraints 3. Pre-compute linear constraint Jacobians for efficiency 4. Create modOptProblem wrapper around the OpenMDAO problem 5. Instantiate and run the selected optimizer 6. Extract optimal design variables and update the model 7. Perform final evaluation at the optimal point

The optimization uses modOpt’s Problem API, which handles callbacks for objective and constraint evaluations as well as derivative computations.

Returns:
bool

Success flag; True if optimization succeeded, False if it failed.

class openmdao.drivers.modopt_driver.modOptProblem(driver, x_info, lin_con_jac, lin_con_bounds, nl_con_bounds, nl_con_jac_sparsity, all_nl_relevant_dvs)[source]

Bases: Problem

modOpt Problem that delegates objective and constraint evaluation to an OpenMDAO driver.

This class wraps an OpenMDAO problem as a modOpt optimization problem, translating between modOpt’s interface and OpenMDAO’s driver interface.

Parameters:
drivermodOptDriver

The OpenMDAO driver managing the optimization.

x_infoOrderedDict

Dictionary with design variable names as keys and dictionaries containing ‘init’, ‘lower’, and ‘upper’ values as the values.

lin_con_jacdict or None

Pre-computed Jacobian for linear constraints in dictionary format with (constraint_name, design_var_name) tuples as keys, or None if no linear constraints.

lin_con_boundsdict

Dictionary with linear constraint names as keys and dictionaries containing ‘lower’, ‘upper’, and ‘size’ as values.

nl_con_boundsdict

Dictionary with nonlinear constraint names as keys and dictionaries containing ‘lower’, ‘upper’, and ‘size’ as values.

nl_con_jac_sparsitydict

Sparsity pattern for nonlinear constraint Jacobian with (constraint_name, design_var_name) tuples as keys and dictionaries containing ‘rows’ and ‘cols’ arrays as values.

Attributes:
drivermodOptDriver

Reference to the OpenMDAO driver.

x_infoOrderedDict

Design variable metadata including initial values and bounds.

lin_con_jacdict or None

Pre-computed linear constraint Jacobian.

lin_con_boundsdict

Linear constraint bounds.

nl_con_boundsdict

Nonlinear constraint bounds.

nl_con_jac_sparsitydict

Sparsity pattern for nonlinear constraint Jacobian.

all_nl_relevant_dvsset

Set of all design variable names that are relevant to any nonlinear constraint.

obj_namestr

Name of the objective function.

_con_cachedict or None

Cached constraint values to avoid redundant evaluations.

_all_constraint_nameslist of str

Ordered list of all constraint names (linear followed by nonlinear).

Methods

add_constraints([name, shape, scaler, ...])

User calls this method within Problem.setup() method to add constraints for the problem.

add_design_variables([name, shape, scaler, ...])

User calls this method within Problem.setup() method to add design variable vectors for the problem.

add_objective([name, scaler])

User calls this method within Problem.setup() method to add an objective with a name and a scaler.

compute_constraint_jacobian(dvs, jac)

Compute the Jacobian of nonlinear constraints.

compute_constraint_jvp(dvs, vec, jvp)

Compute the constraint Jacobian-vector product (JVP) for the given design variable vector and multiplying vector.

compute_constraint_vjp(dvs, vec, vjp)

Compute the constraint vector-Jacobian product (VJP) for the given design variable vector and multiplying vector.

compute_constraints(dvs, cons)

Compute constraint values.

compute_lagrangian(dvs, lag_mult, lag)

Compute the Lagrangian given the design variable and Lagrange multiplier vectors.

compute_lagrangian_gradient(dvs, lag_mult, ...)

Compute the Lagrangian gradient given the design variable and Lagrange multiplier vectors.

compute_lagrangian_hessian(dvs, lag_mult, ...)

Compute the Lagrangian Hessian given the design variable and Lagrange multiplier vectors.

compute_lagrangian_hvp(dvs, lag_mult, vec, ...)

Compute the Lagrangian Hessian-vector product (HVP) for a given design variable vector, Lagrange multiplier vector, and multiplying vector.

compute_objective(dvs, obj)

Evaluate the objective function at the given design point.

compute_objective_gradient(dvs, grad)

Compute the gradient of the objective function.

compute_objective_hessian(dvs, obj_hess)

Compute the objective Hessian given the design variable vector.

compute_objective_hvp(dvs, vec, obj_hvp)

Compute the objective Hessian-vector product (HVP) for a given design variable vector and a multiplying vector.

declare_constraint_jacobian(of, wrt[, ...])

User calls this method within Problem.setup_derivatives() method to declare nonzero constraint Jacobians.

declare_constraint_jvp(of[, vals])

User calls this method within Problem.setup_derivatives() method to declare constraint Jacobian-vector product (JVP).

declare_constraint_vjp(wrt[, vals])

User calls this method within Problem.setup_derivatives() method to declare constraint vector-Jacobian product (VJP).

declare_lagrangian([name])

User calls this method within Problem.setup() method to add the Lagrangian dict with the objective name as key and default value 1.0.

declare_lagrangian_gradient(wrt[, vals])

User calls this method within Problem.setup_derivatives() method to declare nonzero Lagrangian gradients.

declare_lagrangian_hessian(of, wrt[, shape, ...])

User calls this method within Problem.setup_derivatives() method to declare nonzero Lagrangian Hessian components L_{xy} = d^2L/dydx.

declare_lagrangian_hvp(wrt[, vals])

User calls this method within Problem.setup_derivatives() method to declare Lagrangian Hessian-vector product (HVP).

declare_objective_gradient(wrt[, vals])

User calls this method within Problem.setup_derivatives() method to declare nonzero objective gradients.

declare_objective_hessian(of, wrt[, shape, ...])

User calls this method within Problem.setup_derivatives() method to declare nonzero objective Hessian components F_{xy} = d^2F/dydx.

declare_objective_hvp(wrt[, vals])

User calls this method within Problem.setup_derivatives() method to declare objective Hessian-vector product (HVP).

delete_unnecessary_attributes_allocated()

After checking if 'grad', 'jac', 'obj_hess', 'obj_hvp', 'jvp', 'vjp', 'lag_grad', 'lag_hess', or 'lag_hvp' are in self.declared_variables, delete attributes self.pF_px, self.pC_px_dict, self.pC_px, self.jvp, self.vjp, self.obj_hess, self.obj_hvp, self.lag_grad, self.lag_hess, self.lag_hvp, and associated VectorComponentsDict() objects if not declared by the user.

evaluate_augmented_lagrangian_hessian(x, y, ...)

Evaluate the augmented Lagrangian Hessian given the design and state vectors along with the Lagrange multiplier vector.

evaluate_hvp(x, y, lag_mult, rho, vx, vy)

Evaluate the Hessian-vector product along the direction vector specified, for given design and state vectors, for a given Hessian (objective, penalty, Lagrangian, or Augmneted Lagrangian) specified by the Lagrange multipliers and/or penalty parameters.

evaluate_lagrangian_hessian(x, y, lag_mult)

Evaluate the Lagrangian Hessian given the design and state vectors along with the Lagrange multiplier vector.

evaluate_penalty_hessian(x, y, rho)

Evaluate the penalty Hessian given the design and state vectors along with the Lagrange multiplier vector.

evaluate_residual_jacobian(x, y)

Evaluate the residual Jacobian with respect to the state and design variable vector.

evaluate_residuals(x, y)

Evaluate the residual vector given the design and state vectors.

initialize()

Set the modOpt problem name.

raise_issues_with_user_setup()

Raise errors or warnings associated with declarations made by the user in setup() or setup_derivatives().

raise_not_implemented_error(method_name)

Raise NotImplementedError when an optional abstract method is called but not implemented by the user in the derived class.

setup()

Add design variables, constraints, and objective to the modOpt Problem.

setup_derivatives()

Declare objective and constraint gradients to the modOpt problem.

solve_residual_equations(x[, y, tol])

Solve the implicit functions that define the residuals to compute an inexact state vector with given tolerances.

use_finite_differencing(derivative[, step])

User calls this method within compute methods to approximate derivatives.

add_residuals

add_state_variables

compute_adjoint_vector

compute_direct_vector

compute_functions

declare_pC_px_jacobian

declare_pC_py_jacobian

declare_pF_px_gradient

declare_pF_py_gradient

declare_pR_px_jacobian

declare_pR_py_jacobian

evaluate_adjoint_vector

__init__(driver, x_info, lin_con_jac, lin_con_bounds, nl_con_bounds, nl_con_jac_sparsity, all_nl_relevant_dvs)[source]

Initialize the modOptProblem.

Parameters:
drivermodOptDriver

The OpenMDAO driver managing the optimization.

x_infoOrderedDict

Dictionary with design variable names as keys and dictionaries containing ‘init’, ‘lower’, and ‘upper’ values as the values.

lin_con_jacdict or None

Pre-computed Jacobian for linear constraints in dictionary format with (constraint_name, design_var_name) tuples as keys.

lin_con_boundsdict

Dictionary with linear constraint names as keys and dictionaries containing ‘lower’, ‘upper’, and ‘size’ as values.

nl_con_boundsdict

Dictionary with nonlinear constraint names as keys and dictionaries containing ‘lower’, ‘upper’, and ‘size’ as values.

nl_con_jac_sparsitydict

Sparsity pattern for nonlinear constraint Jacobian with (constraint_name, design_var_name) tuples as keys and dictionaries containing ‘rows’ and ‘cols’ arrays defining the sparse structure.

all_nl_relevant_dvsset

Set of all design variable names that are relevant to any nonlinear constraint.

compute_constraint_jacobian(dvs, jac)[source]

Compute the Jacobian of nonlinear constraints.

Linear constraint Jacobians are pre-computed and provided during setup. Only nonlinear constraint Jacobians are computed here. Only computes Jacobians for relevant design variable/constraint pairs based on relevance analysis.

Parameters:
dvs<array_manager.core.native_formats.vector.Vector>

Vector with the current design variable names and values.

jacdict

Dictionary to store the computed constraint Jacobians.

compute_constraints(dvs, cons)[source]

Compute constraint values.

Uses cached constraint values if available, otherwise evaluates model.

Parameters:
dvs<array_manager.core.native_formats.vector.Vector>

Vector with the current design variable names and values.

consdict

Dictionary to store the computed constraint values.

compute_objective(dvs, obj)[source]

Evaluate the objective function at the given design point.

This method updates the OpenMDAO model with new design variables, runs the model, and retrieves the objective value. Constraint values are also cached for efficiency.

Parameters:
dvs<array_manager.core.native_formats.vector.Vector>

Vector with the current design variable names and values.

objdict

Dictionary to store the computed objective value.

compute_objective_gradient(dvs, grad)[source]

Compute the gradient of the objective function.

Parameters:
dvs<array_manager.core.native_formats.vector.Vector>

Vector with the current design variable names and values.

graddict

Dictionary to store the gradient values. Keys are design variable names, values are gradient arrays with respect to the objective.

initialize()[source]

Set the modOpt problem name.

Called by the modOpt Problem base class during initialization.

setup()[source]

Add design variables, constraints, and objective to the modOpt Problem.

setup_derivatives()[source]

Declare objective and constraint gradients to the modOpt problem.

This method informs modOpt about which derivatives are available. For linear constraints, the Jacobian is provided directly. For nonlinear constraints, derivatives are computed on-demand, with sparsity information if available from OpenMDAO. Only relevant Jacobians (based on OpenMDAO’s relevance analysis) are declared.