Optimal Control Problems#
Optimal control problem formulation and solution methods for biosym.
Constraint Functions#
Constraint functions for optimal control problems in biosym.
This module provides constraint handling for optimal control problems, including constraint evaluation, Jacobian computation, and integration with collocation methods for biomechanical motion optimization.
- class biosym.ocp.confun.Constraints(model, settings)#
Bases:
objectHandle constraints for optimal control problems.
This class manages constraint evaluation, Jacobian computation, and constraint structure analysis for biomechanical optimal control problems. It integrates various constraint types including dynamics, periodicity, discretization, and custom user constraints.
- model#
The biomechanical model containing system dynamics
- Type:
BiosymModel
- add_constraint(name, weight, args=None)#
Add a constraint to the optimal control problem.
- Parameters:
name (str or class) – Either a string name of a constraint class or a constraint class object. If string, the class will be looked up in the global namespace.
weight (float, int, or str) – Weighting factor for the constraint in the optimization. Can be numeric value or special string like “1/BW” for bodyweight normalization.
args (dict, optional) – Additional arguments to pass to the constraint constructor.
- Raises:
ValueError – If the constraint class name is not found or invalid constraint object provided.
Notes
Constraints are instantiated with the model and settings objects
Weights are stored for later use in constraint evaluation
Constraint information (number of constraints, Jacobian sparsity) is recorded
- biosym.ocp.confun.evaluate_constraints(constraint_functions, weights, ncon, c_start, states_list, globals_dict=None)#
Evaluate all constraints for the current state of the optimal control problem.
This function efficiently evaluates all constraint functions and assembles them into a single constraint vector with appropriate weighting. It’s designed to be JIT-compiled for performance during optimization.
- Parameters:
constraint_functions (list) – List of constraint functions to evaluate.
weights (list) – Weighting factors for each constraint type.
ncon (int) – Total number of constraints.
c_start (list) – Starting indices for each constraint type in the output vector.
states_list (dict) – Dictionary containing current state variables (positions, velocities, etc.).
globals_dict (dict, optional) – Dictionary containing global variables or parameters.
- Returns:
Constraint vector of shape (ncon,) containing all weighted constraint values.
- Return type:
jnp.ndarray
Notes
This function is typically JIT-compiled for optimal performance
Constraints are weighted and concatenated into a single vector
Used during optimization iterations to evaluate constraint violations
- biosym.ocp.confun.evaluate_jacobian(jacobian_functions, weights, nnz, c_start, nnz_start, states_list, globals_dict=None)#
Evaluate the Jacobian matrix of all constraints for gradient-based optimization.
This function computes the sparse Jacobian matrix of all constraints with respect to the state variables. The Jacobian is essential for efficient gradient-based optimization algorithms used in optimal control.
- Parameters:
jacobian_functions (list) – List of Jacobian functions for each constraint type.
weights (list) – Weighting factors for each constraint type.
nnz (int) – Total number of non-zero entries in the sparse Jacobian.
c_start (list) – Starting row indices for each constraint type in the Jacobian.
nnz_start (list) – Starting indices for non-zero entries of each constraint type.
states_list (dict) – Dictionary containing current state variables.
globals_dict (dict, optional) – Dictionary containing global variables or parameters.
- Returns:
Sparse Jacobian matrix in COO format as (rows, cols, data) where: - rows: Row indices of non-zero entries - cols: Column indices of non-zero entries - data: Values of non-zero entries (weighted)
- Return type:
tuple of jnp.ndarray
Notes
Returns sparse matrix in COO (coordinate) format for efficiency
Jacobian entries are weighted according to constraint weights
Row indices are adjusted to account for constraint concatenation
Used by optimization algorithms for computing search directions