pockit.base.phasebase
Enum class to represent the type of boundary conditions.
Named tuple to store boundary condition information.
A phase is a lower level objective of a multiple-phase optimal control problem.
Initialize a phase with given state, control, and static variables.
States and controls can be given as the number of variables or the list of variable names. If names are given, they are used as the names of the variables. Otherwise, the names are generated automatically as \( x_0, x_1, \dots, x_{n - 1} \).
Static variables should be identical to those defined in the system level.
The identifier
should be unique for each phase in a given system to avoid possible name conflict.
It is recommended to use the System.new_phase()
method of the System
object to create phases instead of manually using this method.
If simplify
is True
, every symbolic expression will be simplified (by sympy.simplify()
) before
being compiled. This will slow down the speed of compilation.
If parallel
is True
, the parallel
flag will be passed to the Numba JIT compiler,
which will generate parallel code for multicore CPUs.
This will slow down the speed of compilation and sometimes the speed of execution.
If fastmath
is True
, the fastmath
flag will be passed to the Numba JIT compiler,
see Numba
and LLVM documentations for details.
Arguments:
- state: Number of state variables or list of state variable names.
- control: Number of control variables or list of control variable names.
- symbol_static_parameter: list of static parameters,
should be identical to those in the
System
object. - identifier: Unique identifier of the phase.
- simplify: Whether to use Sympy to simplify
sympy.Expr
before compilation. - parallel: Whether to use Numba
parallel
mode. - fastmath: Whether to use Numba
fastmath
mode.
Set the dynamics of the phase.
Arguments:
- dynamics: List of time derivatives of states composed with x, u, t, and s.
- cache: Path to the directory to store the compiled functions.
Set the integrals of the phase.
Symbols \( I_0, I_1, \dots, I_{n - 1} \) will be automatically generated and set as a list as the attribute
I
to represent corresponding integrals.
Arguments:
- integral: List of integrals to be concerned composed with x, u, t, and s.
- cache: Path to the directory to store the compiled functions.
Set phase constraints of the system, which is enforced in the entire time interval of the phase.
For equality constraints, set the corresponding entry of lower_bounds
and upper_bounds
to the same value.
For one-sided inequality constraints, set the corresponding entry of lower_bounds
or upper_bounds
to -inf
or inf
.
If the problem to be solved is a bang-bang control problem, set bang_bang_control
as a list of bools indicating
whether the corresponding phase constraint is a bang-bang constraint.
If all phase constraints are bang-bang constraints, bang_bang_control
can also be set to True
directly.
Arguments:
- phase_constraint: List of phase constraints composed with x, u, t, and s
- lower_bound: List of lower bounds of phase constraints
- upper_bound: List of upper bounds of phase constraints
- bang_bang_control: List of bools indicating whether the corresponding phase constraint is a bang-bang constraint.
Alternatively, set
bang_bang_control
as a single bool to apply to all phase constraints. - cache: Path to the directory to store the compiled functions.
Set the boundary condition and initial/terminal time of the phase.
initial_time
, terminal_time
, and each element of initial_value
and terminal_value
can be set as
None
for free, a floating number for fixed, or sympy.Expr
of static parameters.
Arguments:
- initial_value: List of initial values of states.
- terminal_value: List of terminal values of states.
- initial_time: Initial time.
- terminal_time: Terminal time.
- cache: Path to the directory to store the compiled functions.
Set the discretization scheme of the system.
mesh
will be rescaled when needed. If it is set to an integer, a uniform mesh will be used.
If set with an array, it will be used as mesh directly after scaling.
num_point
decides the number of interpolation/integration points in each subinterval.
Same number of points will be used in all subintervals if set with an integer.
Arguments:
- mesh: Number of mesh or mesh points.
- num_point: Number of interpolation and integration points in each subinterval.
Check the continuous error.
Arguments:
- variable: Variable to be checked.
- static_parameter: Static parameter to be checked. Set to
None
if the phase has no static parameters. - absolute_tolerance_continuous: Absolute tolerance for continuous error.
- relative_tolerance_continuous: Relative tolerance for continuous error.
- tolerance_mesh: Skip the check if the mesh width is smaller than this value.
Returns:
True
if the error is within the tolerance,False
otherwise.
Check the discontinuous error.
Arguments:
- variable: Variable to be checked.
- static_parameter: Static parameter to be checked. Set to
None
if the phase has no static parameters. - tolerance_discontinuous: In each subinterval, after scaling to
[0, 1]
, the bang-bang control functions should either be less thantolerance_discontinuous
or greater than1 - tolerance_discontinuous
simultaneously. - tolerance_mesh: Skip the check if the mesh width is smaller than this value.
Returns:
True
if the error is within the tolerance,False
otherwise.
Check the continuous and discontinuous error.
Arguments:
- variable: Variable to be checked.
- static_parameter: Static parameter to be checked. Set to
None
if the phase has no static parameters. - absolute_tolerance_continuous: Absolute tolerance for continuous error.
- relative_tolerance_continuous: Relative tolerance for continuous error.
- tolerance_discontinuous: In each subinterval, after scaling to
[0, 1]
, the bang-bang control functions should either be less thantolerance_discontinuous
or greater than1 - tolerance_discontinuous
simultaneously. - tolerance_mesh: Skip the check if the mesh width is smaller than this value.
Returns:
True
if the error is within the tolerance,False
otherwise.
Adjust the mesh and the number of interpolation points to match the continuous error tolerance.
Arguments:
- variable: Variable of the previous iteration.
- static_parameter: Static parameter of the previous iteration.
Set to
None
if the phase has no static parameters. - absolute_tolerance_continuous: Absolute tolerance for continuous error.
- relative_tolerance_continuous: Relative tolerance for continuous error.
- num_point_min: Minimum number of interpolation points.
- num_point_max: Maximum number of interpolation points.
- mesh_length_min: Minimum mesh length.
- mesh_length_max: Maximum mesh length.
Adjust the mesh and the number of interpolation points to match the discontinuous error tolerance.
Arguments:
- variable: Variable of the previous iteration.
- static_parameter: Static parameter of the previous iteration.
Set to
None
if the phase has no static parameters. - tolerance_discontinuous: In each subinterval, after scaling to
[0, 1]
, the bang-bang control functions should either be less thantolerance_discontinuous
or greater than1 - tolerance_discontinuous
simultaneously. - num_point_min: Minimum number of interpolation points.
- num_point_max: Maximum number of interpolation points.
- mesh_length_min: Minimum mesh length.
- mesh_length_max: Maximum mesh length.
Adjust the mesh and the number of interpolation points to match the error tolerances.
If the discontinuous error is not within the tolerance, refine for discontinuous error. Otherwise, if the continuous error is not within the tolerance, refine for continuous error. At most one of the continuous or discontinuous refinements will be performed.
Arguments:
- variable: Variable of the previous iteration.
- static_parameter: Static parameter of the previous iteration.
Set to
None
if the phase has no static parameters. - absolute_tolerance_continuous: Absolute tolerance for continuous error.
- relative_tolerance_continuous: Relative tolerance for continuous error.
- tolerance_discontinuous: In each subinterval, after scaling to
[0, 1]
, the bang-bang control functions should either be less thantolerance_discontinuous
or greater than1 - tolerance_discontinuous
simultaneously. - num_point_min: Minimum number of interpolation points.
- num_point_max: Maximum number of interpolation points.
- mesh_length_min: Minimum mesh length.
- mesh_length_max: Maximum mesh length.
Lower bounds of optimization constraints.
Upper bounds of optimization constraints.
pockit.base.fastfunc.FastFunc
s of bang-bang constraints.
(scaled to [0, 1]
.)
Index partition of the middle-stage variables.
The left index of the state and control variables.
The right index of the state and control variables (exclusive).
Position of all interpolation nodes in the middle stage, scaled to
[0, 1]
.
The left index of subintervals in the middle stage.
The right index of subintervals in the middle stage (exclusive).
The integration weights of the middle stage.
Function turns the state and control variables into the middle stage.
Translation matrix of a state variable to eliminate the integration coefficient.
The left index of the dynamic constraints of each state variable.
The right index of the dynamic constraints of each state variable (exclusive).
Position of all interpolation nodes in the middle stage, scaled to
[0, 1]
with one additional interpolation point in each
subinterval.
The left index of subintervals in the middle stage, with one additional interpolation point in each subinterval.
The right index of subintervals in the middle stage (exclusive), with one additional interpolation point in each subinterval.
The number of interpolation points in the middle stage, with one additional interpolation point in each subinterval.
The integration weights of each subinterval in the middle stage, with one additional interpolation point in each subinterval.
Function to compute the polynomial matrix given the number of interpolation points.
The value matrix translates all state and control variables to the middle stage with one additional interpolation point in each subinterval.
The translation matrix of all state variables, with one additional interpolation point in each subinterval.
The integration matrix of a middle-stage variable, with one additional interpolation point in each subinterval.
Position of all interpolation nodes of a state variable, scaled to
[0, 1]
.
Position of all interpolation nodes of a control variable, scaled to
[0, 1]
.
The left index of subintervals of a state variable.
The right index of subintervals of a state variable (exclusive).
The left index of subintervals of a control variable.