GammaDDPC#
- class do_dpc.dpc.gamma_ddpc.GammaDDPC(dpc_params, training_data)[source]#
Bases:
DPCImplements gamma-DPC based on DPC.
- gamma_2_cp#
Slack decision variable for the gamma parameter in the optimization.
- Type:
cp.Variable
- lambda_gamma_2#
Tunable parameter associated with the gamma-2 term.
- Type:
float
Methods#
add_custom_constraints#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.add_custom_constraints(self, constraints)#
Adds custom constraints to the optimization problem.
Note
Users should access cvxpy parameters and decision variables directly from the DPC class.
All such attributes are denoted by the _cp suffix.
Ensure that added constraints do not conflict with existing ones.
For simple linear inequality constraints, prefer using set_input_constraint or set_output_constraint.
- Parameters:
constraints (list[Constraint]) – A list of cvxpy constraints.
add_input_constraints#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.add_input_constraints(self, u_bounds)#
Sets input constraints based on provided bounds.
- Parameters:
u_bounds (Bounds) – An instance of Bounds containing upper (max_values) and lower (min_values) constraints for the control inputs.
- Raises:
ValueError – If u_bounds.max_values and u_bounds.min_values have incorrect shapes.
ValueError – If u_bounds.max_values is smaller than u_bounds.min_values.
- Special Cases:
To impose no bounds on specific inputs, set np.inf as the upper bound and -np.inf as the lower bound.
add_linear_constraints#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.add_linear_constraints(self, A, b)#
Adds linear inequality constraints to the optimization problem.
The constraints are applied in the form:
A @ [ y_f u_f ]^T <= b
- where:
A is a matrix of shape (n_constraints, n_y_f + n_u_f)
y_f (future outputs) is of shape (n_y_f, )
u_f (future inputs) is of shape (n_u_f, )
b is a vector of shape (n_constraints, )
- Parameters:
A (np.ndarray) – Constraint matrix of shape (n_constraints, n_y_f + n_u_f).
b (np.ndarray) – Constraint vector of shape (n_constraints, ).
- Raises:
ValueError – If A and b have incompatible shapes.
TypeError – If A or b are not numpy arrays.
Warning – If the first p columns of A are nonzero, indicating constraints on the first outputs.
add_terminal_output_constraints#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.add_terminal_output_constraints(self, y_bounds)#
Adds terminal output constraints for the final step of the prediction horizon.
Notes
Only the final predicted output in the finite future horizon is constrained.
Due to system lag, immediate output constraints may not always be enforceable.
This design choice enhances solver feasibility for a wide range of systems.
If stricter constraints are needed, use add_custom_constraints.
- Parameters:
y_bounds (Bounds) – A Bounds instance defining upper (max_values) and lower (min_values) constraints for system outputs.
- Raises:
ValueError – If y_bounds.max_values and y_bounds.min_values have incorrect shapes.
ValueError – If any element in y_bounds.max_values is smaller than its corresponding y_bounds.min_values.
- Special Cases:
To impose no bounds on specific outputs, set np.inf as the upper bound and -np.inf as the lower bound.
build_optimization_problem#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.build_optimization_problem(self)#
Constructs the DPC optimization problem.
\[\begin{split}\min_{u_f, \cdot} &\quad \|y_f - y_r\|_Q^2 + \|u_f - u_r\|_R^2 + r(\cdot) \\ \text{s.t.} &\quad y_f = f(\cdot) \\ &\quad u_f \in \mathcal{U}, \quad y_f \in \mathcal{Y}\end{split}\]where \(r(\cdot)\), \(f(\cdot)\) are linear functions which utilize matrices calculated in the Offline Calculations. Additional slack decision variables can be introduced. \(r(\cdot)\), \(f(\cdot)\) differ between different DPC algorithm.
The constraints for \(u_f\), \(y_f\) are handled by other methods.
calculate_closed_form_solution_matrices#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.calculate_closed_form_solution_matrices(self)#
Calculates and returns the closed-form gains for the DPC controller.
The gains are calculated using the pseudo-inverse of the transformed matrices.
- Returns:
The closed-form gains for the DPC controller,
- Return type:
Optional[DPCClosedFormSolutionMatrices]
calculate_predictor_matrices#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.calculate_predictor_matrices(self)#
Computes the L matrices.
- Return type:
- Returns:
GammaDDPCOfflineData
calculate_regularization_matrices#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.calculate_regularization_matrices(self)#
Calculates regularization matrices for optimal control.
- Return type:
get_next_control_action#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.get_next_control_action(self)#
Retrieves the next computed control input.
- Returns:
The next control input of shape (m,).
- Return type:
np.ndarray
- Raises:
IndexError – If the control horizon is exceeded.
RuntimeError – If the optimization problem has not been solved or the solution is invalid.
get_predictor_constraint_expression#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.get_predictor_constraint_expression(self)#
Calculates and returns the CVXPY constraint for the predictor constraint.
The predictor constraint is calculated as follows:
\[\begin{split}\gamma_1 = L_{11}^{-1} z_p \\ \begin{bmatrix} u_f \\ y_f \end{bmatrix} = L_{23} \begin{bmatrix} \gamma_1 \\ \gamma_2 \end{bmatrix}\end{split}\]- Returns:
The CVXPY constraint for the predictor constraint.
- Return type:
cp.constraints.Constraint
get_regularization_cost_expression#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.get_regularization_cost_expression(self)#
Calculates and returns the CVXPY expression for the regularization cost.
The regularization cost is calculated as follows:
\[\text{cost} = \lambda_{\gamma_2} \|\gamma_2\|_2^2\]- Returns:
The CVXPY expression for the regularization cost.
- Return type:
cp.Expression
instantiate#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.instantiate(dpc_type, *args, **kwargs)#
Factory method to create an instance of a registered DPC subclass.
- Parameters:
dpc_type (str) – The type of DPC controller to instantiate.
*args – Positional arguments for the subclass constructor.
**kwargs – Keyword arguments for the subclass constructor.
- Returns:
An instance of the corresponding DPC subclass.
- Return type:
- Raises:
ValueError – If the specified dpc_type is not registered.
register#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.register(dpc_type)#
Decorator to register subclasses for dynamic instantiation.
- Usage:
@DPC.register(“my_dpc”) class MyDPC(DPC):
…
- Parameters:
dpc_type (str) – Name of the DPC subclass type.
- Returns:
The decorator function.
- Return type:
Callable[[Type[“DPC”]], Type[“DPC”]]
reset_constraints#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.reset_constraints(self)#
Removes all existing constraints from the optimization problem.
- Return type:
None
set_lambda_gamma_2#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.set_lambda_gamma_2(self, new_lambda_gamma_2)#
Set the value of lambda_gamma_2, ensuring it is positive.
- Parameters:
new_lambda_gamma_2 (float) – The new value for lambda_gamma_2. Must be a positive number.
Note
The closed-form data will be recalculated as the matrices depend on lambda_gamma_2.
- Raises:
TypeError – If new_lambda_gamma_2 is not a number.
ValueError – If new_lambda_gamma_2 is not positive.
set_state_x#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.set_state_x(self, x_new)#
Sets the state (x) for the estimated model.
This method is intended to be overridden by subclasses that have a model, such as MPC variants.
solve#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.solve(self, verbose=False, solver='SCS', **kwargs)#
Solves the optimization problem to calculate the optimal control input.
Recommended to use SCS as it does not require a license. MOSEK should be used for better performance if a license is available.
- Parameters:
verbose (bool) – If True, solver output is displayed.
solver (str) – Solver to use (default: SCS).
**kwargs – Additional solver parameters.
- Raises:
ValueError – If the optimization problem is not properly built.
ValueError – If the cf_gains are None while using the closed-form solution.
RuntimeError – If the solver encounters an issue or produces an infeasible result.
Warning – If another solver than Mosek is used.
update_past_measurements#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.update_past_measurements(self, z_p_new)#
Updates the stored past measurements with new incoming data.
- Parameters:
z_p_new (np.ndarray) – New past measurement data (shape (mp,)).
- Raises:
ValueError – If z_p_new does not match the expected shape.
update_tracking_reference#
- do_dpc.dpc.gamma_ddpc.GammaDDPC.update_tracking_reference(self, y_r_new, u_r_new)#
Updates the tracking reference trajectory.
- Parameters:
y_r_new (np.ndarray) – New reference trajectory for outputs (shape (p,)).
u_r_new (np.ndarray) – New reference trajectory for inputs (shape (m,)).
- Raises:
ValueError – If y_r_new does not match expected dimensions (p,).
ValueError – If u_r_new does not match expected dimensions (m,).