Skip to content

Commit

Permalink
Merge pull request #315 from control-toolbox/314-juliaformatter
Browse files Browse the repository at this point in the history
formatter
  • Loading branch information
ocots authored Aug 25, 2024
2 parents 913faf9 + 75dd4f4 commit dae70b0
Show file tree
Hide file tree
Showing 25 changed files with 776 additions and 557 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/Formatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Formatter

# Modified from https://github.com/julia-actions/julia-format
on:
schedule:
- cron: '0 0 * * *'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: julia-actions/cache@v2
- name: Install JuliaFormatter and format
run: |
julia -e 'import Pkg; Pkg.add("JuliaFormatter")'
julia -e 'using JuliaFormatter; format(".")'
# https://github.com/marketplace/actions/create-pull-request
# https://github.com/peter-evans/create-pull-request#reference-example
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: ":robot: Format .jl files"
title: '[AUTO] JuliaFormatter.jl run'
branch: auto-juliaformatter-pr
delete-branch: true
labels: formatting, automated pr, no changelog
- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
46 changes: 18 additions & 28 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using CTDirect
# to add docstrings from external packages
Modules = [CTBase, CTFlows, CTDirect, OptimalControl]
for Module Modules
isnothing( DocMeta.getdocmeta(Module, :DocTestSetup) ) &&
DocMeta.setdocmeta!(Module, :DocTestSetup, :(using $Module); recursive = true)
isnothing(DocMeta.getdocmeta(Module, :DocTestSetup)) &&
DocMeta.setdocmeta!(Module, :DocTestSetup, :(using $Module); recursive = true)
end

