Skip to content
Closed
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
30 changes: 30 additions & 0 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,28 @@ function MOI.get(
end
end

function _get_variable_if_equivalent(
b::AbstractBridgeOptimizer,
x::MOI.VariableIndex,
)
return _get_variable_if_equivalent(bridged_variable_function(b, x), x)
end

_get_variable_if_equivalent(::AbstractBridgeOptimizer, ::Nothing) = nothing

function _get_variable_if_equivalent(
f::MOI.ScalarAffineFunction,
x::MOI.VariableIndex,
)
if length(f.terms) == 1
term = only(f.terms)
if isone(term.coefficient)
return term.variable
end
end
return nothing
end

function MOI.set(
b::AbstractBridgeOptimizer,
attr::MOI.VariableName,
Expand All @@ -1766,6 +1788,11 @@ function MOI.set(
if is_bridged(b, vi)
b.var_to_name[vi] = name
b.name_to_var = nothing # Invalidate the name map.
# Set the internal variable name iff the bridged variable is equivalent
# to `y := 1 * x`.
if (x = _get_variable_if_equivalent(b, vi)) !== nothing
MOI.set(b.model, attr, x, name)
end
else
MOI.set(b.model, attr, vi, name)
end
Expand Down Expand Up @@ -1839,6 +1866,9 @@ function MOI.get(
end
vi_bridged = get(b.name_to_var, name, nothing)
MOI.Utilities.throw_if_multiple_with_name(vi_bridged, name)
if _get_variable_if_equivalent(b, vi_bridged) !== nothing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment explaining

return vi_bridged
end
return MOI.Utilities.check_type_and_multiple_names(
MOI.VariableIndex,
vi_bridged,
Expand Down
22 changes: 22 additions & 0 deletions test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,28 @@ function test_issue_2817c()
return
end

function test_issue_2762_success()
inner = MOI.Utilities.Model{Float64}()
model = MOI.Bridges.Variable.ParameterToEqualTo{Float64}(inner)
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
MOI.set(model, MOI.VariableName(), p, "p")
@test MOI.get(model, MOI.VariableName(), p) == "p"
x = only(MOI.get(inner, MOI.ListOfVariableIndices()))
@test MOI.get(inner, MOI.VariableName(), x) == "p"
return
end

function test_issue_2762_fail()
inner = MOI.Utilities.Model{Float64}()
model = MOI.Bridges.Variable.NonposToNonneg{Float64}(inner)
y, _ = MOI.add_constrained_variables(model, MOI.Nonpositives(2))
MOI.set(model, MOI.VariableName(), y[1], "y1")
@test MOI.get(model, MOI.VariableName(), y[1]) == "y1"
x = MOI.get(inner, MOI.ListOfVariableIndices())
@test all(isempty, MOI.get(inner, MOI.VariableName(), x))
return
end

end # module

TestBridgeOptimizer.runtests()
Loading