Skip to content

Commit 0d4d582

Browse files
committed
Update
1 parent 3ec4fd9 commit 0d4d582

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/Bridges/bridge_optimizer.jl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,41 @@ function _get_all_including_bridged(
718718
attr::MOI.ListOfVariableIndices,
719719
)
720720
list = MOI.get(b.model, attr)
721-
if !isempty(Variable.bridges(b))
722-
# The conversion from `keys` into a `Vector` happens inside `append!`.
723-
append!(list, keys(Variable.bridges(b)))
721+
# This is about to get confusing. `list` contains the variables in `.model`
722+
# ordered by when they were added to the model. We need to return a list of
723+
# the original user-space variables in the order that they were added. To do
724+
# so, we need to undo any Variable.bridges transformations.
725+
#
726+
# `inner_to_outer` is going to map variable indices in `b.model` to their
727+
# bridged variable indices. If the bridge adds multiple variables, we need
728+
# only to map the first variable, and we can skip the rest. To mark this
729+
# distinction, the tail variables are set to `nothing`.
730+
inner_to_outer = Dict{MOI.VariableIndex,Union{Nothing,MOI.VariableIndex}}()
731+
for (user_variable, bridge) in Variable.bridges(b)
732+
variables = MOI.get(bridge, MOI.ListOfVariableIndices())
733+
for bridged_variable in variables
734+
inner_to_outer[bridged_variable] = nothing
735+
end
736+
inner_to_outer[first(variables)] = user_variable
737+
end
738+
ret = MOI.VariableIndex[]
739+
for inner_variable in list
740+
outer_variable = get(inner_to_outer, inner_variable, missing)
741+
if ismissing(outer_variable)
742+
# inner_variable does not exist in inner_to_outer, which means that
743+
# it is not bridged. Pass through unchanged.
744+
push!(ret, inner_variable)
745+
elseif isnothing(outer_variable)
746+
# inner_variable exists in inner_to_outer, but it is set to `nothing`
747+
# which means that it is not the first variable in the bridge. Skip
748+
# it because it should be hidden from the user.
749+
else
750+
# inner_variable exists in inner_to_outer. It must be the first
751+
# variable in the bridge. Report it back to the user.
752+
push!(ret, outer_variable)
753+
end
724754
end
725-
return list
755+
return ret
726756
end
727757

728758
function _get_all_including_bridged(

0 commit comments

Comments
 (0)