Skip to content

Commit 0a3cddd

Browse files
committed
add tests and fixes
1 parent d2ec92f commit 0a3cddd

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

src/JuMP.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ function _get_dual_constraint(dual_model, primal_ref::JuMP.VariableRef)
7272
return JuMP.constraint_ref_with_index(dual_model, moi_dual_ci), idx
7373
end
7474

75-
# necessary?
76-
# function get_primal_constraint(primal_vi::JuMP.VariableRef)
77-
# primal_model = JuMP.owner_model(dual_model)
78-
# map = _get_primal_dual_map(primal_model)
79-
# moi_primal_vi = JuMP.index(primal_vi)
80-
# primal_ci, idx = get_primal_constraint(map, moi_primal_vi)
81-
# return JuMP.constraint_ref_with_index(primal_model, primal_ci), idx
82-
# end
75+
function _get_primal_constraint(
76+
dual_model::JuMP.Model,
77+
primal_vi::JuMP.VariableRef,
78+
)
79+
primal_model = JuMP.owner_model(primal_vi)
80+
map = _get_primal_dual_map(dual_model)
81+
moi_primal_vi = JuMP.index(primal_vi)
82+
primal_ci, idx = _get_primal_constraint(map, moi_primal_vi)
83+
if primal_ci === nothing
84+
return nothing, idx
85+
end
86+
return JuMP.constraint_ref_with_index(primal_model, primal_ci), idx
87+
end
8388

8489
function _get_dual_variables(
8590
dual_model::JuMP.Model,

src/structures.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function _get_primal_constraint(m::PrimalDualMap, vi::MOI.VariableIndex)
163163
end
164164

165165
function _get_dual_variables(m::PrimalDualMap, ci::MOI.ConstraintIndex)
166-
if haskey(m.primal_constrained_variables, ci)
166+
if !haskey(m.primal_constrained_variables, ci)
167167
# if the constraint is a constrained variable, then the dual variable
168168
# is the first element of the vector of dual variables
169169
return m.primal_constraint_data[ci].dual_variables
@@ -172,7 +172,7 @@ function _get_dual_variables(m::PrimalDualMap, ci::MOI.ConstraintIndex)
172172
end
173173

174174
function _get_dual_constraint(m::PrimalDualMap, ci::MOI.ConstraintIndex)
175-
if haskey(m.primal_constrained_variables, ci)
175+
if !haskey(m.primal_constrained_variables, ci)
176176
# if the constraint is a constrained variable, then the dual variable
177177
# is the first element of the vector of dual variables
178178
return m.primal_constraint_data[ci].dual_constrained_variable_constraint

test/Tests/test_JuMP_dualize.jl

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,44 @@ end
6060

6161
# Test that unnamed objects don't create a key `Symbol("")` in `dual_model`.
6262
@variable(model)
63-
@constraint(model, x == y)
63+
@constraint(model, c, x == y)
6464

6565
dual_model = dualize(model; dual_names = DualNames("dual", ""))
6666

6767
@test typeof(dual_model[:dualeqcon]) == VariableRef
6868
@test !haskey(dual_model, Symbol(""))
69+
70+
for var in (x, y, z)
71+
con = Dualization._get_dual_constraint(dual_model, x)
72+
@test con[1] isa ConstraintRef
73+
@test con[2] isa Int
74+
end
75+
76+
var = Dualization._get_dual_variables(dual_model, soccon)
77+
@test var === nothing
78+
con = Dualization._get_dual_constraint(dual_model, soccon)
79+
@test con === nothing
80+
81+
con = Dualization._get_primal_constraint(dual_model, y)
82+
@test con[1] isa ConstraintRef
83+
@test con[2] == 2
84+
85+
var = Dualization._get_dual_variables(dual_model, eqcon)
86+
@test length(var) == 1
87+
@test var[1] isa VariableRef
88+
con = Dualization._get_dual_constraint(dual_model, eqcon)
89+
@test con === nothing
90+
91+
var = Dualization._get_dual_variables(dual_model, c)
92+
@test length(var) == 1
93+
@test var[1] isa VariableRef
94+
con = Dualization._get_dual_constraint(dual_model, c)
95+
@test con === nothing
6996
end
7097
@testset "JuMP_dualize_kwargs" begin
7198
model = Model()
7299
@variable(model, x >= 0)
73-
@constraint(model, x <= 2)
100+
@constraint(model, c, x <= 2)
74101
@objective(model, Max, 2 * x + 1)
75102
dual_model = Dualization.dualize(
76103
model;
@@ -80,5 +107,25 @@ end
80107
)
81108
@test dual_model isa Model
82109
@test num_variables(dual_model) == 2
110+
con = Dualization._get_dual_constraint(dual_model, x)
111+
@test con[1] isa ConstraintRef
112+
@test con[2] == -1
113+
114+
con = Dualization._get_primal_constraint(dual_model, x)
115+
@test con[1] === nothing
116+
@test con[2] == -1
117+
118+
var = Dualization._get_dual_variables(dual_model, c)
119+
@test length(var) == 1
120+
@test var[] isa VariableRef
121+
con = Dualization._get_dual_constraint(dual_model, c)
122+
@test con isa ConstraintRef
123+
124+
cv = LowerBoundRef(x)
125+
var = Dualization._get_dual_variables(dual_model, cv)
126+
@test length(var) == 1
127+
@test var[] isa VariableRef
128+
con = Dualization._get_dual_constraint(dual_model, cv)
129+
@test con isa ConstraintRef
83130
end
84131
end

0 commit comments

Comments
 (0)