-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimal_control_model-type.jl
324 lines (264 loc) · 8.52 KB
/
optimal_control_model-type.jl
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# ----------------------------------------------------------------------
#
# Definition of an optimal control model
#
"""
$(TYPEDEF)
**Fields**
$(TYPEDFIELDS)
"""
@with_kw mutable struct OptimalControlModel{
time_dependence<:TimeDependence,variable_dependence<:VariableDependence
} <: AbstractOptimalControlModel
model_expression::Union{Nothing,Expr} = nothing
initial_time::Union{Time,Index,Nothing} = nothing
initial_time_name::Union{String,Nothing} = nothing
final_time::Union{Time,Index,Nothing} = nothing
final_time_name::Union{String,Nothing} = nothing
time_name::Union{String,Nothing} = nothing
control_dimension::Union{Dimension,Nothing} = nothing
control_components_names::Union{Vector{String},Nothing} = nothing
control_name::Union{String,Nothing} = nothing
state_dimension::Union{Dimension,Nothing} = nothing
state_components_names::Union{Vector{String},Nothing} = nothing
state_name::Union{String,Nothing} = nothing
variable_dimension::Union{Dimension,Nothing} = nothing
variable_components_names::Union{Vector{String},Nothing} = nothing
variable_name::Union{String,Nothing} = nothing
lagrange::Union{Lagrange,Lagrange!,Nothing} = nothing
mayer::Union{Mayer,Mayer!,Nothing} = nothing
criterion::Union{Symbol,Nothing} = nothing
dynamics::Union{Dynamics,Dynamics!,Nothing} = nothing
constraints::Dict{Symbol,Tuple{Vararg{Any}}} = Dict{Symbol,Tuple{Vararg{Any}}}()
dim_control_constraints::Union{Dimension,Nothing} = nothing
dim_state_constraints::Union{Dimension,Nothing} = nothing
dim_mixed_constraints::Union{Dimension,Nothing} = nothing
dim_boundary_constraints::Union{Dimension,Nothing} = nothing
dim_variable_constraints::Union{Dimension,Nothing} = nothing
dim_control_range::Union{Dimension,Nothing} = nothing
dim_state_range::Union{Dimension,Nothing} = nothing
dim_variable_range::Union{Dimension,Nothing} = nothing
in_place::Union{Bool,Nothing} = nothing
end
# ----------------------------------------------------------------------
#
# checkings: internal functions
#
"""
$(TYPEDSIGNATURES)
"""
function __is_variable_not_set(ocp::OptimalControlModel)
conditions = [
isnothing(ocp.variable_dimension),
isnothing(ocp.variable_name),
isnothing(ocp.variable_components_names),
]
@assert all(conditions) || !any(conditions) # either all or none
return isnothing(ocp.variable_dimension)
end
"""
$(TYPEDSIGNATURES)
"""
__is_variable_set(ocp::OptimalControlModel) = !__is_variable_not_set(ocp)
"""
$(TYPEDSIGNATURES)
"""
function __is_time_not_set(ocp::OptimalControlModel)
conditions = [
isnothing(ocp.initial_time),
isnothing(ocp.initial_time_name),
isnothing(ocp.final_time),
isnothing(ocp.final_time_name),
isnothing(ocp.time_name),
]
@assert all(conditions) || !any(conditions) # either all or none
return isnothing(ocp.initial_time)
end
"""
$(TYPEDSIGNATURES)
"""
__is_time_set(ocp::OptimalControlModel) = !__is_time_not_set(ocp)
"""
$(TYPEDSIGNATURES)
"""
function __is_state_not_set(ocp::OptimalControlModel)
conditions = [
isnothing(ocp.state_dimension),
isnothing(ocp.state_name),
isnothing(ocp.state_components_names),
]
@assert all(conditions) || !any(conditions) # either all or none
return isnothing(ocp.state_dimension)
end
"""
$(TYPEDSIGNATURES)
"""
__is_state_set(ocp::OptimalControlModel) = !__is_state_not_set(ocp)
"""
$(TYPEDSIGNATURES)
"""
function __is_control_not_set(ocp::OptimalControlModel)
conditions = [
isnothing(ocp.control_dimension),
isnothing(ocp.control_name),
isnothing(ocp.control_components_names),
]
@assert all(conditions) || !any(conditions) # either all or none
return isnothing(ocp.control_dimension)
end
"""
$(TYPEDSIGNATURES)
"""
__is_control_set(ocp::OptimalControlModel) = !__is_control_not_set(ocp)
"""
$(TYPEDSIGNATURES)
"""
__is_dynamics_not_set(ocp::OptimalControlModel) = isnothing(ocp.dynamics)
"""
$(TYPEDSIGNATURES)
"""
__is_dynamics_set(ocp::OptimalControlModel) = !__is_dynamics_not_set(ocp)
"""
$(TYPEDSIGNATURES)
"""
function __is_objective_not_set(ocp::OptimalControlModel)
conditions = [isnothing(ocp.lagrange) && isnothing(ocp.mayer), isnothing(ocp.criterion)]
@assert all(conditions) || !any(conditions) # either all or none
return isnothing(ocp.criterion)
end
"""
$(TYPEDSIGNATURES)
"""
__is_objective_set(ocp::OptimalControlModel) = !__is_objective_not_set(ocp)
"""
$(TYPEDSIGNATURES)
"""
__is_empty(ocp::OptimalControlModel) = begin
isnothing(ocp.initial_time) &&
isnothing(ocp.initial_time_name) &&
isnothing(ocp.final_time) &&
isnothing(ocp.final_time_name) &&
isnothing(ocp.time_name) &&
isnothing(ocp.lagrange) &&
isnothing(ocp.mayer) &&
isnothing(ocp.criterion) &&
isnothing(ocp.dynamics) &&
isnothing(ocp.state_dimension) &&
isnothing(ocp.state_name) &&
isnothing(ocp.state_components_names) &&
isnothing(ocp.control_dimension) &&
isnothing(ocp.control_name) &&
isnothing(ocp.control_components_names) &&
isnothing(ocp.variable_dimension) &&
isnothing(ocp.variable_name) &&
isnothing(ocp.variable_components_names) &&
isempty(ocp.constraints)
end
"""
$(TYPEDSIGNATURES)
"""
__is_initial_time_free(ocp) = ocp.initial_time isa Index
"""
$(TYPEDSIGNATURES)
"""
__is_final_time_free(ocp) = ocp.final_time isa Index
"""
$(TYPEDSIGNATURES)
"""
__is_incomplete(ocp) = begin
__is_time_not_set(ocp) ||
__is_state_not_set(ocp) ||
__is_control_not_set(ocp) ||
__is_dynamics_not_set(ocp) ||
__is_objective_not_set(ocp) ||
(__is_variable_not_set(ocp) && is_variable_dependent(ocp))
end
"""
$(TYPEDSIGNATURES)
"""
__is_complete(ocp) = !__is_incomplete(ocp)
"""
$(TYPEDSIGNATURES)
"""
__is_criterion_valid(criterion::Symbol) = criterion ∈ [:min, :max]
# ----------------------------------------------------------------------
#
# checkings: internal functions, return exceptions
#
"""
$(TYPEDSIGNATURES)
Throw ```IncorrectArgument``` exception if dependencies arguments are incorrect.
"""
function __check_dependencies(dependencies::Tuple{Vararg{DataType}})
size(filter(p -> p <: VariableDependence, dependencies), 1) > 1 && throw(
IncorrectArgument(
"the number of arguments about variable dependence must be equal at most to 1",
),
)
size(filter(p -> p <: TimeDependence, dependencies), 1) > 1 && throw(
IncorrectArgument(
"the number of arguments about time dependence must be equal at most to 1"
),
)
size(dependencies, 1) > 2 && throw(
IncorrectArgument(
"the number of arguments about dependencies must be equal at most to 2"
),
)
return size(
filter(p -> !(p <: Union{TimeDependence,VariableDependence}), dependencies), 1
) > 0 && throw(
IncorrectArgument(
"wrong type arguments, possible arguments are: NonAutonomous, Autonomous, Fixed, NonFixed",
),
)
end
"""
$(TYPEDSIGNATURES)
Throw ```UnauthorizedCall``` exception if the state of an ocp is not set.
"""
function __check_state_set(ocp::OptimalControlModel)
return __is_state_not_set(ocp) &&
throw(UnauthorizedCall("the state dimension has to be set before."))
end
"""
$(TYPEDSIGNATURES)
Throw ```UnauthorizedCall``` exception if the control of an ocp is not set.
"""
function __check_control_set(ocp::OptimalControlModel)
return __is_control_not_set(ocp) &&
throw(UnauthorizedCall("the control dimension has to be set before."))
end
"""
$(TYPEDSIGNATURES)
Throw ```UnauthorizedCall``` exception if the time of an ocp is not set.
"""
function __check_is_time_set(ocp::OptimalControlModel)
return __is_time_not_set(ocp) &&
throw(UnauthorizedCall("the time dimension has to be set before."))
end
"""
$(TYPEDSIGNATURES)
Throw ```UnauthorizedCall``` exception if the variable of an ocp is not set.
"""
function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence,NonFixed})
return __is_variable_not_set(ocp) &&
throw(UnauthorizedCall("the variable dimension has to be set before."))
end
"""
$(TYPEDSIGNATURES)
Do nothing, no variable for fixed ocp.
"""
function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence,Fixed})
return nothing
end
"""
$(TYPEDSIGNATURES)
Check if the parameters of an ocp are set.
"""
function __check_all_set(ocp::OptimalControlModel)
__check_state_set(ocp)
__check_control_set(ocp)
__check_is_time_set(ocp)
return __check_variable_set(ocp)
end