Skip to content

Commit

Permalink
MOI: load Hessian only if available
Browse files Browse the repository at this point in the history
  • Loading branch information
frapac committed Mar 10, 2021
1 parent 6406b88 commit 524d6ef
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Evaluators/Evaluators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ constraints, or `:mixed` if problem has both types of constraints.
"""
function constraints_type end

"Check if Hessian of objective is implemented."
has_hessian(nlp::AbstractNLPEvaluator) = false
"Check if Hessian of Lagrangian is implemented."
has_hessian_lagrangian(nlp::AbstractNLPEvaluator) = false

"""
reset!(nlp::AbstractNLPEvaluator)
Expand Down
3 changes: 2 additions & 1 deletion src/Evaluators/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ mutable struct MOIEvaluator{Evaluator<:AbstractNLPEvaluator} <: MOI.AbstractNLPE
hash_x::UInt
has_hess::Bool
end
MOIEvaluator(nlp) = MOIEvaluator(nlp, UInt64(0), true)
# MOI needs Hessian of Lagrangian function
MOIEvaluator(nlp) = MOIEvaluator(nlp, UInt64(0), has_hessian_lagrangian(nlp))

function _update!(ev::MOIEvaluator, x)
hx = hash(x)
Expand Down
2 changes: 2 additions & 0 deletions src/Evaluators/auglag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ function AugLagEvaluator(
return AugLagEvaluator(nlp, u0; options...)
end

has_hessian(nlp::AugLagEvaluator) = has_hessian(nlp.inner)

# Default fallback
function _update_internal!(ag::AugLagEvaluator, ::CONSTRAINTS_TYPE)
# Update (shifted) infeasibility error
Expand Down
3 changes: 3 additions & 0 deletions src/Evaluators/feasibility_evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ n_constraints(nlp::FeasibilityEvaluator) = 0

constraints_type(::FeasibilityEvaluator) = :bound

has_hessian(nlp::FeasibilityEvaluator) = has_hessian(nlp.inner)
has_hessian_lagrangian(nlp::FeasibilityEvaluator) = has_hessian(nlp)

# Getters
get(nlp::FeasibilityEvaluator, attr::AbstractNLPAttribute) = get(nlp.inner, attr)
get(nlp::FeasibilityEvaluator, attr::AbstractVariable) = get(nlp.inner, attr)
Expand Down
3 changes: 3 additions & 0 deletions src/Evaluators/penalty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ n_constraints(ag::AbstractPenaltyEvaluator) = 0

constraints_type(ag::AbstractPenaltyEvaluator) = :bound

# Penalty evaluators don't have constraints so Lagrangian is objective
has_hessian_lagrangian(nlp::AbstractPenaltyEvaluator) = has_hessian(nlp)

# Getters
get(ag::AbstractPenaltyEvaluator, attr::AbstractNLPAttribute) = get(ag.inner, attr)
get(ag::AbstractPenaltyEvaluator, attr::AbstractVariable) = get(ag.inner, attr)
Expand Down
1 change: 1 addition & 0 deletions src/Evaluators/reduced_evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ n_variables(nlp::ReducedSpaceEvaluator) = length(nlp.u_min)
n_constraints(nlp::ReducedSpaceEvaluator) = length(nlp.g_min)

constraints_type(::ReducedSpaceEvaluator) = :inequality
has_hessian(::ReducedSpaceEvaluator) = true

# Getters
get(nlp::ReducedSpaceEvaluator, ::Constraints) = nlp.constraints
Expand Down
1 change: 1 addition & 0 deletions src/Evaluators/slack_evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ n_variables(nlp::SlackEvaluator) = nlp.nv + nlp.ns
n_constraints(nlp::SlackEvaluator) = n_constraints(nlp.inner)

constraints_type(::SlackEvaluator) = :equality
has_hessian(nlp::SlackEvaluator) = has_hessian(nlp.inner)

# Getters
get(nlp::SlackEvaluator, attr::AbstractNLPAttribute) = get(nlp.inner, attr)
Expand Down
3 changes: 3 additions & 0 deletions test/Evaluators/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
@test isa(buffer, ExaPF.AbstractBuffer)
@test ExaPF.constraints_type(nlp) in [:bound, :equality, :inequality]

@test isa(ExaPF.has_hessian(nlp), Bool)
@test isa(ExaPF.has_hessian_lagrangian(nlp), Bool)

# setters
nbus = get(nlp, PS.NumberOfBuses())
loads = similar(u, nbus) ; fill!(loads, 1)
Expand Down

0 comments on commit 524d6ef

Please sign in to comment.