Skip to content

Commit d203fbd

Browse files
fixed insurance picture; more tests added
1 parent 6bdf512 commit d203fbd

File tree

6 files changed

+218
-16
lines changed

6 files changed

+218
-16
lines changed

test/problems/fuller.jl.4148.mem

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/problems/insurance.png

706 Bytes
Loading

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ include("problems/goddard.jl")
1313
#
1414
@testset verbose = true showtiming = true "Optimal control tests" begin
1515
for name (
16+
:abstract_ocp,
1617
:basic,
18+
:continuation,
1719
:goddard_direct,
1820
:goddard_indirect,
1921
:grid,

test/test_abstract_ocp.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# for parser testing
2+
function test_abstract_ocp()
3+
4+
# double integrator min tf, abstract definition
5+
@def ocp1 begin
6+
tf R, variable
7+
t [ 0, tf ], time
8+
x R², state
9+
u R, control
10+
-1 u(t) 1
11+
x(0) == [ 0, 0 ]
12+
x(tf) == [ 1, 0 ]
13+
0.1 tf Inf
14+
(t) == [ x₂(t), u(t) ]
15+
tf min
16+
end
17+
18+
@testset verbose = true showtiming = true ":double_integrator :min_tf :abstract" begin
19+
sol1 = solve(ocp1, print_level=0, tol=1e-12)
20+
@test sol1.objective 2.0 rtol=1e-2
21+
end
22+
23+
@testset verbose = true showtiming = true ":goddard :max_rf :abstract :constr" begin
24+
ocp3 = goddard_a().ocp
25+
sol3 = solve(ocp3, print_level=0, tol=1e-12)
26+
@test sol3.objective 1.0125 rtol=1e-2
27+
end
28+
29+
end

test/test_continuation.jl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# discrete continuation
2+
function test_continuation()
3+
4+
test1 = true
5+
test2 = true
6+
test3 = true
7+
draw_plot = false
8+
9+
# parametric ocp definition
10+
if test1
11+
function ocp_T(T)
12+
@def ocp begin
13+
t [ 0, T ], time
14+
x R², state
15+
u R, control
16+
q = x₁
17+
v = x₂
18+
q(0) == 0
19+
v(0) == 0
20+
q(T) == 1
21+
v(T) == 0
22+
(t) == [ v(t), u(t) ]
23+
(u(t)^2) min
24+
end
25+
return ocp
26+
end
27+
@testset verbose = true showtiming = true ":parametric_ocp :warm_start" begin
28+
init = ()
29+
obj_list = []
30+
for T=1:5
31+
ocp = ocp_T(T)
32+
sol = solve(ocp, print_level=0, init=init)
33+
init = sol
34+
push!(obj_list, sol.objective)
35+
end
36+
@test obj_list [12, 1.5, 0.44, 0.19, 0.096] rtol=1e-2
37+
end
38+
end
39+
40+
41+
# parametric ocp definition
42+
if test2
43+
relu(x) = max(0, x)
44+
μ = 10
45+
p_relu(x) = log(abs(1 + exp*x)))/μ
46+
f(x) = 1-x
47+
m(x) = (p_reluf)(x)
48+
T = 2
49+
function myocp(ρ)
50+
@def ocp begin
51+
τ R, variable
52+
s [ 0, 1 ], time
53+
x R², state
54+
u R², control
55+
x₁(0) == 0
56+
x₂(0) == 1
57+
x₁(1) == 1
58+
(s) ==*(u₁(s)+2), (T-τ)*u₂(s)]
59+
-1 u₁(s) 1
60+
-1 u₂(s) 1
61+
0 τ T
62+
-(x₂(1)-2)^3 - ( ρ * ( τ*m(x₁(s))^2 + (T-τ)*m(x₂(s))^2 ) ) min
63+
end
64+
return ocp
65+
end
66+
67+
@testset verbose = true showtiming = true ":parametric_ocp :warm_start" begin
68+
init = ()
69+
obj_list = []
70+
for ρ in [0.1, 5, 10, 30, 100]
71+
ocp = myocp(ρ)
72+
sol = solve(ocp, print_level=0, init=init)
73+
init = sol
74+
push!(obj_list, sol.objective)
75+
end
76+
@test obj_list [-0.034, -1.7, -6.2, -35, -148] rtol=1e-2
77+
end
78+
end
79+
80+
81+
# parametric ocp definition
82+
if test3
83+
sol0 = solve(goddard().ocp, print_level=0)
84+
85+
@testset verbose = true showtiming = true ":global_variable :warm_start" begin
86+
sol = sol0
87+
Tmax_list = []
88+
obj_list = []
89+
for Tmax=3.5:-0.5:1
90+
sol = solve(goddard(Tmax=Tmax).ocp, print_level=0, init=sol)
91+
push!(Tmax_list, Tmax)
92+
push!(obj_list, sol.objective)
93+
end
94+
@test obj_list [1.0125, 1.0124, 1.0120, 1.0112, 1.0092, 1.0036] rtol=1e-2
95+
96+
if draw_plot
97+
# plot obj(vmax)
98+
pobj = plot(Tmax_list, obj_list, label="r(tf)", xlabel="Maximal thrust (Tmax)", ylabel="Maximal altitude r(tf)",seriestype=:scatter)
99+
# plot multiple solutions
100+
plot(sol0)
101+
p = plot!(sol)
102+
display(plot(pobj, p, layout=2, reuse=false, size=(1000,500)))
103+
end
104+
end
105+
end
106+
107+
end

