Skip to content

Commit

Permalink
Correctly initialise disable_2ndorder (jump-dev#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin authored Jan 5, 2019
1 parent ae39128 commit 370b70a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,6 @@ end
function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
nldata::NLPData = d.m.nlp_data

# Check if we have any user-defined operators, in which case we need to
# disable hessians. The result of features_available depends on this.
has_nlobj = nldata.nlobj !== nothing
has_user_mv_operator = false
for nlexpr in nldata.nlexpr
has_user_mv_operator |= Derivatives.has_user_multivariate_operators(nlexpr.nd)
end
if has_nlobj
has_user_mv_operator |= Derivatives.has_user_multivariate_operators(nldata.nlobj.nd)
end
for nlconstr in nldata.nlconstr
has_user_mv_operator |= Derivatives.has_user_multivariate_operators(nlconstr.terms.nd)
end
d.disable_2ndorder = has_user_mv_operator

for feat in requested_features
if !(feat in MOI.features_available(d))
error("Unsupported feature $feat")
Expand Down Expand Up @@ -452,11 +437,34 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
nothing
end

function recompute_disable_2ndorder(evaluator::NLPEvaluator)
# Check if we have any user-defined operators, in which case we need to
# disable hessians. The result of features_available depends on this.
nldata::NLPData = evaluator.m.nlp_data
has_nlobj = nldata.nlobj !== nothing
has_user_mv_operator = false
for nlexpr in nldata.nlexpr
has_user_mv_operator |= Derivatives.
has_user_multivariate_operators(nlexpr.nd)
end
if has_nlobj
has_user_mv_operator |= Derivatives.
has_user_multivariate_operators(nldata.nlobj.nd)
end
for nlconstr in nldata.nlconstr
has_user_mv_operator |= Derivatives.
has_user_multivariate_operators(nlconstr.terms.nd)
end
evaluator.disable_2ndorder = has_user_mv_operator
return
end

function MOI.features_available(d::NLPEvaluator)
recompute_disable_2ndorder(d)
features = [:Grad, :Jac, :ExprGraph]
if !d.disable_2ndorder
push!(features,:Hess)
push!(features,:HessVec)
push!(features, :Hess)
push!(features, :HessVec)
end
return features
end
Expand Down
10 changes: 10 additions & 0 deletions test/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,14 @@
@test JuMP.value(ex2, JuMP.start_value) sin(2.0) + 4.0
@test JuMP.value(ex3, JuMP.start_value) 2.5 * sin(2.0) + 2.0
end

@testset "Hessians disabled with user-defined multivariate functions" begin
model = Model()
my_f(x, y) = (x - 1)^2 + (y - 2)^2
JuMP.register(model, :my_f, 2, my_f, autodiff = true)
@variable(model, x[1:2])
@NLobjective(model, Min, my_f(x[1], x[2]))
evaluator = JuMP.NLPEvaluator(model)
@test !(:Hess in MOI.features_available(evaluator))
end
end

0 comments on commit 370b70a

Please sign in to comment.