#
Expand All @@ -18,10 +18,7 @@ makedocs(
sitename = "OptimalControl.jl",
format = Documenter.HTML(
prettyurls = false,
size_threshold_ignore = [
"api-ctbase/types.md",
"tutorial-plot.md",
],
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"),
Expand All @@ -30,47 +27,40 @@ makedocs(
pages = [
"Introduction" => "index.md",
"Basic examples" => [
"Energy minimisation" => "tutorial-basic-example.md",
"Energy minimisation" => "tutorial-basic-example.md",
"Time mininimisation" => "tutorial-double-integrator.md",
],
"Manual" => [
"Abstract syntax" => "tutorial-abstract.md",
"Initial guess" => "tutorial-initial-guess.md",
"Solve" => "tutorial-solve.md",
"Plot a solution" => "tutorial-plot.md",
"Flow" => "tutorial-flow.md",
"Functional syntax" => "tutorial-functional.md",
"Control-toolbox REPL" => "tutorial-repl.md",
"Abstract syntax" => "tutorial-abstract.md",
"Initial guess" => "tutorial-initial-guess.md",
"Solve" => "tutorial-solve.md",
"Plot a solution" => "tutorial-plot.md",
"Flow" => "tutorial-flow.md",
"Functional syntax" => "tutorial-functional.md",
"Control-toolbox REPL" => "tutorial-repl.md",
],
"Tutorials" => [
"tutorial-continuation.md",
"Goddard: direct, indirect" => "tutorial-goddard.md",
"Goddard: direct, indirect" => "tutorial-goddard.md",
"tutorial-iss.md",
"Linear–quadratic regulator" => "tutorial-lqr-basic.md",
"tutorial-nlp.md",
],
"API" => [
"api-optimalcontrol.md",
"Subpackages" => [
"api-ctbase.md",
"api-ctdirect.md",
"api-ctflows.md",
],
"Subpackages" => ["api-ctbase.md", "api-ctdirect.md", "api-ctflows.md"],
],
"Developers" => [
"OptimalControl.jl" => "dev-optimalcontrol.md",
"Subpackages" => [
"CTBase.jl" => "dev-ctbase.md",
"CTBase.jl" => "dev-ctbase.md",
"CTDirect.jl" => "dev-ctdirect.md",
"CTFlows.jl" => "dev-ctflows.md",
"CTFlows.jl" => "dev-ctflows.md",
],
],
"JuliaCon 2024"=> "juliacon2024.md",
]
"JuliaCon 2024" => "juliacon2024.md",
],
)

#
deploydocs(
repo = "github.com/control-toolbox/OptimalControl.jl.git",
devbranch = "main"
)
deploydocs(repo = "github.com/control-toolbox/OptimalControl.jl.git", devbranch = "main")
12 changes: 10 additions & 2 deletions src/OptimalControl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using CTFlows
import CommonSolve: solve, CommonSolve

# declarations
const __display = CTBase.__display
const __display = CTBase.__display
const __ocp_init = CTBase.__ocp_init

include("solve.jl")
Expand Down Expand Up @@ -53,7 +53,15 @@ export OptimalControlModel, OptimalControlSolution
export Autonomous, NonAutonomous
export NonFixed, Fixed
export Model, __OCPModel
export variable!, time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, constraint
export variable!,
time!,
constraint!,
dynamics!,
objective!,
state!,
control!,
remove_constraint!,
constraint
#export is_time_independent, is_time_dependent, is_min, is_max, is_variable_dependent, is_variable_independent
export Lie, @Lie, Poisson, Lift, , ∂ₜ
export @def
Expand Down
11 changes: 4 additions & 7 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Remove from the description, the Symbol that are specific to [OptimalControl.jl]
be passed.
"""
function clean(d::Description)
return remove(d, (:direct, ))
return remove(d, (:direct,))
end

"""
Expand Down Expand Up @@ -52,15 +52,12 @@ julia> sol = solve(ocp, init=(state=t->[-1+t, t*(t-1)], control=0.5))
julia> sol = solve(ocp, init=(state=t->[-1+t, t*(t-1)], control=t->6-12*t))
```
"""
function CommonSolve.solve(
ocp::OptimalControlModel,
description::Symbol...;
kwargs...)
function CommonSolve.solve(ocp::OptimalControlModel, description::Symbol...; kwargs...)

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

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

end
end
26 changes: 13 additions & 13 deletions test/Goddard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Goddard()
vmax = 0.1
m0 = 1
mf = 0.6
x0 = [ r0, v0, m0 ]
x0 = [r0, v0, m0]

# the model
@def ocp begin
Expand All @@ -26,26 +26,26 @@ function Goddard()
vmax = 0.1
m0 = 1
mf = 0.6
x0 = [ r0, v0, m0 ]
x0 = [r0, v0, m0]

# variables
tf R, variable
t [ t0, tf ], time
t [t0, tf], time
x R³, state
u R, control
r = x₁
v = x₂
m = x₃

# constraints
0 u(t) 1, (u_con)
r(t) r0, (x_con_rmin)
0 v(t) vmax, (x_con_vmax)
x(t0) == x0, (initial_con)
m(tf) == mf, (final_con)
0 u(t) 1, (u_con)
r(t) r0, (x_con_rmin)
0 v(t) vmax, (x_con_vmax)
x(t0) == x0, (initial_con)
m(tf) == mf, (final_con)

# dynamics
(t) == F0(x(t)) + u(t)*F1(x(t))
(t) == F0(x(t)) + u(t) * F1(x(t))

# objective
r(tf) max
Expand All @@ -54,17 +54,17 @@ function Goddard()
# dynamics
function F0(x)
r, v, m = x
D = Cd * v^2 * exp(-β*(r - 1))
return [ v, -D/m - 1/r^2, 0 ]
D = Cd * v^2 * exp(-β * (r - 1))
return [v, -D / m - 1 / r^2, 0]
end
function F1(x)
r, v, m = x
return [ 0, Tmax/m, -b*Tmax ]
return [0, Tmax / m, -b * Tmax]
end

#
objective = 1.0125762700748699

return ocp, objective

end
end
12 changes: 6 additions & 6 deletions test/problems/beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
function beam()

@def beam begin
t [ 0, 1 ], time
t [0, 1], time
x R², state
u R, control
x(0) == [0,1]
x(1) == [0,-1]
x(0) == [0, 1]
x(1) == [0, -1]
(t) == [x₂(t), u(t)]
0 x₁(t) 0.1
0 x₁(t) 0.1
-10 u(t) 10
(u(t)^2) min
end

return ((ocp=beam, obj=8.898598, name="beam", init=nothing))
end
return ((ocp = beam, obj = 8.898598, name = "beam", init = nothing))
end
54 changes: 34 additions & 20 deletions test/problems/bioreactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

# growth model MONOD
function growth(s, mu2m, Ks)
return mu2m * s / (s+Ks)
return mu2m * s / (s + Ks)
end

# light model: max^2 (0,sin) * mubar
# DAY/NIGHT CYCLE: [0,2 halfperiod] rescaled to [0,2pi]
function light(time, halfperiod)
pi = 3.141592653589793
days = time / (halfperiod*2)
tau = (days - floor(days)) * 2*pi
return max(0,sin(tau))^2
days = time / (halfperiod * 2)
tau = (days - floor(days)) * 2 * pi
return max(0, sin(tau))^2
end

# 1 day periodic problem
Expand All @@ -34,26 +34,33 @@ function bioreactor_1day_periodic()
T = halfperiod * 2

# ocp
t [ 0, T ], time
t [0, T], time
x R³, state
u R, control
y = x[1]
s = x[2]
b = x[3]
mu = light(t, halfperiod) * mubar
mu2 = growth(s(t), mu2m, Ks)
[0,0,0.001] x(t) [Inf, Inf, Inf]
[0, 0, 0.001] x(t) [Inf, Inf, Inf]
0 u(t) 1
1 y(0) Inf
1 b(0) Inf
x(0)- x(T) == [0,0,0]
(t) == [mu*y(t)/(1+y(t))-(r+u(t))*y(t),
-mu2*b(t) + u(t)*beta*(gamma*y(t)-s(t)),
(mu2-u(t)*beta)*b(t)]
(mu2*b(t)/(beta+c)) max
x(0) - x(T) == [0, 0, 0]
(t) == [
mu * y(t) / (1 + y(t)) - (r + u(t)) * y(t),
-mu2 * b(t) + u(t) * beta * (gamma * y(t) - s(t)),
(mu2 - u(t) * beta) * b(t),
]
(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


Expand All @@ -73,24 +80,31 @@ function bioreactor_Ndays()
T = 10 * N

# ocp
t [ 0, T ], time
t [0, T], time
x R³, state
u R, control
y = x[1]
s = x[2]
b = x[3]
mu = light(t, halfperiod) * mubar
mu2 = growth(s(t), mu2m, Ks)
[0,0,0.001] x(t) [Inf, Inf, Inf]
[0, 0, 0.001] x(t) [Inf, Inf, Inf]
0 u(t) 1
0.05 y(0) 0.25
0.5 s(0) 5
0.5 b(0) 3
(t) == [mu*y(t)/(1+y(t))-(r+u(t))*y(t),
-mu2*b(t) + u(t)*beta*(gamma*y(t)-s(t)),
(mu2-u(t)*beta)*b(t)]
(mu2*b(t)/(beta+c)) max
(t) == [
mu * y(t) / (1 + y(t)) - (r + u(t)) * y(t),
-mu2 * b(t) + u(t) * beta * (gamma * y(t) - s(t)),
(mu2 - u(t) * beta) * b(t),
]
(mu2 * b(t) / (beta + c)) max
end

return ((ocp=bioreactor_N, obj=19.0745, init=(state=[50,50,50],), name="bioreactor_Ndays"))
end
return ((
ocp = bioreactor_N,
obj = 19.0745,
init = (state = [50, 50, 50],),
name = "bioreactor_Ndays",
))
end
12 changes: 6 additions & 6 deletions test/problems/fuller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
function fuller()

@def fuller begin
t [ 0, 3.5 ], time
t [0, 3.5], time
x R², state
u R, control
-1 u(t) 1
x(0) == [ 0, 1 ]
x(3.5) == [ 0, 0 ]
(t) == [ x₂(t), u(t) ]
x(0) == [0, 1]
x(3.5) == [0, 0]
(t) == [x₂(t), u(t)]
(x₁(t)^2) min
end

return((ocp=fuller, obj=2.683944e-1, name="fuller", init=nothing))
end
return ((ocp = fuller, obj = 2.683944e-1, name = "fuller", init = nothing))
end
Loading

0 comments on commit dae70b0

Please sign in to comment.