pytrajopt 0.1.0__tar.gz
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.
- pytrajopt-0.1.0/LICENSE +165 -0
- pytrajopt-0.1.0/PKG-INFO +170 -0
- pytrajopt-0.1.0/README.md +151 -0
- pytrajopt-0.1.0/pyproject.toml +26 -0
- pytrajopt-0.1.0/pytrajopt/__init__.py +13 -0
- pytrajopt-0.1.0/pytrajopt/costate_estimate.py +61 -0
- pytrajopt-0.1.0/pytrajopt/guess.py +175 -0
- pytrajopt-0.1.0/pytrajopt/model.py +173 -0
- pytrajopt-0.1.0/pytrajopt/ocp.py +705 -0
- pytrajopt-0.1.0/pytrajopt/ocp2nlp.py +173 -0
- pytrajopt-0.1.0/pytrajopt/plots.py +278 -0
- pytrajopt-0.1.0/pytrajopt/pub_qual.mplstyle +51 -0
- pytrajopt-0.1.0/pytrajopt/transcription.py +319 -0
pytrajopt-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
pytrajopt-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytrajopt
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal python package to solve multiphase and single phase ocps using direct multiple shooting and collocation.
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: R Sandeepkumar
|
|
8
|
+
Requires-Python: >=3.14,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
13
|
+
Requires-Dist: casadi (>=3.8.2,<4.0.0)
|
|
14
|
+
Requires-Dist: matplotlib (>=3.11.2,<4.0.0)
|
|
15
|
+
Requires-Dist: pydantic (>=2.14.1,<3.0.0)
|
|
16
|
+
Requires-Dist: scipy (>=1.18.1,<2.0.0)
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
<img src="pytrajopt.png" alt="Alt text" width="1000">
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
## PyTrajOpt
|
|
24
|
+
|
|
25
|
+
**PyTrajOpt** (Python-based Trajectory Optimization) is a modern Python framework for solving complex optimal control problems (OCPs). Powered by CasADi for high-performance symbolic differentiation and Pydantic for strict runtime configuration validation, PyTrajOpt supports both single-phase and multiphase formulations via multiple shooting and direct collocation. It goes a step further by estimating state costates and performing rigorous feasibility checks to ensure the reliability of your generated solutions.
|
|
26
|
+
|
|
27
|
+
## OCP
|
|
28
|
+
|
|
29
|
+
$$
|
|
30
|
+
\text{min.} J= \int_{t_0}^{t_f} L(t, \boldsymbol{x}(t), \boldsymbol{u}(t)) \, dt+ E(t_0, t_f,\boldsymbol{x}_0, \boldsymbol{x}_f )
|
|
31
|
+
$$
|
|
32
|
+
|
|
33
|
+
$$
|
|
34
|
+
\boldsymbol{e}_{lb} \leq \boldsymbol{e}(t_0, t_f,\boldsymbol{x}_0, \boldsymbol{x}_f ) \leq \boldsymbol{e}_{ub}
|
|
35
|
+
$$
|
|
36
|
+
|
|
37
|
+
$$
|
|
38
|
+
\dot{\boldsymbol{x}}=\boldsymbol{f}(t, \boldsymbol{x}(t), \boldsymbol{u}(t))
|
|
39
|
+
$$
|
|
40
|
+
|
|
41
|
+
$$
|
|
42
|
+
\boldsymbol{h}_{lb} \leq \boldsymbol{h}(t, \boldsymbol{x}(t), \boldsymbol{u}(t)) \leq \boldsymbol{h}_{ub}
|
|
43
|
+
$$
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Prerequisites
|
|
47
|
+
|
|
48
|
+
Usage of this package requires familiarity with
|
|
49
|
+
|
|
50
|
+
1. understanding of direct methods for optimal control
|
|
51
|
+
2. solving nonlinear optimization problems
|
|
52
|
+
3. symbolic manipulation of variables in CasADi
|
|
53
|
+
|
|
54
|
+
To simply code the OCP into pytrajopt, 1 and 2 are not important.
|
|
55
|
+
|
|
56
|
+
## Example
|
|
57
|
+
|
|
58
|
+
$$\text{min.} t_f$$
|
|
59
|
+
|
|
60
|
+
$$
|
|
61
|
+
\dot{x} = v \sin \theta
|
|
62
|
+
$$
|
|
63
|
+
|
|
64
|
+
$$
|
|
65
|
+
\dot{y} = v \cos \theta
|
|
66
|
+
$$
|
|
67
|
+
|
|
68
|
+
$$
|
|
69
|
+
\dot{v} = g \cos \theta
|
|
70
|
+
$$
|
|
71
|
+
|
|
72
|
+
$$
|
|
73
|
+
(t_0, x_0, y_0, v_0) = (0, 0, 0, 0)
|
|
74
|
+
$$
|
|
75
|
+
|
|
76
|
+
$$
|
|
77
|
+
(x_f, y_f) = (1000, 1)
|
|
78
|
+
$$
|
|
79
|
+
|
|
80
|
+
Given below is a sample code to solve Brachistochrone problem with scaling applied via direct multiple shooting.
|
|
81
|
+
```python
|
|
82
|
+
import casadi as cs
|
|
83
|
+
|
|
84
|
+
from pytrajopt import (
|
|
85
|
+
LinGuess,
|
|
86
|
+
Ocp,
|
|
87
|
+
Syms,
|
|
88
|
+
casadi_integrator,
|
|
89
|
+
check_feasibility,
|
|
90
|
+
disc_opt,
|
|
91
|
+
generate_NLP,
|
|
92
|
+
plot_solution,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def brach():
|
|
96
|
+
ocp_data = {}
|
|
97
|
+
ocp_data["nx"] = 3
|
|
98
|
+
ocp_data["nu"] = 1
|
|
99
|
+
syms = Syms(nx=ocp_data["nx"], nu=ocp_data["nu"])
|
|
100
|
+
t0s, tfs, x0s, xfs = syms.get_event()
|
|
101
|
+
ts, xs, us = syms.get_path()
|
|
102
|
+
e = cs.vertcat(x0s - cs.DM([0, 0, 0]), xfs[0:2, :] - cs.DM([1e3, 1]))
|
|
103
|
+
E=tfs
|
|
104
|
+
g=9.8
|
|
105
|
+
f = cs.vcat([xs[2] * cs.sin(us), xs[2] * cs.cos(us), g * cs.cos(us)])
|
|
106
|
+
syms.collect_expression({'E':E,'e':e,'f':f})
|
|
107
|
+
ocp_data.update(syms.generate_funcs())
|
|
108
|
+
ocp_data["t0"] = 0
|
|
109
|
+
ocp_data["e_lb"] = [0] * 5
|
|
110
|
+
ocp_data["e_ub"] = [0] * 5
|
|
111
|
+
ocp_data["tf_ub"] = 100
|
|
112
|
+
ocp_data["J_s"] = 10
|
|
113
|
+
ocp_data["t_s"] = 10
|
|
114
|
+
ocp_data["x_s"] = [1e3, 160, 20]
|
|
115
|
+
ocp_data["u_s"] = [1]
|
|
116
|
+
ocp_data["e_s"] = [1e3, 160, 20] + [1e3, 160]
|
|
117
|
+
ocp = Ocp(**ocp_data)
|
|
118
|
+
|
|
119
|
+
disc_data = {}
|
|
120
|
+
disc_data["n"] = 20
|
|
121
|
+
disc_data["method"] = "rk4"
|
|
122
|
+
disc = disc_opt(**disc_data)
|
|
123
|
+
|
|
124
|
+
nlp = cs.Opti()
|
|
125
|
+
nlp.solver("ipopt")
|
|
126
|
+
dis_ocp = generate_NLP(ocp, nlp, disc)
|
|
127
|
+
|
|
128
|
+
guess_data = {
|
|
129
|
+
"n": disc.n,
|
|
130
|
+
"xl": [1, 1, 1],
|
|
131
|
+
"xr": [1e3, 1e2, 3e2],
|
|
132
|
+
"ul": [1e2],
|
|
133
|
+
"ur": [1e2],
|
|
134
|
+
"tr": 25,
|
|
135
|
+
}
|
|
136
|
+
guess = LinGuess(**guess_data)
|
|
137
|
+
guess.set_guess(dis_ocp, nlp)
|
|
138
|
+
sol = nlp.solve()
|
|
139
|
+
plot_solution(sol, dis_ocp, "brachistochrone_sol")
|
|
140
|
+
check_feasibility(ocp, dis_ocp, sol, "brachistochrone_feas")
|
|
141
|
+
|
|
142
|
+
if __name__ == "__main__":
|
|
143
|
+
brach()
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
[View the Brachistochrone Solution PDF](./examples/brachistochrone_sol.pdf)
|
|
147
|
+
|
|
148
|
+
## Documentation
|
|
149
|
+
|
|
150
|
+
For more details about problems supported, installation, examples and api references refer to the docs.
|
|
151
|
+
|
|
152
|
+
## References
|
|
153
|
+
|
|
154
|
+
<!-- BEGIN GENERATED REFERENCES -->
|
|
155
|
+
1. [ally_chen.py](examples/ally_chen.py) — hen, Weifeng, and Lorenz T. Biegler. “Nested Direct Transcription Optimization for Singular Optimal Control Problems.” AIChE Journal, vol. 62, no. 10, Oct. 2016, pp. 3611–27. DOI.org (Crossref), https://doi.org/10.1002/aic.15272.
|
|
156
|
+
2. [bachistochrone_v2.py](examples/bachistochrone_v2.py) — Ross, I. M., et al. “Scaling and Balancing for High-Performance Computation of Optimal Controls.” Journal of Guidance, Control, and Dynamics, vol. 41, no. 10, Oct. 2018, pp. 2086–97. DOI.org (Crossref), https://doi.org/10.2514/1.G003382.
|
|
157
|
+
3. [brachistochrone.py](examples/brachistochrone.py) — Ross, I. M., et al. “Scaling and Balancing for High-Performance Computation of Optimal Controls.” Journal of Guidance, Control, and Dynamics, vol. 41, no. 10, Oct. 2018, pp. 2086–97. DOI.org (Crossref), https://doi.org/10.2514/1.G003382.
|
|
158
|
+
4. [cart_costate.py](examples/cart_costate.py) — Conway, B. A., and K. M. Larson. “Collocation Versus Differential Inclusion in Direct Optimization.” Journal of Guidance, Control, and Dynamics, vol. 21, no. 5, Sept. 1998, pp. 780–85. DOI.org (Crossref), https://doi.org/10.2514/2.4306.
|
|
159
|
+
5. [container_crane.py](examples/container_crane.py) — https://archiv.ub.uni-heidelberg.de/volltextserver/24212/1/Dissertation_Bergner17.pdf
|
|
160
|
+
6. [free_flying_robot.py](examples/free_flying_robot.py) — Betts, John T. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Second, Society for Industrial and Applied Mathematics, 2010. DOI.org (Crossref), https://doi.org/10.1137/1.9780898718577.
|
|
161
|
+
7. [hypersensitive.py](examples/hypersensitive.py) — Betts, John T. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Second, Society for Industrial and Applied Mathematics, 2010. DOI.org (Crossref), https://doi.org/10.1137/1.9780898718577.
|
|
162
|
+
8. [lee_ramirez_reactor.py](examples/lee_ramirez_reactor.py) — hen, Weifeng, and Lorenz T. Biegler. “Nested Direct Transcription Optimization for Singular Optimal Control Problems.” AIChE Journal, vol. 62, no. 10, Oct. 2016, pp. 3611–27. DOI.org (Crossref), https://doi.org/10.1002/aic.15272.
|
|
163
|
+
9. [low_thrust.py](examples/low_thrust.py) — Ross, I. Michael, et al. “Low-Thrust, High-Accuracy Trajectory Optimization.” Journal of Guidance, Control, and Dynamics, vol. 30, no. 4, July 2007, pp. 921–33. DOI.org (Crossref), https://doi.org/10.2514/1.23181.
|
|
164
|
+
10. [moon_lander_time_dilation.py](examples/moon_lander_time_dilation.py) — Meditch, J.. (1964). On the problem of optimal thrust programming for a lunar soft landing. IEEE Transactions on Automatic Control, 9(4), 477–484. doi:10.1109/tac.1964.1105758 https://gpops2.com/examples/moonlander.html
|
|
165
|
+
11. [rocket.py](examples/rocket.py) — https://web.casadi.org/blog/nlp-scaling/
|
|
166
|
+
12. [rocket_multiphase.py](examples/rocket_multiphase.py) — Ross, I. Michael, and Fariba Fahroo. Pseudospectral Knotting Methods for Solving Nonsmooth Optimal Control Problems. Journal of Guidance, Control, and Dynamics, vol. 27, no. 3, May 2004, pp. 397–405. DOI.org (Crossref), https://doi.org/10.2514/1.3426.
|
|
167
|
+
13. [shuttle_cross_range.py](examples/shuttle_cross_range.py) — Betts, John T. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Second, Society for Industrial and Applied Mathematics, 2010. DOI.org (Crossref), https://doi.org/10.1137/1.9780898718577.
|
|
168
|
+
<!-- END GENERATED REFERENCES -->
|
|
169
|
+
|
|
170
|
+
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="pytrajopt.png" alt="Alt text" width="1000">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
## PyTrajOpt
|
|
6
|
+
|
|
7
|
+
**PyTrajOpt** (Python-based Trajectory Optimization) is a modern Python framework for solving complex optimal control problems (OCPs). Powered by CasADi for high-performance symbolic differentiation and Pydantic for strict runtime configuration validation, PyTrajOpt supports both single-phase and multiphase formulations via multiple shooting and direct collocation. It goes a step further by estimating state costates and performing rigorous feasibility checks to ensure the reliability of your generated solutions.
|
|
8
|
+
|
|
9
|
+
## OCP
|
|
10
|
+
|
|
11
|
+
$$
|
|
12
|
+
\text{min.} J= \int_{t_0}^{t_f} L(t, \boldsymbol{x}(t), \boldsymbol{u}(t)) \, dt+ E(t_0, t_f,\boldsymbol{x}_0, \boldsymbol{x}_f )
|
|
13
|
+
$$
|
|
14
|
+
|
|
15
|
+
$$
|
|
16
|
+
\boldsymbol{e}_{lb} \leq \boldsymbol{e}(t_0, t_f,\boldsymbol{x}_0, \boldsymbol{x}_f ) \leq \boldsymbol{e}_{ub}
|
|
17
|
+
$$
|
|
18
|
+
|
|
19
|
+
$$
|
|
20
|
+
\dot{\boldsymbol{x}}=\boldsymbol{f}(t, \boldsymbol{x}(t), \boldsymbol{u}(t))
|
|
21
|
+
$$
|
|
22
|
+
|
|
23
|
+
$$
|
|
24
|
+
\boldsymbol{h}_{lb} \leq \boldsymbol{h}(t, \boldsymbol{x}(t), \boldsymbol{u}(t)) \leq \boldsymbol{h}_{ub}
|
|
25
|
+
$$
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Prerequisites
|
|
29
|
+
|
|
30
|
+
Usage of this package requires familiarity with
|
|
31
|
+
|
|
32
|
+
1. understanding of direct methods for optimal control
|
|
33
|
+
2. solving nonlinear optimization problems
|
|
34
|
+
3. symbolic manipulation of variables in CasADi
|
|
35
|
+
|
|
36
|
+
To simply code the OCP into pytrajopt, 1 and 2 are not important.
|
|
37
|
+
|
|
38
|
+
## Example
|
|
39
|
+
|
|
40
|
+
$$\text{min.} t_f$$
|
|
41
|
+
|
|
42
|
+
$$
|
|
43
|
+
\dot{x} = v \sin \theta
|
|
44
|
+
$$
|
|
45
|
+
|
|
46
|
+
$$
|
|
47
|
+
\dot{y} = v \cos \theta
|
|
48
|
+
$$
|
|
49
|
+
|
|
50
|
+
$$
|
|
51
|
+
\dot{v} = g \cos \theta
|
|
52
|
+
$$
|
|
53
|
+
|
|
54
|
+
$$
|
|
55
|
+
(t_0, x_0, y_0, v_0) = (0, 0, 0, 0)
|
|
56
|
+
$$
|
|
57
|
+
|
|
58
|
+
$$
|
|
59
|
+
(x_f, y_f) = (1000, 1)
|
|
60
|
+
$$
|
|
61
|
+
|
|
62
|
+
Given below is a sample code to solve Brachistochrone problem with scaling applied via direct multiple shooting.
|
|
63
|
+
```python
|
|
64
|
+
import casadi as cs
|
|
65
|
+
|
|
66
|
+
from pytrajopt import (
|
|
67
|
+
LinGuess,
|
|
68
|
+
Ocp,
|
|
69
|
+
Syms,
|
|
70
|
+
casadi_integrator,
|
|
71
|
+
check_feasibility,
|
|
72
|
+
disc_opt,
|
|
73
|
+
generate_NLP,
|
|
74
|
+
plot_solution,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def brach():
|
|
78
|
+
ocp_data = {}
|
|
79
|
+
ocp_data["nx"] = 3
|
|
80
|
+
ocp_data["nu"] = 1
|
|
81
|
+
syms = Syms(nx=ocp_data["nx"], nu=ocp_data["nu"])
|
|
82
|
+
t0s, tfs, x0s, xfs = syms.get_event()
|
|
83
|
+
ts, xs, us = syms.get_path()
|
|
84
|
+
e = cs.vertcat(x0s - cs.DM([0, 0, 0]), xfs[0:2, :] - cs.DM([1e3, 1]))
|
|
85
|
+
E=tfs
|
|
86
|
+
g=9.8
|
|
87
|
+
f = cs.vcat([xs[2] * cs.sin(us), xs[2] * cs.cos(us), g * cs.cos(us)])
|
|
88
|
+
syms.collect_expression({'E':E,'e':e,'f':f})
|
|
89
|
+
ocp_data.update(syms.generate_funcs())
|
|
90
|
+
ocp_data["t0"] = 0
|
|
91
|
+
ocp_data["e_lb"] = [0] * 5
|
|
92
|
+
ocp_data["e_ub"] = [0] * 5
|
|
93
|
+
ocp_data["tf_ub"] = 100
|
|
94
|
+
ocp_data["J_s"] = 10
|
|
95
|
+
ocp_data["t_s"] = 10
|
|
96
|
+
ocp_data["x_s"] = [1e3, 160, 20]
|
|
97
|
+
ocp_data["u_s"] = [1]
|
|
98
|
+
ocp_data["e_s"] = [1e3, 160, 20] + [1e3, 160]
|
|
99
|
+
ocp = Ocp(**ocp_data)
|
|
100
|
+
|
|
101
|
+
disc_data = {}
|
|
102
|
+
disc_data["n"] = 20
|
|
103
|
+
disc_data["method"] = "rk4"
|
|
104
|
+
disc = disc_opt(**disc_data)
|
|
105
|
+
|
|
106
|
+
nlp = cs.Opti()
|
|
107
|
+
nlp.solver("ipopt")
|
|
108
|
+
dis_ocp = generate_NLP(ocp, nlp, disc)
|
|
109
|
+
|
|
110
|
+
guess_data = {
|
|
111
|
+
"n": disc.n,
|
|
112
|
+
"xl": [1, 1, 1],
|
|
113
|
+
"xr": [1e3, 1e2, 3e2],
|
|
114
|
+
"ul": [1e2],
|
|
115
|
+
"ur": [1e2],
|
|
116
|
+
"tr": 25,
|
|
117
|
+
}
|
|
118
|
+
guess = LinGuess(**guess_data)
|
|
119
|
+
guess.set_guess(dis_ocp, nlp)
|
|
120
|
+
sol = nlp.solve()
|
|
121
|
+
plot_solution(sol, dis_ocp, "brachistochrone_sol")
|
|
122
|
+
check_feasibility(ocp, dis_ocp, sol, "brachistochrone_feas")
|
|
123
|
+
|
|
124
|
+
if __name__ == "__main__":
|
|
125
|
+
brach()
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
[View the Brachistochrone Solution PDF](./examples/brachistochrone_sol.pdf)
|
|
129
|
+
|
|
130
|
+
## Documentation
|
|
131
|
+
|
|
132
|
+
For more details about problems supported, installation, examples and api references refer to the docs.
|
|
133
|
+
|
|
134
|
+
## References
|
|
135
|
+
|
|
136
|
+
<!-- BEGIN GENERATED REFERENCES -->
|
|
137
|
+
1. [ally_chen.py](examples/ally_chen.py) — hen, Weifeng, and Lorenz T. Biegler. “Nested Direct Transcription Optimization for Singular Optimal Control Problems.” AIChE Journal, vol. 62, no. 10, Oct. 2016, pp. 3611–27. DOI.org (Crossref), https://doi.org/10.1002/aic.15272.
|
|
138
|
+
2. [bachistochrone_v2.py](examples/bachistochrone_v2.py) — Ross, I. M., et al. “Scaling and Balancing for High-Performance Computation of Optimal Controls.” Journal of Guidance, Control, and Dynamics, vol. 41, no. 10, Oct. 2018, pp. 2086–97. DOI.org (Crossref), https://doi.org/10.2514/1.G003382.
|
|
139
|
+
3. [brachistochrone.py](examples/brachistochrone.py) — Ross, I. M., et al. “Scaling and Balancing for High-Performance Computation of Optimal Controls.” Journal of Guidance, Control, and Dynamics, vol. 41, no. 10, Oct. 2018, pp. 2086–97. DOI.org (Crossref), https://doi.org/10.2514/1.G003382.
|
|
140
|
+
4. [cart_costate.py](examples/cart_costate.py) — Conway, B. A., and K. M. Larson. “Collocation Versus Differential Inclusion in Direct Optimization.” Journal of Guidance, Control, and Dynamics, vol. 21, no. 5, Sept. 1998, pp. 780–85. DOI.org (Crossref), https://doi.org/10.2514/2.4306.
|
|
141
|
+
5. [container_crane.py](examples/container_crane.py) — https://archiv.ub.uni-heidelberg.de/volltextserver/24212/1/Dissertation_Bergner17.pdf
|
|
142
|
+
6. [free_flying_robot.py](examples/free_flying_robot.py) — Betts, John T. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Second, Society for Industrial and Applied Mathematics, 2010. DOI.org (Crossref), https://doi.org/10.1137/1.9780898718577.
|
|
143
|
+
7. [hypersensitive.py](examples/hypersensitive.py) — Betts, John T. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Second, Society for Industrial and Applied Mathematics, 2010. DOI.org (Crossref), https://doi.org/10.1137/1.9780898718577.
|
|
144
|
+
8. [lee_ramirez_reactor.py](examples/lee_ramirez_reactor.py) — hen, Weifeng, and Lorenz T. Biegler. “Nested Direct Transcription Optimization for Singular Optimal Control Problems.” AIChE Journal, vol. 62, no. 10, Oct. 2016, pp. 3611–27. DOI.org (Crossref), https://doi.org/10.1002/aic.15272.
|
|
145
|
+
9. [low_thrust.py](examples/low_thrust.py) — Ross, I. Michael, et al. “Low-Thrust, High-Accuracy Trajectory Optimization.” Journal of Guidance, Control, and Dynamics, vol. 30, no. 4, July 2007, pp. 921–33. DOI.org (Crossref), https://doi.org/10.2514/1.23181.
|
|
146
|
+
10. [moon_lander_time_dilation.py](examples/moon_lander_time_dilation.py) — Meditch, J.. (1964). On the problem of optimal thrust programming for a lunar soft landing. IEEE Transactions on Automatic Control, 9(4), 477–484. doi:10.1109/tac.1964.1105758 https://gpops2.com/examples/moonlander.html
|
|
147
|
+
11. [rocket.py](examples/rocket.py) — https://web.casadi.org/blog/nlp-scaling/
|
|
148
|
+
12. [rocket_multiphase.py](examples/rocket_multiphase.py) — Ross, I. Michael, and Fariba Fahroo. Pseudospectral Knotting Methods for Solving Nonsmooth Optimal Control Problems. Journal of Guidance, Control, and Dynamics, vol. 27, no. 3, May 2004, pp. 397–405. DOI.org (Crossref), https://doi.org/10.2514/1.3426.
|
|
149
|
+
13. [shuttle_cross_range.py](examples/shuttle_cross_range.py) — Betts, John T. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Second, Society for Industrial and Applied Mathematics, 2010. DOI.org (Crossref), https://doi.org/10.1137/1.9780898718577.
|
|
150
|
+
<!-- END GENERATED REFERENCES -->
|
|
151
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "pytrajopt"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A minimal python package to solve multiphase and single phase ocps using direct multiple shooting and collocation."
|
|
5
|
+
authors = ["R Sandeepkumar"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.14"
|
|
11
|
+
casadi = "^3.8.2"
|
|
12
|
+
scipy = "^1.18.1"
|
|
13
|
+
pydantic = "^2.14.1"
|
|
14
|
+
matplotlib = "^3.11.2"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
[build-system]
|
|
18
|
+
requires = ["poetry-core"]
|
|
19
|
+
build-backend = "poetry.core.masonry.api"
|
|
20
|
+
|
|
21
|
+
[dependency-groups]
|
|
22
|
+
dev = [
|
|
23
|
+
"zensical (>=0.0.70,<0.0.71)",
|
|
24
|
+
"mkdocs-autoapi (>=0.4.1,<0.5.0)",
|
|
25
|
+
"mkdocstrings-python (>=2.0.9,<3.0.0)",
|
|
26
|
+
]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from pytrajopt.guess import CusGuess, LinGuess
|
|
2
|
+
from pytrajopt.model import Expression, Syms
|
|
3
|
+
from pytrajopt.ocp import DisOcp, Ocp
|
|
4
|
+
from pytrajopt.ocp2nlp import generate_NLP
|
|
5
|
+
from pytrajopt.plots import (
|
|
6
|
+
check_feasibility,
|
|
7
|
+
cubic_spline_interp,
|
|
8
|
+
pc_interp,
|
|
9
|
+
plot_solution,
|
|
10
|
+
plot_solution_all_phase,
|
|
11
|
+
plot_sparsity,
|
|
12
|
+
)
|
|
13
|
+
from pytrajopt.transcription import DynamicMesh, Integrator, Transcription
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import casadi as cs
|
|
2
|
+
|
|
3
|
+
from .ocp import Ocp
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# @validate_call(config=ConfigDict(arbitrary_types_allowed=True))
|
|
7
|
+
def kkt_ub_lb(kkt: cs.DM):
|
|
8
|
+
"""
|
|
9
|
+
lb<=g<=ub
|
|
10
|
+
|
|
11
|
+
Multiplier
|
|
12
|
+
|
|
13
|
+
lb-g<=0
|
|
14
|
+
|
|
15
|
+
g-ub<=0
|
|
16
|
+
|
|
17
|
+
There will be 2 multipliers for 2 constraints. But for the
|
|
18
|
+
bounded constraint can be merged into 1.
|
|
19
|
+
|
|
20
|
+
lam_ub-lam_lb
|
|
21
|
+
"""
|
|
22
|
+
if kkt.shape != (1, 1):
|
|
23
|
+
pieces = kkt.shape[1] // 2
|
|
24
|
+
lbub = cs.horzsplit(kkt, pieces)
|
|
25
|
+
else:
|
|
26
|
+
pass
|
|
27
|
+
return lbub[1] - lbub[0]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# @validate_call(config=ConfigDict(arbitrary_types_allowed=True))
|
|
31
|
+
def get_costate(ocp: Ocp, nlp: cs.Opti, constraints: dict, t: cs.DM | cs.MX):
|
|
32
|
+
"""
|
|
33
|
+
extracts the kkt multipliers for all the
|
|
34
|
+
constraints
|
|
35
|
+
|
|
36
|
+
for certain constraints the costates are also
|
|
37
|
+
estimated from the kkt multipliers
|
|
38
|
+
|
|
39
|
+
kkt multipliers of the defect constraints are
|
|
40
|
+
costates for states (only for explicit rk method or
|
|
41
|
+
multiple shooting constraints of the form x(k+1)=phi(x(k),u(k))).
|
|
42
|
+
|
|
43
|
+
rk4, fw_euler, exp_mid, casadi integrators (implicit and explicit)
|
|
44
|
+
all provide costate estimates
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
dual = {}
|
|
48
|
+
|
|
49
|
+
for name, constraint in constraints.items():
|
|
50
|
+
dual[f"kkt_{name}"] = kkt_ub_lb(nlp.dual(constraint[0]))
|
|
51
|
+
if name == "e":
|
|
52
|
+
dual[f"lam_{name}"] = dual[f"kkt_{name}"]
|
|
53
|
+
if name == "f":
|
|
54
|
+
dual[f"lam_{name}"] = -dual[f"kkt_{name}"]
|
|
55
|
+
if name == "h":
|
|
56
|
+
dt = t[:, 1:] - t[:, 0:-1]
|
|
57
|
+
dual[f"lam_{name}"] = dual[f"kkt_{name}"][:, 0:-1] / cs.repmat(
|
|
58
|
+
dt, dual[f"kkt_{name}"].shape[0], 1
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
return dual
|