test/test_grid.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,84 @@ function test_grid()
1616
sol = solve(ocp; time_grid=time_grid, display=false)
1717
@test sol.objective 0.309 rtol=1e-2
1818

19+
20+
# 1. simple integrator min energy (dual control for test)
21+
ocp = Model()
22+
state!(ocp, 1)
23+
control!(ocp, 2)
24+
time!(ocp, t0=0, tf=1)
25+
constraint!(ocp, :initial, lb=-1, ub=-1)
26+
constraint!(ocp, :final, lb=0, ub=0)
27+
constraint!(ocp, :control, lb=[0,0], ub=[Inf, Inf])
28+
dynamics!(ocp, (x, u) -> -x - u[1] + u[2])
29+
objective!(ocp, :lagrange, (x, u) -> (u[1]+u[2])^2)
30+
sol0 = solve(ocp, print_level=0)
31+
32+
# solve with explicit and non uniform time grid
33+
@testset verbose = true showtiming = true ":explicit_grid" begin
34+
time_grid = LinRange(0,1,101)
35+
sol = solve(ocp, time_grid=time_grid, print_level=0)
36+
@test (sol.objective == sol0.objective) && (sol.iterations == sol0.iterations)
37+
end
38+
39+
@testset verbose = true showtiming = true ":non_uniform_grid" begin
40+
time_grid = [0,0.1,0.3,0.6,0.98,0.99,1]
41+
sol = solve(ocp, time_grid=time_grid, print_level=0)
42+
@test sol.objective 0.309 rtol=1e-2
43+
end
44+
45+
46+
# 2. integrator free times
47+
ocp = Model(variable=true)
48+
state!(ocp, 2)
49+
control!(ocp, 1)
50+
variable!(ocp, 2)
51+
time!(ocp, ind0=1, indf=2)
52+
constraint!(ocp, :initial, lb=[0,0], ub=[0,0])
53+
constraint!(ocp, :final, lb=[1,0], ub=[1,0])
54+
constraint!(ocp, :control, lb=-1, ub=1)
55+
constraint!(ocp, :variable, lb=[0.1,0.1], ub=[10,10])
56+
constraint!(ocp, :variable, f=v->v[2]-v[1], lb=0.1, ub=Inf)
57+
dynamics!(ocp, (x, u, v) -> [x[2], u])
58+
objective!(ocp, :mayer, (x0, xf, v) -> v[1], :max)
59+
sol0 = solve(ocp, print_level=0)
60+
61+
@testset verbose = true showtiming = true ":explicit_grid" begin
62+
sol = solve(ocp, time_grid=LinRange(0,1,101), print_level=0)
63+
@test (sol.objective == sol0.objective) && (sol.iterations == sol0.iterations)
64+
end
65+
66+
@testset verbose = true showtiming = true ":non_uniform_grid" begin
67+
sol = solve(ocp, time_grid=[0,0.1,0.3,0.5,0.6,0.8,0.95,1], print_level=0)
68+
#plot(sol, show=true)
69+
@test sol.objective 7.96 rtol=1e-2
70+
end
71+
72+
# 3. parametric ocp (T=2) with explicit / non-uniform grid
73+
@def ocpT2 begin
74+
t [ 0, 2 ], time
75+
x R², state
76+
u R, control
77+
q = x₁
78+
v = x₂
79+
q(0) == 0
80+
v(0) == 0
81+
q(2) == 1
82+
v(2) == 0
83+
(t) == [ v(t), u(t) ]
84+
(u(t)^2) min
85+
end
86+
sol0 = solve(ocpT2, print_level=0)
87+
88+
@testset verbose = true showtiming = true ":explicit_grid" begin
89+
sol = solve(ocpT2, time_grid=LinRange(0,1,101),print_level=0)
90+
@test (sol.objective == sol0.objective) && (sol.iterations == sol0.iterations)
91+
end
92+
93+
@testset verbose = true showtiming = true ":non_uniform_grid" begin
94+
sol = solve(ocpT2, time_grid=[0,0.3,1,1.9,2],print_level=0)
95+
@test sol.objective 2.43 rtol=1e-2
96+
end
97+
98+
1999
end

0 commit comments

Comments
 (0)