@@ -33,6 +33,8 @@ function Optimization.instantiate_function(f::OptimizationFunction{true}, x,
33
33
grad = (G, θ, args... ) -> f. grad (G, θ, p, args... )
34
34
end
35
35
36
+ hess_sparsity = f. hess_prototype
37
+ hess_colors = f. hess_colorvec
36
38
if f. hess === nothing
37
39
hess_sparsity = Symbolics. hessian_sparsity (_f, x)
38
40
hess_colors = matrix_colors (tril (hess_sparsity))
@@ -62,6 +64,8 @@ function Optimization.instantiate_function(f::OptimizationFunction{true}, x,
62
64
cons_oop = (x) -> (_res = zeros (eltype (x), num_cons); cons (_res, x); _res)
63
65
end
64
66
67
+ cons_jac_prototype = f. cons_jac_prototype
68
+ cons_jac_colorvec = f. cons_jac_colorvec
65
69
if cons != = nothing && f. cons_j === nothing
66
70
cons_jac_prototype = Symbolics. jacobian_sparsity (cons, zeros (eltype (x), num_cons),
67
71
x)
@@ -79,6 +83,7 @@ function Optimization.instantiate_function(f::OptimizationFunction{true}, x,
79
83
cons_j = (J, θ) -> f. cons_j (J, θ, p)
80
84
end
81
85
86
+ cons_hess_caches = [(; sparsity = f. cons_hess_prototype, colors = f. cons_hess_colorvec)]
82
87
if cons != = nothing && f. cons_h === nothing
83
88
function gen_conshess_cache (_f, x)
84
89
conshess_sparsity = copy (Symbolics. hessian_sparsity (_f, x))
@@ -91,9 +96,10 @@ function Optimization.instantiate_function(f::OptimizationFunction{true}, x,
91
96
fcons = [(x) -> (_res = zeros (eltype (x), num_cons);
92
97
cons (_res, x);
93
98
_res[i]) for i in 1 : num_cons]
99
+ cons_hess_caches = gen_conshess_cache .(fcons, Ref (x))
94
100
cons_h = function (res, θ)
95
101
for i in 1 : num_cons
96
- numauto_color_hessian! (res[i], fcons[i], θ, gen_conshess_cache (fcons [i], θ) )
102
+ numauto_color_hessian! (res[i], fcons[i], θ, cons_hess_caches [i])
97
103
end
98
104
end
99
105
else
@@ -107,9 +113,12 @@ function Optimization.instantiate_function(f::OptimizationFunction{true}, x,
107
113
end
108
114
return OptimizationFunction {true} (f. f, adtype; grad = grad, hess = hess, hv = hv,
109
115
cons = cons, cons_j = cons_j, cons_h = cons_h,
110
- hess_prototype = f. hess_prototype,
111
- cons_jac_prototype = f. cons_jac_prototype,
112
- cons_hess_prototype = f. cons_hess_prototype,
116
+ hess_prototype = hess_sparsity,
117
+ hess_colorvec = hess_colors,
118
+ cons_jac_colorvec = cons_jac_colorvec,
119
+ cons_jac_prototype = cons_jac_prototype,
120
+ cons_hess_prototype = getfield .(cons_hess_caches, :sparsity ),
121
+ cons_hess_colorvec = getfield .(cons_hess_caches, :colors ),
113
122
lag_h, f. lag_hess_prototype)
114
123
end
115
124
@@ -132,6 +141,8 @@ function Optimization.instantiate_function(f::OptimizationFunction{true},
132
141
grad = (G, θ, args... ) -> f. grad (G, θ, cache. p, args... )
133
142
end
134
143
144
+ hess_sparsity = f. hess_prototype
145
+ hess_colors = f. hess_colorvec
135
146
if f. hess === nothing
136
147
hess_sparsity = Symbolics. hessian_sparsity (_f, cache. u0)
137
148
hess_colors = matrix_colors (tril (hess_sparsity))
@@ -161,6 +172,8 @@ function Optimization.instantiate_function(f::OptimizationFunction{true},
161
172
cons_oop = (x) -> (_res = zeros (eltype (x), num_cons); cons (_res, x); _res)
162
173
end
163
174
175
+ cons_jac_prototype = f. cons_jac_prototype
176
+ cons_jac_colorvec = f. cons_jac_colorvec
164
177
if cons != = nothing && f. cons_j === nothing
165
178
cons_jac_prototype = Symbolics. jacobian_sparsity (cons,
166
179
zeros (eltype (cache. u0), num_cons),
@@ -177,6 +190,7 @@ function Optimization.instantiate_function(f::OptimizationFunction{true},
177
190
cons_j = (J, θ) -> f. cons_j (J, θ, cache. p)
178
191
end
179
192
193
+ cons_hess_caches = [(; sparsity = f. cons_hess_prototype, colors = f. cons_hess_colorvec)]
180
194
if cons != = nothing && f. cons_h === nothing
181
195
function gen_conshess_cache (_f, x)
182
196
conshess_sparsity = copy (Symbolics. hessian_sparsity (_f, x))
@@ -208,8 +222,11 @@ function Optimization.instantiate_function(f::OptimizationFunction{true},
208
222
return OptimizationFunction {true} (f. f, adtype; grad = grad, hess = hess, hv = hv,
209
223
cons = cons, cons_j = cons_j, cons_h = cons_h,
210
224
hess_prototype = hess_sparsity,
225
+ hess_colorvec = hess_colors,
211
226
cons_jac_prototype = cons_jac_prototype,
227
+ cons_jac_colorvec = cons_jac_colorvec,
212
228
cons_hess_prototype = getfield .(cons_hess_caches, :sparsity ),
229
+ cons_hess_colorvec = getfield .(cons_hess_caches, :colors ),
213
230
lag_h, f. lag_hess_prototype)
214
231
end
215
232
@@ -230,7 +247,9 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseFiniteDiff, p
230
247
else
231
248
grad = (G, θ, args... ) -> f. grad (G, θ, p, args... )
232
249
end
233
-
250
+
251
+ hess_sparsity = f. hess_prototype
252
+ hess_colors = f. hess_colorvec
234
253
if f. hess === nothing
235
254
hess_sparsity = Symbolics. hessian_sparsity (_f, x)
236
255
hess_colors = matrix_colors (tril (hess_sparsity))
@@ -259,6 +278,8 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseFiniteDiff, p
259
278
cons = (res, θ) -> f. cons (res, θ, p)
260
279
end
261
280
281
+ cons_jac_prototype = f. cons_jac_prototype
282
+ cons_jac_colorvec = f. cons_jac_colorvec
262
283
if cons != = nothing && f. cons_j === nothing
263
284
cons_jac_prototype = f. cons_jac_prototype === nothing ?
264
285
Symbolics. jacobian_sparsity (cons,
@@ -279,6 +300,7 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseFiniteDiff, p
279
300
cons_j = (J, θ) -> f. cons_j (J, θ, p)
280
301
end
281
302
303
+ conshess_caches = [(; sparsity = f. cons_hess_prototype, colors = f. cons_hess_colorvec)]
282
304
if cons != = nothing && f. cons_h === nothing
283
305
function gen_conshess_cache (_f, x)
284
306
conshess_sparsity = Symbolics. hessian_sparsity (_f, x)
@@ -290,44 +312,48 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseFiniteDiff, p
290
312
fcons = [(x) -> (_res = zeros (eltype (x), num_cons);
291
313
cons (_res, x);
292
314
_res[i]) for i in 1 : num_cons]
293
-
315
+ conshess_caches = gen_conshess_cache .(fcons, Ref (x))
294
316
cons_h = function (res, θ)
295
317
for i in 1 : num_cons
296
- numauto_color_hessian! (res[i], fcons[i], θ, gen_conshess_cache (fcons [i], θ) )
318
+ numauto_color_hessian! (res[i], fcons[i], θ, conshess_caches [i])
297
319
end
298
320
end
299
321
else
300
322
cons_h = (res, θ) -> f. cons_h (res, θ, p)
301
323
end
302
324
303
325
if f. lag_h === nothing
304
- lag_hess_cache = FD. HessianCache (copy (x))
305
- c = zeros (num_cons)
306
- h = zeros (length (x), length (x))
307
- lag_h = let c = c, h = h
308
- lag = function (θ, σ, μ)
309
- f. cons (c, θ, p)
310
- l = μ' c
311
- if ! iszero (σ)
312
- l += σ * f. f (θ, p)
313
- end
314
- l
315
- end
316
- function (res, θ, σ, μ)
317
- FD. finite_difference_hessian! (res,
318
- (x) -> lag (x, σ, μ),
319
- θ,
320
- updatecache (lag_hess_cache, θ))
321
- end
322
- end
326
+ # lag_hess_cache = FD.HessianCache(copy(x))
327
+ # c = zeros(num_cons)
328
+ # h = zeros(length(x), length(x))
329
+ # lag_h = let c = c, h = h
330
+ # lag = function (θ, σ, μ)
331
+ # f.cons(c, θ, p)
332
+ # l = μ'c
333
+ # if !iszero(σ)
334
+ # l += σ * f.f(θ, p)
335
+ # end
336
+ # l
337
+ # end
338
+ # function (res, θ, σ, μ)
339
+ # FD.finite_difference_hessian!(res,
340
+ # (x) -> lag(x, σ, μ),
341
+ # θ,
342
+ # updatecache(lag_hess_cache, θ))
343
+ # end
344
+ # end
345
+ lag_h = nothing
323
346
else
324
347
lag_h = (res, θ, σ, μ) -> f. lag_h (res, θ, σ, μ, p)
325
348
end
326
349
return OptimizationFunction {true} (f, adtype; grad = grad, hess = hess, hv = hv,
327
350
cons = cons, cons_j = cons_j, cons_h = cons_h,
328
- hess_prototype = f. hess_prototype,
329
- cons_jac_prototype = f. cons_jac_prototype,
330
- cons_hess_prototype = f. cons_hess_prototype,
351
+ hess_prototype = hess_sparsity,
352
+ hess_colorvec = hess_colors,
353
+ cons_jac_prototype = cons_jac_prototype,
354
+ cons_jac_colorvec = cons_jac_colorvec,
355
+ cons_hess_prototype = getfield .(conshess_caches, :sparsity ),
356
+ cons_hess_colorvec = getfield .(conshess_caches, :colors ),
331
357
lag_h, f. lag_hess_prototype)
332
358
end
333
359
@@ -337,7 +363,6 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
337
363
error (" $(string (adtype)) with SparseDiffTools does not support functions with more than 2 arguments" )
338
364
end
339
365
_f = (θ, args... ) -> first (f. f (θ, cache. p, args... ))
340
- updatecache = (cache, x) -> (cache. xmm .= x; cache. xmp .= x; cache. xpm .= x; cache. xpp .= x; return cache)
341
366
342
367
if f. grad === nothing
343
368
gradcache = FD. GradientCache (cache. u0, cache. u0)
@@ -346,7 +371,9 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
346
371
else
347
372
grad = (G, θ, args... ) -> f. grad (G, θ, cache. p, args... )
348
373
end
349
-
374
+
375
+ hess_sparsity = f. hess_prototype
376
+ hess_colors = f. hess_colorvec
350
377
if f. hess === nothing
351
378
hess_sparsity = Symbolics. hessian_sparsity (_f, cache. u0)
352
379
hess_colors = matrix_colors (tril (hess_sparsity))
@@ -375,6 +402,8 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
375
402
cons = (res, θ) -> f. cons (res, θ, cache. p)
376
403
end
377
404
405
+ cons_jac_prototype = f. cons_jac_prototype
406
+ cons_jac_colorvec = f. cons_jac_colorvec
378
407
if cons != = nothing && f. cons_j === nothing
379
408
cons_jac_prototype = f. cons_jac_prototype === nothing ?
380
409
Symbolics. jacobian_sparsity (cons, zeros (eltype (cache. u0), num_cons),
@@ -394,6 +423,7 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
394
423
cons_j = (J, θ) -> f. cons_j (J, θ, cache. p)
395
424
end
396
425
426
+ conshess_caches = [(; sparsity = f. cons_hess_prototype, colors = f. cons_hess_colorvec)]
397
427
if cons != = nothing && f. cons_h === nothing
398
428
function gen_conshess_cache (_f, x)
399
429
conshess_sparsity = copy (Symbolics. hessian_sparsity (_f, x))
@@ -406,10 +436,10 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
406
436
fcons = [(x) -> (_res = zeros (eltype (x), num_cons);
407
437
cons (_res, x);
408
438
_res[i]) for i in 1 : num_cons]
409
- hesscaches = [gen_conshess_cache (fcons[i], cache. u0) for i in 1 : num_cons]
439
+ conshess_caches = [gen_conshess_cache (fcons[i], cache. u0) for i in 1 : num_cons]
410
440
cons_h = function (res, θ)
411
441
for i in 1 : num_cons
412
- numauto_color_hessian! (res[i], fcons[i], θ, hesscaches [i])
442
+ numauto_color_hessian! (res[i], fcons[i], θ, conshess_caches [i])
413
443
end
414
444
end
415
445
else
@@ -447,8 +477,11 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
447
477
return OptimizationFunction {true} (f, adtype; grad = grad, hess = hess, hv = hv,
448
478
cons = cons, cons_j = cons_j, cons_h = cons_h,
449
479
hess_prototype = hess_sparsity,
480
+ hess_colorvec = hess_colors,
450
481
cons_jac_prototype = cons_jac_prototype,
451
- cons_hess_prototype = getfield .(hesscaches, :sparsity ),
482
+ cons_jac_colorvec = cons_jac_colorvec,
483
+ cons_hess_prototype = getfield .(conshess_caches, :sparsity ),
484
+ cons_hess_colorvec = getfield .(conshess_caches, :colors ),
452
485
lag_h, f. lag_hess_prototype)
453
486
end
454
487
@@ -464,6 +497,8 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseReverseDiff,
464
497
grad = (G, θ, args... ) -> f. grad (G, θ, p, args... )
465
498
end
466
499
500
+ hess_sparsity = f. hess_prototype
501
+ hess_colors = f. hess_colorvec
467
502
if f. hess === nothing
468
503
hess_sparsity = Symbolics. hessian_sparsity (_f, x)
469
504
hess_colors = SparseDiffTools. matrix_colors (tril (hess_sparsity))
@@ -494,6 +529,8 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseReverseDiff,
494
529
cons_oop = (x) -> (_res = zeros (eltype (x), num_cons); cons (_res, x); _res)
495
530
end
496
531
532
+ cons_jac_prototype = f. cons_jac_prototype
533
+ cons_jac_colorvec = f. cons_jac_colorvec
497
534
if cons != = nothing && f. cons_j === nothing
498
535
cons_jac_prototype = Symbolics. jacobian_sparsity (cons,
499
536
zeros (eltype (x), num_cons),
@@ -509,12 +546,16 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseReverseDiff,
509
546
else
510
547
cons_j = (J, θ) -> f. cons_j (J, θ, p)
511
548
end
512
-
549
+
550
+ conshess_sparsity = f. cons_hess_prototype
551
+ conshess_colors = f. cons_hess_colorvec
513
552
if cons != = nothing && f. cons_h === nothing
514
-
553
+ fncs = [(x) -> cons_oop (x)[i] for i in 1 : num_cons]
554
+ conshess_sparsity = Symbolics. hessian_sparsity .(fncs, Ref (x))
555
+ conshess_colors = SparseDiffTools. matrix_colors .(conshess_sparsity)
515
556
cons_h = function (res, θ)
516
557
for i in 1 : num_cons
517
- res[i] .= SparseDiffTools. forwarddiff_color_jacobian (θ, ) do θ
558
+ res[i] .= SparseDiffTools. forwarddiff_color_jacobian (θ, colorvec = conshess_colors[i], sparsity = conshess_sparsity[i] ) do θ
518
559
ReverseDiff. gradient (fncs[i], θ)
519
560
end
520
561
end
@@ -530,9 +571,12 @@ function Optimization.instantiate_function(f, x, adtype::AutoSparseReverseDiff,
530
571
end
531
572
return OptimizationFunction {true} (f. f, adtype; grad = grad, hess = hess, hv = hv,
532
573
cons = cons, cons_j = cons_j, cons_h = cons_h,
533
- hess_prototype = f. hess_prototype,
534
- cons_jac_prototype = f. cons_jac_prototype,
535
- cons_hess_prototype = f. cons_hess_prototype,
574
+ hess_prototype = hess_sparsity,
575
+ hess_colorvec = hess_colors,
576
+ cons_jac_prototype = cons_jac_prototype,
577
+ cons_jac_colorvec = cons_jac_colorvec,
578
+ cons_hess_prototype = conshess_sparsity,
579
+ cons_hess_colorvec = conshess_colors,
536
580
lag_h, f. lag_hess_prototype)
537
581
end
538
582
@@ -546,6 +590,8 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
546
590
grad = (G, θ, args... ) -> f. grad (G, θ, cache. p, args... )
547
591
end
548
592
593
+ hess_sparsity = f. hess_prototype
594
+ hess_colors = f. hess_colorvec
549
595
if f. hess === nothing
550
596
hess_sparsity = Symbolics. hessian_sparsity (_f, cache. u0)
551
597
hess_colors = SparseDiffTools. matrix_colors (tril (hess_sparsity))
@@ -574,6 +620,8 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
574
620
cons_oop = (x) -> (_res = zeros (eltype (x), num_cons); cons (_res, x); _res)
575
621
end
576
622
623
+ cons_jac_prototype = f. cons_jac_prototype
624
+ cons_jac_colorvec = f. cons_jac_colorvec
577
625
if cons != = nothing && f. cons_j === nothing
578
626
cons_jac_prototype = Symbolics. jacobian_sparsity (cons,
579
627
zeros (eltype (cache. u0), num_cons),
@@ -589,7 +637,9 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
589
637
else
590
638
cons_j = (J, θ) -> f. cons_j (J, θ, cache. p)
591
639
end
592
-
640
+
641
+ conshess_sparsity = f. cons_hess_prototype
642
+ conshess_colors = f. cons_hess_colorvec
593
643
if cons != = nothing && f. cons_h === nothing
594
644
fncs = [(x) -> cons_oop (x)[i] for i in 1 : num_cons]
595
645
conshess_sparsity = Symbolics. hessian_sparsity .(fncs, Ref (cache. u0))
@@ -614,8 +664,11 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
614
664
return OptimizationFunction {true} (f. f, adtype; grad = grad, hess = hess, hv = hv,
615
665
cons = cons, cons_j = cons_j, cons_h = cons_h,
616
666
hess_prototype = hess_sparsity,
667
+ hess_colorvec = hess_colors,
617
668
cons_jac_prototype = cons_jac_prototype,
669
+ cons_jac_colorvec = cons_jac_colorvec,
618
670
cons_hess_prototype = conshess_sparsity,
671
+ cons_hess_colorvec = conshess_colors,
619
672
lag_h, f. lag_hess_prototype)
620
673
end
621
674
0 commit comments