pockit-optimal-control 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pockit/__init__.py +16 -0
- pockit/base/__init__.py +4 -0
- pockit/base/autoupdate.py +56 -0
- pockit/base/discretizationbase.py +572 -0
- pockit/base/easyderiv.py +459 -0
- pockit/base/fastfunc.py +331 -0
- pockit/base/phasebase.py +2345 -0
- pockit/base/systembase.py +1312 -0
- pockit/base/variablebase.py +472 -0
- pockit/base/vectypes.py +9 -0
- pockit/lobatto/__init__.py +18 -0
- pockit/lobatto/discretization.py +569 -0
- pockit/lobatto/phase.py +148 -0
- pockit/lobatto/system.py +131 -0
- pockit/lobatto/variable.py +48 -0
- pockit/optimizer/__init__.py +3 -0
- pockit/optimizer/_common.py +63 -0
- pockit/optimizer/ipopt.py +61 -0
- pockit/optimizer/scipy.py +100 -0
- pockit/radau/__init__.py +18 -0
- pockit/radau/discretization.py +649 -0
- pockit/radau/phase.py +29 -0
- pockit/radau/system.py +17 -0
- pockit/radau/variable.py +48 -0
- pockit_optimal_control-0.1.0.dist-info/METADATA +144 -0
- pockit_optimal_control-0.1.0.dist-info/RECORD +28 -0
- pockit_optimal_control-0.1.0.dist-info/WHEEL +4 -0
- pockit_optimal_control-0.1.0.dist-info/licenses/LICENSE +674 -0
pockit/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Copyright (c) 2024 Yilin Zou
|
|
2
|
+
"""# pockit: Python Optimal Control KIT
|
|
3
|
+
|
|
4
|
+
**😃 Welcome to the pockit API documentation!**
|
|
5
|
+
|
|
6
|
+
**Pockit** is a Python package for solving optimal control problems numerically. It combines advanced techniques to deliver a powerful, user-friendly, and fast solution.
|
|
7
|
+
|
|
8
|
+
- 💪 **Powerful:** Pockit is designed to solve multi-phase optimal control problems with support for path, algebraic, and boundary condition constraints.
|
|
9
|
+
- 🔢 **User-friendly:** Pockit features a [SymPy](https://www.sympy.org/)-based interface that makes defining and solving problems intuitive.
|
|
10
|
+
- âš¡ **Fast:** Pockit achieves high performance through symbolic differentiation (with [SymPy](https://www.sympy.org/)), just-in-time compilation (with [Numba](https://numba.pydata.org/)), vectorization, and other advanced techniques.
|
|
11
|
+
|
|
12
|
+
Please visit the [project page](https://github.com/zouyilin2000/pockit) and the [documentation](https://pockit.pages.dev) for more information.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
__author__ = "Yilin Zou"
|
|
16
|
+
__copyright__ = "Copyright (c) 2024 Yilin Zou"
|
pockit/base/__init__.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Copyright (c) 2024 Yilin Zou
|
|
2
|
+
from typing import Callable
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class AutoUpdate:
|
|
6
|
+
"""Utility class for managing dependencies between functions."""
|
|
7
|
+
|
|
8
|
+
n_in: int
|
|
9
|
+
"""Number of sources."""
|
|
10
|
+
f_target: list[Callable[[], None]]
|
|
11
|
+
"""List of target functions."""
|
|
12
|
+
dependent_table: list[list[int]]
|
|
13
|
+
"""Table of dependencies."""
|
|
14
|
+
todo: list[bool]
|
|
15
|
+
"""List of targets that need to be updated."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, n_source: int, f_target: list[Callable[[], None]]) -> None:
|
|
18
|
+
"""
|
|
19
|
+
Args:
|
|
20
|
+
n_source: Number of sources.
|
|
21
|
+
f_target: List of target functions
|
|
22
|
+
"""
|
|
23
|
+
self.n_in = n_source
|
|
24
|
+
self.f_target = f_target
|
|
25
|
+
self.dependent_table = [[-1] * n_source for _ in f_target]
|
|
26
|
+
self.todo = [False for _ in f_target]
|
|
27
|
+
|
|
28
|
+
def set_dependency(self, i_target: int, i_sources: list[int]) -> None:
|
|
29
|
+
"""Set dependency of the target function with index ``i_target`` on the
|
|
30
|
+
sources with indices ``i_sources``."""
|
|
31
|
+
for i_source in i_sources:
|
|
32
|
+
self.dependent_table[i_target][i_source] = 0
|
|
33
|
+
|
|
34
|
+
def update(self, i_source: int) -> None:
|
|
35
|
+
"""Call the target functions that depend on the source with index
|
|
36
|
+
``i_source``."""
|
|
37
|
+
for i_target, dt in enumerate(self.dependent_table):
|
|
38
|
+
if dt[i_source] == 0:
|
|
39
|
+
dt[i_source] = 1
|
|
40
|
+
elif dt[i_source] == -1:
|
|
41
|
+
continue
|
|
42
|
+
if all(dt):
|
|
43
|
+
self.todo[i_target] = True
|
|
44
|
+
|
|
45
|
+
while True:
|
|
46
|
+
for i_target, td in enumerate(self.todo):
|
|
47
|
+
if td:
|
|
48
|
+
self.todo[i_target] = False
|
|
49
|
+
self.f_target[i_target]()
|
|
50
|
+
break
|
|
51
|
+
else:
|
|
52
|
+
break
|
|
53
|
+
|
|
54
|
+
def update_all(self) -> None:
|
|
55
|
+
"""Update all targets."""
|
|
56
|
+
[f() for f in self.f_target]
|
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
# Copyright (c) 2024 Yilin Zou
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from typing import Callable, Optional
|
|
4
|
+
import scipy.sparse
|
|
5
|
+
import numba as nb
|
|
6
|
+
|
|
7
|
+
from .vectypes import *
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def lr_c(num_point: VecInt) -> tuple[VecInt, VecInt]:
|
|
11
|
+
"""Compute the left and right indices if the border variables are shared.
|
|
12
|
+
The left index is ``l_c[i]`` and the right index is ``r_c[i]``, which
|
|
13
|
+
represents the range of interval ``i`` in a half-open interval ``[l_c[i],
|
|
14
|
+
r_c[i])``.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
num_point: Number of interpolation points of each interval.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
Left and right index ``l_c, r_c`` for shared border variables.
|
|
21
|
+
"""
|
|
22
|
+
l = np.concatenate(([0], np.cumsum(num_point[:-1] - 1)))
|
|
23
|
+
return l, l + num_point
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def lr_nc(num_point: VecInt) -> tuple[VecInt, VecInt]:
|
|
27
|
+
"""Compute the left and right indices if separate variables represent the
|
|
28
|
+
common borders. The left index is ``l_nc[i]`` and the right index is
|
|
29
|
+
``r_nc[i]``, which represents the range of interval ``i`` in a half-open
|
|
30
|
+
interval ``[l_nc[i], r_nc[i])``.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
num_point: Number of interpolation points of each interval.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
Left and right index ``l_nc, r_nc`` for separate border variables.
|
|
37
|
+
"""
|
|
38
|
+
return np.concatenate(([0], np.cumsum(num_point[:-1]))), np.cumsum(num_point)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@nb.njit
|
|
42
|
+
def _evaluate_all_L_j_at_xk(
|
|
43
|
+
eval_points: VecFloat, nodes_interp: VecFloat, weights_bary: VecFloat
|
|
44
|
+
) -> VecFloat:
|
|
45
|
+
"""Calculate all barycentric interpolation basis functions L_j values at given evaluation points.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
eval_points: A one-dimensional array where L_j values need to be calculated.
|
|
49
|
+
nodes_interp: Interpolation nodes x_i.
|
|
50
|
+
weights_bary: Barycentric weights w_i corresponding to nodes_interp.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
A matrix with shape (len(eval_points), len(nodes_interp)),
|
|
54
|
+
where L_values[k, j] = L_j(eval_points[k]).
|
|
55
|
+
"""
|
|
56
|
+
num_eval = len(eval_points)
|
|
57
|
+
num_nodes = len(nodes_interp)
|
|
58
|
+
L_values = np.zeros((num_eval, num_nodes), dtype=np.float64)
|
|
59
|
+
|
|
60
|
+
if num_nodes == 0 or num_eval == 0:
|
|
61
|
+
return L_values
|
|
62
|
+
if num_nodes == 1: # Single interpolation node, L_0(x) = 1
|
|
63
|
+
L_values[:, 0] = 1.0
|
|
64
|
+
return L_values
|
|
65
|
+
|
|
66
|
+
# Vectorized calculation for (eval_points[k] - nodes_interp[s])
|
|
67
|
+
# diff_matrix[k, s] = eval_points[k] - nodes_interp[s]
|
|
68
|
+
diff_matrix = eval_points[:, np.newaxis] - nodes_interp[np.newaxis, :]
|
|
69
|
+
|
|
70
|
+
# Use the barycentric formula L_j(x) = (w_j / (x-x_j)) / sum_s (w_s / (x-x_s))
|
|
71
|
+
# term_matrix[k,s] = weights_bary[s] / diff_matrix[k,s]
|
|
72
|
+
# (Note: If eval_points[k] == nodes_interp[s], diff_matrix[k,s] is 0)
|
|
73
|
+
# with np.errstate(divide='ignore', invalid='ignore'): # Ignore divide-by-zero/invalid value warnings that will be corrected later
|
|
74
|
+
term_matrix = weights_bary[np.newaxis, :] / diff_matrix
|
|
75
|
+
denominator_sum = np.sum(
|
|
76
|
+
term_matrix, axis=1
|
|
77
|
+
) # denominator_sum[k] = sum_s (w_s / (eval_k - node_s))
|
|
78
|
+
|
|
79
|
+
# L_values_kj[k, j] = term_matrix[k,j] / denominator_sum[k]
|
|
80
|
+
L_values = term_matrix / denominator_sum[:, np.newaxis]
|
|
81
|
+
|
|
82
|
+
# Handle cases where the denominator is zero or invalid (e.g., x is far from all nodes, or x is a node causing inf/inf -> nan)
|
|
83
|
+
# If the patching loop is correct, this special handling may not be needed, but as a defensive measure
|
|
84
|
+
L_values[np.isclose(denominator_sum, 0.0) | ~np.isfinite(denominator_sum), :] = (
|
|
85
|
+
0.0 # Or other appropriate fill value
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# Key correction: If evaluation point xk coincides with an interpolation node nodes_interp[s]
|
|
89
|
+
for k_eval, xk in enumerate(eval_points):
|
|
90
|
+
for s_node, node_val in enumerate(nodes_interp):
|
|
91
|
+
if np.isclose(xk, node_val, rtol=1e-13, atol=1e-13):
|
|
92
|
+
L_values[k_eval, :] = 0.0
|
|
93
|
+
L_values[k_eval, s_node] = 1.0
|
|
94
|
+
break # Found matching node, this row of L_values has been corrected
|
|
95
|
+
return L_values
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def integral_matrix(nodes_in: VecFloat, nodes_out: VecFloat) -> VecFloat:
|
|
99
|
+
"""Compute the integral matrix using barycentric weights for improved numerical stability.
|
|
100
|
+
|
|
101
|
+
The integration is performed from 1 to x_out, so the integral at x = 1 is zero.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
nodes_in: Distinct nodes where function values are known.
|
|
105
|
+
nodes_out: Nodes where integral values are needed.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
A matrix I such that I @ f = ∫f(t)dt (integrated from 1 to x_out).
|
|
109
|
+
|
|
110
|
+
Raises:
|
|
111
|
+
ValueError: If nodes_in contains non-distinct nodes.
|
|
112
|
+
"""
|
|
113
|
+
nodes_in = np.asarray(nodes_in, dtype=np.float64)
|
|
114
|
+
nodes_out = np.asarray(nodes_out, dtype=np.float64)
|
|
115
|
+
n = len(nodes_in)
|
|
116
|
+
m = len(nodes_out)
|
|
117
|
+
|
|
118
|
+
if n == 0:
|
|
119
|
+
return np.zeros((m, 0), dtype=np.float64)
|
|
120
|
+
if m == 0:
|
|
121
|
+
return np.zeros((0, n), dtype=np.float64)
|
|
122
|
+
|
|
123
|
+
# Check if nodes in nodes_in are distinct
|
|
124
|
+
if n > 1:
|
|
125
|
+
for i_node in range(n):
|
|
126
|
+
for j_node in range(i_node + 1, n):
|
|
127
|
+
if np.isclose(
|
|
128
|
+
nodes_in[i_node], nodes_in[j_node], rtol=1e-13, atol=1e-13
|
|
129
|
+
):
|
|
130
|
+
raise ValueError("nodes_in must contain distinct nodes")
|
|
131
|
+
|
|
132
|
+
# Initialize integral matrix
|
|
133
|
+
I = np.zeros((m, n), dtype=np.float64)
|
|
134
|
+
|
|
135
|
+
# Calculate barycentric weights for nodes_in
|
|
136
|
+
w = np.ones(n, dtype=np.float64)
|
|
137
|
+
if n > 1: # If n=1, w[0] remains 1 (empty product is 1)
|
|
138
|
+
for j in range(n):
|
|
139
|
+
for k in range(n):
|
|
140
|
+
if k != j:
|
|
141
|
+
# Since nodes are checked to be distinct, nodes_in[j] - nodes_in[k] won't be zero
|
|
142
|
+
w[j] /= nodes_in[j] - nodes_in[k]
|
|
143
|
+
|
|
144
|
+
# Set the number of Gaussian quadrature points
|
|
145
|
+
quad_n_points = max(30, 3 * n)
|
|
146
|
+
quad_x_ref, quad_w_ref = np.polynomial.legendre.leggauss(
|
|
147
|
+
quad_n_points
|
|
148
|
+
) # On [-1, 1]
|
|
149
|
+
|
|
150
|
+
# --- Main loop: Calculate ∫ L_j(t) dt (from 1 to x_out) for each output node ---
|
|
151
|
+
for i_row in range(m):
|
|
152
|
+
x_target = nodes_out[i_row]
|
|
153
|
+
|
|
154
|
+
# If x_target equals 1.0, the integral value is 0
|
|
155
|
+
if np.isclose(x_target, 1.0, rtol=1e-13, atol=1e-13):
|
|
156
|
+
I[i_row, :] = 0.0
|
|
157
|
+
continue
|
|
158
|
+
|
|
159
|
+
# Define the integration interval [a, b]
|
|
160
|
+
a = 1.0
|
|
161
|
+
b = x_target
|
|
162
|
+
|
|
163
|
+
# Map quadrature nodes from the reference interval [-1, 1] to [a, b]
|
|
164
|
+
# t = alpha * y + beta; y from quad_x_ref (in [-1,1]), t is mapped_x_quad (in [a,b])
|
|
165
|
+
alpha = 0.5 * (b - a) # Jacobian of transformation / scaling factor
|
|
166
|
+
beta = 0.5 * (b + a) # Midpoint / translation factor
|
|
167
|
+
|
|
168
|
+
mapped_x_quad = alpha * quad_x_ref + beta
|
|
169
|
+
mapped_w_quad = alpha * quad_w_ref
|
|
170
|
+
|
|
171
|
+
# Calculate values of all Lagrange basis functions L_j at the mapped quadrature points
|
|
172
|
+
lagrange_values_at_mapped_x = _evaluate_all_L_j_at_xk(
|
|
173
|
+
mapped_x_quad, nodes_in, w
|
|
174
|
+
)
|
|
175
|
+
# Shape of lagrange_values_at_mapped_x is (quad_n_points, n)
|
|
176
|
+
|
|
177
|
+
# Calculate integral ∫ L_j(t) dt (from a=1 to b=x_target), resulting in one row of matrix I
|
|
178
|
+
I[i_row, :] = np.dot(mapped_w_quad, lagrange_values_at_mapped_x)
|
|
179
|
+
|
|
180
|
+
return I
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class CooMatrix:
|
|
184
|
+
"""Hold the data of a sparse matrix in COO format."""
|
|
185
|
+
|
|
186
|
+
row: VecInt
|
|
187
|
+
col: VecInt
|
|
188
|
+
data: VecFloat
|
|
189
|
+
|
|
190
|
+
def __init__(self, row: VecInt, col: VecInt, data: VecFloat) -> None:
|
|
191
|
+
self.row = row
|
|
192
|
+
self.col = col
|
|
193
|
+
self.data = data
|
|
194
|
+
|
|
195
|
+
def __len__(self) -> int:
|
|
196
|
+
return len(self.row)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class IndexNode:
|
|
200
|
+
"""In the discretization scheme, the state and control variables are
|
|
201
|
+
represented by values at discrete time points. The scheme requires
|
|
202
|
+
additional information about whether the vector contains the variable's
|
|
203
|
+
initial and terminal values to handle boundary conditions.
|
|
204
|
+
|
|
205
|
+
The data structure is applied to state, control, and middle-stage
|
|
206
|
+
variables.
|
|
207
|
+
"""
|
|
208
|
+
|
|
209
|
+
_front: Optional[int]
|
|
210
|
+
_middle: tuple[int, int]
|
|
211
|
+
_back: Optional[int]
|
|
212
|
+
|
|
213
|
+
def __init__(
|
|
214
|
+
self, front: Optional[int], middle: tuple[int, int], back: Optional[int]
|
|
215
|
+
) -> None:
|
|
216
|
+
"""
|
|
217
|
+
Args:
|
|
218
|
+
front: The index of the front variable or ``None`` if the vector does not contain the initial value.
|
|
219
|
+
middle: The range of the middle variables.
|
|
220
|
+
back: The index of the back variable or ``None`` if the vector does not contain the terminal value.
|
|
221
|
+
"""
|
|
222
|
+
self._front = front
|
|
223
|
+
self._middle = middle
|
|
224
|
+
self._back = back
|
|
225
|
+
self._length_middle = middle[1] - middle[0]
|
|
226
|
+
|
|
227
|
+
@property
|
|
228
|
+
def f(self) -> bool:
|
|
229
|
+
"""Whether the vector contains the front variable."""
|
|
230
|
+
return self._front is not None
|
|
231
|
+
|
|
232
|
+
@property
|
|
233
|
+
def m(self) -> slice:
|
|
234
|
+
"""The slice of the middle variables."""
|
|
235
|
+
return slice(*self._middle)
|
|
236
|
+
|
|
237
|
+
@property
|
|
238
|
+
def b(self) -> bool:
|
|
239
|
+
"""Whether the vector contains the back variable."""
|
|
240
|
+
return self._back is not None
|
|
241
|
+
|
|
242
|
+
@property
|
|
243
|
+
def L_m(self) -> int:
|
|
244
|
+
"""The length of the middle variables."""
|
|
245
|
+
return self._length_middle
|
|
246
|
+
|
|
247
|
+
@property
|
|
248
|
+
def l_m(self) -> int:
|
|
249
|
+
"""The left index of the middle variables."""
|
|
250
|
+
return self._middle[0]
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def r_m(self) -> int:
|
|
254
|
+
"""The right index of the middle variables (exclusive)."""
|
|
255
|
+
return self._middle[1]
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class CooMatrixNode:
|
|
259
|
+
"""Classify the COO matrix into front, middle, and back parts according to
|
|
260
|
+
the column index and the index partition.
|
|
261
|
+
|
|
262
|
+
The data structure is used to generate the Jacobian and Hessian
|
|
263
|
+
matrices of the discretized problem.
|
|
264
|
+
"""
|
|
265
|
+
|
|
266
|
+
_front: CooMatrix
|
|
267
|
+
_middle: CooMatrix
|
|
268
|
+
_back: CooMatrix
|
|
269
|
+
|
|
270
|
+
def __init__(self, csr_matrix: scipy.sparse.csr_array, index: IndexNode) -> None:
|
|
271
|
+
"""
|
|
272
|
+
Args:
|
|
273
|
+
csr_matrix: The CSR matrix to be classified.
|
|
274
|
+
index: The index partition of the vector.
|
|
275
|
+
"""
|
|
276
|
+
coo_matrix = csr_matrix.tocoo()
|
|
277
|
+
coo_matrix.eliminate_zeros()
|
|
278
|
+
row_front = []
|
|
279
|
+
col_front = []
|
|
280
|
+
data_front = []
|
|
281
|
+
row_middle = []
|
|
282
|
+
col_middle = []
|
|
283
|
+
data_middle = []
|
|
284
|
+
row_back = []
|
|
285
|
+
col_back = []
|
|
286
|
+
data_back = []
|
|
287
|
+
for r_, c_, d_ in zip(coo_matrix.row, coo_matrix.col, coo_matrix.data):
|
|
288
|
+
if c_ == index._front:
|
|
289
|
+
row_front.append(r_)
|
|
290
|
+
col_front.append(c_)
|
|
291
|
+
data_front.append(d_)
|
|
292
|
+
elif c_ == index._back:
|
|
293
|
+
row_back.append(r_)
|
|
294
|
+
col_back.append(c_)
|
|
295
|
+
data_back.append(d_)
|
|
296
|
+
else:
|
|
297
|
+
row_middle.append(r_)
|
|
298
|
+
col_middle.append(c_)
|
|
299
|
+
data_middle.append(d_)
|
|
300
|
+
self._front = CooMatrix(
|
|
301
|
+
np.array(row_front, dtype=np.int32),
|
|
302
|
+
np.array(col_front, dtype=np.int32),
|
|
303
|
+
np.array(data_front, dtype=np.float64),
|
|
304
|
+
)
|
|
305
|
+
self._middle = CooMatrix(
|
|
306
|
+
np.array(row_middle, dtype=np.int32),
|
|
307
|
+
np.array(col_middle, dtype=np.int32),
|
|
308
|
+
np.array(data_middle, dtype=np.float64),
|
|
309
|
+
)
|
|
310
|
+
self._back = CooMatrix(
|
|
311
|
+
np.array(row_back, dtype=np.int32),
|
|
312
|
+
np.array(col_back, dtype=np.int32),
|
|
313
|
+
np.array(data_back, dtype=np.float64),
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
@property
|
|
317
|
+
def f(self) -> CooMatrix:
|
|
318
|
+
"""The front part of the COO matrix."""
|
|
319
|
+
return self._front
|
|
320
|
+
|
|
321
|
+
@property
|
|
322
|
+
def m(self) -> CooMatrix:
|
|
323
|
+
"""The middle part of the COO matrix."""
|
|
324
|
+
return self._middle
|
|
325
|
+
|
|
326
|
+
@property
|
|
327
|
+
def b(self) -> CooMatrix:
|
|
328
|
+
"""The back part of the COO matrix."""
|
|
329
|
+
return self._back
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
class DiscretizationBase(ABC):
|
|
333
|
+
"""Essential information for a discretization scheme.
|
|
334
|
+
|
|
335
|
+
The class contains the information to discretize the continuous-time
|
|
336
|
+
optimal control problem into a discrete-time problem and generate
|
|
337
|
+
the derivative matrices.
|
|
338
|
+
"""
|
|
339
|
+
|
|
340
|
+
mesh: VecFloat
|
|
341
|
+
"""The mesh points of the time interval, scaled to ``[0, 1]``."""
|
|
342
|
+
num_point: VecInt
|
|
343
|
+
"""The number of interpolation points in each subinterval."""
|
|
344
|
+
n_x: int
|
|
345
|
+
"""The number of state variables."""
|
|
346
|
+
n_u: int
|
|
347
|
+
"""The number of control variables."""
|
|
348
|
+
|
|
349
|
+
def __init__(self, mesh: VecFloat, num_point: VecInt, n_x: int, n_u: int) -> None:
|
|
350
|
+
"""
|
|
351
|
+
Args:
|
|
352
|
+
mesh: The mesh points of the time interval.
|
|
353
|
+
num_point: The number of interpolation points in each subinterval.
|
|
354
|
+
n_x: The number of state variables.
|
|
355
|
+
n_u: The number of control variables.
|
|
356
|
+
"""
|
|
357
|
+
self.mesh = mesh
|
|
358
|
+
self.num_point = num_point
|
|
359
|
+
self.n_x = n_x
|
|
360
|
+
self.n_u = n_u
|
|
361
|
+
|
|
362
|
+
@property
|
|
363
|
+
@abstractmethod
|
|
364
|
+
def index_state(self) -> IndexNode:
|
|
365
|
+
"""Index partition of the state variables."""
|
|
366
|
+
pass
|
|
367
|
+
|
|
368
|
+
@property
|
|
369
|
+
@abstractmethod
|
|
370
|
+
def index_control(self) -> IndexNode:
|
|
371
|
+
"""Index partition of the control variables."""
|
|
372
|
+
pass
|
|
373
|
+
|
|
374
|
+
@property
|
|
375
|
+
@abstractmethod
|
|
376
|
+
def index_mstage(self) -> IndexNode:
|
|
377
|
+
"""Index partition of the middle-stage variables."""
|
|
378
|
+
pass
|
|
379
|
+
|
|
380
|
+
@property
|
|
381
|
+
@abstractmethod
|
|
382
|
+
def l_v(self) -> VecInt:
|
|
383
|
+
"""The left index of the state and control variables."""
|
|
384
|
+
pass
|
|
385
|
+
|
|
386
|
+
@property
|
|
387
|
+
@abstractmethod
|
|
388
|
+
def r_v(self) -> VecInt:
|
|
389
|
+
"""The right index of the state and control variables (exclusive)."""
|
|
390
|
+
pass
|
|
391
|
+
|
|
392
|
+
@property
|
|
393
|
+
@abstractmethod
|
|
394
|
+
def t_m(self) -> VecFloat:
|
|
395
|
+
"""Position of all interpolation nodes in the middle stage, scaled to
|
|
396
|
+
``[0, 1]``."""
|
|
397
|
+
pass
|
|
398
|
+
|
|
399
|
+
@property
|
|
400
|
+
@abstractmethod
|
|
401
|
+
def l_m(self) -> VecInt:
|
|
402
|
+
"""The left index of subintervals in the middle stage."""
|
|
403
|
+
pass
|
|
404
|
+
|
|
405
|
+
@property
|
|
406
|
+
@abstractmethod
|
|
407
|
+
def r_m(self) -> VecInt:
|
|
408
|
+
"""The right index of subintervals in the middle stage (exclusive)."""
|
|
409
|
+
pass
|
|
410
|
+
|
|
411
|
+
@property
|
|
412
|
+
@abstractmethod
|
|
413
|
+
def L_m(self) -> int:
|
|
414
|
+
"""The number of interpolation points in the middle stage."""
|
|
415
|
+
pass
|
|
416
|
+
|
|
417
|
+
@property
|
|
418
|
+
@abstractmethod
|
|
419
|
+
def w_m(self) -> VecFloat:
|
|
420
|
+
"""The integration weights of the middle stage."""
|
|
421
|
+
pass
|
|
422
|
+
|
|
423
|
+
@property
|
|
424
|
+
@abstractmethod
|
|
425
|
+
def f_v2m(self) -> Callable[[VecFloat], VecFloat]:
|
|
426
|
+
"""Function turns the state and control variables into the middle
|
|
427
|
+
stage."""
|
|
428
|
+
pass
|
|
429
|
+
|
|
430
|
+
@property
|
|
431
|
+
@abstractmethod
|
|
432
|
+
def T_v(self) -> scipy.sparse.csr_array:
|
|
433
|
+
"""Translation matrix of a state variable to eliminate the integration
|
|
434
|
+
coefficient."""
|
|
435
|
+
pass
|
|
436
|
+
|
|
437
|
+
@property
|
|
438
|
+
@abstractmethod
|
|
439
|
+
def T_v_coo(self) -> CooMatrixNode:
|
|
440
|
+
"""Translation matrix of a state variable in COO format partitioned by
|
|
441
|
+
the index."""
|
|
442
|
+
pass
|
|
443
|
+
|
|
444
|
+
@property
|
|
445
|
+
@abstractmethod
|
|
446
|
+
def I_m(self) -> scipy.sparse.csr_array:
|
|
447
|
+
"""Integration matrix of a middle-stage variable."""
|
|
448
|
+
pass
|
|
449
|
+
|
|
450
|
+
@property
|
|
451
|
+
@abstractmethod
|
|
452
|
+
def I_m_coo(self) -> CooMatrixNode:
|
|
453
|
+
"""Integration matrix of a middle-stage variable in COO format
|
|
454
|
+
partitioned by the index."""
|
|
455
|
+
pass
|
|
456
|
+
|
|
457
|
+
@property
|
|
458
|
+
@abstractmethod
|
|
459
|
+
def l_d(self) -> VecInt:
|
|
460
|
+
"""The left index of the dynamic constraints of each state variable."""
|
|
461
|
+
pass
|
|
462
|
+
|
|
463
|
+
@property
|
|
464
|
+
@abstractmethod
|
|
465
|
+
def r_d(self) -> VecInt:
|
|
466
|
+
"""The right index of the dynamic constraints of each state variable
|
|
467
|
+
(exclusive)."""
|
|
468
|
+
pass
|
|
469
|
+
|
|
470
|
+
@property
|
|
471
|
+
@abstractmethod
|
|
472
|
+
def t_m_aug(self) -> VecFloat:
|
|
473
|
+
"""Position of all interpolation nodes in the middle stage, scaled to
|
|
474
|
+
``[0, 1]`` with one additional interpolation point in each
|
|
475
|
+
subinterval."""
|
|
476
|
+
pass
|
|
477
|
+
|
|
478
|
+
@property
|
|
479
|
+
@abstractmethod
|
|
480
|
+
def l_m_aug(self) -> VecInt:
|
|
481
|
+
"""The left index of subintervals in the middle stage, with one
|
|
482
|
+
additional interpolation point in each subinterval."""
|
|
483
|
+
pass
|
|
484
|
+
|
|
485
|
+
@property
|
|
486
|
+
@abstractmethod
|
|
487
|
+
def r_m_aug(self) -> VecInt:
|
|
488
|
+
"""The right index of subintervals in the middle stage (exclusive),
|
|
489
|
+
with one additional interpolation point in each subinterval."""
|
|
490
|
+
pass
|
|
491
|
+
|
|
492
|
+
@property
|
|
493
|
+
@abstractmethod
|
|
494
|
+
def L_m_aug(self) -> int:
|
|
495
|
+
"""The number of interpolation points in the middle stage, with one
|
|
496
|
+
additional interpolation point in each subinterval."""
|
|
497
|
+
pass
|
|
498
|
+
|
|
499
|
+
@property
|
|
500
|
+
@abstractmethod
|
|
501
|
+
def w_aug(self) -> list[VecFloat]:
|
|
502
|
+
"""The integration weights of each subinterval in the middle stage,
|
|
503
|
+
with one additional interpolation point in each subinterval."""
|
|
504
|
+
pass
|
|
505
|
+
|
|
506
|
+
@property
|
|
507
|
+
@abstractmethod
|
|
508
|
+
def P(self) -> Callable[[int], VecFloat]:
|
|
509
|
+
"""Function to compute the polynomial matrix given the number of
|
|
510
|
+
interpolation points."""
|
|
511
|
+
pass
|
|
512
|
+
|
|
513
|
+
@property
|
|
514
|
+
@abstractmethod
|
|
515
|
+
def V_xu_aug(self) -> scipy.sparse.csr_array:
|
|
516
|
+
"""The value matrix translates all state and control variables to the
|
|
517
|
+
middle stage with one additional interpolation point in each
|
|
518
|
+
subinterval."""
|
|
519
|
+
pass
|
|
520
|
+
|
|
521
|
+
@property
|
|
522
|
+
@abstractmethod
|
|
523
|
+
def T_x_aug(self) -> scipy.sparse.csr_array:
|
|
524
|
+
"""The translation matrix of all state variables, with one additional
|
|
525
|
+
interpolation point in each subinterval."""
|
|
526
|
+
pass
|
|
527
|
+
|
|
528
|
+
@property
|
|
529
|
+
@abstractmethod
|
|
530
|
+
def I_m_aug(self) -> scipy.sparse.csr_array:
|
|
531
|
+
"""The integration matrix of a middle-stage variable, with one
|
|
532
|
+
additional interpolation point in each subinterval."""
|
|
533
|
+
pass
|
|
534
|
+
|
|
535
|
+
@property
|
|
536
|
+
@abstractmethod
|
|
537
|
+
def t_x(self) -> VecFloat:
|
|
538
|
+
"""Position of all interpolation nodes of a state variable, scaled to
|
|
539
|
+
``[0, 1]``."""
|
|
540
|
+
pass
|
|
541
|
+
|
|
542
|
+
@property
|
|
543
|
+
@abstractmethod
|
|
544
|
+
def t_u(self) -> VecFloat:
|
|
545
|
+
"""Position of all interpolation nodes of a control variable, scaled to
|
|
546
|
+
``[0, 1]``."""
|
|
547
|
+
pass
|
|
548
|
+
|
|
549
|
+
@property
|
|
550
|
+
@abstractmethod
|
|
551
|
+
def l_x(self) -> VecInt:
|
|
552
|
+
"""The left index of subintervals of a state variable."""
|
|
553
|
+
pass
|
|
554
|
+
|
|
555
|
+
@property
|
|
556
|
+
@abstractmethod
|
|
557
|
+
def r_x(self) -> VecInt:
|
|
558
|
+
"""The right index of subintervals of a state variable (exclusive)."""
|
|
559
|
+
pass
|
|
560
|
+
|
|
561
|
+
@property
|
|
562
|
+
@abstractmethod
|
|
563
|
+
def l_u(self) -> VecInt:
|
|
564
|
+
"""The left index of subintervals of a control variable."""
|
|
565
|
+
pass
|
|
566
|
+
|
|
567
|
+
@property
|
|
568
|
+
@abstractmethod
|
|
569
|
+
def r_u(self) -> VecInt:
|
|
570
|
+
"""The right index of subintervals of a control variable
|
|
571
|
+
(exclusive)."""
|
|
572
|
+
pass
|