@@ -84,7 +84,7 @@ namespace_operation(sys::OptimizationSystem) = namespace_operation(sys.op,sys.na
84
84
hessian_sparsity (sys:: OptimizationSystem ) =
85
85
hessian_sparsity (sys. op,[dv () for dv in states (sys)])
86
86
87
- struct ManualModelingToolkit <: DiffEqBase.AbstractADType end
87
+ struct AutoModelingToolkit <: DiffEqBase.AbstractADType end
88
88
89
89
"""
90
90
```julia
@@ -134,7 +134,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0,
134
134
_hess = nothing
135
135
end
136
136
137
- _f = OptimizationFunction {iip,typeof(f),typeof(_grad),typeof(_hess),Nothing,Nothing,Nothing,Nothing} (f,_grad,_hess,nothing ,ManualModelingToolkit (),nothing ,nothing ,nothing ,0 )
137
+ _f = OptimizationFunction {iip,typeof(f),typeof(_grad),typeof(_hess),Nothing,Nothing,Nothing,Nothing} (f,_grad,_hess,nothing ,AutoModelingToolkit (),nothing ,nothing ,nothing ,0 )
138
138
139
139
p = varmap_to_vars (parammap,ps)
140
140
lb = varmap_to_vars (lb,dvs)
@@ -170,9 +170,23 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
170
170
kwargs... ) where iip
171
171
dvs = states (sys)
172
172
ps = parameters (sys)
173
-
173
+ idx = iip ? 2 : 1
174
174
f = generate_function (sys,checkbounds= checkbounds,linenumbers= linenumbers,
175
175
expression= Val{true })
176
+ if grad
177
+ _grad = generate_gradient (sys,checkbounds= checkbounds,linenumbers= linenumbers,
178
+ parallel= parallel,expression= Val{false })[idx]
179
+ else
180
+ _grad = :nothing
181
+ end
182
+
183
+ if hess
184
+ _hess = generate_hessian (sys,checkbounds= checkbounds,linenumbers= linenumbers,
185
+ sparse= sparse,parallel= parallel,expression= Val{false })[idx]
186
+ else
187
+ _hess = :nothing
188
+ end
189
+
176
190
u0 = varmap_to_vars (u0,dvs)
177
191
p = varmap_to_vars (parammap,ps)
178
192
lb = varmap_to_vars (lb,dvs)
@@ -181,8 +195,25 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
181
195
f = $ f
182
196
p = $ p
183
197
u0 = $ u0
198
+ grad = $ _grad
199
+ hess = $ _hess
184
200
lb = $ lb
185
201
ub = $ ub
186
- OptimizationProblem (f,u0,p;lb= lb,ub= ub,kwargs... )
202
+ _f = OptimizationFunction {$iip,typeof(f),typeof(grad),typeof(hess),Nothing,Nothing,Nothing,Nothing} (f,grad,hess,nothing ,AutoModelingToolkit (),nothing ,nothing ,nothing ,0 )
203
+ OptimizationProblem {$iip} (_f,u0,p;lb= lb,ub= ub,kwargs... )
204
+ end
205
+ end
206
+
207
+ function OptimizationFunction (f, x, :: AutoModelingToolkit ,p = DiffEqBase. NullParameters ();
208
+ grad= nothing , hess= nothing , cons = nothing , cons_j = nothing , cons_h = nothing ,
209
+ num_cons = 0 , chunksize = 1 , hv = nothing )
210
+
211
+ sys = modelingtoolkitize (OptimizationProblem (f,x,p))
212
+ u0map = states (sys) .=> x
213
+ if p == DiffEqBase. NullParameters ()
214
+ parammap = DiffEqBase. NullParameters ()
215
+ else
216
+ parammap = parameters (sys) .=> p
187
217
end
218
+ OptimiationProblem (sys,u0map,parammap,grad= grad,hess= hess)
188
219
end
0 commit comments