Skip to content

Commit

Permalink
Merge pull request #366 from control-toolbox/auto-juliaformatter-pr
Browse files Browse the repository at this point in the history
[AUTO] JuliaFormatter.jl run
  • Loading branch information
ocots authored Sep 20, 2024
2 parents f97ec41 + 7729b0d commit 0aad020
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 190 deletions.
24 changes: 12 additions & 12 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ using CTDirect

# to add docstrings from external packages
Modules = [CTBase, CTFlows, CTDirect, OptimalControl]
for Module Modules
for Module in Modules
isnothing(DocMeta.getdocmeta(Module, :DocTestSetup)) &&
DocMeta.setdocmeta!(Module, :DocTestSetup, :(using $Module); recursive = true)
DocMeta.setdocmeta!(Module, :DocTestSetup, :(using $Module); recursive=true)
end

repo_url = "github.com/control-toolbox/OptimalControl.jl"

makedocs(
warnonly = [:cross_references, :autodocs_block],
sitename = "OptimalControl.jl",
format = Documenter.HTML(;
repolink = "https://" * repo_url,
prettyurls = false,
size_threshold_ignore = ["api-ctbase/types.md", "tutorial-plot.md"],
assets = [
makedocs(;
warnonly=[:cross_references, :autodocs_block],
sitename="OptimalControl.jl",
format=Documenter.HTML(;
repolink="https://" * repo_url,
prettyurls=false,
size_threshold_ignore=["api-ctbase/types.md", "tutorial-plot.md"],
assets=[
asset("https://control-toolbox.org/assets/css/documentation.css"),
asset("https://control-toolbox.org/assets/js/documentation.js"),
],
),
pages = [
pages=[
"Introduction" => "index.md",
"Basic examples" => [
"Energy minimisation" => "tutorial-double-integrator-energy.md",
Expand Down Expand Up @@ -64,4 +64,4 @@ makedocs(
],
)

deploydocs(; repo = repo_url * ".git", devbranch = "main")
deploydocs(; repo=repo_url * ".git", devbranch="main")
5 changes: 3 additions & 2 deletions src/OptimalControl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export OptimalControlModel, OptimalControlSolution
export Autonomous, NonAutonomous
export NonFixed, Fixed
export Model, __OCPModel
export variable!, time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!
export variable!,
time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!
export constraint
export time_grid, control, state, variable, costate, objective
export iterations, stopping, message, infos
Expand All @@ -65,7 +66,7 @@ export ParsingError
# repl
function __init__()
isdefined(Base, :active_repl) && ct_repl()
nothing
return nothing
end

end
9 changes: 5 additions & 4 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Return the list of available methods that can be used to solve the optimal contr
function available_methods()
# by order of preference: from top to bottom
methods = ()
for method CTDirect.available_methods()
for method in CTDirect.available_methods()
methods = add(methods, (:direct, method...))
end
return methods
Expand Down Expand Up @@ -84,14 +84,15 @@ julia> sol = solve(ocp, init=(state=[-0.5, 0.2], control=0.5))
For more information on how to provide the initial guess, see the [tutorial on the initial guess](@ref tutorial-init).
"""
function CommonSolve.solve(ocp::OptimalControlModel, description::Symbol...; kwargs...)::OptimalControlSolution
function CommonSolve.solve(
ocp::OptimalControlModel, description::Symbol...; kwargs...
)::OptimalControlSolution

# get the full description
method = getFullDescription(description, available_methods())

# solve the problem
if :direct method
if :direct method
return CTDirect.direct_solve(ocp, clean(description)...; kwargs...)
end

end
2 changes: 1 addition & 1 deletion test/problems/beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ function beam()
(u(t)^2) min
end

return ((ocp = beam, obj = 8.898598, name = "beam", init = nothing))
return ((ocp=beam, obj=8.898598, name="beam", init=nothing))
end
7 changes: 2 additions & 5 deletions test/problems/bioreactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function bioreactor_1day_periodic()
(mu2 * b(t) / (beta + c)) max
end

return ((ocp = bioreactor_1, obj = 0.614134, name = "bioreactor_1day_periodic", init = nothing))
return ((ocp=bioreactor_1, obj=0.614134, name="bioreactor_1day_periodic", init=nothing))
end

# N days (non periodic)
Expand Down Expand Up @@ -96,9 +96,6 @@ function bioreactor_Ndays()
end

return ((
ocp = bioreactor_N,
obj = 19.0745,
init = (state = [50, 50, 50],),
name = "bioreactor_Ndays",
ocp=bioreactor_N, obj=19.0745, init=(state=[50, 50, 50],), name="bioreactor_Ndays"
))
end
2 changes: 1 addition & 1 deletion test/problems/fuller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ function fuller()
(x₁(t)^2) min
end

return ((ocp = fuller, obj = 2.683944e-1, name = "fuller", init = nothing))
return ((ocp=fuller, obj=2.683944e-1, name="fuller", init=nothing))
end
33 changes: 15 additions & 18 deletions test/problems/goddard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function F1(x, Tmax, b)
return [0, Tmax / m, -b * Tmax]
end

function goddard(; vmax = 0.1, Tmax = 3.5, functional_constraints = false)
function goddard(; vmax=0.1, Tmax=3.5, functional_constraints=false)
# constants
Cd = 310
beta = 500
Expand All @@ -25,31 +25,33 @@ function goddard(; vmax = 0.1, Tmax = 3.5, functional_constraints = false)
x0 = [r0, v0, m0]

#ocp
goddard = Model(variable = true)
goddard = Model(; variable=true)
state!(goddard, 3)
control!(goddard, 1)
variable!(goddard, 1)
time!(goddard, t0 = 0, indf = 1)
constraint!(goddard, :initial, lb = x0, ub = x0)
constraint!(goddard, :final, rg = 3, lb = mf, ub = Inf)
time!(goddard; t0=0, indf=1)
constraint!(goddard, :initial; lb=x0, ub=x0)
constraint!(goddard, :final; rg=3, lb=mf, ub=Inf)
if functional_constraints
# note: the equations do not handle r<1 well
# without the box constraint on x, the default init (0.1) is not suitable
constraint!(goddard, :state, f = (x, v) -> x, lb = [r0, v0, mf], ub = [r0 + 0.2, vmax, m0])
constraint!(goddard, :control, f = (u, v) -> u, lb = 0, ub = 1)
constraint!(
goddard, :state; f=(x, v) -> x, lb=[r0, v0, mf], ub=[r0 + 0.2, vmax, m0]
)
constraint!(goddard, :control; f=(u, v) -> u, lb=0, ub=1)
else
constraint!(goddard, :state, lb = [r0, v0, mf], ub = [r0 + 0.2, vmax, m0])
constraint!(goddard, :control, lb = 0, ub = 1)
constraint!(goddard, :state; lb=[r0, v0, mf], ub=[r0 + 0.2, vmax, m0])
constraint!(goddard, :control; lb=0, ub=1)
end
constraint!(goddard, :variable, lb = 0.01, ub = Inf)
constraint!(goddard, :variable; lb=0.01, ub=Inf)
objective!(goddard, :mayer, (x0, xf, v) -> xf[1], :max)
dynamics!(goddard, (x, u, v) -> F0(x, Cd, beta) + u * F1(x, Tmax, b))

return ((ocp = goddard, obj = 1.01257, name = "goddard", init = (state = [1.01, 0.05, 0.8],)))
return ((ocp=goddard, obj=1.01257, name="goddard", init=(state=[1.01, 0.05, 0.8],)))
end

# abstratc definition
function goddard_a(; vmax = 0.1, Tmax = 3.5)
function goddard_a(; vmax=0.1, Tmax=3.5)
# constants
Cd = 310
beta = 500
Expand Down Expand Up @@ -79,10 +81,5 @@ function goddard_a(; vmax = 0.1, Tmax = 3.5)
r(tf) max
end

return ((
ocp = goddard_a,
obj = 1.01257,
name = "goddard_a",
init = (state = [1.01, 0.05, 0.8],),
))
return ((ocp=goddard_a, obj=1.01257, name="goddard_a", init=(state=[1.01, 0.05, 0.8],)))
end
2 changes: 1 addition & 1 deletion test/problems/insurance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ function insurance()
(U(t) * fx) max
end

return ((ocp = insurance, obj = 2.059511, name = "insurance", init = nothing))
return ((ocp=insurance, obj=2.059511, name="insurance", init=nothing))
end
2 changes: 1 addition & 1 deletion test/problems/jackson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ function jackson()
x[3](4) max
end

return ((ocp = jackson, obj = 0.192011, name = "jackson", init = nothing))
return ((ocp=jackson, obj=0.192011, name="jackson", init=nothing))
end
2 changes: 1 addition & 1 deletion test/problems/parametric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ function myocp(ρ)
end

# return problem and objective
return ((ocp = myocp(1.0), obj = -0.335936, name = "param"))
return ((ocp=myocp(1.0), obj=-0.335936, name="param"))
2 changes: 1 addition & 1 deletion test/problems/robbins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ function robbins()
(alpha * x[1](t) + beta * x[1](t)^2 + gamma * u(t)^2) min
end

return ((ocp = robbins, obj = 20.0, name = "robbins", init = nothing))
return ((ocp=robbins, obj=20.0, name="robbins", init=nothing))
end
35 changes: 20 additions & 15 deletions test/problems/swimmer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,29 @@ function swimmer()
2 * cos(2 * (b1 - b3)) +
12 * cos(2 * b1 - b3) +
186 * cos(b3) +
37 * cos(2 * b3) - 6 * cos(b1 + b3) - 3 * cos(2 * (b1 + b3)) - 6 * cos(2 * b1 + b3) -
6 * cos(b1 + 2 * b3)
37 * cos(2 * b3) - 6 * cos(b1 + b3) - 3 * cos(2 * (b1 + b3)) -
6 * cos(2 * b1 + b3) - 6 * cos(b1 + 2 * b3)

g11 =
(
-42 * sin(b1 - th) - 2 * sin(2 * b1 - th) - 24 * sin(th) - 300 * sin(b1 + th) -
12 * sin(2 * b1 + th) - 6 * sin(b1 - th - 2 * b3) - sin(2 * b1 - th - 2 * b3) +
4 * sin(th - 2 * b3) - 12 * sin(b1 + th - 2 * b3) - sin(2 * b1 + th - 2 * b3) +
-42 * sin(b1 - th) - 2 * sin(2 * b1 - th) - 24 * sin(th) -
300 * sin(b1 + th) - 12 * sin(2 * b1 + th) - 6 * sin(b1 - th - 2 * b3) -
sin(2 * b1 - th - 2 * b3) + 4 * sin(th - 2 * b3) -
12 * sin(b1 + th - 2 * b3) - sin(2 * b1 + th - 2 * b3) +
18 * sin(b1 - th - b3) +
8 * sin(th - b3) - 54 * sin(b1 + th - b3) - 2 * sin(2 * b1 + th - b3) -
18 * sin(b1 - th + b3) - 38 * sin(th + b3) - 90 * sin(b1 + th + b3) -
6 * sin(b1 - th + 2 * b3) - 18 * sin(th + 2 * b3) - 30 * sin(b1 + th + 2 * b3)
6 * sin(b1 - th + 2 * b3) - 18 * sin(th + 2 * b3) -
30 * sin(b1 + th + 2 * b3)
) / (4 * aux)

g12 =
(
-42 * cos(b1 - th) - 2 * cos(2 * b1 - th) +
24 * cos(th) +
300 * cos(b1 + th) +
12 * cos(2 * b1 + th) - 6 * cos(b1 - th - 2 * b3) - cos(2 * b1 - th - 2 * b3) -
4 * cos(th - 2 * b3) +
12 * cos(2 * b1 + th) - 6 * cos(b1 - th - 2 * b3) -
cos(2 * b1 - th - 2 * b3) - 4 * cos(th - 2 * b3) +
12 * cos(b1 + th - 2 * b3) +
cos(2 * b1 + th - 2 * b3) +
18 * cos(b1 - th - b3) - 8 * cos(th - b3) +
Expand All @@ -82,7 +84,8 @@ function swimmer()
2 * cos(2 * b1) +
12 * cos(b1 - 2 * b3) +
30 * cos(b1 - b3) +
cos(2 * (b1 - b3)) - 4 * cos(2 * b3) - 6 * cos(b1 + b3) - 6 * cos(b1 + 2 * b3)
cos(2 * (b1 - b3)) - 4 * cos(2 * b3) - 6 * cos(b1 + b3) -
6 * cos(b1 + 2 * b3)
) / (2 * aux)

g21 =
Expand All @@ -91,8 +94,9 @@ function swimmer()
4 * sin(2 * b1 - th) +
24 * sin(th) +
38 * sin(b1 + th) +
18 * sin(2 * b1 + th) - 2 * sin(b1 - th - 2 * b3) - sin(2 * b1 - th - 2 * b3) -
2 * sin(th - 2 * b3) - sin(2 * b1 + th - 2 * b3) - 54 * sin(b1 - th - b3) -
18 * sin(2 * b1 + th) - 2 * sin(b1 - th - 2 * b3) -
sin(2 * b1 - th - 2 * b3) - 2 * sin(th - 2 * b3) -
sin(2 * b1 + th - 2 * b3) - 54 * sin(b1 - th - b3) -
12 * sin(2 * b1 - th - b3) - 42 * sin(th - b3) + 18 * sin(b1 + th - b3) -
6 * sin(2 * b1 + th - b3) +
18 * sin(b1 - th + b3) +
Expand All @@ -106,10 +110,11 @@ function swimmer()
g22 =
(
8 * cos(b1 - th) + 4 * cos(2 * b1 - th) - 24 * cos(th) - 38 * cos(b1 + th) -
18 * cos(2 * b1 + th) - 2 * cos(b1 - th - 2 * b3) - cos(2 * b1 - th - 2 * b3) +
18 * cos(2 * b1 + th) - 2 * cos(b1 - th - 2 * b3) -
cos(2 * b1 - th - 2 * b3) +
2 * cos(th - 2 * b3) +
cos(2 * b1 + th - 2 * b3) - 54 * cos(b1 - th - b3) - 12 * cos(2 * b1 - th - b3) +
42 * cos(th - b3) - 18 * cos(b1 + th - b3) +
cos(2 * b1 + th - 2 * b3) - 54 * cos(b1 - th - b3) -
12 * cos(2 * b1 - th - b3) + 42 * cos(th - b3) - 18 * cos(b1 + th - b3) +
6 * cos(2 * b1 + th - b3) +
18 * cos(b1 - th + b3) +
6 * cos(2 * b1 - th + b3) - 300 * cos(th + b3) - 90 * cos(b1 + th + b3) -
Expand All @@ -131,5 +136,5 @@ function swimmer()
#∫(u1^2 + u2^2) → min
end

return ((ocp = swimmer, obj = 0.984273, name = "swimmer", init = nothing))
return ((ocp=swimmer, obj=0.984273, name="swimmer", init=nothing))
end
5 changes: 3 additions & 2 deletions test/problems/vanderpol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ function vanderpol()
x R², state
u R, control
x(0) == [1, 0]
(t) == [x[2](t), epsilon * omega * (1 - x[1](t)^2) * x[2](t) - omega^2 * x[1](t) + u(t)]
(t) ==
[x[2](t), epsilon * omega * (1 - x[1](t)^2) * x[2](t) - omega^2 * x[1](t) + u(t)]
(0.5 * (x[1](t)^2 + x[2](t)^2 + u(t)^2)) min
end

return ((ocp = vanderpol, obj = 1.047921, name = "vanderpol", init = nothing))
return ((ocp=vanderpol, obj=1.047921, name="vanderpol", init=nothing))
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include("problems/goddard.jl")

#
@testset verbose = true showtiming = true "Optimal control tests" begin
for name (
for name in (
:abstract_ocp,
:basic,
:continuation,
Expand Down
10 changes: 6 additions & 4 deletions test/test-nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ docp, nlp = direct_transcription(ocp)
#println(nlp.meta.x0)

using NLPModelsIpopt
nlp_sol = ipopt(nlp; print_level = 5, mu_strategy = "adaptive", tol = 1e-8, sb = "yes")
sol = OptimalControlSolution(docp, primal = nlp_sol.solution, dual = nlp_sol.multipliers)
nlp_sol = ipopt(nlp; print_level=5, mu_strategy="adaptive", tol=1e-8, sb="yes")
sol = OptimalControlSolution(docp; primal=nlp_sol.solution, dual=nlp_sol.multipliers)
plot(sol)

using MadNLP
Expand All @@ -30,10 +30,12 @@ set_initial_guess(docp, nlp, sol)
#println(nlp.meta.x0)
mad_nlp_sol = madnlp(nlp)

docp, nlp = direct_transcription(ocp, init = sol)
docp, nlp = direct_transcription(ocp; init=sol)
mad_nlp_sol = madnlp(nlp)

mad_sol = OptimalControlSolution(docp, primal = madnlp_sol.solution, dual = madnlp_sol.multipliers)
mad_sol = OptimalControlSolution(
docp; primal=madnlp_sol.solution, dual=madnlp_sol.multipliers
)
plot(mad_sol)

#=
Expand Down
4 changes: 2 additions & 2 deletions test/test_abstract_ocp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function test_abstract_ocp()
end

@testset verbose = true showtiming = true ":double_integrator :min_tf :abstract" begin
sol1 = solve(ocp1, print_level = 0, tol = 1e-12)
sol1 = solve(ocp1; print_level=0, tol=1e-12)
@test sol1.objective 2.0 rtol = 1e-2
end

@testset verbose = true showtiming = true ":goddard :max_rf :abstract :constr" begin
ocp3 = goddard_a().ocp
sol3 = solve(ocp3, print_level = 0, tol = 1e-12)
sol3 = solve(ocp3; print_level=0, tol=1e-12)
@test sol3.objective 1.0125 rtol = 1e-2
end
end
2 changes: 1 addition & 1 deletion test/test_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ function test_basic()
(0.5u(t)^2) min
end

sol = solve(ocp; display = false)
sol = solve(ocp; display=false)
@test objective(sol) 6 atol = 1e-2
end
Loading

0 comments on commit 0aad020

Please sign in to comment.