Skip to content

Commit

Permalink
fix issue with dual
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Jul 9, 2024
1 parent f3127d0 commit a7e5867
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
GraphOptInterface = "2c3d953e-07eb-42f7-9900-e75950e41d88"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
Expand Down
8 changes: 8 additions & 0 deletions src/core_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ const OptiElement = Union{OptiNode{<:AbstractOptiGraph},OptiEdge{<:AbstractOptiG
const OptiObject = Union{
OptiNode{<:AbstractOptiGraph},OptiEdge{<:AbstractOptiGraph},OptiGraph
}

const NodeConstraintRef = JuMP.ConstraintRef{
OptiNode{GT},MOI.ConstraintIndex{FT,ST},<:JuMP.AbstractShape
} where {GT<:AbstractOptiGraph,FT<:MOI.AbstractFunction,ST<:MOI.AbstractSet}

const EdgeConstraintRef = JuMP.ConstraintRef{
OptiEdge{GT},MOI.ConstraintIndex{FT,ST},<:JuMP.AbstractShape
} where {GT<:AbstractOptiGraph,FT<:MOI.AbstractFunction,ST<:MOI.AbstractSet}
14 changes: 14 additions & 0 deletions src/optiedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function Base.getindex(edge::OptiEdge, name::Symbol)
return edge.source_graph.edge_obj_dict[t]
end

const EdgeConstraintRef = JuMP.ConstraintRef{
OptiEdge{GT},MOI.ConstraintIndex{FT,ST},<:JuMP.AbstractShape
} where {GT<:AbstractOptiGraph,FT<:MOI.AbstractFunction,ST<:MOI.AbstractSet}

"""
graph_backend(edge::OptiEdge)
Expand Down Expand Up @@ -110,3 +114,13 @@ end
function JuMP.is_valid(edge::OptiEdge, cref::ConstraintRef)
return edge === JuMP.owner_model(cref) && MOI.is_valid(graph_backend(edge), cref)
end

"""
JuMP.dual(cref::EdgeConstraintRef; result::Int=1)
Return the dual for an `EdgeConstraintRef`. This returns the dual for the source graph that
corresponds to the constraint reference.
"""
function JuMP.dual(cref::EdgeConstraintRef; result::Int=1)
return MOI.get(graph_backend(cref.model), MOI.ConstraintDual(result), cref)
end
22 changes: 22 additions & 0 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,28 @@ function JuMP.value(graph::OptiGraph, expr::GenericNonlinearExpr; result::Int=1)
end
end

"""
JuMP.dual(graph::OptiGraph, cref::NodeConstraintRef; result::Int=1)
Return the dual value of `cref` in `graph`. Note that this value is specific to
the optimizer solution to the `graph`. The `cref` can have different values for
different optigraphs it is contained in.
"""
function JuMP.dual(graph::OptiGraph, cref::NodeConstraintRef; result::Int=1)
return MOI.get(graph_backend(graph), MOI.ConstraintDual(result), cref)
end

"""
JuMP.dual(graph::OptiGraph, cref::EdgeConstraintRef; result::Int=1)
Return the dual value of `cref` in `graph`. Note that this value is specific to
the optimizer solution to the `graph`. The `cref` can have different values for
different optigraphs it is contained in.
"""
function JuMP.dual(graph::OptiGraph, cref::EdgeConstraintRef; result::Int=1)
return MOI.get(graph_backend(graph), MOI.ConstraintDual(result), cref)
end

### Constraints

"""
Expand Down
12 changes: 12 additions & 0 deletions src/optinode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ function JuMP.delete(node::OptiNode, cref::ConstraintRef)
return nothing
end

### Duals

"""
JuMP.dual(cref::NodeConstraintRef; result::Int=1)
Return the dual for a `NodeConstraintRef`. This returns the dual for the source graph that
corresponds to the constraint reference.
"""
function JuMP.dual(cref::NodeConstraintRef; result::Int=1)
return MOI.get(graph_backend(cref.model), MOI.ConstraintDual(result), cref)
end

### Constraints

"""
Expand Down
5 changes: 5 additions & 0 deletions test/test_optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function test_simple_graph()
@test JuMP.dual_status(graph) == MOI.FEASIBLE_POINT
@test JuMP.result_count(graph) == 1
@test JuMP.raw_status(graph) == "kHighsModelStatusOptimal"

constraints = all_constraints(graph)
@test JuMP.dual(constraints[1]) == 1.0
@test JuMP.dual(constraints[2]) == 0.0
@test JuMP.dual(constraints[3]) == -2.0
end

function _create_test_nonlinear_optigraph()
Expand Down

0 comments on commit a7e5867

Please sign in to comment.