-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added failsafe in solution generation to ensure that t0 > tf (#76)
* added failsafe in solution generation to ensure that t0 > tf * some reorg in test scripts
- Loading branch information
1 parent
c2af80f
commit 8e86d64
Showing
6 changed files
with
134 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using CTDirect | ||
|
||
println("Test: abstract OCP definition") | ||
|
||
# double integrator min tf, abstract definition | ||
@def ocp1 begin | ||
tf ∈ R, variable | ||
t ∈ [ 0, tf ], time | ||
x ∈ R², state | ||
u ∈ R, control | ||
-1 ≤ u(t) ≤ 1 | ||
x(0) == [ 0, 0 ] | ||
x(tf) == [ 1, 0 ] | ||
0.1 ≤ tf ≤ Inf | ||
ẋ(t) == [ x₂(t), u(t) ] | ||
tf → min | ||
end | ||
|
||
@testset verbose = true showtiming = true ":double_integrator :min_tf :abstract" begin | ||
sol1 = solveDirect(ocp1, grid_size=100, print_level=0, tol=1e-12) | ||
@test sol1.objective ≈ 2.0 rtol=1e-2 | ||
end | ||
|
||
# same with some random constraints | ||
@def ocp2 begin | ||
tf ∈ R, variable | ||
t ∈ [ 0, tf ], time | ||
x ∈ R², state | ||
u ∈ R, control | ||
tf ≥ 0.1 | ||
-1 ≤ u(t) ≤ 1 | ||
q = x₁ | ||
v = x₂ | ||
q(0) == 1 | ||
v(0) == 2 | ||
q(tf) == 0 | ||
v(tf) == 0 | ||
0 ≤ q(t) ≤ 5 | ||
-2 ≤ v(t) ≤ 3 | ||
(u^2)(t) ≤ 100 | ||
ẋ(t) == [ v(t), u(t) ] | ||
tf → min | ||
end | ||
|
||
@testset verbose = true showtiming = true ":double_integrator :min_tf :abstract :constraints" begin | ||
sol2 = solveDirect(ocp2, grid_size=100, print_level=0, tol=1e-12) | ||
@test sol2.objective ≈ 5.46 rtol=1e-2 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using CTDirect | ||
|
||
# simple integrator min energy | ||
ocp = Model() | ||
state!(ocp, 1) | ||
control!(ocp, 1) | ||
time!(ocp, [0, 1]) | ||
constraint!(ocp, :initial, -1, :initial_constraint) | ||
constraint!(ocp, :final, 0, :final_constraint) | ||
dynamics!(ocp, (x, u) -> -x + u) | ||
objective!(ocp, :lagrange, (x, u) -> u^2) | ||
|
||
# all-in-one solve call | ||
println("Test simple integrator: all in one solve call") | ||
sol = solveDirect(ocp, grid_size=100, print_level=5, tol=1e-12) | ||
println("Expected Objective 0.313, found ", sol.objective) | ||
|
||
# split calls | ||
println("Test simple integrator: split calls") | ||
println("Direct transcription") | ||
docp = directTranscription(ocp, grid_size=100) | ||
nlp = getNLP(docp) | ||
println("Solve discretized problem and retrieve solution") | ||
sol = solveDOCP(docp, print_level=5, tol=1e-12) | ||
println("Expected Objective 0.313, found ", sol.objective) | ||
|
||
# different starting guess | ||
println("with constant init x=0.5 and u=0") | ||
init_constant = OptimalControlInit(x_init=[-0.5], u_init=0) | ||
setDOCPInit(docp, init_constant) | ||
sol = solveDOCP(docp, print_level=5, tol=1e-12) | ||
println("Expected Objective 0.313, found ", sol.objective) | ||
|
||
# init from solution | ||
init_sol = OptimalControlInit(sol) | ||
setDOCPInit(docp, init_sol) | ||
sol = solveDOCP(docp, print_level=5, tol=1e-12) | ||
println("Expected Objective 0.313, found ", sol.objective) | ||
|
||
# pass init directly to solve call | ||
setDOCPInit(docp, OptimalControlInit()) # reset init in docp | ||
sol = solveDOCP(docp, init=init_sol, print_level=5, tol=1e-12) | ||
println("Expected Objective 0.313, found ", sol.objective) | ||
sol = solveDOCP(docp, print_level=5, tol=1e-12) | ||
println("Expected Objective 0.313, found ", sol.objective) | ||
|
||
|
||
# check types on objective and constraints functions | ||
#@code_warntype ipopt_objective(xu, docp) |