Skip to content

Commit dea3a9e

Browse files
committed
Update
1 parent bd18fe1 commit dea3a9e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Nonlinear/ReverseAD/utils.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ function Base.setindex!(x::_UnsafeVectorView{T}, value::T, i::Integer) where {T}
5353
return value
5454
end
5555

56-
function Base.setindex!(
57-
x::_UnsafeVectorView{T},
58-
value::T,
59-
i::CartesianIndex{1},
60-
) where {T}
56+
function Base.setindex!(x::_UnsafeVectorView{T}, value, i::Integer) where {T}
57+
return setindex!(x, convert(T, value), i)
58+
end
59+
60+
function Base.setindex!(x::_UnsafeVectorView, value, i::CartesianIndex{1})
6161
return setindex!(x, value, i[1])
6262
end
6363

test/Nonlinear/ReverseAD.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,38 @@ function test_eval_user_defined_operator_ForwardDiff_gradient!()
13311331
return
13321332
end
13331333

1334+
function test_eval_user_defined_operator_type_mismatch()
1335+
model = MOI.Nonlinear.Model()
1336+
x = MOI.VariableIndex.(1:4)
1337+
p = MOI.Nonlinear.add_parameter(model, 2.0)
1338+
ex = MOI.Nonlinear.add_expression(model, :($p * $(x[1])))
1339+
ψ(x) = sin(x)
1340+
t(x, y) = x + 3y
1341+
function ∇t(ret, x, y)
1342+
ret[1] = 1 # These are intentionall the wrong type
1343+
ret[2] = 3 // 1 # These are intentionall the wrong type
1344+
return
1345+
end
1346+
MOI.Nonlinear.register_operator(model, , 1, ψ, x -> -cos(x))
1347+
MOI.Nonlinear.register_operator(model, :t, 2, t, ∇t)
1348+
MOI.Nonlinear.add_constraint(
1349+
model,
1350+
:($ex^3 + sin($(x[2])) / ψ($(x[2])) + t($(x[3]), $(x[4]))),
1351+
MOI.LessThan(0.0),
1352+
)
1353+
d = MOI.Nonlinear.Evaluator(model, MOI.Nonlinear.SparseReverseMode(), x)
1354+
MOI.initialize(d, [:Jac])
1355+
X = [1.1, 1.2, 1.3, 1.4]
1356+
g = [NaN]
1357+
MOI.eval_constraint(d, g, X)
1358+
@test only(g) 17.148
1359+
@test MOI.jacobian_structure(d) == [(1, 1), (1, 2), (1, 3), (1, 4)]
1360+
J = [NaN, NaN, NaN, NaN]
1361+
MOI.eval_constraint_jacobian(d, J, X)
1362+
@test J [2.0^3 * 3.0 * 1.1^2, 0.0, 1.0, 3.0]
1363+
return
1364+
end
1365+
13341366
end # module
13351367

13361368
TestReverseAD.runtests()

0 commit comments

Comments
 (0)