Skip to content

[Bridges.Variable] change ParameterToEqualToBridge to <: SetMapBridge #2664

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 1 commit into from
Feb 28, 2025

Conversation

odow
Copy link
Member

@odow odow commented Feb 26, 2025

@blegat it doesn't look like a SetMapBridge{T,<:MOI.AbstractScalarSet} bridge was ever implemented or tested?

@odow
Copy link
Member Author

odow commented Feb 26, 2025

Okay, one is implemented here in test:

const F{T} = MOI.ScalarAffineFunction{T}
const S{T} = MOI.EqualTo{T}
struct VariableBridge{T} <: MOI.Bridges.Variable.SetMapBridge{T,S{T},S{T}}
variable::MOI.VariableIndex
constraint::MOI.ConstraintIndex{MOI.VariableIndex,S{T}}
end
struct ConstraintBridge{T} <:
MOI.Bridges.Constraint.SetMapBridge{T,S{T},S{T},F{T},F{T}}
constraint::MOI.ConstraintIndex{F{T},S{T}}
end
const IdentityBridge{T} = Union{VariableBridge{T},ConstraintBridge{T}}
MOI.Bridges.map_set(::Type{<:IdentityBridge}, set::S) = set
MOI.Bridges.inverse_map_set(::Type{<:IdentityBridge}, set::S) = set
MOI.Bridges.map_function(::Type{<:IdentityBridge}, func) = func
MOI.Bridges.inverse_map_function(::Type{<:IdentityBridge}, func) = func
MOI.Bridges.adjoint_map_function(::Type{<:IdentityBridge}, func) = func
MOI.Bridges.inverse_adjoint_map_function(::Type{<:IdentityBridge}, func) = func

but it doesn't get properly tested, so we don't hit the relevant methods:

include("identity_bridge.jl")
function test_recursive_model_variable(::Type{T} = Int) where {T}
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}())
BT = IdentityBridges.VariableBridge{T}
b = MOI.Bridges.Variable.SingleBridgeOptimizer{BT}(model)
x, cx = MOI.add_constrained_variable(b, MOI.EqualTo(one(T)))
@test MOI.Bridges.is_bridged(b, x)
@test MOI.Bridges.is_bridged(b, cx)
@test MOI.get(b, MOI.ConstraintFunction(), cx) == x
@test MOI.get(b, MOI.ConstraintSet(), cx) == MOI.EqualTo(one(T))
MOI.set(b, MOI.ConstraintSet(), cx, MOI.EqualTo(zero(T)))
@test MOI.get(b, MOI.ConstraintSet(), cx) == MOI.EqualTo(zero(T))
@test MOI.is_valid(b, x)
MOI.delete(b, x)
@test !MOI.is_valid(b, x)
end

There is, for example, no check for NumberOfVariables or ListOfVariableIndices.

@odow odow force-pushed the od/scalar-set-map branch from f5594b3 to faba7c5 Compare February 28, 2025 06:39
function MOI.get(bridge::SetMapBridge, ::MOI.NumberOfVariables)::Int64
return length(bridge.variables)
function MOI.get(
model::MOI.ModelLike,
Copy link
Member

@blegat blegat Feb 28, 2025

Choose a reason for hiding this comment

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

It's a bit difficult to review if the methods are moved around in the same PR so it's unclear what changed but it looks correct

x::MOI.VariableIndex
ci::MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{T}}
struct ParameterToEqualToBridge{T} <:
SetMapBridge{T,MOI.EqualTo{T},MOI.Parameter{T}}
Copy link
Member

Choose a reason for hiding this comment

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

This is much better

@odow odow merged commit b3a78e6 into master Feb 28, 2025
16 checks passed
@odow odow deleted the od/scalar-set-map branch February 28, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants