Skip to content

[Bridges] fix get ListOfVariableIndices when there is a chain of bridges #2716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Bridges/Variable/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ function number_of_variables(map::Map)
num += length_of_vector_of_variables(map, MOI.VariableIndex(-i))
end
end
count(bridge -> bridge !== nothing, map.bridges)
end
return num
end
Expand Down
5 changes: 5 additions & 0 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@
ret = MOI.VariableIndex[]
for inner_variable in MOI.get(b.model, attr)
outer_variable = get(inner_to_outer, inner_variable, missing)
# If there is a chain of variable bridges, the `outer_variable` may need
# to be mapped.
while haskey(inner_to_outer, outer_variable)
outer_variable = inner_to_outer[outer_variable]
end

Check warning on line 767 in src/Bridges/bridge_optimizer.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/bridge_optimizer.jl#L765-L767

Added lines #L765 - L767 were not covered by tests
if ismissing(outer_variable)
# inner_variable does not exist in inner_to_outer, which means that
# it is not bridged. Pass through unchanged.
Expand Down
30 changes: 30 additions & 0 deletions test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,36 @@ function test_fallback_delete_objective_bridge()
return
end

struct Model2714 <: MOI.ModelLike
dimensions::Vector{Int}
Model2714() = new(Int[])
end

MOI.is_empty(model::Model2714) = isempty(model.dimensions)

function MOI.supports_add_constrained_variables(
::Model2714,
::Type{MOI.Nonnegatives},
)
return true
end

function MOI.add_constrained_variables(model::Model2714, set::MOI.Nonnegatives)
n = MOI.dimension(set)
x = MOI.VariableIndex(sum(model.dimensions) .+ (1:n))
push!(model.dimensions, n)
F, S = MOI.VectorOfVariables, MOI.Nonnegatives
return x, MOI.ConstraintIndex{F,S}(length(model.dimensions))
end

function test_issue_2714()
model = MOI.instantiate(Model2714; with_bridge_type = Float64)
t, _ = MOI.add_constrained_variable(model, MOI.LessThan(10.0))
@test MOI.get(model, MOI.NumberOfVariables()) == 1
@test MOI.get(model, MOI.ListOfVariableIndices()) == [t]
return
end

end # module

TestBridgeOptimizer.runtests()
Loading