-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjective.jl
More file actions
193 lines (154 loc) · 4.28 KB
/
objective.jl
File metadata and controls
193 lines (154 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
$(TYPEDSIGNATURES)
Set the objective of the optimal control problem.
# Arguments
- `ocp::PreModel`: the optimal control problem.
- `criterion::Symbol`: the type of criterion. Either :min or :max. Default is :min.
- `mayer::Union{Function, Nothing}`: the Mayer function (inplace). Default is nothing.
- `lagrange::Union{Function, Nothing}`: the Lagrange function (inplace). Default is nothing.
!!! note
- The state, control and variable must be set before the objective.
- The objective must not be set before.
- At least one of the two functions must be given. Please provide a Mayer or a Lagrange function.
# Examples
```julia-repl
julia> function mayer(x0, xf, v)
return x0[1] + xf[1] + v[1]
end
julia> function lagrange(t, x, u, v)
return x[1] + u[1] + v[1]
end
julia> objective!(ocp, :min, mayer=mayer, lagrange=lagrange)
```
"""
function objective!(
ocp::PreModel,
criterion::Symbol=__criterion_type();
mayer::Union{Function,Nothing}=nothing,
lagrange::Union{Function,Nothing}=nothing,
)::Nothing
# checks: times, state, and control must be set before the objective
@ensure __is_state_set(ocp) CTBase.UnauthorizedCall(
"the state must be set before the objective."
)
@ensure __is_control_set(ocp) CTBase.UnauthorizedCall(
"the control must be set before the objective."
)
@ensure __is_times_set(ocp) CTBase.UnauthorizedCall(
"the times must be set before the objective."
)
# checks: the objective must not already be set
@ensure !__is_objective_set(ocp) CTBase.UnauthorizedCall(
"the objective has already been set."
)
# checks: at least one of the two functions must be given
@ensure !(isnothing(mayer) && isnothing(lagrange)) CTBase.IncorrectArgument(
"at least one of the two functions must be given. Please provide a Mayer or a Lagrange function.",
)
# set the objective
if !isnothing(mayer) && isnothing(lagrange)
ocp.objective = MayerObjectiveModel(mayer, criterion)
elseif isnothing(mayer) && !isnothing(lagrange)
ocp.objective = LagrangeObjectiveModel(lagrange, criterion)
else
ocp.objective = BolzaObjectiveModel(mayer, lagrange, criterion)
end
return nothing
end
# ------------------------------------------------------------------------------ #
# GETTERS
# ------------------------------------------------------------------------------ #
# From MayerObjectiveModel
"""
$(TYPEDSIGNATURES)
Return the criterion (:min or :max).
"""
function criterion(model::MayerObjectiveModel)::Symbol
return model.criterion
end
"""
$(TYPEDSIGNATURES)
Return the Mayer function.
"""
function mayer(model::MayerObjectiveModel{M})::M where {M<:Function}
return model.mayer
end
"""
$(TYPEDSIGNATURES)
Return true.
"""
function has_mayer_cost(::MayerObjectiveModel)::Bool
return true
end
"""
$(TYPEDSIGNATURES)
Return false.
"""
function has_lagrange_cost(::MayerObjectiveModel)::Bool
return false
end
# From LagrangeObjectiveModel
"""
$(TYPEDSIGNATURES)
Return the criterion (:min or :max).
"""
function criterion(model::LagrangeObjectiveModel)::Symbol
return model.criterion
end
"""
$(TYPEDSIGNATURES)
Return the Lagrange function.
"""
function lagrange(model::LagrangeObjectiveModel{L})::L where {L<:Function}
return model.lagrange
end
"""
$(TYPEDSIGNATURES)
Return false.
"""
function has_mayer_cost(::LagrangeObjectiveModel)::Bool
return false
end
"""
$(TYPEDSIGNATURES)
Return true.
"""
function has_lagrange_cost(::LagrangeObjectiveModel)::Bool
return true
end
# From BolzaObjectiveModel
"""
$(TYPEDSIGNATURES)
Return the criterion (:min or :max).
"""
function criterion(model::BolzaObjectiveModel)::Symbol
return model.criterion
end
"""
$(TYPEDSIGNATURES)
Return the Mayer function.
"""
function mayer(model::BolzaObjectiveModel{M,<:Function})::M where {M<:Function}
return model.mayer
end
"""
$(TYPEDSIGNATURES)
Return the Lagrange function.
"""
function lagrange(model::BolzaObjectiveModel{<:Function,L})::L where {L<:Function}
return model.lagrange
end
"""
$(TYPEDSIGNATURES)
Return true.
"""
function has_mayer_cost(::BolzaObjectiveModel)::Bool
return true
end
"""
$(TYPEDSIGNATURES)
Return true.
"""
function has_lagrange_cost(::BolzaObjectiveModel)::Bool
return true
end