76
76
77
77
function MTK. generate_state_variable! (m:: ConcreteModel , u0, ns, ts)
78
78
m. u_idxs = pyomo. RangeSet (1 , ns)
79
- m. U = pyomo. Var (m. u_idxs, m. t, initialize = 0 )
79
+ init_f = Pyomo. pyfunc ((m, i, t) -> (u0[Pyomo. pyconvert (Int, i)]))
80
+ m. U = pyomo. Var (m. u_idxs, m. t, initialize = init_f)
80
81
PyomoVar (m. U)
81
82
end
82
83
83
84
function MTK. generate_input_variable! (m:: ConcreteModel , c0, nc, ts)
84
85
m. v_idxs = pyomo. RangeSet (1 , nc)
85
- m. V = pyomo. Var (m. v_idxs, m. t, initialize = 0 )
86
+ init_f = Pyomo. pyfunc ((m, i, t) -> (c0[Pyomo. pyconvert (Int, i)]))
87
+ m. V = pyomo. Var (m. v_idxs, m. t, initialize = init_f)
86
88
PyomoVar (m. V)
87
89
end
88
90
89
91
function MTK. generate_timescale! (m:: ConcreteModel , guess, is_free_t)
90
- m. tₛ = is_free_t ? PyomoVar (pyomo. Var (initialize = guess, bounds = (0 , Inf ))) : PyomoVar (Pyomo. Py (1 ))
92
+ m. tₛ = is_free_t ? pyomo. Var (initialize = guess, bounds = (0 , Inf )) : Pyomo. Py (1 )
93
+ PyomoVar (m. tₛ)
91
94
end
92
95
93
96
function MTK. add_constraint! (pmodel:: PyomoDynamicOptModel , cons; n_idxs = 1 )
@@ -100,7 +103,6 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1)
100
103
cons. lhs - cons. rhs ≤ 0
101
104
end
102
105
expr = Symbolics. substitute (expr, SPECIAL_FUNCTIONS_DICT)
103
- @show expr
104
106
105
107
cons_sym = Symbol (" cons" , hash (cons))
106
108
if occursin (Symbolics. unwrap (t_sym), expr)
133
135
134
136
function MTK. lowered_integral (m:: PyomoDynamicOptModel , arg, lo, hi)
135
137
@unpack model, model_sym, t_sym = m
136
- @show arg
137
- @show Symbolics. build_function (arg, model_sym, t_sym)
138
138
arg_f = eval (Symbolics. build_function (arg, model_sym, t_sym))
139
- @show arg_f
140
139
int_sym = Symbol (:int , hash (arg))
141
- @show int_sym
142
140
setproperty! (model, int_sym, dae. Integral (model. t, wrt = model. t, rule= Pyomo. pyfunc (arg_f)))
143
141
PyomoVar (model. tₛ * model. int_sym)
144
142
end
@@ -168,21 +166,36 @@ function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; ve
168
166
dm = collocation. derivative_method
169
167
discretizer = TransformationFactory (dm)
170
168
ncp = Pyomo. is_finite_difference (dm) ? 1 : dm. np
171
- discretizer. apply_to (solver_m, wrt = solver_m. t, nfe = solver_m. steps, scheme = Pyomo. scheme_string (dm))
169
+ discretizer. apply_to (solver_m, wrt = solver_m. t, nfe = solver_m. steps - 1 , scheme = Pyomo. scheme_string (dm))
172
170
solver = SolverFactory (string (collocation. solver))
173
171
results = solver. solve (solver_m, tee = true )
174
- ConcreteModel (solver_m)
172
+ PyomoOutput (results, solver_m)
173
+ end
174
+
175
+ struct PyomoOutput
176
+ result:: Pyomo.Py
177
+ model:: Pyomo.Py
175
178
end
176
179
177
- MTK. get_U_values (m:: ConcreteModel ) = [[Pyomo. pyconvert (Float64, pyomo. value (m. U[i, t])) for i in m. u_idxs] for t in m. t]
178
- MTK. get_V_values (m:: ConcreteModel ) = [[Pyomo. pyconvert (Float64, pyomo. value (m. V[i, t])) for i in m. v_idxs] for t in m. t]
179
- MTK. get_t_values (m:: ConcreteModel ) = [Pyomo. pyconvert (Float64, t) for t in m. t]
180
+ function MTK. get_U_values (output:: PyomoOutput )
181
+ m = output. model
182
+ [[Pyomo. pyconvert (Float64, pyomo. value (m. U[i, t])) for i in m. u_idxs] for t in m. t]
183
+ end
184
+ function MTK. get_V_values (output:: PyomoOutput )
185
+ m = output. model
186
+ [[Pyomo. pyconvert (Float64, pyomo. value (m. V[i, t])) for i in m. v_idxs] for t in m. t]
187
+ end
188
+ function MTK. get_t_values (output:: PyomoOutput )
189
+ m = output. model
190
+ Pyomo. pyconvert (Float64, pyomo. value (m. tₛ)) * [Pyomo. pyconvert (Float64, t) for t in m. t]
191
+ end
180
192
181
- function MTK. successful_solve (m:: ConcreteModel )
182
- return true
183
- ss = m. solver. status
184
- tc = m. solver. termination_condition
185
- if ss == opt. SolverStatus. ok && (tc == opt. TerminationStatus. optimal || tc == opt. TerminationStatus. locallyOptimal)
193
+ function MTK. successful_solve (output:: PyomoOutput )
194
+ r = output. result
195
+ ss = r. solver. status
196
+ Main. xx[] = ss
197
+ tc = r. solver. termination_condition
198
+ if Bool (ss == opt. SolverStatus. ok) && (Bool (tc == opt. TerminationCondition. optimal) || Bool (tc == opt. TerminationCondition. locallyOptimal))
186
199
return true
187
200
else
188
201
return false
0 commit comments