Skip to content

Commit 6e2d6fd

Browse files
committed
foo
1 parent 73d1a9a commit 6e2d6fd

17 files changed

+125
-23
lines changed

docs/make.jl

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ makedocs(
1515
format = Documenter.HTML(prettyurls = false),
1616
pages = [
1717
"Introduction" => "index.md",
18-
"Tutorials" => ["basic-example.md", "goddard.md"],
19-
"API" => ["api.md", "api-ctbase.md", "api-ctflows.md", "api-ctdirect.md"],
18+
"Tutorials" => ["tutorial-basic-example.md",
19+
"tutorial-goddard.md",
20+
"tutorial-model.md",
21+
"tutorial-solvers.md",
22+
"tutorial-init.md",
23+
"tutorial-plot.md",
24+
"tutorial-iss.md",
25+
"tutorial-ctrepl.md"],
26+
"API" => ["api.md",
27+
"api-ctbase.md",
28+
"api-ctflows.md",
29+
"api-ctdirect.md"],
2030
]
2131
)
2232

docs/src/api.md

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ Order = [:module, :constant, :type, :function, :macro]
99
Private = false
1010
```
1111

12+
## Available methods
13+
14+
```@example
15+
using OptimalControl
16+
Methods()
17+
```
18+
1219
## Documentation
1320

1421
```@autodocs

