Skip to content
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
10 changes: 0 additions & 10 deletions src/Bridges/Constraint/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ bridges.
"""
abstract type AbstractBridge <: MOIB.AbstractBridge end

# TODO [breaking] merge with `Bridges.Variable.IndexInVector` into `Bridges.IndexInVector`
"""
IndexInVector

Index of variable in vector of variables.
"""
struct IndexInVector
value::Int
end

"""
bridge_constraint(
BT::Type{<:AbstractBridge},
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/flip_sign.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
function MOI.delete(
model::MOI.ModelLike,
bridge::FlipSignBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
func = MOI.get(model, MOI.ConstraintFunction(), bridge.constraint)
idx = setdiff(1:MOI.output_dimension(func), i.value)
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/functionize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ end
function MOI.delete(
model::MOI.ModelLike,
bridge::VectorFunctionizeBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
func = MOI.get(model, MOI.ConstraintFunction(), bridge.constraint)
idx = setdiff(1:MOI.output_dimension(func), i.value)
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/scalarize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ end
function MOI.delete(
model::MOI.ModelLike,
bridge::ScalarizeBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
MOI.delete(model, bridge.scalar_constraints[i.value])
deleteat!(bridge.scalar_constraints, i.value)
Expand Down
21 changes: 6 additions & 15 deletions src/Bridges/Variable/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ bridges.
"""
abstract type AbstractBridge <: MOIB.AbstractBridge end

"""
IndexInVector

Index of variable in vector of variables.
"""
struct IndexInVector
value::Int
end

"""
bridge_constrained_variable(
BT::Type{<:AbstractBridge},
Expand Down Expand Up @@ -56,7 +47,7 @@ end
model::MOI.ModelLike,
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)

Return the value of the attribute `attr` of the model `model` for the variable
Expand All @@ -66,7 +57,7 @@ function MOI.get(
::MOI.ModelLike,
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
::IndexInVector,
::MOIB.IndexInVector,
)
return throw(
ArgumentError(
Expand Down Expand Up @@ -98,7 +89,7 @@ end
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
value[,
::IndexInVector],
::MOIB.IndexInVector],
)

Return the value of the attribute `attr` of the model `model` for the variable
Expand All @@ -109,7 +100,7 @@ function MOI.set(
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
value,
::IndexInVector...,
::MOIB.IndexInVector...,
)
if MOI.is_copyable(attr) && !MOI.supports(model, attr, typeof(bridge))
throw(MOI.UnsupportedAttribute(attr))
Expand Down Expand Up @@ -271,7 +262,7 @@ the following method for every variable of `vis`.
unbridged_map(
bridge::MOI.Bridges.Variable.AbstractBridge,
vi::MOI.VariableIndex,
i::IndexInVector,
i::MOIB.IndexInVector,
)

For a bridged variable in a vector set, return a tuple of pairs mapping the
Expand All @@ -287,7 +278,7 @@ function unbridged_map end
function unbridged_map(bridge::AbstractBridge, vis::Vector{MOI.VariableIndex})
mappings = Pair{MOI.VariableIndex,MOI.AbstractScalarFunction}[]
for (i, vi) in enumerate(vis)
vi_mappings = unbridged_map(bridge, vi, IndexInVector(i))
vi_mappings = unbridged_map(bridge, vi, MOIB.IndexInVector(i))
if vi_mappings === nothing
return
end
Expand Down
10 changes: 5 additions & 5 deletions src/Bridges/Variable/flip_sign.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ end
function MOI.delete(
model::MOI.ModelLike,
bridge::FlipSignBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
MOI.delete(model, bridge.flipped_variables[i.value])
deleteat!(bridge.flipped_variables, i.value)
Expand Down Expand Up @@ -86,14 +86,14 @@ function MOI.get(
model::MOI.ModelLike,
attr::Union{MOI.VariablePrimal,MOI.VariablePrimalStart},
bridge::FlipSignBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
return -MOI.get(model, attr, bridge.flipped_variables[i.value])
end

function MOIB.bridged_function(
bridge::FlipSignBridge{T},
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
func = MOI.SingleVariable(bridge.flipped_variables[i.value])
return MOIU.operate(-, T, func)
Expand All @@ -102,7 +102,7 @@ end
function unbridged_map(
bridge::FlipSignBridge{T},
vi::MOI.VariableIndex,
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
func = MOIU.operate(-, T, MOI.SingleVariable(vi))
return (bridge.flipped_variables[i.value] => func,)
Expand All @@ -121,7 +121,7 @@ function MOI.set(
attr::MOI.VariablePrimalStart,
bridge::FlipSignBridge,
value,
i::IndexInVector,
i::MOIB.IndexInVector,
)
MOI.set(model, attr, bridge.flipped_variables[i.value], -value)
return
Expand Down
14 changes: 9 additions & 5 deletions src/Bridges/Variable/free.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ function MOI.delete(model::MOI.ModelLike, bridge::FreeBridge)
return
end

function MOI.delete(model::MOI.ModelLike, bridge::FreeBridge, i::IndexInVector)
function MOI.delete(
model::MOI.ModelLike,
bridge::FreeBridge,
i::MOIB.IndexInVector,
)
n = div(length(bridge.variables), 2)
MOI.delete(model, bridge.variables[i.value])
MOI.delete(model, bridge.variables[n+i.value])
Expand Down Expand Up @@ -101,7 +105,7 @@ function MOI.get(
model::MOI.ModelLike,
attr::Union{MOI.VariablePrimal,MOI.VariablePrimalStart},
bridge::FreeBridge{T},
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
n = div(length(bridge.variables), 2)
return MOI.get(model, attr, bridge.variables[i.value]) -
Expand All @@ -110,7 +114,7 @@ end

function MOIB.bridged_function(
bridge::FreeBridge{T},
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
n = div(length(bridge.variables), 2)
return MOIU.operate(
Expand All @@ -126,7 +130,7 @@ end
function unbridged_map(
bridge::FreeBridge{T},
vi::MOI.VariableIndex,
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
sv = MOI.SingleVariable(vi)
# `unbridged_map` is required to return a `MOI.ScalarAffineFunction`.
Expand All @@ -149,7 +153,7 @@ function MOI.set(
attr::MOI.VariablePrimalStart,
bridge::FreeBridge,
value,
i::IndexInVector,
i::MOIB.IndexInVector,
)
if value < 0
nonneg = zero(value)
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/Variable/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ function length_of_vector_of_variables(map::Map, vi::MOI.VariableIndex)
end

"""
index_in_vector_of_variables(map::Map, vi::MOI.VariableIndex)::IndexInVector
index_in_vector_of_variables(map::Map, vi::MOI.VariableIndex)::MOIB.IndexInVector

Return the index of `vi` in the vector of variables in which it was bridged.
"""
function index_in_vector_of_variables(map::Map, vi::MOI.VariableIndex)
return IndexInVector(map.index_in_vector[-vi.value])
return MOIB.IndexInVector(map.index_in_vector[-vi.value])
end

"""
Expand Down
14 changes: 7 additions & 7 deletions src/Bridges/Variable/rsoc_to_psd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function trimap(i::Integer, j::Integer)
end
end

function _variable_map(bridge::RSOCtoPSDBridge, i::IndexInVector)
function _variable_map(bridge::RSOCtoPSDBridge, i::MOIB.IndexInVector)
if bridge.psd isa
MOI.ConstraintIndex{MOI.VectorOfVariables,MOI.Nonnegatives}
return i.value
Expand All @@ -182,7 +182,7 @@ function _variable_map(bridge::RSOCtoPSDBridge, i::IndexInVector)
end
end

function _variable(bridge::RSOCtoPSDBridge, i::IndexInVector)
function _variable(bridge::RSOCtoPSDBridge, i::MOIB.IndexInVector)
return bridge.variables[_variable_map(bridge, i)]
end

Expand All @@ -193,7 +193,7 @@ function MOI.get(
)
values = MOI.get(model, attr, bridge.psd)
n = MOI.dimension(MOI.get(model, MOI.ConstraintSet(), bridge))
mapped = [values[_variable_map(bridge, IndexInVector(i))] for i in 1:n]
mapped = [values[_variable_map(bridge, MOIB.IndexInVector(i))] for i in 1:n]
if length(mapped) >= 2
mapped[2] /= 2
end
Expand All @@ -207,7 +207,7 @@ function MOI.get(
)
dual = MOI.get(model, attr, bridge.psd)
n = MOI.dimension(MOI.get(model, MOI.ConstraintSet(), bridge))
mapped = [dual[_variable_map(bridge, IndexInVector(i))] for i in 1:n]
mapped = [dual[_variable_map(bridge, MOIB.IndexInVector(i))] for i in 1:n]
for ci in bridge.diag
mapped[2] += MOI.get(model, attr, ci)
end
Expand All @@ -224,7 +224,7 @@ function MOI.get(
model::MOI.ModelLike,
attr::MOI.VariablePrimal,
bridge::RSOCtoPSDBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
value = MOI.get(model, attr, _variable(bridge, i))
if i.value == 2
Expand All @@ -236,7 +236,7 @@ end

function MOIB.bridged_function(
bridge::RSOCtoPSDBridge{T},
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
func = MOI.SingleVariable(_variable(bridge, i))
if i.value == 2
Expand All @@ -249,7 +249,7 @@ end
function unbridged_map(
bridge::RSOCtoPSDBridge{T},
vi::MOI.VariableIndex,
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
sv = MOI.SingleVariable(vi)
if i.value == 2
Expand Down
8 changes: 4 additions & 4 deletions src/Bridges/Variable/rsoc_to_soc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function rotate_result(
model,
attr::MOI.VariablePrimal,
variables,
i::IndexInVector,
i::MOIB.IndexInVector,
)
if i.value == 1 || i.value == 2
t, u = MOI.get(model, attr, variables[1:2])
Expand All @@ -29,7 +29,7 @@ function rotate_result(
end
end

function rotate_bridged_function(T::Type, variables, i::IndexInVector)
function rotate_bridged_function(T::Type, variables, i::MOIB.IndexInVector)
s2 = √T(2)
if i.value == 1 || i.value == 2
t = MOIU.operate(/, T, MOI.SingleVariable(variables[1]), s2)
Expand Down Expand Up @@ -154,14 +154,14 @@ function MOI.get(
model::MOI.ModelLike,
attr::MOI.VariablePrimal,
bridge::RSOCtoSOCBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
return rotate_result(model, attr, bridge.variables, i)
end

function MOIB.bridged_function(
bridge::RSOCtoSOCBridge{T},
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
return rotate_bridged_function(T, bridge.variables, i)
end
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/Variable/soc_to_rsoc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ function MOI.get(
model::MOI.ModelLike,
attr::MOI.VariablePrimal,
bridge::SOCtoRSOCBridge,
i::IndexInVector,
i::MOIB.IndexInVector,
)
return rotate_result(model, attr, bridge.variables, i)
end

function MOIB.bridged_function(
bridge::SOCtoRSOCBridge{T},
i::IndexInVector,
i::MOIB.IndexInVector,
) where {T}
return rotate_bridged_function(T, bridge.variables, i)
end
Expand Down
8 changes: 5 additions & 3 deletions src/Bridges/Variable/zeros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ function MOI.get(
::MOI.ModelLike,
::MOI.VariablePrimal,
::ZerosBridge{T},
::IndexInVector,
::MOIB.IndexInVector,
) where {T}
return zero(T)
end

function MOIB.bridged_function(::ZerosBridge{T}, ::IndexInVector) where {T}
function MOIB.bridged_function(::ZerosBridge{T}, ::MOIB.IndexInVector) where {T}
return zero(MOI.ScalarAffineFunction{T})
end

unbridged_map(::ZerosBridge, ::MOI.VariableIndex, ::IndexInVector) = nothing
function unbridged_map(::ZerosBridge, ::MOI.VariableIndex, ::MOIB.IndexInVector)
return nothing
end
9 changes: 9 additions & 0 deletions src/Bridges/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ the bridge so they should be implemented by the bridge.
"""
abstract type AbstractBridge end

"""
IndexInVector

Index of variable in vector of variables.
"""
struct IndexInVector
value::Int
end

"""
MOI.get(b::AbstractBridge, ::MOI.NumberOfConstraints{F, S}) where {F, S}

Expand Down
3 changes: 1 addition & 2 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ function _delete_variables_in_vector_of_variables_constraint(
call_in_context(
b,
ci,
bridge ->
MOI.delete(b, bridge, Constraint.IndexInVector(i)),
bridge -> MOI.delete(b, bridge, IndexInVector(i)),
)
else
MOIU.throw_delete_variable_in_vov(vi)
Expand Down
2 changes: 1 addition & 1 deletion test/Bridges/Variable/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct DummyVariableBridge <: MOIB.Variable.AbstractBridge end
bridge = DummyVariableBridge()
attr = MOI.VariablePrimalStart()
@test !MOI.supports(model, attr, typeof(bridge))
i = MOIB.Variable.IndexInVector(1)
i = MOIB.IndexInVector(1)
@test_throws MOI.UnsupportedAttribute(attr) MOI.set(
model,
attr,
Expand Down
Loading