63
63
64
64
function generate_hessian (sys:: OptimizationSystem , vs = states (sys), ps = parameters (sys);
65
65
sparse = false , kwargs... )
66
- hes = calculate_hessian (sys)
67
66
if sparse
68
- hes = sparse (hes)
67
+ hess = sparsehessian (equations (sys),[dv () for dv in states (sys)])
68
+ else
69
+ hess = calculate_hessian (sys)
69
70
end
70
- return build_function (hes , convert .(Variable,vs), convert .(Variable,ps);
71
+ return build_function (hess , convert .(Variable,vs), convert .(Variable,ps);
71
72
conv = AbstractSysToExpr (sys),kwargs... )
72
73
end
73
74
@@ -84,12 +85,18 @@ namespace_operation(sys::OptimizationSystem) = namespace_operation(sys.op,sys.na
84
85
hessian_sparsity (sys:: OptimizationSystem ) =
85
86
hessian_sparsity (sys. op,[dv () for dv in states (sys)])
86
87
88
+ struct AutoModelingToolkit <: DiffEqBase.AbstractADType end
89
+
90
+ DiffEqBase. OptimizationProblem (sys:: OptimizationSystem ,args... ;kwargs... ) =
91
+ DiffEqBase. OptimizationProblem {true} (sys:: OptimizationSystem ,args... ;kwargs... )
92
+
87
93
"""
88
94
```julia
89
95
function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem,
90
96
parammap=DiffEqBase.NullParameters();
91
97
u0=nothing, lb=nothing, ub=nothing,
92
- hes = false, sparse = false,
98
+ grad = false,
99
+ hess = false, sparse = false,
93
100
checkbounds = false,
94
101
linenumbers = true, parallel=SerialForm(),
95
102
kwargs...) where iip
@@ -98,30 +105,53 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem,
98
105
Generates an OptimizationProblem from an OptimizationSystem and allows for automatically
99
106
symbolically calculating numerical enhancements.
100
107
"""
101
- function DiffEqBase. OptimizationProblem {iip} (sys:: OptimizationSystem ,
108
+ function DiffEqBase. OptimizationProblem {iip} (sys:: OptimizationSystem , u0,
102
109
parammap= DiffEqBase. NullParameters ();
103
- u0= nothing , lb= nothing , ub= nothing ,
104
- hes = false , sparse = false ,
110
+ lb= nothing , ub= nothing ,
111
+ grad = false ,
112
+ hess = false , sparse = false ,
105
113
checkbounds = false ,
106
114
linenumbers = true , parallel= SerialForm (),
107
115
kwargs... ) where iip
108
116
dvs = states (sys)
109
117
ps = parameters (sys)
110
118
111
119
f = generate_function (sys,checkbounds= checkbounds,linenumbers= linenumbers,
112
- parallel = parallel, expression= Val{false })
120
+ expression= Val{false })
113
121
u0 = varmap_to_vars (u0,dvs)
122
+
123
+ if grad
124
+ grad_oop,grad_iip = generate_gradient (sys,checkbounds= checkbounds,linenumbers= linenumbers,
125
+ parallel= parallel,expression= Val{false })
126
+ _grad (u,p) = grad_oop (u,p)
127
+ _grad (J,u,p) = (grad_iip (J,u,p); J)
128
+ else
129
+ _grad = nothing
130
+ end
131
+
132
+ if hess
133
+ hess_oop,hess_iip = generate_hessian (sys,checkbounds= checkbounds,linenumbers= linenumbers,
134
+ sparse= sparse,parallel= parallel,expression= Val{false })
135
+ _hess (u,p) = hess_oop (u,p)
136
+ _hess (J,u,p) = (hess_iip (J,u,p); J)
137
+ else
138
+ _hess = nothing
139
+ end
140
+
141
+ _f = DiffEqBase. OptimizationFunction {iip,AutoModelingToolkit,typeof(f),typeof(_grad),typeof(_hess),Nothing,Nothing,Nothing,Nothing} (f,AutoModelingToolkit (),_grad,_hess,nothing ,nothing ,nothing ,nothing ,0 )
142
+
114
143
p = varmap_to_vars (parammap,ps)
115
144
lb = varmap_to_vars (lb,dvs)
116
145
ub = varmap_to_vars (ub,dvs)
117
- OptimizationProblem (f,p;u0 = u0, lb= lb,ub= ub,kwargs... )
146
+ OptimizationProblem {iip} (_f,u0,p; lb= lb,ub= ub,kwargs... )
118
147
end
119
148
120
149
"""
121
150
```julia
122
151
function DiffEqBase.OptimizationProblemExpr{iip}(sys::OptimizationSystem,
123
152
parammap=DiffEqBase.NullParameters();
124
153
u0=nothing, lb=nothing, ub=nothing,
154
+ grad = false,
125
155
hes = false, sparse = false,
126
156
checkbounds = false,
127
157
linenumbers = true, parallel=SerialForm(),
@@ -134,18 +164,36 @@ calculating numerical enhancements.
134
164
"""
135
165
struct OptimizationProblemExpr{iip} end
136
166
137
- function OptimizationProblemExpr {iip} (sys:: OptimizationSystem ,
167
+ OptimizationProblemExpr (sys:: OptimizationSystem ,args... ;kwargs... ) =
168
+ OptimizationProblemExpr {true} (sys:: OptimizationSystem ,args... ;kwargs... )
169
+
170
+ function OptimizationProblemExpr {iip} (sys:: OptimizationSystem , u0,
138
171
parammap= DiffEqBase. NullParameters ();
139
- u0= nothing , lb= nothing , ub= nothing ,
140
- hes = false , sparse = false ,
172
+ lb= nothing , ub= nothing ,
173
+ grad = false ,
174
+ hess = false , sparse = false ,
141
175
checkbounds = false ,
142
176
linenumbers = false , parallel= SerialForm (),
143
177
kwargs... ) where iip
144
178
dvs = states (sys)
145
179
ps = parameters (sys)
146
-
180
+ idx = iip ? 2 : 1
147
181
f = generate_function (sys,checkbounds= checkbounds,linenumbers= linenumbers,
148
- parallel= parallel,expression= Val{true })
182
+ expression= Val{true })
183
+ if grad
184
+ _grad = generate_gradient (sys,checkbounds= checkbounds,linenumbers= linenumbers,
185
+ parallel= parallel,expression= Val{false })[idx]
186
+ else
187
+ _grad = :nothing
188
+ end
189
+
190
+ if hess
191
+ _hess = generate_hessian (sys,checkbounds= checkbounds,linenumbers= linenumbers,
192
+ sparse= sparse,parallel= parallel,expression= Val{false })[idx]
193
+ else
194
+ _hess = :nothing
195
+ end
196
+
149
197
u0 = varmap_to_vars (u0,dvs)
150
198
p = varmap_to_vars (parammap,ps)
151
199
lb = varmap_to_vars (lb,dvs)
@@ -154,8 +202,25 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem,
154
202
f = $ f
155
203
p = $ p
156
204
u0 = $ u0
205
+ grad = $ _grad
206
+ hess = $ _hess
157
207
lb = $ lb
158
208
ub = $ ub
159
- OptimizationProblem (f,p;u0= u0,lb= lb,ub= ub,kwargs... )
209
+ _f = OptimizationFunction {$iip,typeof(f),typeof(grad),typeof(hess),Nothing,Nothing,Nothing,Nothing} (f,grad,hess,nothing ,AutoModelingToolkit (),nothing ,nothing ,nothing ,0 )
210
+ OptimizationProblem {$iip} (_f,u0,p;lb= lb,ub= ub,kwargs... )
211
+ end
212
+ end
213
+
214
+ function OptimizationFunction (f, x, :: AutoModelingToolkit ,p = DiffEqBase. NullParameters ();
215
+ grad= false , hess= false , cons = nothing , cons_j = nothing , cons_h = nothing ,
216
+ num_cons = 0 , chunksize = 1 , hv = nothing )
217
+
218
+ sys = modelingtoolkitize (OptimizationProblem (f,x,p))
219
+ u0map = states (sys) .=> x
220
+ if p == DiffEqBase. NullParameters ()
221
+ parammap = DiffEqBase. NullParameters ()
222
+ else
223
+ parammap = parameters (sys) .=> p
160
224
end
225
+ OptimizationProblem (sys,u0map,parammap,grad= grad,hess= hess). f
161
226
end
0 commit comments