-
Notifications
You must be signed in to change notification settings - Fork 1
/
betts_10_7_8_opty.py
213 lines (165 loc) · 6.16 KB
/
betts_10_7_8_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
# %%
"""
Hypersensitive Control
======================
This is example 10.7 from Betts' Test Problems.
Note: Below is example 10.8, which is mathematically EXACTLY the same, but the
formulationis different. this has a big impact on the optimization.
**States**
- y : state variable
- uy: its speed
**Specifieds**
- u : control variable
Note: the state variable uy is only needed because opt needs minimum two
differential equations in the equations of motion.
"""
import numpy as np
import sympy as sm
import sympy.physics.mechanics as me
from opty.direct_collocation import Problem
from opty.utils import create_objective_function, parse_free
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
# %%
# Equations of motion.
t = me.dynamicsymbols._t
y, uy, u = me.dynamicsymbols('y uy, u')
eom = sm.Matrix([ uy - y.diff(t), -uy - y**3 + u])
sm.pprint(eom)
# %%
def solve_optimization(nodes, tf, iterations=1):
t0, tf = 0.0, tf
num_nodes = nodes
interval_value = (tf - t0)/(num_nodes - 1)
#times = np.linspace(t0, tf, num=num_nodes)
state_symbols = (y, uy)
specified_symbols = (u,)
# Specify the objective function and form the gradient.
start = time.time()
obj_func = sm.Integral(y**2 + u**2, t)
sm.pprint(obj_func)
obj, obj_grad = create_objective_function(obj_func,
state_symbols,
specified_symbols,
tuple(),
num_nodes,
node_time_interval=interval_value,
)
# Specify the symbolic instance constraints, as per the example
instance_constraints = (
y.func(t0) - 1,
y.func(tf) - 1.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,
)
prob.add_option('nlp_scaling_method', 'gradient-based')
# Give some rough estimates for y, uy, u trajectories.
initial_guess = np.zeros(prob.num_free)
# Find the optimal solution.
for _ in range(iterations):
solution, info = prob.solve(initial_guess)
initial_guess = solution
print(info['status_msg'])
print(f'Objective value achieved: {info['obj_val']:.4f}, as per the book ' +
f'it is {6.7241}, so the error is: '
f'{(info['obj_val'] - 6.7241)/6.7241*100:.3f} % ')
print(f'Time taken for the simulation: {time.time() - start:.2f} s')
# 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()
# %%
# As per the example tf = 10000
tf = 10000
num_nodes = 5001
solve_optimization(num_nodes, tf)
# %%
# As per the plot of the solution y(t) it seems, that most of the time y(t) = 0,
# only at the very beginning and the very end it is different from 0.
# So, it may make sense to use a smaller tf
tf = 10
num_nodes = 5001
solve_optimization(num_nodes, tf, iterations=1)
# %%
"""
Hypersensitive Control
======================
This is example 10.8 from Betts' Test Problems.
Mathematically it is EXACTLY the same as the previous example, only
the formulation is different.
The comparison to example 10.7 above shows that different formulations of the
same problem, may have (drastic) influences of the optimization.
Interesting: While here example 10.8 takes much longer than the mathematically
equivalent 10.7, in the timings Betts give in appendix A.2. the opposite is the
case.
**States**
- y, z : state variables
- uy, uz: their speeds
**Specifieds**
- u : control variable
"""
# %%
# Equations of motion.
t = me.dynamicsymbols._t
y, z, uy, uz, u = me.dynamicsymbols('y z uy uz u')
eom = sm.Matrix([uy - y.diff(t), uz - z.diff(t), uy + y**3 - u, uz - y**2 - u**2])
sm.pprint(eom)
# %%
def solve_optimization(nodes, tf, iterations=1):
t0, tf = 0.0, tf
num_nodes = nodes
interval_value = (tf - t0)/(num_nodes - 1)
state_symbols = (y, z, uy, uz)
specified_symbols = (u,)
# Specify the objective function and form the gradient.
start = time.time()
obj_func = sm.Integral(uz, t)
sm.pprint(obj_func)
obj, obj_grad = create_objective_function(obj_func,
state_symbols,
specified_symbols,
tuple(),
num_nodes,
node_time_interval=interval_value,
)
# Specify the symbolic instance constraints, as per the example
instance_constraints = (
# uy.func(t0),
y.func(t0) - 1,
y.func(tf) - 1.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,
)
prob.add_option('nlp_scaling_method', 'gradient-based')
# Give some rough estimates for the trajectories.
initial_guess = np.zeros(prob.num_free)
# Find the optimal solution.
for _ in range(iterations):
solution, info = prob.solve(initial_guess)
initial_guess = solution
print(info['status_msg'])
print(f'Objective value achieved: {info['obj_val']:.4f}, as per the book ' +
f'it is {6.7241}, so the error is: '
f'{(info['obj_val'] - 6.7241)/6.7241*100:.3f} % ')
print(f'Time taken for the simulation: {time.time() - start:.2f} s')
# 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()
# %%
# As we konw from above, that tf = 10 gives better results, we use this value
# here only.
tf = 10
num_nodes = 5001
solve_optimization(num_nodes, tf)