Skip to content

Commit 0b468ce

Browse files
committed
[Bridges] pass variable names through ParameterToEqualToBridge
1 parent bd8918d commit 0b468ce

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Bridges/bridge_optimizer.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,20 @@ function MOI.get(
17571757
end
17581758
end
17591759

1760+
function _get_variable_if_equivalent(b, x)
1761+
return _get_variable_if_equivalent(bridged_variable_function(b, x))
1762+
end
1763+
1764+
function _get_variable_if_equivalent(f::MOI.ScalarAffineFunction)
1765+
if length(f.terms) == 1
1766+
term = only(f.terms)
1767+
if isone(term.coefficient)
1768+
return term.variable
1769+
end
1770+
end
1771+
return nothing
1772+
end
1773+
17601774
function MOI.set(
17611775
b::AbstractBridgeOptimizer,
17621776
attr::MOI.VariableName,
@@ -1766,6 +1780,11 @@ function MOI.set(
17661780
if is_bridged(b, vi)
17671781
b.var_to_name[vi] = name
17681782
b.name_to_var = nothing # Invalidate the name map.
1783+
# Set the internal variable name iff the bridged variable is equivalent
1784+
# to `y := 1 * x`.
1785+
if (x = _get_variable_if_equivalent(b, vi)) !== nothing
1786+
MOI.set(b.model, attr, x, name)
1787+
end
17691788
else
17701789
MOI.set(b.model, attr, vi, name)
17711790
end

test/Bridges/bridge_optimizer.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,28 @@ function test_issue_2817c()
15701570
return
15711571
end
15721572

1573+
function test_issue_2762_success()
1574+
inner = MOI.Utilities.Model{Float64}()
1575+
model = MOI.Bridges.Variable.ParameterToEqualTo{Float64}(inner)
1576+
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
1577+
MOI.set(model, MOI.VariableName(), p, "p")
1578+
@test MOI.get(model, MOI.VariableName(), p) == "p"
1579+
x = only(MOI.get(inner, MOI.ListOfVariableIndices()))
1580+
@test MOI.get(inner, MOI.VariableName(), x) == "p"
1581+
return
1582+
end
1583+
1584+
function test_issue_2762_fail()
1585+
inner = MOI.Utilities.Model{Float64}()
1586+
model = MOI.Bridges.Variable.NonposToNonneg{Float64}(inner)
1587+
y, _ = MOI.add_constrained_variables(model, MOI.Nonpositives(2))
1588+
MOI.set(model, MOI.VariableName(), y[1], "y1")
1589+
@test MOI.get(model, MOI.VariableName(), y[1]) == "y1"
1590+
x = MOI.get(inner, MOI.ListOfVariableIndices())
1591+
@test all(isempty, MOI.get(inner, MOI.VariableName(), x))
1592+
return
1593+
end
1594+
15731595
end # module
15741596

15751597
TestBridgeOptimizer.runtests()

0 commit comments

Comments
 (0)