Skip to content

Commit 212f3d1

Browse files
authored
Merge pull request #81 from control-toolbox/develop
Develop
2 parents 379db0f + 7170242 commit 212f3d1

40 files changed

+7972
-3635
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ Manifest.toml
2626

2727
# Notebooks checkpoints
2828
*/.ipynb_checkpoints
29+
30+
# garbage
31+
.\#*
32+
\#*
33+
.*.swp

Project.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,32 @@ authors = ["Olivier Cots <[email protected]>"]
44
version = "0.6.0"
55

66
[deps]
7+
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
78
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
89
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
910
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
11+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1012
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
13+
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1114
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1215
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
16+
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
1317
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1418
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
19+
ReplMaker = "b873ce64-0db9-51f5-a568-4457d8e49576"
20+
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1521

1622
[compat]
23+
DataStructures = "0.18"
1724
DocStringExtensions = "0.9"
1825
ForwardDiff = "0.10"
1926
Interpolations = "0.14"
2027
MLStyle = "0.4"
28+
MacroTools = "0.5"
2129
Parameters = "0.12"
2230
Plots = "1.38"
31+
PrettyTables = "2.2"
2332
Printf = "1.8"
2433
Reexport = "1.2"
34+
ReplMaker = "0.2"
2535
julia = "1.8"

docs/src/api-plot.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ sol.control_dimension = m
4444
sol.times = times
4545
sol.time_name="t"
4646
sol.state = x
47-
sol.state_names = [ "x" * ctindices(i) for i ∈ range(1, n)]
48-
sol.adjoint = p
47+
sol.state_components_names = [ "x" * ctindices(i) for i ∈ range(1, n)]
48+
sol.costate = p
4949
sol.control = u
50-
sol.control_names = [ "u" ]
50+
sol.control_components_names = [ "u" ]
5151
sol.objective = objective
5252
sol.iterations = 0
5353
sol.stopping = :dummy
@@ -85,7 +85,7 @@ You can specify some styles:
8585
```@example main
8686
plot(sol,
8787
state_style=(color=:blue,),
88-
adjoint_style=(color=:black, linestyle=:dash),
88+
costate_style=(color=:black, linestyle=:dash),
8989
control_style=(color=:red, linewidth=2),
9090
size=(800, 600))
9191
```

docs/src/api-print.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ A = [ 0 1
5252
0 0 ]
5353
B = [ 0
5454
1 ]
55-
constraint!(ocp, :dynamics, (x, u) -> A*x + B*u)
55+
dynamics!(ocp, (x, u) -> A*x + B*u)
5656

5757
objective!(ocp, :lagrange, (x, u) -> 0.5u^2)
5858
nothing # hide

src/CTBase.jl

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ import Base
1616
using DocStringExtensions
1717
using ForwardDiff: jacobian, gradient, ForwardDiff # automatic differentiation
1818
using Interpolations: linear_interpolation, Line, Interpolations # for default interpolation
19-
using MLStyle
19+
using Reexport
20+
@reexport using MLStyle # pattern matching
2021
using Parameters # @with_kw: to have default values in struct
2122
using Plots
2223
import Plots: plot, plot! # import instead of using to overload the plot and plot! functions
2324
using Printf # to print an OptimalControlModel
24-
using Reexport
25+
using DataStructures # OrderedDict for aliases
26+
using Unicode # unicode primitives
27+
using PrettyTables # to print a table
28+
using ReplMaker
29+
using MacroTools: inexpr, striplines, MacroTools
30+
using LinearAlgebra
2531

