-
Notifications
You must be signed in to change notification settings - Fork 1
/
betts_10_73_74_opty.py
253 lines (201 loc) · 6.03 KB
/
betts_10_73_74_opty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# %%
"""
Linear Tangent Steering
=======================
This is example 10.74 from Betts' book "Practical Methods for Optimal Control
Using NonlinearProgramming", 3rd edition, Chapter 10: Test Problems.
**States**
- :math:`x_0, x_1, x_2, x_3` : state variables
**Controls**
- :math:`u` : control variable
Note: I simply copied the equations of motion, the bounds and the constants
from the book. I do not know their meaning.
"""
import numpy as np
import sympy as sm
import sympy.physics.mechanics as me
from opty.direct_collocation import Problem
import matplotlib.pyplot as plt
import time
# %%
# Equations of motion.
start = time.time()
t = me.dynamicsymbols._t
x = me.dynamicsymbols('x:4')
u = me.dynamicsymbols('u')
h = sm.symbols('h')
#Parameters
a = 100.0
eom = sm.Matrix([
-x[0].diff(t) + x[2],
-x[1].diff(t) + x[3],
-x[2].diff(t) + a * sm.cos(u),
-x[3].diff(t) + a * sm.sin(u),
])
sm.pprint(eom)
# %%
# Define and solve the optimization problem.
num_nodes = 301
t0, tf = 0*h, (num_nodes - 1) * h
interval_value = h
state_symbols = x
unkonwn_input_trajectories = (u, )
# Specify the objective function and form the gradient.
def obj(free):
return free[-1]
def obj_grad(free):
grad = np.zeros_like(free)
grad[-1] = 1.0
return grad
instance_constraints = (
x[0].func(t0),
x[1].func(t0),
x[2].func(t0),
x[3].func(t0),
x[1].func(tf) - 5.0,
x[2].func(tf) - 45.0,
x[3].func(tf),
)
bounds = {
h: (0.0, 0.5),
u: (-np.pi/2, np.pi/2),
}
# %%
# Create the optimization problem and set any options.
prob = Problem(obj,
obj_grad,
eom,
state_symbols,
num_nodes,
interval_value,
instance_constraints= instance_constraints,
bounds=bounds,
)
# prob.add_option('nlp_scaling_method', 'gradient-based')
prob.add_option('max_iter', 1000)
# Give some rough estimates for the trajectories.
initial_guess = np.ones(prob.num_free) * 0.1
# Find the optimal solution.
for i in range(1):
solution, info = prob.solve(initial_guess)
initial_guess = solution
print(info['status_msg'])
print(f'Objective value achieved: {info['obj_val']*(num_nodes-1):.4f}, ' +
f'as per the book it is {0.554570879}, so the error is: ' +
f'{(info['obj_val']*(num_nodes-1) - 0.554570879)/0.554570879 :.3e} ')
solution1 = solution
# %%
# Plot the optimal state and input trajectories.
prob.plot_trajectories(solution)
# %%
# Plot the constraint violations.
prob.plot_constraint_violations(solution)
# %%
# Plot the objective function as a function of optimizer iteration.
prob.plot_objective_value()
print(f'Time taken: {time.time() - start:.2f} seconds')
# =============================================================================
# =============================================================================
# %%
# Example 10.73 from Betts' book "Practical Methods for Optimal Control Using
# Nonlinear Programming", 3rd edition, Chapter 10: Test Problems.
# This is equivalent to problem 10.74, but formulated differently.
# Presently opty does not allow me to use a boundary condition given, maybe
# the reason that the result is less accurate by a factor of 100.
#
# **states**
#
# - :math:`x_0, x_1, x_2, x_3` : state variables
# - :math:`lam_0, lam_1, lam_2, lamb_3` : costate variables
#
# Equations of motion.
start = time.time()
t = me.dynamicsymbols._t
x = me.dynamicsymbols('x:4')
lam = me.dynamicsymbols('lam:4')
h = sm.symbols('h')
# Parameters
a = 100.0
cosu = - lam[2] / sm.sqrt(lam[2]**2 + lam[3]**2)
sinu = - lam[3] / sm.sqrt(lam[2]**2 + lam[3]**2)
eom = sm.Matrix([
-x[0].diff(t) + x[2],
-x[1].diff(t) + x[3],
-x[2].diff(t) + a * cosu,
-x[3].diff(t) + a * sinu,
-lam[0].diff(t),
-lam[1].diff(t),
-lam[2].diff(t) - lam[0],
-lam[3].diff(t) - lam[1],
])
sm.pprint(eom)
# %%
# Define and solve the optimization problem.
state_symbols = x + lam
t0, tf = 0*h, (num_nodes - 1) * h
interval_value = h
# Specify the objective function and form the gradient.
def obj(free):
return free[-1]
def obj_grad(free):
grad = np.zeros_like(free)
grad[-1] = 1.0
return grad
instance_constraints = (
x[0].func(t0),
x[1].func(t0),
x[2].func(t0),
x[3].func(t0),
x[1].func(tf) - 5.0,
x[2].func(tf) - 45.0,
x[3].func(tf),
lam[0].func(t0),
# lam[2].func(t0),
# lam[3].func(tf),
)
bounds = {
h: (0.0, 0.5),
}
# Create the optimization problem and set any options.
prob = Problem(obj,
obj_grad,
eom,
state_symbols,
num_nodes,
interval_value,
instance_constraints= instance_constraints,
bounds=bounds,
)
# prob.add_option('nlp_scaling_method', 'gradient-based')
prob.add_option('max_iter', 1000)
# Give some rough estimates for the trajectories.
initial_guess = np.ones(prob.num_free) * 0.1
# %%
# Find the optimal solution.
for i in range(1):
solution, info = prob.solve(initial_guess)
initial_guess = solution
print(info['status_msg'])
print(f'Objective value achieved: {info['obj_val']*(num_nodes-1):.4f}, ' +
f'as per the book it is {0.55457088}, so the error is: ' +
f'{(info['obj_val']*(num_nodes-1) - 0.55457088)/0.55457088*100:.3e}')
# Plot the optimal state and input trajectories.
prob.plot_trajectories(solution)
# Plot the constraint violations.
prob.plot_constraint_violations(solution)
# Plot the objective function as a function of optimizer iteration.
prob.plot_objective_value()
print(f'Time taken: {time.time() - start:.2f} seconds')
# %%
# Compare the two solutions.
difference = np.empty(4*num_nodes)
for i in range(4*num_nodes):
difference[i] = solution1[i] - solution[i]
fig, ax = plt.subplots(4, 1, figsize=(8, 6), constrained_layout=True)
names = ['x0', 'x1', 'x2', 'x3']
times = np.linspace(t0, (num_nodes-1)*solution[-1], num_nodes)
for i in range(4):
ax[i].plot(times, difference[i*num_nodes:(i+1)*num_nodes])
ax[i].set_ylabel(f'difference in {names[i]}')
ax[0].set_title('Difference in the state variables between the two solutions')
ax[-1].set_xlabel('time');