opengen.ocp package
Submodules
opengen.ocp.builder module
Builders and runtime wrappers for OCP-generated optimizers.
- class opengen.ocp.builder.GeneratedOptimizer(optimizer_name, target_dir, backend, backend_kind, ocp=None, symbolic_model=None, metadata=None)
Bases:
objectHigh-level runtime wrapper around a generated optimizer.
This class hides whether the underlying solver is consumed through direct Python bindings or through the TCP interface and exposes a uniform
solve(x0=..., xref=...)API based on named parameters.- __init__(optimizer_name, target_dir, backend, backend_kind, ocp=None, symbolic_model=None, metadata=None)
Construct a generated optimizer wrapper.
- Parameters:
optimizer_name – generated optimizer name
target_dir – generated optimizer directory
backend – low-level backend object
backend_kind – backend type, e.g.
"direct"or"tcp"ocp – source OCP definition
symbolic_model – precomputed symbolic model of the OCP
metadata – serialized optimizer metadata used when reloading an optimizer from disk
- property backend_kind
Backend kind used by this optimizer wrapper.
- kill()
Stop the backend if it is a local TCP server.
- classmethod load(json_path)
Load a previously saved optimizer manifest.
- Parameters:
json_path – path to a JSON manifest created by
save()- Returns:
reconstructed
GeneratedOptimizer
- property optimizer_name
Name of the generated optimizer.
- save(json_path=None)
Save a manifest that can later recreate this optimizer.
The manifest is stored in the generated optimizer directory by default. For single-shooting optimizers, the rollout function is stored in a separate
rollout.casadifile next to the manifest.- Parameters:
json_path – optional destination manifest path
- Returns:
current instance
- solve(initial_guess=None, initial_lagrange_multipliers=None, initial_penalty=None, **parameter_values)
Solve the generated OCP optimizer.
Named keyword arguments are packed according to the declared OCP parameters. For example, if the OCP declares
x0andxref, the solver can be called asoptimizer.solve(x0=x0, xref=xref)to solve the OCP for a given initial condition \(x_0\) and reference \(x^{\mathrm{ref}}\).- Parameters:
initial_guess – optional initial decision-variable guess
initial_lagrange_multipliers – optional initial ALM multipliers
initial_penalty – optional initial penalty parameter
parameter_values – named parameter values
- Returns:
OcpSolution- Raises:
RuntimeError – if the backend is unavailable or the low-level solve call fails
- start()
Start the backend if it is a local TCP server.
- Returns:
current instance
- property target_dir
Directory of the generated optimizer project.
- class opengen.ocp.builder.OCPBuilder(problem, metadata, build_configuration=<opengen.config.build_config.BuildConfiguration object>, solver_configuration=None)
Bases:
objectBuilder that lowers an OCP to the low-level OpEn builder stack.
- __init__(problem, metadata, build_configuration=<opengen.config.build_config.BuildConfiguration object>, solver_configuration=None)
Construct an OCP builder.
- Parameters:
problem – instance of
OptimalControlProblemmetadata – optimizer metadata
build_configuration – OpEn build configuration
solver_configuration – OpEn solver configuration
- build()
Generate, compile, and wrap the optimizer.
- Returns:
- build_problem(symbolic_model=None)
Lower the OCP to a low-level
opengen.builder.problem.Problem.- Returns:
low-level OpEn problem
- with_generate_not_build_flag(flag)
Generate code without compiling it.
- Parameters:
flag – whether to generate only
- Returns:
current instance
opengen.ocp.constraint_utils module
Helpers for simplifying products of OCP constraint sets.
- opengen.ocp.constraint_utils.make_constraint_product(segments, constraints)
Build the most specific set representing a block product of constraints.
- opengen.ocp.constraint_utils.rectangle_bounds(constraint, dimension)
Return explicit box bounds when a set admits a rectangle representation.
- opengen.ocp.constraint_utils.segment_dimensions(segments)
Compute segment dimensions from Cartesian-product end indices.
opengen.ocp.dynamics module
Continuous-time dynamics discretization helpers for OCP problems.
- class opengen.ocp.dynamics.DynamicsDiscretizer(continuous_dynamics, sampling_time)
Bases:
objectDiscretizer for continuous-time dynamics.
This helper wraps a continuous-time model of the form \(\dot{x} = f(x, u, p)\) and produces a discrete-time callback compatible with
opengen.ocp.problem.OptimalControlProblem.with_dynamics().- __init__(continuous_dynamics, sampling_time)
Construct a dynamics discretizer.
- Parameters:
continuous_dynamics – callable implementing \(\dot{x} = f(x, u, p)\)
sampling_time – sampling time \(T_s\)
- Raises:
ValueError – if the sampling time is not positive
- property continuous_dynamics
Continuous-time dynamics callback.
- discretize(method='euler')
Return a discrete-time dynamics callback for a chosen method.
Supported methods are
"euler","midpoint","heun", and"rk4".- Parameters:
method – discretization method
- Returns:
discrete-time dynamics callback
- Raises:
ValueError – if the method is unknown
- euler()
Return an explicit Euler discretization callback.
The returned callback implements:
\(x^+ = x + T_s f(x, u, p)\).
- heun()
Return a Heun discretization callback.
The returned callback implements the explicit trapezoidal method:
\(x^+ = x + \tfrac{T_s}{2}(k_1 + k_2)\),
where \(k_1 = f(x, u, p)\) and \(k_2 = f(x + T_s k_1, u, p)\).
- midpoint()
Return an explicit midpoint discretization callback.
The returned callback implements the second-order Runge-Kutta rule:
\(x^+ = x + T_s f(x + \tfrac{T_s}{2} k_1, u, p)\),
where \(k_1 = f(x, u, p)\).
- multistep(method='rk4', num_steps=1)
Return a discrete-time callback with internal substeps.
This helper subdivides the sampling interval \(T_s\) into
num_stepssmaller steps and applies the chosen explicit method on each subinterval.Supported methods are
"euler","midpoint","heun", and"rk4".- Parameters:
method – base discretization method
num_steps – number of internal substeps
- Returns:
discrete-time dynamics callback
- Raises:
ValueError – if
num_stepsis not a positive integer or the method is unknown
- rk4()
Return a classical fourth-order Runge-Kutta discretization callback.
The returned callback implements:
\(x^+ = x + \frac{T_s}{6}(k_1 + 2k_2 + 2k_3 + k_4)\),
where the intermediate slopes are computed from the continuous-time dynamics.
- property sampling_time
Sampling time \(T_s\).
opengen.ocp.parameter module
Utilities for named OCP parameters and flat OpEn parameter vectors.
- class opengen.ocp.parameter.ParameterDefinition(name, size, default=None)
Bases:
objectDefinition of a named parameter block.
A parameter block has a name, a dimension and, optionally, a default numeric value. Multiple parameter blocks are later packed into the flat parameter vector expected by the low-level OpEn builder.
- __init__(name, size, default=None)
Construct a parameter definition.
- Parameters:
name – parameter name
size – block dimension
default – optional default numeric value
- Raises:
ValueError – if the name, size or default are invalid
- property default
Default value as a list of floats, or
Noneif absent.
- has_default()
Whether this parameter block has a default value.
- property name
Name of the parameter block.
- property size
Dimension of the parameter block.
- class opengen.ocp.parameter.ParameterPack(symbol_type=<function GenSX.sym>)
Bases:
objectRegistry and packer for named parameters.
This class stores the declared parameter blocks of an OCP and provides helpers to:
construct a flat symbolic parameter vector,
create named symbolic views into that vector, and
pack concrete numeric values for calls to the generated solver.
- __init__(symbol_type=<function GenSX.sym>)
Construct an empty parameter registry.
- Parameters:
symbol_type – CasADi symbol constructor, typically
cs.SX.symorcs.MX.sym
- add(name, size, default=None)
Add a named parameter block.
- Parameters:
name – parameter name
size – block dimension
default – optional default numeric value
- Returns:
current instance
- Raises:
ValueError – if a parameter with the same name already exists
- definitions()
Return the declared parameter definitions.
- pack(values=None)
Pack numeric values into the flat parameter vector.
- Parameters:
values – dictionary mapping parameter names to values
- Returns:
flat list of floats
- Raises:
ValueError – if a required parameter is missing or dimensions are inconsistent
- slices()
Return the slice map of all parameter blocks.
- Returns:
dictionary
name -> (start, stop)
- symbol(name='p')
Construct the packed symbolic parameter vector.
- Parameters:
name – symbolic variable name
- Returns:
CasADi symbolic vector containing all parameter blocks
- total_size()
Return the total dimension of the packed parameter vector.
- view(packed_symbol)
Create a named symbolic view of a packed parameter vector.
- class opengen.ocp.parameter.ParameterView(packed_symbol, slices)
Bases:
objectDictionary-like accessor for symbolic parameter slices.
Instances of this class are passed to user-defined callbacks so they can access named symbolic parameters using expressions such as
param["xref"].- __init__(packed_symbol, slices)
Construct a parameter view.
- Parameters:
packed_symbol – flat symbolic parameter vector
slices – dictionary mapping parameter names to index pairs
- get(name, default=None)
Return the symbolic slice associated with
nameif it exists.
- keys()
Return all known parameter names.
- property packed
The underlying flat symbolic parameter vector.
opengen.ocp.problem module
High-level optimal control problem definitions for opengen.
- class opengen.ocp.problem.OptimalControlProblem(nx, nu, horizon, shooting=ShootingMethod.SINGLE)
Bases:
objectHigh-level specification of a finite-horizon optimal control problem.
The OCP layer collects dynamics, costs, parameters and constraints in an OCP-oriented format and later lowers them to the low-level
opengen.builder.problem.Problemabstraction.- __init__(nx, nu, horizon, shooting=ShootingMethod.SINGLE)
Construct a new optimal control problem.
- Parameters:
nx – state dimension
nu – input dimension
horizon – prediction horizon
shooting – shooting formulation, either single or multiple
- Raises:
ValueError – if dimensions are invalid
- add_parameter(name, size, default=None)
Add a named parameter block to the OCP.
- Parameters:
name – parameter name
size – parameter dimension
default – optional default numeric value
- Returns:
current instance
- Raises:
ValueError – if the parameter definition is invalid or a parameter with the same name has already been declared
- build_symbolic_model()
Lower the OCP to a symbolic model understood by the OCP builder.
The returned dictionary contains the decision variables, packed parameter vector, symbolic cost, optional ALM/penalty mappings and the symbolic state trajectory. In particular, the returned mappings are the low-level objects later used to define constraints of the form \(F_1(u, p) \in C\) and \(F_2(u, p) = 0\).
- Returns:
symbolic model dictionary
- property dynamics
User-defined dynamics callback.
- property hard_stage_state_input_constraints
Hard multiple-shooting constraints on stage-wise
(x_t, u_t).
- property hard_terminal_state_constraints
Hard multiple-shooting constraints on the terminal state
x_N.
- property horizon
Prediction horizon.
- property input_constraints
Hard stage-wise input constraints.
- property nu
Input dimension.
- property nx
State dimension.
- property parameters
Declared named parameters.
- property path_constraints
Stage-wise symbolic PM/ALM constraints.
- property shooting
Selected shooting formulation.
- property symbol_type
CasADi symbol constructor used internally.
- property terminal_constraints
Terminal symbolic PM/ALM constraints.
- validate()
Validate the OCP definition before lowering it.
- Returns:
current instance
- Raises:
ValueError – if mandatory components are missing or a hard state-based constraint is used in single shooting
- with_dynamics(dynamics)
Specify the system dynamics callback.
The callback must accept
(x, u, param)and return the next state \(x^+ = F(x, u, p)\).Note that the callback must be a function involving symbolic computations using CasADi functions.
- Parameters:
dynamics – dynamics callback
- Returns:
current instance
- with_dynamics_constraints(kind='penalty')
Choose how multiple-shooting defect constraints are imposed.
Supported values are
"penalty"and"alm". This option is only relevant in multiple shooting. The corresponding defect constraints are of the form \(x_{t+1} - F(x_t, u_t, p)\).- Parameters:
kind – constraint treatment for dynamics defects
- Returns:
current instance
- Raises:
ValueError – if
kindis invalid
- with_hard_stage_state_input_constraints(constraints)
Specify hard stage-wise constraints on \((x_t, u_t)\).
This method is only meaningful in the multiple-shooting formulation.
- Parameters:
constraints – set acting on the stacked vector \(\operatorname{vertcat}(x_t, u_t)\)
- Returns:
current instance
- with_hard_terminal_state_constraints(constraints)
Specify hard constraints on the terminal state \(x_N\).
This method is only meaningful in the multiple-shooting formulation.
- Parameters:
constraints – set acting on \(x_N\)
- Returns:
current instance
- with_input_constraints(constraints)
Specify hard constraints on the stage-wise control inputs.
In single shooting these are the only hard constraints that act directly on decision variables. In multiple shooting they are applied to every input block \(u_t\).
- Parameters:
constraints – instance of
opengen.constraints.Constraint- Returns:
current instance
- with_path_constraint(constraint, kind='penalty', set_c=None, set_y=None)
Add a stage-wise symbolic constraint.
The callback must accept
(x, u, param, stage_idx). Whenkind="penalty", its output is appended to the penalty mapping \(F_2\). Whenkind="alm", its output is appended to the ALM mapping \(F_1\) and the user must also provideset_cso that the constraints take the form \(F_1(x_t, u_t, p) \in C\).- Parameters:
constraint – stage constraint callback
kind – either
"penalty"or"alm"set_c – set \(C\) for ALM constraints
set_y – optional dual set \(Y\) for ALM constraints
- Returns:
current instance
- with_stage_cost(stage_cost)
Specify the stage cost callback.
The callback must accept
(x, u, param, stage_idx).- Parameters:
stage_cost – stage cost callback
- Returns:
current instance
- with_terminal_constraint(constraint, kind='penalty', set_c=None, set_y=None)
Add a terminal symbolic constraint.
The callback must accept
(x, param). PM and ALM semantics are the same as inwith_path_constraint().- Parameters:
constraint – terminal constraint callback
kind – either
"penalty"or"alm"set_c – set \(C\) for ALM constraints
set_y – optional dual set \(Y\) for ALM constraints
- Returns:
current instance
- with_terminal_cost(terminal_cost)
Specify the terminal cost callback.
The callback must accept
(x, param).- Parameters:
terminal_cost – terminal cost callback
- Returns:
current instance
opengen.ocp.solution module
High-level solution objects returned by OCP-generated optimizers.
- class opengen.ocp.solution.OcpSolution(raw, inputs, states)
Bases:
objectHigh-level solution returned by
GeneratedOptimizer.solve().The object wraps the raw low-level solver status and adds OCP-oriented views such as stage-wise inputs and the reconstructed state trajectory.
- __init__(raw, inputs, states)
Construct an OCP solution object.
- Parameters:
raw – raw low-level solver status
inputs – stage-wise input sequence
states – state trajectory
Module contents
High-level OCP interface for building optimizers with opengen.
- class opengen.ocp.DynamicsDiscretizer(continuous_dynamics, sampling_time)
Bases:
objectDiscretizer for continuous-time dynamics.
This helper wraps a continuous-time model of the form \(\dot{x} = f(x, u, p)\) and produces a discrete-time callback compatible with
opengen.ocp.problem.OptimalControlProblem.with_dynamics().- __init__(continuous_dynamics, sampling_time)
Construct a dynamics discretizer.
- Parameters:
continuous_dynamics – callable implementing \(\dot{x} = f(x, u, p)\)
sampling_time – sampling time \(T_s\)
- Raises:
ValueError – if the sampling time is not positive
- property continuous_dynamics
Continuous-time dynamics callback.
- discretize(method='euler')
Return a discrete-time dynamics callback for a chosen method.
Supported methods are
"euler","midpoint","heun", and"rk4".- Parameters:
method – discretization method
- Returns:
discrete-time dynamics callback
- Raises:
ValueError – if the method is unknown
- euler()
Return an explicit Euler discretization callback.
The returned callback implements:
\(x^+ = x + T_s f(x, u, p)\).
- heun()
Return a Heun discretization callback.
The returned callback implements the explicit trapezoidal method:
\(x^+ = x + \tfrac{T_s}{2}(k_1 + k_2)\),
where \(k_1 = f(x, u, p)\) and \(k_2 = f(x + T_s k_1, u, p)\).
- midpoint()
Return an explicit midpoint discretization callback.
The returned callback implements the second-order Runge-Kutta rule:
\(x^+ = x + T_s f(x + \tfrac{T_s}{2} k_1, u, p)\),
where \(k_1 = f(x, u, p)\).
- multistep(method='rk4', num_steps=1)
Return a discrete-time callback with internal substeps.
This helper subdivides the sampling interval \(T_s\) into
num_stepssmaller steps and applies the chosen explicit method on each subinterval.Supported methods are
"euler","midpoint","heun", and"rk4".- Parameters:
method – base discretization method
num_steps – number of internal substeps
- Returns:
discrete-time dynamics callback
- Raises:
ValueError – if
num_stepsis not a positive integer or the method is unknown
- rk4()
Return a classical fourth-order Runge-Kutta discretization callback.
The returned callback implements:
\(x^+ = x + \frac{T_s}{6}(k_1 + 2k_2 + 2k_3 + k_4)\),
where the intermediate slopes are computed from the continuous-time dynamics.
- property sampling_time
Sampling time \(T_s\).
- class opengen.ocp.GeneratedOptimizer(optimizer_name, target_dir, backend, backend_kind, ocp=None, symbolic_model=None, metadata=None)
Bases:
objectHigh-level runtime wrapper around a generated optimizer.
This class hides whether the underlying solver is consumed through direct Python bindings or through the TCP interface and exposes a uniform
solve(x0=..., xref=...)API based on named parameters.- __init__(optimizer_name, target_dir, backend, backend_kind, ocp=None, symbolic_model=None, metadata=None)
Construct a generated optimizer wrapper.
- Parameters:
optimizer_name – generated optimizer name
target_dir – generated optimizer directory
backend – low-level backend object
backend_kind – backend type, e.g.
"direct"or"tcp"ocp – source OCP definition
symbolic_model – precomputed symbolic model of the OCP
metadata – serialized optimizer metadata used when reloading an optimizer from disk
- property backend_kind
Backend kind used by this optimizer wrapper.
- kill()
Stop the backend if it is a local TCP server.
- classmethod load(json_path)
Load a previously saved optimizer manifest.
- Parameters:
json_path – path to a JSON manifest created by
save()- Returns:
reconstructed
GeneratedOptimizer
- property optimizer_name
Name of the generated optimizer.
- save(json_path=None)
Save a manifest that can later recreate this optimizer.
The manifest is stored in the generated optimizer directory by default. For single-shooting optimizers, the rollout function is stored in a separate
rollout.casadifile next to the manifest.- Parameters:
json_path – optional destination manifest path
- Returns:
current instance
- solve(initial_guess=None, initial_lagrange_multipliers=None, initial_penalty=None, **parameter_values)
Solve the generated OCP optimizer.
Named keyword arguments are packed according to the declared OCP parameters. For example, if the OCP declares
x0andxref, the solver can be called asoptimizer.solve(x0=x0, xref=xref)to solve the OCP for a given initial condition \(x_0\) and reference \(x^{\mathrm{ref}}\).- Parameters:
initial_guess – optional initial decision-variable guess
initial_lagrange_multipliers – optional initial ALM multipliers
initial_penalty – optional initial penalty parameter
parameter_values – named parameter values
- Returns:
- Raises:
RuntimeError – if the backend is unavailable or the low-level solve call fails
- start()
Start the backend if it is a local TCP server.
- Returns:
current instance
- property target_dir
Directory of the generated optimizer project.
- class opengen.ocp.OCPBuilder(problem, metadata, build_configuration=<opengen.config.build_config.BuildConfiguration object>, solver_configuration=None)
Bases:
objectBuilder that lowers an OCP to the low-level OpEn builder stack.
- __init__(problem, metadata, build_configuration=<opengen.config.build_config.BuildConfiguration object>, solver_configuration=None)
Construct an OCP builder.
- Parameters:
problem – instance of
OptimalControlProblemmetadata – optimizer metadata
build_configuration – OpEn build configuration
solver_configuration – OpEn solver configuration
- build()
Generate, compile, and wrap the optimizer.
- Returns:
- build_problem(symbolic_model=None)
Lower the OCP to a low-level
opengen.builder.problem.Problem.- Returns:
low-level OpEn problem
- with_generate_not_build_flag(flag)
Generate code without compiling it.
- Parameters:
flag – whether to generate only
- Returns:
current instance
- class opengen.ocp.OcpSolution(raw, inputs, states)
Bases:
objectHigh-level solution returned by
GeneratedOptimizer.solve().The object wraps the raw low-level solver status and adds OCP-oriented views such as stage-wise inputs and the reconstructed state trajectory.
- __init__(raw, inputs, states)
Construct an OCP solution object.
- Parameters:
raw – raw low-level solver status
inputs – stage-wise input sequence
states – state trajectory
- class opengen.ocp.OptimalControlProblem(nx, nu, horizon, shooting=ShootingMethod.SINGLE)
Bases:
objectHigh-level specification of a finite-horizon optimal control problem.
The OCP layer collects dynamics, costs, parameters and constraints in an OCP-oriented format and later lowers them to the low-level
opengen.builder.problem.Problemabstraction.- __init__(nx, nu, horizon, shooting=ShootingMethod.SINGLE)
Construct a new optimal control problem.
- Parameters:
nx – state dimension
nu – input dimension
horizon – prediction horizon
shooting – shooting formulation, either single or multiple
- Raises:
ValueError – if dimensions are invalid
- add_parameter(name, size, default=None)
Add a named parameter block to the OCP.
- Parameters:
name – parameter name
size – parameter dimension
default – optional default numeric value
- Returns:
current instance
- Raises:
ValueError – if the parameter definition is invalid or a parameter with the same name has already been declared
- build_symbolic_model()
Lower the OCP to a symbolic model understood by the OCP builder.
The returned dictionary contains the decision variables, packed parameter vector, symbolic cost, optional ALM/penalty mappings and the symbolic state trajectory. In particular, the returned mappings are the low-level objects later used to define constraints of the form \(F_1(u, p) \in C\) and \(F_2(u, p) = 0\).
- Returns:
symbolic model dictionary
- property dynamics
User-defined dynamics callback.
- property hard_stage_state_input_constraints
Hard multiple-shooting constraints on stage-wise
(x_t, u_t).
- property hard_terminal_state_constraints
Hard multiple-shooting constraints on the terminal state
x_N.
- property horizon
Prediction horizon.
- property input_constraints
Hard stage-wise input constraints.
- property nu
Input dimension.
- property nx
State dimension.
- property parameters
Declared named parameters.
- property path_constraints
Stage-wise symbolic PM/ALM constraints.
- property shooting
Selected shooting formulation.
- property symbol_type
CasADi symbol constructor used internally.
- property terminal_constraints
Terminal symbolic PM/ALM constraints.
- validate()
Validate the OCP definition before lowering it.
- Returns:
current instance
- Raises:
ValueError – if mandatory components are missing or a hard state-based constraint is used in single shooting
- with_dynamics(dynamics)
Specify the system dynamics callback.
The callback must accept
(x, u, param)and return the next state \(x^+ = F(x, u, p)\).Note that the callback must be a function involving symbolic computations using CasADi functions.
- Parameters:
dynamics – dynamics callback
- Returns:
current instance
- with_dynamics_constraints(kind='penalty')
Choose how multiple-shooting defect constraints are imposed.
Supported values are
"penalty"and"alm". This option is only relevant in multiple shooting. The corresponding defect constraints are of the form \(x_{t+1} - F(x_t, u_t, p)\).- Parameters:
kind – constraint treatment for dynamics defects
- Returns:
current instance
- Raises:
ValueError – if
kindis invalid
- with_hard_stage_state_input_constraints(constraints)
Specify hard stage-wise constraints on \((x_t, u_t)\).
This method is only meaningful in the multiple-shooting formulation.
- Parameters:
constraints – set acting on the stacked vector \(\operatorname{vertcat}(x_t, u_t)\)
- Returns:
current instance
- with_hard_terminal_state_constraints(constraints)
Specify hard constraints on the terminal state \(x_N\).
This method is only meaningful in the multiple-shooting formulation.
- Parameters:
constraints – set acting on \(x_N\)
- Returns:
current instance
- with_input_constraints(constraints)
Specify hard constraints on the stage-wise control inputs.
In single shooting these are the only hard constraints that act directly on decision variables. In multiple shooting they are applied to every input block \(u_t\).
- Parameters:
constraints – instance of
opengen.constraints.Constraint- Returns:
current instance
- with_path_constraint(constraint, kind='penalty', set_c=None, set_y=None)
Add a stage-wise symbolic constraint.
The callback must accept
(x, u, param, stage_idx). Whenkind="penalty", its output is appended to the penalty mapping \(F_2\). Whenkind="alm", its output is appended to the ALM mapping \(F_1\) and the user must also provideset_cso that the constraints take the form \(F_1(x_t, u_t, p) \in C\).- Parameters:
constraint – stage constraint callback
kind – either
"penalty"or"alm"set_c – set \(C\) for ALM constraints
set_y – optional dual set \(Y\) for ALM constraints
- Returns:
current instance
- with_stage_cost(stage_cost)
Specify the stage cost callback.
The callback must accept
(x, u, param, stage_idx).- Parameters:
stage_cost – stage cost callback
- Returns:
current instance
- with_terminal_constraint(constraint, kind='penalty', set_c=None, set_y=None)
Add a terminal symbolic constraint.
The callback must accept
(x, param). PM and ALM semantics are the same as inwith_path_constraint().- Parameters:
constraint – terminal constraint callback
kind – either
"penalty"or"alm"set_c – set \(C\) for ALM constraints
set_y – optional dual set \(Y\) for ALM constraints
- Returns:
current instance
- with_terminal_cost(terminal_cost)
Specify the terminal cost callback.
The callback must accept
(x, param).- Parameters:
terminal_cost – terminal cost callback
- Returns:
current instance
- class opengen.ocp.ParameterDefinition(name, size, default=None)
Bases:
objectDefinition of a named parameter block.
A parameter block has a name, a dimension and, optionally, a default numeric value. Multiple parameter blocks are later packed into the flat parameter vector expected by the low-level OpEn builder.
- __init__(name, size, default=None)
Construct a parameter definition.
- Parameters:
name – parameter name
size – block dimension
default – optional default numeric value
- Raises:
ValueError – if the name, size or default are invalid
- property default
Default value as a list of floats, or
Noneif absent.
- has_default()
Whether this parameter block has a default value.
- property name
Name of the parameter block.
- property size
Dimension of the parameter block.
- class opengen.ocp.ParameterPack(symbol_type=<function GenSX.sym>)
Bases:
objectRegistry and packer for named parameters.
This class stores the declared parameter blocks of an OCP and provides helpers to:
construct a flat symbolic parameter vector,
create named symbolic views into that vector, and
pack concrete numeric values for calls to the generated solver.
- __init__(symbol_type=<function GenSX.sym>)
Construct an empty parameter registry.
- Parameters:
symbol_type – CasADi symbol constructor, typically
cs.SX.symorcs.MX.sym
- add(name, size, default=None)
Add a named parameter block.
- Parameters:
name – parameter name
size – block dimension
default – optional default numeric value
- Returns:
current instance
- Raises:
ValueError – if a parameter with the same name already exists
- definitions()
Return the declared parameter definitions.
- pack(values=None)
Pack numeric values into the flat parameter vector.
- Parameters:
values – dictionary mapping parameter names to values
- Returns:
flat list of floats
- Raises:
ValueError – if a required parameter is missing or dimensions are inconsistent
- slices()
Return the slice map of all parameter blocks.
- Returns:
dictionary
name -> (start, stop)
- symbol(name='p')
Construct the packed symbolic parameter vector.
- Parameters:
name – symbolic variable name
- Returns:
CasADi symbolic vector containing all parameter blocks
- total_size()
Return the total dimension of the packed parameter vector.
- view(packed_symbol)
Create a named symbolic view of a packed parameter vector.
- class opengen.ocp.ParameterView(packed_symbol, slices)
Bases:
objectDictionary-like accessor for symbolic parameter slices.
Instances of this class are passed to user-defined callbacks so they can access named symbolic parameters using expressions such as
param["xref"].- __init__(packed_symbol, slices)
Construct a parameter view.
- Parameters:
packed_symbol – flat symbolic parameter vector
slices – dictionary mapping parameter names to index pairs
- get(name, default=None)
Return the symbolic slice associated with
nameif it exists.
- keys()
Return all known parameter names.
- property packed
The underlying flat symbolic parameter vector.