@@ -48,20 +48,26 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <:
4848end
4949
5050MTK. 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
5460
5561function MTK. generate_timescale! (m:: InfiniteModel , guess, is_free_t)
5662 @variable (m, tₛ ≥ 0 , start = guess)
5763 if ! is_free_t
58- fix (tₛ, 1 , force= true )
64+ fix (tₛ, 1 , force = true )
5965 set_start_value (tₛ, 1 )
6066 end
6167 tₛ
6268end
6369
64- function MTK. add_constraint! (m:: InfiniteOptModel , expr:: Union{Equation, Inequality} )
70+ function MTK. add_constraint! (m:: InfiniteOptModel , expr:: Union{Equation, Inequality} )
6571 if expr isa Equation
6672 @constraint (m. model, expr. lhs - expr. rhs == 0 )
6773 elseif expr. relational_op === Symbolics. geq
@@ -76,20 +82,26 @@ function MTK.JuMPDynamicOptProblem(sys::System, op, tspan;
7682 dt = nothing ,
7783 steps = nothing ,
7884 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... )
8088 prob
8189end
8290
8391function MTK. InfiniteOptDynamicOptProblem (sys:: System , op, tspan;
8492 dt = nothing ,
8593 steps = nothing ,
8694 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... )
8898 MTK. add_equational_constraints! (prob. wrapped_model, sys, pmap, tspan)
8999 prob
90100end
91101
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
93105MTK. lowered_derivative (model:: InfiniteOptModel , i) = ∂ (model. U[i], model. model[:t ])
94106
95107function MTK. process_integral_bounds (model:: InfiniteOptModel , integral_span, tspan)
@@ -125,7 +137,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
125137 nᵥ = length (V)
126138 if MTK. is_explicit (tableau)
127139 K = Any[]
128- for τ in tsteps[1 : end - 1 ]
140+ for τ in tsteps[1 : ( end - 1 ) ]
129141 for (i, h) in enumerate (c)
130142 ΔU = sum ([A[i, j] * K[j] for j in 1 : (i - 1 )], init = zeros (nᵤ))
131143 Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1 : nᵤ]
@@ -142,14 +154,15 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
142154 K = @variable (model, K[1 : length (α), 1 : nᵤ], Infinite (model[:t ]))
143155 ΔUs = A * K
144156 ΔU_tot = dt * (K' * α)
145- for τ in tsteps[1 : end - 1 ]
157+ for τ in tsteps[1 : ( end - 1 ) ]
146158 for (i, h) in enumerate (c)
147159 ΔU = @view ΔUs[i, :]
148160 Uₙ = U + ΔU * dt
149161 @constraint (model, [j = 1 : nᵤ], K[i, j]== (tₛ * f (Uₙ, V, p, τ + h * dt)[j]),
150162 DomainRestrictions (t => τ), base_name= " solve_K$i ($τ )" )
151163 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 ])),
153166 DomainRestrictions (t => τ), base_name= " solve_U($τ )" )
154167 end
155168 end
@@ -159,15 +172,21 @@ struct JuMPCollocation <: AbstractCollocation
159172 solver:: Any
160173 tableau:: DiffEqBase.ODERKTableau
161174end
162- MTK. JuMPCollocation (solver, tableau = MTK. constructDefault ()) = JuMPCollocation (solver, tableau)
175+ function MTK. JuMPCollocation (solver, tableau = MTK. constructDefault ())
176+ JuMPCollocation (solver, tableau)
177+ end
163178
164179struct InfiniteOptCollocation <: AbstractCollocation
165180 solver:: Any
166181 derivative_method:: InfiniteOpt.AbstractDerivativeMethod
167182end
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
169187
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... )
171190 model = prob. wrapped_model. model
172191 verbose || set_silent (model)
173192 # Unregister current solver constraints
@@ -190,7 +209,8 @@ function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPColl
190209 model
191210end
192211
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... )
194214 model = prob. wrapped_model. model
195215 verbose || set_silent (model)
196216 set_derivative_method (model[:t ], solver. derivative_method)
@@ -223,8 +243,8 @@ function MTK.successful_solve(model::InfiniteModel)
223243 error (" Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE." )
224244
225245 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)
228248end
229249
230250import InfiniteOpt: JuMP, GeneralVariableRef
0 commit comments