|
1 | | -@testset "Basic Hessian derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64), |
2 | | - (backend, kw) in list_sparse_hess_backend |
| 1 | +function sparse_hessian(backend, kw) |
| 2 | + @testset "Basic Hessian derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64), |
| 3 | + c!(cx, x) = begin |
| 4 | + cx[1] = x[1] - 1 |
| 5 | + cx[2] = 10 * (x[2] - x[1]^2) |
| 6 | + cx[3] = x[2] + 1 |
| 7 | + cx |
| 8 | + end |
| 9 | + x0 = T[-1.2; 1.0] |
| 10 | + nvar = 2 |
| 11 | + ncon = 3 |
| 12 | + nlp = ADNLPModel!( |
| 13 | + x -> x[1] * x[2]^2 + x[1]^2 * x[2], |
| 14 | + x0, |
| 15 | + c!, |
| 16 | + zeros(T, ncon), |
| 17 | + zeros(T, ncon), |
| 18 | + hessian_backend = backend; |
| 19 | + kw..., |
| 20 | + ) |
3 | 21 |
|
4 | | - c!(cx, x) = begin |
5 | | - cx[1] = x[1] - 1 |
6 | | - cx[2] = 10 * (x[2] - x[1]^2) |
7 | | - cx[3] = x[2] + 1 |
8 | | - cx |
9 | | - end |
10 | | - x0 = T[-1.2; 1.0] |
11 | | - nvar = 2 |
12 | | - ncon = 3 |
13 | | - nlp = ADNLPModel!( |
14 | | - x -> x[1] * x[2]^2 + x[1]^2 * x[2], |
15 | | - x0, |
16 | | - c!, |
17 | | - zeros(T, ncon), |
18 | | - zeros(T, ncon), |
19 | | - hessian_backend = backend; |
20 | | - kw..., |
21 | | - ) |
22 | | - |
23 | | - x = rand(T, 2) |
24 | | - y = rand(T, 3) |
25 | | - rows, cols = zeros(Int, nlp.meta.nnzh), zeros(Int, nlp.meta.nnzh) |
26 | | - vals = zeros(T, nlp.meta.nnzh) |
27 | | - hess_structure!(nlp, rows, cols) |
28 | | - hess_coord!(nlp, x, vals) |
29 | | - @test eltype(vals) == T |
30 | | - H = sparse(rows, cols, vals, nvar, nvar) |
31 | | - @test H == [2*x[2] 0; 2*(x[1] + x[2]) 2*x[1]] |
| 22 | + x = rand(T, 2) |
| 23 | + y = rand(T, 3) |
| 24 | + rows, cols = zeros(Int, nlp.meta.nnzh), zeros(Int, nlp.meta.nnzh) |
| 25 | + vals = zeros(T, nlp.meta.nnzh) |
| 26 | + hess_structure!(nlp, rows, cols) |
| 27 | + hess_coord!(nlp, x, vals) |
| 28 | + @test eltype(vals) == T |
| 29 | + H = sparse(rows, cols, vals, nvar, nvar) |
| 30 | + @test H == [2*x[2] 0; 2*(x[1] + x[2]) 2*x[1]] |
32 | 31 |
|
33 | | - # Test also the implementation of the backends |
34 | | - b = nlp.adbackend.hessian_backend |
35 | | - obj_weight = 0.5 |
36 | | - @test nlp.meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar) |
37 | | - ADNLPModels.hess_structure!(b, nlp, rows, cols) |
38 | | - ADNLPModels.hess_coord!(b, nlp, x, obj_weight, vals) |
39 | | - @test eltype(vals) == T |
40 | | - H = sparse(rows, cols, vals, nvar, nvar) |
41 | | - @test H == [x[2] 0; x[1]+x[2] x[1]] |
42 | | - ADNLPModels.hess_coord!(b, nlp, x, y, obj_weight, vals) |
43 | | - @test eltype(vals) == T |
44 | | - H = sparse(rows, cols, vals, nvar, nvar) |
45 | | - @test H == [x[2] 0; x[1]+x[2] x[1]] + y[2] * [-20 0; 0 0] |
| 32 | + # Test also the implementation of the backends |
| 33 | + b = nlp.adbackend.hessian_backend |
| 34 | + obj_weight = 0.5 |
| 35 | + @test nlp.meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar) |
| 36 | + ADNLPModels.hess_structure!(b, nlp, rows, cols) |
| 37 | + ADNLPModels.hess_coord!(b, nlp, x, obj_weight, vals) |
| 38 | + @test eltype(vals) == T |
| 39 | + H = sparse(rows, cols, vals, nvar, nvar) |
| 40 | + @test H == [x[2] 0; x[1]+x[2] x[1]] |
| 41 | + ADNLPModels.hess_coord!(b, nlp, x, y, obj_weight, vals) |
| 42 | + @test eltype(vals) == T |
| 43 | + H = sparse(rows, cols, vals, nvar, nvar) |
| 44 | + @test H == [x[2] 0; x[1]+x[2] x[1]] + y[2] * [-20 0; 0 0] |
46 | 45 |
|
47 | | - if backend != ADNLPModels.ForwardDiffADHessian |
48 | | - H_sp = get_sparsity_pattern(nlp, :hessian) |
49 | | - @test H_sp == SparseMatrixCSC{Bool, Int}([ |
50 | | - 1 0 |
51 | | - 1 1 |
52 | | - ]) |
53 | | - end |
| 46 | + if backend != ADNLPModels.ForwardDiffADHessian |
| 47 | + H_sp = get_sparsity_pattern(nlp, :hessian) |
| 48 | + @test H_sp == SparseMatrixCSC{Bool, Int}([ |
| 49 | + 1 0 |
| 50 | + 1 1 |
| 51 | + ]) |
| 52 | + end |
54 | 53 |
|
55 | | - nlp = ADNLPModel!( |
56 | | - x -> x[1] * x[2]^2 + x[1]^2 * x[2], |
57 | | - x0, |
58 | | - c!, |
59 | | - zeros(T, ncon), |
60 | | - zeros(T, ncon), |
61 | | - matrix_free = true; |
62 | | - kw..., |
63 | | - ) |
64 | | - @test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend |
| 54 | + nlp = ADNLPModel!( |
| 55 | + x -> x[1] * x[2]^2 + x[1]^2 * x[2], |
| 56 | + x0, |
| 57 | + c!, |
| 58 | + zeros(T, ncon), |
| 59 | + zeros(T, ncon), |
| 60 | + matrix_free = true; |
| 61 | + kw..., |
| 62 | + ) |
| 63 | + @test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend |
65 | 64 |
|
66 | | - n = 4 |
67 | | - x = ones(T, 4) |
68 | | - nlp = ADNLPModel( |
69 | | - x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)), |
70 | | - x, |
71 | | - hessian_backend = backend, |
72 | | - name = "Extended Rosenbrock", |
73 | | - ) |
74 | | - @test hess(nlp, x) == T[802 -400 0 0; -400 1002 -400 0; 0 -400 1002 -400; 0 0 -400 200] |
| 65 | + n = 4 |
| 66 | + x = ones(T, 4) |
| 67 | + nlp = ADNLPModel( |
| 68 | + x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)), |
| 69 | + x, |
| 70 | + hessian_backend = backend, |
| 71 | + name = "Extended Rosenbrock", |
| 72 | + ) |
| 73 | + @test hess(nlp, x) == T[802 -400 0 0; -400 1002 -400 0; 0 -400 1002 -400; 0 0 -400 200] |
75 | 74 |
|
76 | | - x = ones(T, 2) |
77 | | - nlp = ADNLPModel(x -> x[1]^2 + x[1] * x[2], x, hessian_backend = backend) |
78 | | - @test hess(nlp, x) == T[2 1; 1 0] |
| 75 | + x = ones(T, 2) |
| 76 | + nlp = ADNLPModel(x -> x[1]^2 + x[1] * x[2], x, hessian_backend = backend) |
| 77 | + @test hess(nlp, x) == T[2 1; 1 0] |
79 | 78 |
|
80 | | - nlp = ADNLPModel( |
81 | | - x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)), |
82 | | - x, |
83 | | - name = "Extended Rosenbrock", |
84 | | - matrix_free = true, |
85 | | - ) |
86 | | - @test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend |
| 79 | + nlp = ADNLPModel( |
| 80 | + x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)), |
| 81 | + x, |
| 82 | + name = "Extended Rosenbrock", |
| 83 | + matrix_free = true, |
| 84 | + ) |
| 85 | + @test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend |
| 86 | + end |
87 | 87 | end |
0 commit comments