-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
155 additions
and
102 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[deps] | ||
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63" | ||
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
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 |
---|---|---|
@@ -1,54 +1,65 @@ | ||
function test_print() | ||
|
||
# | ||
@test display(Model()) isa Nothing | ||
# | ||
@test display(Model()) isa Nothing | ||
|
||
# | ||
ocp = Model(autonomous=false) | ||
state!(ocp, 2, "state", ["r", "v"]) # dimension of the state with the names of the components | ||
control!(ocp, 1) # dimension of the control | ||
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time | ||
__constraint!(ocp, :initial, [-1, 0], [-1, 0]) | ||
__constraint!(ocp, :final , [ 0, 0], [0, 0]) | ||
A = [ 0 1 | ||
0 0 ] | ||
B = [ 0 | ||
1 ] | ||
dynamics!(ocp, (t, x, u) -> A*x + B*u) | ||
__constraint!(ocp, :state, (t, x) -> x[2], 0, 1) | ||
__constraint!(ocp, :control, (t, u) -> u, -1, 1) | ||
__constraint!(ocp, :mixed, (t, x, u) -> x[1]+u, 2, 3) | ||
__constraint!(ocp, :state, Index(1), -10, 10) | ||
__constraint!(ocp, :control, -2, 2) | ||
objective!(ocp, :bolza, (t0, x0, tf, xf) -> tf, (t, x, u) -> 0.5u^2) | ||
@test display(ocp) isa Nothing | ||
@def ocp begin | ||
t ∈ [ 0, 1 ], time | ||
x ∈ R², state | ||
u ∈ R, control | ||
x(0) == [ -1, 0 ] | ||
x(1) == [ 0, 0 ] | ||
ẋ(t) == [ x₂(t), u(t) ] | ||
∫( 0.5u(t)^2 ) → min | ||
end | ||
@test display(ocp) isa Nothing | ||
|
||
# | ||
ocp = Model(autonomous=false) | ||
state!(ocp, 2, "state", ["r", "v"]) # dimension of the state with the names of the components | ||
control!(ocp, 1) # dimension of the control | ||
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time | ||
__constraint!(ocp, :initial, [-1, 0], [-1, 0]) | ||
__constraint!(ocp, :final , [ 0, 0], [0, 0]) | ||
A = [ 0 1 | ||
0 0 ] | ||
B = [ 0 | ||
1 ] | ||
dynamics!(ocp, (t, x, u) -> A*x + B*u) | ||
__constraint!(ocp, :state, (t, x) -> x[2], 0, 1) | ||
__constraint!(ocp, :control, (t, u) -> u, -1, 1) | ||
__constraint!(ocp, :mixed, (t, x, u) -> x[1]+u, 2, 3) | ||
__constraint!(ocp, :state, Index(1), -10, 10) | ||
__constraint!(ocp, :control, -2, 2) | ||
objective!(ocp, :bolza, (t0, x0, tf, xf) -> tf, (t, x, u) -> 0.5u^2) | ||
@test display(ocp) isa Nothing | ||
|
||
# | ||
ocp = Model(autonomous=false) | ||
state!(ocp, 1, "y") # dimension of the state with the names of the components | ||
control!(ocp, 1, "v") # dimension of the control | ||
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time | ||
__constraint!(ocp, :initial, -1, -1) | ||
__constraint!(ocp, :final , 0, 0) | ||
dynamics!(ocp, (t, x, u) -> x+u) | ||
__constraint!(ocp, :state, (t, x) -> x, 0, 1) | ||
__constraint!(ocp, :control, (t, u) -> u, -1, 1) | ||
__constraint!(ocp, :mixed, (t, x, u) -> x+u, 2, 3) | ||
__constraint!(ocp, :state, -10, 10) | ||
__constraint!(ocp, :control, -2, 2) | ||
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) | ||
@test display(ocp) isa Nothing | ||
# | ||
ocp = Model(autonomous=false) | ||
state!(ocp, 1, "y") # dimension of the state with the names of the components | ||
control!(ocp, 1, "v") # dimension of the control | ||
__time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time | ||
__constraint!(ocp, :initial, -1, -1) | ||
__constraint!(ocp, :final , 0, 0) | ||
dynamics!(ocp, (t, x, u) -> x+u) | ||
__constraint!(ocp, :state, (t, x) -> x, 0, 1) | ||
__constraint!(ocp, :control, (t, u) -> u, -1, 1) | ||
__constraint!(ocp, :mixed, (t, x, u) -> x+u, 2, 3) | ||
__constraint!(ocp, :state, -10, 10) | ||
__constraint!(ocp, :control, -2, 2) | ||
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) | ||
@test display(ocp) isa Nothing | ||
|
||
# | ||
ocp = Model(autonomous=false, variable=true) | ||
variable!(ocp, 1) | ||
state!(ocp, 1, "y") # dimension of the state with the names of the components | ||
control!(ocp, 2) # dimension of the control | ||
__time!(ocp, 0, Index(1), "s") # initial and final time, with the name of the variable time | ||
__constraint!(ocp, :initial, -1, -1) | ||
__constraint!(ocp, :final , 0, 0) | ||
dynamics!(ocp, (t, x, u) -> x+u) | ||
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) | ||
@test display(ocp) isa Nothing | ||
# | ||
ocp = Model(autonomous=false, variable=true) | ||
variable!(ocp, 1) | ||
state!(ocp, 1, "y") # dimension of the state with the names of the components | ||
control!(ocp, 2) # dimension of the control | ||
__time!(ocp, 0, Index(1), "s") # initial and final time, with the name of the variable time | ||
__constraint!(ocp, :initial, -1, -1) | ||
__constraint!(ocp, :final , 0, 0) | ||
dynamics!(ocp, (t, x, u) -> x+u) | ||
objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) | ||
@test display(ocp) isa Nothing | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,85 @@ | ||
function test_utils() | ||
|
||
v = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0] | ||
n = 2 | ||
u = vec2vec(v, n) | ||
w = vec2vec(u) | ||
@test v == w | ||
|
||
|
||
A = [ 0 1 | ||
2 3 ] | ||
|
||
V = CTBase.matrix2vec(A) | ||
@test V[1] == [0, 1] | ||
@test V[2] == [2, 3] | ||
|
||
W = CTBase.matrix2vec(A, 2) | ||
@test W[1] == [0, 2] | ||
@test W[2] == [1, 3] | ||
|
||
@test_throws IncorrectArgument CTBase.ctindice(-1) | ||
@test_throws IncorrectArgument CTBase.ctindice(10) | ||
|
||
@test CTBase.ctindice(0) == '₀' | ||
@test CTBase.ctindice(1) == '₁' | ||
@test CTBase.ctindice(2) == '₂' | ||
@test CTBase.ctindice(3) == '₃' | ||
@test CTBase.ctindice(4) == '₄' | ||
@test CTBase.ctindice(5) == '₅' | ||
@test CTBase.ctindice(6) == '₆' | ||
@test CTBase.ctindice(7) == '₇' | ||
@test CTBase.ctindice(8) == '₈' | ||
@test CTBase.ctindice(9) == '₉' | ||
|
||
@test_throws IncorrectArgument CTBase.ctindices(-1) | ||
@test CTBase.ctindices(019) == "₁₉" | ||
@test CTBase.ctindices(314) == "₃₁₄" | ||
|
||
@test_throws IncorrectArgument CTBase.ctupperscript(-1) | ||
@test_throws IncorrectArgument CTBase.ctupperscript(10) | ||
@test CTBase.ctupperscript(0) == '⁰' | ||
@test CTBase.ctupperscript(1) == '¹' | ||
@test CTBase.ctupperscript(2) == '²' | ||
@test CTBase.ctupperscript(3) == '³' | ||
@test CTBase.ctupperscript(4) == '⁴' | ||
@test CTBase.ctupperscript(5) == '⁵' | ||
@test CTBase.ctupperscript(6) == '⁶' | ||
@test CTBase.ctupperscript(7) == '⁷' | ||
@test CTBase.ctupperscript(8) == '⁸' | ||
@test CTBase.ctupperscript(9) == '⁹' | ||
|
||
@test_throws IncorrectArgument CTBase.ctupperscripts(-1) | ||
@test CTBase.ctupperscripts(019) == "¹⁹" | ||
@test CTBase.ctupperscripts(109) == "¹⁰⁹" | ||
@testset "AD" begin | ||
|
||
x0 = 1.0 | ||
|
||
f = x -> cos(x) | ||
@test CTBase.ctgradient(f, x0) ≈ -sin(x0) atol=1e-10 | ||
@test CTBase.__ctgradient(f, x0) ≈ -sin(x0) atol=1e-10 | ||
@test CTBase.ctgradient(VectorField(f), x0) ≈ -sin(x0) atol=1e-10 | ||
@test CTBase.__ctgradient(VectorField(f), x0) ≈ -sin(x0) atol=1e-10 | ||
|
||
f = x -> [cos(x)] | ||
@test CTBase.ctjacobian(f, x0) ≈ [-sin(x0);;] atol=1e-10 | ||
@test CTBase.__ctjacobian(f, x0) ≈ [-sin(x0);;] atol=1e-10 | ||
@test CTBase.ctjacobian(VectorField(f), x0) ≈ [-sin(x0);;] atol=1e-10 | ||
@test CTBase.__ctjacobian(VectorField(f), x0) ≈ [-sin(x0);;] atol=1e-10 | ||
|
||
g = x -> cos(x[1]) + sin(x[2]) | ||
@test CTBase.ctgradient(g, [x0, x0]) ≈ [-sin(x0), cos(x0)] atol=1e-10 | ||
@test CTBase.__ctgradient(g, [x0, x0]) ≈ [-sin(x0), cos(x0)] atol=1e-10 | ||
|
||
g = x -> [cos(x[1]) + sin(x[2])] | ||
@test CTBase.ctjacobian(g, [x0, x0]) ≈ [-sin(x0) cos(x0)] atol=1e-10 | ||
@test CTBase.__ctjacobian(g, [x0, x0]) ≈ [-sin(x0) cos(x0)] atol=1e-10 | ||
|
||
end | ||
|
||
@testset "Other" begin | ||
|
||
v = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0] | ||
n = 2 | ||
u = vec2vec(v, n) | ||
w = vec2vec(u) | ||
@test v == w | ||
|
||
A = [ 0 1 | ||
2 3 ] | ||
|
||
V = CTBase.matrix2vec(A) | ||
@test V[1] == [0, 1] | ||
@test V[2] == [2, 3] | ||
|
||
W = CTBase.matrix2vec(A, 2) | ||
@test W[1] == [0, 2] | ||
@test W[2] == [1, 3] | ||
|
||
@test_throws IncorrectArgument CTBase.ctindice(-1) | ||
@test_throws IncorrectArgument CTBase.ctindice(10) | ||
|
||
@test CTBase.ctindice(0) == '₀' | ||
@test CTBase.ctindice(1) == '₁' | ||
@test CTBase.ctindice(2) == '₂' | ||
@test CTBase.ctindice(3) == '₃' | ||
@test CTBase.ctindice(4) == '₄' | ||
@test CTBase.ctindice(5) == '₅' | ||
@test CTBase.ctindice(6) == '₆' | ||
@test CTBase.ctindice(7) == '₇' | ||
@test CTBase.ctindice(8) == '₈' | ||
@test CTBase.ctindice(9) == '₉' | ||
|
||
@test_throws IncorrectArgument CTBase.ctindices(-1) | ||
@test CTBase.ctindices(019) == "₁₉" | ||
@test CTBase.ctindices(314) == "₃₁₄" | ||
|
||
@test_throws IncorrectArgument CTBase.ctupperscript(-1) | ||
@test_throws IncorrectArgument CTBase.ctupperscript(10) | ||
@test CTBase.ctupperscript(0) == '⁰' | ||
@test CTBase.ctupperscript(1) == '¹' | ||
@test CTBase.ctupperscript(2) == '²' | ||
@test CTBase.ctupperscript(3) == '³' | ||
@test CTBase.ctupperscript(4) == '⁴' | ||
@test CTBase.ctupperscript(5) == '⁵' | ||
@test CTBase.ctupperscript(6) == '⁶' | ||
@test CTBase.ctupperscript(7) == '⁷' | ||
@test CTBase.ctupperscript(8) == '⁸' | ||
@test CTBase.ctupperscript(9) == '⁹' | ||
|
||
@test_throws IncorrectArgument CTBase.ctupperscripts(-1) | ||
@test CTBase.ctupperscripts(019) == "¹⁹" | ||
@test CTBase.ctupperscripts(109) == "¹⁰⁹" | ||
|
||
end | ||
|
||
end |