docs/src/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The `OptimalControl.jl` package aims to provide tools to solve optimal control p
88
It is part of the [control-toolbox ecosystem](https://github.com/control-toolbox):
99

1010
```@raw html
11-
<img src="./assets/diagram.png" style="display: block; margin: 0 auto 20px auto;" width="400px">
11+
<img src="./assets/diagram.png" style="display: block; margin: 0 auto 20px auto;" width="320px">
1212
```
1313

1414
!!! note "Install and documentation"
File renamed without changes.

docs/src/tutorial-ctrepl.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ct repl
2+
3+
See [`ctrepl`](@ref) method.
File renamed without changes.

docs/src/tutorial-init.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Initialisation
2+
3+
```@meta
4+
CurrentModule = OptimalControl
5+
```
6+
7+
See [`solve`](@ref) method.

docs/src/tutorial-iss.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Indirect simple shooting
2+
3+
In construction.

docs/src/tutorial-model.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Modelisation
2+
3+
See [`OptimalControlModel`](@ref) type.

docs/src/tutorial-plot.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Plot solution
2+
3+
See [`plot`](@ref) method.

docs/src/tutorial-solvers.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Solvers and options
2+
3+
```@meta
4+
CurrentModule = OptimalControl
5+
```
6+
7+
See [`solve`](@ref) method.

src/OptimalControl.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ using CTFlows
2121

2222
# declarations
2323
const __display = CTBase.__display
24-
const ctrepl = CTBase.__init_repl
2524

26-
# resources
2725
include("solve.jl")
2826

2927
# export functions only for user
3028
export solve
29+
export Methods
3130

3231
# export from other modules
3332

src/solve.jl

+30-11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
# Resolution
33

44
# by order of preference
5-
methods = ()
5+
algorithmes = ()
66

77
# descent methods
8-
methods = add(methods, (:direct, :adnlp, :ipopt))
8+
algorithmes = add(algorithmes, (:direct, :adnlp, :ipopt))
9+
10+
"""
11+
$(TYPEDSIGNATURES)
12+
13+
Return the list of available methods to solve the optimal control problem.
14+
"""
15+
function Methods()::Tuple{Tuple{Vararg{Symbol}}}
16+
return algorithmes
17+
end
918

1019
"""
1120
$(TYPEDSIGNATURES)
@@ -16,7 +25,7 @@ Solve the the optimal control problem `ocp` by the method given by the (optional
1625
1726
You can pass a partial description.
1827
If you give a partial description, then, if several complete descriptions contains the partial one,
19-
then, the method with the highest priority is chosen. The higher in the list `OptimalControl.methods`,
28+
then, the method with the highest priority is chosen. The higher in the list,
2029
the higher is the priority.
2130
2231
Keyword arguments:
@@ -28,12 +37,14 @@ Keyword arguments:
2837
2938
There is only one available method for the moment: a direct method which transforms
3039
the optimal control problem into a nonlinear programming problem (NLP) solved
31-
by `IPOPT`, thanks to the package `ADNLProblems`. The direct method comes from
32-
the `CTDirect` package.
40+
by [`Ipopt`](https://coin-or.github.io/Ipopt/), thanks to the package
41+
[`ADNLPModels`](https://github.com/JuliaSmoothOptimizers/ADNLPModels.jl).
42+
The direct method comes from the
43+
[`CTDirect`](https://github.com/control-toolbox/CTDirect.jl) package.
3344
3445
!!! tip
3546
36-
- To see the list of available methods, simply print `OptimalControl.methods`.
47+
- To see the list of available methods, simply call `Methods()`.
3748
- You can pass any other option by a pair `keyword=value` according to the chosen method.
3849
3950
# Examples
@@ -42,6 +53,14 @@ Keyword arguments:
4253
julia> sol = solve(ocp)
4354
julia> sol = solve(ocp, :direct)
4455
julia> sol = solve(ocp, :direct, :ipopt)
56+
julia> sol = solve(ocp, :direct, :ipopt, display=false)
57+
julia> sol = solve(ocp, :direct, :ipopt, display=false, init=sol)
58+
julia> sol = solve(ocp, init=(state=[-0.5, 0.2],))
59+
julia> sol = solve(ocp, init=(state=[-0.5, 0.2], control=0.5))
60+
julia> sol = solve(ocp, init=(state=[-0.5, 0.2], control=0.5, variable=[1, 2]))
61+
julia> sol = solve(ocp, init=(state=[-0.5, 0.2], control=t->6-12*t))
62+
julia> sol = solve(ocp, init=(state=t->[-1+t, t*(t-1)], control=0.5))
63+
julia> sol = solve(ocp, init=(state=t->[-1+t, t*(t-1)], control=t->6-12*t))
4564
```
4665
4766
"""
@@ -51,7 +70,7 @@ function solve(ocp::OptimalControlModel, description::Symbol...;
5170
kwargs...)
5271

5372
#
54-
method = getFullDescription(description, methods)
73+
method = getFullDescription(description, Methods())
5574

5675
# todo: OptimalControlInit must be in CTBase, it is for the moment in CTDirect
5776

@@ -60,11 +79,11 @@ function solve(ocp::OptimalControlModel, description::Symbol...;
6079
elseif init isa CTBase.OptimalControlSolution
6180
init = OptimalControlInit(init)
6281
else
63-
init = OptimalControlInit(x_init=init[rg(1, ocp.state_dimension)],
64-
u_init=init[rg(ocp.state_dimension+1, ocp.state_dimension+ocp.control_dimension)],
65-
v_init=init[rg(ocp.state_dimension+ocp.control_dimension+1, lastindex(init))])
82+
x_init = :state keys(init) ? init[:state] : nothing
83+
u_init = :control keys(init) ? init[:control] : nothing
84+
v_init = :variable keys(init) ? init[:variable] : nothing
85+
init = OptimalControlInit(x_init=x_init, u_init=u_init, v_init=v_init)
6686
end
67-
6887

6988
# print chosen method
7089
display ? println("Method = ", method) : nothing

test/runtests.jl

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ using CTProblems
66

77
#
88
@testset verbose = true showtiming = true "Optimal control tests" begin
9-
for name in (
10-
"goddard_direct",
11-
"goddard_indirect",
9+
for name (
10+
:goddard_direct,
11+
:goddard_indirect,
12+
:init,
1213
)
13-
@testset "$name" begin
14-
include("test_$name.jl")
14+
@testset "$(name)" begin
15+
test_name = Symbol(:test_, name)
16+
include("$(test_name).jl")
17+
@eval $test_name()
1518
end
1619
end
1720
end

test/test_goddard_direct.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
function test_goddard_direct()
2+
13
# goddard with state constraint - maximize altitude
24
prob = Problem(:goddard, :classical, :altitude, :x_dim_3, :u_dim_1, :mayer, :x_cons, :u_cons, :singular_arc)
35
ocp = prob.model
46

57
# initial guess (constant state and control functions)
6-
#init = ([1.01, 0.05, 0.8], 0.1, 0.2)
7-
init = [1.01, 0.05, 0.8, 0.1, 0.2]
8+
init = (state=[1.01, 0.05, 0.8], control=0.1, variable=0.2)
89

910
# solve
1011
sol = solve(ocp, grid_size=10, print_level=5, init=init)
1112

1213
# test
1314
@test sol.objective prob.solution.objective atol=5e-3
15+
16+
end

test/test_goddard_indirect.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
function test_goddard_indirect()
2+
13
prob = Problem(:goddard, :classical, :altitude, :x_dim_3, :u_dim_1, :mayer, :x_cons, :u_cons, :singular_arc)
24
ocp = prob.model
35
sol = prob.solution
@@ -106,3 +108,5 @@ shoot!(s, p0, t1, t2, t3, tf)
106108

107109
@test sol.converged
108110
@test norm(s) < 1e-6
111+
112+
end

test/test_init.jl

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function test_init()
2+
3+
# get double integrator enregy min problem
4+
prob = Problem(:integrator, :energy, :x_dim_2, :u_dim_1, :lagrange, :noconstraints)
5+
ocp = prob.model
6+
7+
#
8+
N = 50
9+
tol = 1e-2
10+
11+
# initial guess (constant state and control)
12+
init = (state=[-0.5, 0.2], control=0.5)
13+
sol = solve(ocp, grid_size=N, init=init)
14+
@test sol.objective prob.solution.objective atol=tol
15+
16+
# initial guess (constant state and functional control)
17+
init = (state=[-0.5, 0.2], control=t->6-12*t)
18+
sol = solve(ocp, grid_size=N, init=init)
19+
@test sol.objective prob.solution.objective atol=tol
20+
21+
# initial guess (functional state and constant control)
22+
init = (state=t->[-1+t, t*(t-1)], control=0.5)
23+
sol = solve(ocp, grid_size=N, init=init)
24+
@test sol.objective prob.solution.objective atol=tol
25+
26+
# initial guess (functional state and functional control)
27+
init = (state=t->[-1+t, t*(t-1)], control=t->6-12*t)
28+
sol = solve(ocp, grid_size=N, init=init)
29+
@test sol.objective prob.solution.objective atol=tol
30+
31+
end

0 commit comments

Comments
 (0)