1
1
module MTKJuMPControlExt
2
2
using ModelingToolkit
3
3
using JuMP, InfiniteOpt
4
- using DiffEqDevTools, DiffEqBase, SciMLBase
4
+ using DiffEqDevTools, DiffEqBase
5
5
using LinearAlgebra
6
6
const MTK = ModelingToolkit
7
7
8
- abstract type AbstractOptimalControlProblem{uType, tType, isinplace} < :
9
- SciMLBase. AbstractODEProblem{uType, tType, isinplace} end
10
-
11
8
struct JuMPControlProblem{uType, tType, isinplace, P, F, K} < :
12
9
AbstractOptimalControlProblem{uType, tType, isinplace}
13
10
f:: F
@@ -56,6 +53,7 @@ The constraints are:
56
53
function MTK. JuMPControlProblem (sys:: ODESystem , u0map, tspan, pmap;
57
54
dt = error (" dt must be provided for JuMPControlProblem." ),
58
55
guesses = Dict (), kwargs... )
56
+ MTK. warn_overdetermined (sys, u0map)
59
57
_u0map = has_alg_eqs (sys) ? u0map : merge (Dict (u0map), Dict (guesses))
60
58
f, u0, p = MTK. process_SciMLProblem (ODEFunction, sys, _u0map, pmap;
61
59
t = tspan != = nothing ? tspan[1 ] : tspan, kwargs... )
@@ -77,6 +75,7 @@ of the system as derivative constraints, rather than using a solver tableau.
77
75
function MTK. InfiniteOptControlProblem (sys:: ODESystem , u0map, tspan, pmap;
78
76
dt = error (" dt must be provided for InfiniteOptControlProblem." ),
79
77
guesses = Dict (), kwargs... )
78
+ MTK. warn_overdetermined (sys, u0map)
80
79
_u0map = has_alg_eqs (sys) ? u0map : merge (Dict (u0map), Dict (guesses))
81
80
f, u0, p = MTK. process_SciMLProblem (ODEFunction, sys, _u0map, pmap;
82
81
t = tspan != = nothing ? tspan[1 ] : tspan, kwargs... )
@@ -87,12 +86,6 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap;
87
86
end
88
87
89
88
function init_model (sys, tsteps, u0map, u0)
90
- constraintsys = MTK. get_constraintsystem (sys)
91
- if ! isnothing (constraintsys)
92
- (length (constraints (constraintsys)) + length (u0map) > length (unknowns (sys))) &&
93
- @warn " The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization."
94
- end
95
-
96
89
ctrls = controls (sys)
97
90
states = unknowns (sys)
98
91
model = InfiniteModel ()
@@ -110,7 +103,7 @@ function init_model(sys, tsteps, u0map, u0)
110
103
return model
111
104
end
112
105
113
- function add_jump_cost_function! (model, sys)
106
+ function add_jump_cost_function! (model:: InfiniteModel , sys)
114
107
jcosts = MTK. get_costs (sys)
115
108
consolidate = MTK. get_consolidate (sys)
116
109
if isnothing (jcosts) || isempty (jcosts)
@@ -141,7 +134,7 @@ function add_jump_cost_function!(model, sys)
141
134
@objective (model, Min, consolidate (jcosts))
142
135
end
143
136
144
- function add_user_constraints! (model, sys)
137
+ function add_user_constraints! (model:: InfiniteModel , sys)
145
138
conssys = MTK. get_constraintsystem (sys)
146
139
jconstraints = isnothing (conssys) ? nothing : MTK. get_constraints (conssys)
147
140
(isnothing (jconstraints) || isempty (jconstraints)) && return nothing
@@ -178,14 +171,14 @@ function add_user_constraints!(model, sys)
178
171
end
179
172
end
180
173
181
- function add_initial_constraints! (model, u0, u0_idxs, ts)
174
+ function add_initial_constraints! (model:: InfiniteModel , u0, u0_idxs, ts)
182
175
U = model[:U ]
183
176
@constraint (model, initial[i in u0_idxs], U[i](ts)== u0[i])
184
177
end
185
178
186
179
is_explicit (tableau) = tableau isa DiffEqDevTools. ExplicitRKTableau
187
180
188
- function add_infopt_solve_constraints! (model, sys, pmap)
181
+ function add_infopt_solve_constraints! (model:: InfiniteModel , sys, pmap)
189
182
iv = MTK. get_iv (sys)
190
183
t = model[:t ]
191
184
U = model[:U ]
@@ -257,14 +250,6 @@ function add_jump_solve_constraints!(prob, tableau)
257
250
end
258
251
end
259
252
260
- """
261
- """
262
- struct JuMPControlSolution
263
- model:: InfiniteModel
264
- sol:: ODESolution
265
- input_sol:: Union{Nothing, ODESolution}
266
- end
267
-
268
253
"""
269
254
Solve JuMPControlProblem. Arguments:
270
255
- prob: a JumpControlProblem
@@ -334,7 +319,7 @@ function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver)
334
319
input_sol, SciMLBase. ReturnCode. ConvergenceFailure))
335
320
end
336
321
337
- JuMPControlSolution (model, sol, input_sol)
322
+ OptimalControlSolution (model, sol, input_sol)
338
323
end
339
324
340
325
end
0 commit comments