@@ -48,20 +48,26 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <:
48
48
end
49
49
50
50
MTK. generate_internal_model (m:: Type{InfiniteOptModel} ) = InfiniteModel ()
51
- MTK. generate_time_variable! (m:: InfiniteModel , tspan, tsteps) = @infinite_parameter (m, t in [tspan[1 ], tspan[2 ]], num_supports = length (tsteps))
52
- MTK. generate_state_variable! (m:: InfiniteModel , u0:: Vector , ns, ts) = @variable (m, U[i = 1 : ns], Infinite (m[:t ]), start= u0[i])
53
- MTK. generate_input_variable! (m:: InfiniteModel , c0, nc, ts) = @variable (m, V[i = 1 : nc], Infinite (m[:t ]), start= c0[i])
51
+ function MTK. generate_time_variable! (m:: InfiniteModel , tspan, tsteps)
52
+ @infinite_parameter (m, t in [tspan[1 ], tspan[2 ]], num_supports = length (tsteps))
53
+ end
54
+ function MTK. generate_state_variable! (m:: InfiniteModel , u0:: Vector , ns, ts)
55
+ @variable (m, U[i = 1 : ns], Infinite (m[:t ]), start= u0[i])
56
+ end
57
+ function MTK. generate_input_variable! (m:: InfiniteModel , c0, nc, ts)
58
+ @variable (m, V[i = 1 : nc], Infinite (m[:t ]), start= c0[i])
59
+ end
54
60
55
61
function MTK. generate_timescale! (m:: InfiniteModel , guess, is_free_t)
56
62
@variable (m, tₛ ≥ 0 , start = guess)
57
63
if ! is_free_t
58
- fix (tₛ, 1 , force= true )
64
+ fix (tₛ, 1 , force = true )
59
65
set_start_value (tₛ, 1 )
60
66
end
61
67
tₛ
62
68
end
63
69
64
- function MTK. add_constraint! (m:: InfiniteOptModel , expr:: Union{Equation, Inequality} )
70
+ function MTK. add_constraint! (m:: InfiniteOptModel , expr:: Union{Equation, Inequality} )
65
71
if expr isa Equation
66
72
@constraint (m. model, expr. lhs - expr. rhs == 0 )
67
73
elseif expr. relational_op === Symbolics. geq
@@ -76,20 +82,26 @@ function MTK.JuMPDynamicOptProblem(sys::System, op, tspan;
76
82
dt = nothing ,
77
83
steps = nothing ,
78
84
guesses = Dict (), kwargs... )
79
- prob, _ = MTK. process_DynamicOptProblem (JuMPDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs... )
85
+ prob,
86
+ _ = MTK. process_DynamicOptProblem (JuMPDynamicOptProblem, InfiniteOptModel, sys,
87
+ op, tspan; dt, steps, guesses, kwargs... )
80
88
prob
81
89
end
82
90
83
91
function MTK. InfiniteOptDynamicOptProblem (sys:: System , op, tspan;
84
92
dt = nothing ,
85
93
steps = nothing ,
86
94
guesses = Dict (), kwargs... )
87
- prob, pmap = MTK. process_DynamicOptProblem (InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs... )
95
+ prob,
96
+ pmap = MTK. process_DynamicOptProblem (InfiniteOptDynamicOptProblem, InfiniteOptModel,
97
+ sys, op, tspan; dt, steps, guesses, kwargs... )
88
98
MTK. add_equational_constraints! (prob. wrapped_model, sys, pmap, tspan)
89
99
prob
90
100
end
91
101
92
- MTK. lowered_integral (model:: InfiniteOptModel , expr, lo, hi) = model. tₛ * InfiniteOpt.∫ (expr, model. model[:t ], lo, hi)
102
+ function MTK. lowered_integral (model:: InfiniteOptModel , expr, lo, hi)
103
+ model. tₛ * InfiniteOpt.∫ (expr, model. model[:t ], lo, hi)
104
+ end
93
105
MTK. lowered_derivative (model:: InfiniteOptModel , i) = ∂ (model. U[i], model. model[:t ])
94
106
95
107
function MTK. process_integral_bounds (model:: InfiniteOptModel , integral_span, tspan)
@@ -125,7 +137,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
125
137
nᵥ = length (V)
126
138
if MTK. is_explicit (tableau)
127
139
K = Any[]
128
- for τ in tsteps[1 : end - 1 ]
140
+ for τ in tsteps[1 : ( end - 1 ) ]
129
141
for (i, h) in enumerate (c)
130
142
ΔU = sum ([A[i, j] * K[j] for j in 1 : (i - 1 )], init = zeros (nᵤ))
131
143
Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1 : nᵤ]
@@ -142,14 +154,15 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
142
154
K = @variable (model, K[1 : length (α), 1 : nᵤ], Infinite (model[:t ]))
143
155
ΔUs = A * K
144
156
ΔU_tot = dt * (K' * α)
145
- for τ in tsteps[1 : end - 1 ]
157
+ for τ in tsteps[1 : ( end - 1 ) ]
146
158
for (i, h) in enumerate (c)
147
159
ΔU = @view ΔUs[i, :]
148
160
Uₙ = U + ΔU * dt
149
161
@constraint (model, [j = 1 : nᵤ], K[i, j]== (tₛ * f (Uₙ, V, p, τ + h * dt)[j]),
150
162
DomainRestrictions (t => τ), base_name= " solve_K$i ($τ )" )
151
163
end
152
- @constraint (model, [n = 1 : nᵤ], U[n](τ) + ΔU_tot[n]== U[n](min (τ + dt, tsteps[end ])),
164
+ @constraint (model,
165
+ [n = 1 : nᵤ], U[n](τ) + ΔU_tot[n]== U[n](min (τ + dt, tsteps[end ])),
153
166
DomainRestrictions (t => τ), base_name= " solve_U($τ )" )
154
167
end
155
168
end
@@ -159,15 +172,21 @@ struct JuMPCollocation <: AbstractCollocation
159
172
solver:: Any
160
173
tableau:: DiffEqBase.ODERKTableau
161
174
end
162
- MTK. JuMPCollocation (solver, tableau = MTK. constructDefault ()) = JuMPCollocation (solver, tableau)
175
+ function MTK. JuMPCollocation (solver, tableau = MTK. constructDefault ())
176
+ JuMPCollocation (solver, tableau)
177
+ end
163
178
164
179
struct InfiniteOptCollocation <: AbstractCollocation
165
180
solver:: Any
166
181
derivative_method:: InfiniteOpt.AbstractDerivativeMethod
167
182
end
168
- MTK. InfiniteOptCollocation (solver, derivative_method = InfiniteOpt. FiniteDifference (InfiniteOpt. Backward ())) = InfiniteOptCollocation (solver, derivative_method)
183
+ function MTK. InfiniteOptCollocation (
184
+ solver, derivative_method = InfiniteOpt. FiniteDifference (InfiniteOpt. Backward ()))
185
+ InfiniteOptCollocation (solver, derivative_method)
186
+ end
169
187
170
- function MTK. prepare_and_optimize! (prob:: JuMPDynamicOptProblem , solver:: JuMPCollocation ; verbose = false , kwargs... )
188
+ function MTK. prepare_and_optimize! (
189
+ prob:: JuMPDynamicOptProblem , solver:: JuMPCollocation ; verbose = false , kwargs... )
171
190
model = prob. wrapped_model. model
172
191
verbose || set_silent (model)
173
192
# Unregister current solver constraints
@@ -190,7 +209,8 @@ function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPColl
190
209
model
191
210
end
192
211
193
- function MTK. prepare_and_optimize! (prob:: InfiniteOptDynamicOptProblem , solver:: InfiniteOptCollocation ; verbose = false , kwargs... )
212
+ function MTK. prepare_and_optimize! (prob:: InfiniteOptDynamicOptProblem ,
213
+ solver:: InfiniteOptCollocation ; verbose = false , kwargs... )
194
214
model = prob. wrapped_model. model
195
215
verbose || set_silent (model)
196
216
set_derivative_method (model[:t ], solver. derivative_method)
@@ -223,8 +243,8 @@ function MTK.successful_solve(model::InfiniteModel)
223
243
error (" Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE." )
224
244
225
245
pstatus === FEASIBLE_POINT &&
226
- (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL ||
227
- tstatus === ALMOST_LOCALLY_SOLVED)
246
+ (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL ||
247
+ tstatus === ALMOST_LOCALLY_SOLVED)
228
248
end
229
249
230
250
import InfiniteOpt: JuMP, GeneralVariableRef
0 commit comments