Skip to content

Commit 0f6813c

Browse files
authored
[Bridges] fix get ListOfVariableIndices when there is a chain of bridges (#2716)
1 parent 784d0fa commit 0f6813c

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/Bridges/Variable/map.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ function number_of_variables(map::Map)
174174
num += length_of_vector_of_variables(map, MOI.VariableIndex(-i))
175175
end
176176
end
177-
count(bridge -> bridge !== nothing, map.bridges)
178177
end
179178
return num
180179
end

src/Bridges/bridge_optimizer.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,11 @@ function _get_all_including_bridged(
760760
ret = MOI.VariableIndex[]
761761
for inner_variable in MOI.get(b.model, attr)
762762
outer_variable = get(inner_to_outer, inner_variable, missing)
763+
# If there is a chain of variable bridges, the `outer_variable` may need
764+
# to be mapped.
765+
while haskey(inner_to_outer, outer_variable)
766+
outer_variable = inner_to_outer[outer_variable]
767+
end
763768
if ismissing(outer_variable)
764769
# inner_variable does not exist in inner_to_outer, which means that
765770
# it is not bridged. Pass through unchanged.

test/Bridges/bridge_optimizer.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,36 @@ function test_fallback_delete_objective_bridge()
14221422
return
14231423
end
14241424

1425+
struct Model2714 <: MOI.ModelLike
1426+
dimensions::Vector{Int}
1427+
Model2714() = new(Int[])
1428+
end
1429+
1430+
MOI.is_empty(model::Model2714) = isempty(model.dimensions)
1431+
1432+
function MOI.supports_add_constrained_variables(
1433+
::Model2714,
1434+
::Type{MOI.Nonnegatives},
1435+
)
1436+
return true
1437+
end
1438+
1439+
function MOI.add_constrained_variables(model::Model2714, set::MOI.Nonnegatives)
1440+
n = MOI.dimension(set)
1441+
x = MOI.VariableIndex(sum(model.dimensions) .+ (1:n))
1442+
push!(model.dimensions, n)
1443+
F, S = MOI.VectorOfVariables, MOI.Nonnegatives
1444+
return x, MOI.ConstraintIndex{F,S}(length(model.dimensions))
1445+
end
1446+
1447+
function test_issue_2714()
1448+
model = MOI.instantiate(Model2714; with_bridge_type = Float64)
1449+
t, _ = MOI.add_constrained_variable(model, MOI.LessThan(10.0))
1450+
@test MOI.get(model, MOI.NumberOfVariables()) == 1
1451+
@test MOI.get(model, MOI.ListOfVariableIndices()) == [t]
1452+
return
1453+
end
1454+
14251455
end # module
14261456

14271457
TestBridgeOptimizer.runtests()

0 commit comments

Comments
 (0)