2632
# --------------------------------------------------------------------------------------------------
2733
# Aliases for types
@@ -30,12 +36,10 @@ using Reexport
3036
Type alias for a real number.
3137
"""
3238
const ctNumber = Real
33-
const MyNumber = Real
3439
"""
3540
Type alias for a vector of real numbers.
3641
"""
37-
const ctVector = AbstractVector{<:ctNumber}
38-
const MyVector = AbstractVector{<:MyNumber}
42+
const ctVector = Union{ctNumber, AbstractVector{<:ctNumber}} # [] must be defined as Vector{Real}()
3943
"""
4044
Type alias for a time.
4145
"""
@@ -53,51 +57,62 @@ Type alias for a state.
5357
"""
5458
const State = ctVector
5559
"""
56-
Type alias for an adjoint.
60+
Type alias for an costate.
5761
"""
58-
const Adjoint = ctVector # todo: add ajoint to write p*f(x, u) instead of p'*f(x,u)
62+
const Costate = ctVector # todo: add ajoint to write p*f(x, u) instead of p'*f(x,u)
5963
"""
6064
Type alias for a control.
6165
"""
6266
const Control = ctVector
6367
"""
68+
Type alias for a variable.
69+
"""
70+
const Variable = ctVector
71+
"""
6472
Type alias for a vector of states.
6573
"""
66-
const States = Vector{<:State}
74+
const States = AbstractVector{<:State}
6775
"""
68-
Type alias for a vector of adjoints.
76+
Type alias for a vector of costates.
6977
"""
70-
const Adjoints = Vector{<:Adjoint}
78+
const Costates = AbstractVector{<:Costate}
7179
"""
7280
Type alias for a vector of controls.
7381
"""
74-
const Controls = Vector{<:Control}
82+
const Controls = AbstractVector{<:Control}
7583
"""
7684
Type alias for a dimension.
7785
"""
7886
const Dimension = Integer
7987

8088
#
81-
include("exceptions.jl")
89+
include("exception.jl")
8290
include("description.jl")
83-
include("callbacks.jl")
84-
include("functions.jl")
85-
#
91+
include("callback.jl")
8692
include("default.jl")
8793
include("utils.jl")
88-
include("model.jl")
8994
#
90-
include("ctparser_utils.jl")
91-
#include("ctparser.jl")
92-
#@reexport using .CtParser
95+
include("types.jl")
96+
#
97+
include("checking.jl")
9398
#
9499
include("print.jl")
95-
include("solution.jl")
96100
include("plot.jl")
101+
#
102+
include("functions.jl")
103+
include("model.jl")
104+
#
105+
include("ctparser_utils.jl")
106+
#include("ctparser.jl")
107+
include("onepass.jl")
108+
include("repl.jl")
97109

98110
# numeric types
99111
export ctNumber, ctVector, Time, Times, TimesDisc
100-
export States, Adjoints, Controls, State, Adjoint, Dimension, Index
112+
113+
export States, Costates, Controls, State, Costate, Control, Variable, Dimension, Index
114+
export TimeDependence, Autonomous, NonAutonomous
115+
export VariableDependence, NonFixed, Fixed
101116

102117
# callback
103118
export CTCallback, CTCallbacks, PrintCallback, StopCallback
@@ -107,29 +122,35 @@ export get_priority_print_callbacks, get_priority_stop_callbacks
107122
export Description, makeDescription, add, getFullDescription
108123

109124
# exceptions
110-
export CTException, AmbiguousDescription, InconsistentArgument, IncorrectMethod
125+
export CTException, ParsingError, AmbiguousDescription, IncorrectMethod
111126
export IncorrectArgument, IncorrectOutput, NotImplemented, UnauthorizedCall
112127

113128
# functions
114129
export Hamiltonian, HamiltonianVectorField, VectorField
115130
export Mayer, Lagrange, Dynamics, ControlLaw, FeedbackControl, Multiplier
116-
export BoundaryConstraint, StateConstraint, ControlConstraint, MixedConstraint
131+
export BoundaryConstraint, StateConstraint, ControlConstraint, MixedConstraint, VariableConstraint
117132

118133
# model
119134
export OptimalControlModel
120135
export Model
121-
export time!, constraint!, objective!, state!, control!, remove_constraint!, constraint
122-
export isautonomous, isnonautonomous, ismin, ismax
136+
export variable!, time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, constraint
137+
export is_time_independent, is_time_dependent, is_min, is_max, is_variable_dependent, is_variable_independent
123138
export nlp_constraints, constraints_labels
124139

125140
# solution
126141
export OptimalControlSolution
127-
export plot
142+
export plot, plot!
128143

129144
# utils
130145
export Ad, Poisson, ctgradient, ctjacobian, ctinterpolate, ctindices, ctupperscripts
131146

132-
# _Time
133-
export _Time
147+
# ctparser_utils
148+
export replace_call, constraint_type
149+
150+
# onepass
151+
export @def
152+
153+
# repl
154+
isdefined(Base, :active_repl) && __init_repl()
134155

135156
end
File renamed without changes.

src/checking.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function __check_time_dependence(time_dependence::DataType)
2+
time_dependence [Autonomous, NonAutonomous] && throw(IncorrectArgument("time_dependence must be either Autonomous or NonAutonomous"))
3+
end
4+
5+
function __check_variable_dependence(variable_dependence::DataType)
6+
variable_dependence [Fixed, NonFixed] && throw(IncorrectArgument("variable_dependence must be either Fixed or NonFixed"))
7+
end
8+
9+
function __check_dependences(dependences::Tuple{Vararg{DataType}})
10+
size(filter(p->p<:VariableDependence,dependences),1) > 1 && throw(IncorrectArgument("the number of arguments about variable dependence must be equal at most to 1"))
11+
size(filter(p->p<:TimeDependence,dependences),1) > 1 && throw(IncorrectArgument("the number of arguments about time dependence must be equal at most to 1"))
12+
size(dependences,1) > 2 && throw(IncorrectArgument("the number of arguments about dependences must be equal at most to 2"))
13+
size(filter(p->!(p<:Union{TimeDependence,VariableDependence}),dependences),1) > 0 && throw(IncorrectArgument("the wrong of arguments, possible arguments are : NonAutonomous, Autonomous, Fixed, NonFixed"))
14+
end
15+
16+
function __check_criterion(criterion::Symbol)
17+
criterion [:min, :max] && throw(IncorrectArgument("criterion must be either :min or :max"))
18+
end
19+
20+
function __check_state_set(ocp::OptimalControlModel)
21+
__is_state_not_set(ocp) && throw(UnauthorizedCall("the state dimension has to be set before. Use state!."))
22+
end
23+
24+
function __check_control_set(ocp::OptimalControlModel)
25+
__is_control_not_set(ocp) && throw(UnauthorizedCall("the control dimension has to be set before. Use control!."))
26+
end
27+
28+
function __check___is_time_set(ocp::OptimalControlModel)
29+
__is_time_not_set(ocp) && throw(UnauthorizedCall("the time dimension has to be set before. Use time!."))
30+
end
31+
32+
function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence, NonFixed})
33+
__is_variable_not_set(ocp) && throw(UnauthorizedCall("the variable dimension has to be set before. Use variable!."))
34+
end
35+
36+
function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence, Fixed})
37+
nothing
38+
end
39+
40+
function __check_all_set(ocp::OptimalControlModel)
41+
__check_state_set(ocp)
42+
__check_control_set(ocp)
43+
__check___is_time_set(ocp)
44+
__check_variable_set(ocp)
45+
end
46+
47+
macro __check(s::Symbol)
48+
s == :dependences && return esc(quote __check_dependences($s) end)
49+
s == :time_dependence && return esc(quote __check_time_dependence($s) end)
50+
s == :variable_dependence && return esc(quote __check_variable_dependence($s) end)
51+
s == :criterion && return esc(quote __check_criterion($s) end)
52+
s == :ocp && return esc(quote __check_all_set($s) end)
53+
error("s must be either :time_dependence, :variable_dependence or :criterion")
54+
end

0 commit comments

Comments
 (0)