Skip to content
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

Implementing bridge cost #1125

Merged
merged 1 commit into from
Aug 30, 2020
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
103 changes: 32 additions & 71 deletions src/Bridges/Constraint/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,38 @@ struct Map <: AbstractDict{MOI.ConstraintIndex, AbstractBridge}
# of creation so we need `OrderedDict` and not `Dict`.
# For `SingleVariable` constraints: (variable, set type) -> bridge
single_variable_constraints::OrderedDict{Tuple{Int64, DataType}, AbstractBridge}
# For `VectorVariable` constraints: (variable, set type) -> bridge
vector_of_variables_constraints::OrderedDict{Tuple{Int64, DataType}, AbstractBridge}
end
function Map()
return Map(Union{Nothing, AbstractBridge}[],
Tuple{DataType, DataType}[],
OrderedDict{Tuple{Int64, DataType}, AbstractBridge}(),
OrderedDict{Tuple{Int64, DataType}, AbstractBridge}())
end

# Implementation of `AbstractDict` interface.

function Base.isempty(map::Map)
return all(bridge -> bridge === nothing, map.bridges) &&
isempty(map.single_variable_constraints) &&
isempty(map.vector_of_variables_constraints)
isempty(map.single_variable_constraints)
end
function Base.empty!(map::Map)
empty!(map.bridges)
empty!(map.constraint_types)
empty!(map.single_variable_constraints)
empty!(map.vector_of_variables_constraints)
return map
end
function Base.length(map::Map)
return count(bridge -> bridge !== nothing, map.bridges) +
length(map.single_variable_constraints) +
length(map.vector_of_variables_constraints)
length(map.single_variable_constraints)
end
_index(ci::MOI.ConstraintIndex) = ci.value
_index(ci::MOI.ConstraintIndex{MOI.VectorOfVariables}) = -ci.value
function Base.haskey(map::Map, ci::MOI.ConstraintIndex{F, S}) where {F, S}
return 1 ≤ ci.value ≤ length(map.bridges) &&
map.bridges[ci.value] !== nothing &&
(F, S) == map.constraint_types[ci.value]
return 1 ≤ _index(ci) ≤ length(map.bridges) &&
map.bridges[_index(ci)] !== nothing &&
(F, S) == map.constraint_types[_index(ci)]
end
function Base.getindex(map::Map,
ci::MOI.ConstraintIndex)
return map.bridges[ci.value]
function Base.getindex(map::Map, ci::MOI.ConstraintIndex)
return map.bridges[_index(ci)]
end
function Base.haskey(map::Map, ci::MOI.ConstraintIndex{MOI.SingleVariable, S}) where S
return haskey(map.single_variable_constraints, (ci.value, S))
Expand All @@ -59,56 +54,40 @@ function Base.getindex(map::Map,
ci::MOI.ConstraintIndex{MOI.SingleVariable, S}) where S
return map.single_variable_constraints[(ci.value, S)]
end
function Base.haskey(map::Map, ci::MOI.ConstraintIndex{MOI.VectorOfVariables, S}) where S
return haskey(map.vector_of_variables_constraints, (ci.value, S))
end
function Base.getindex(map::Map,
ci::MOI.ConstraintIndex{MOI.VectorOfVariables, S}) where S
return map.vector_of_variables_constraints[(ci.value, S)]
end
function Base.delete!(map::Map, ci::MOI.ConstraintIndex)
map.bridges[ci.value] = nothing
map.bridges[_index(ci)] = nothing
return map
end
function Base.delete!(map::Map, ci::MOI.ConstraintIndex{MOI.SingleVariable, S}) where S
delete!(map.single_variable_constraints, (ci.value, S))
return map
end
function Base.delete!(map::Map, ci::MOI.ConstraintIndex{MOI.VectorOfVariables, S}) where S
delete!(map.vector_of_variables_constraints, (ci.value, S))
return map
end
function Base.values(map::Map)
return Base.Iterators.flatten((
# See comment in `values(::Variable.Map)`.
Base.Iterators.Filter(bridge -> bridge !== nothing, map.bridges),
values(map.single_variable_constraints),
values(map.vector_of_variables_constraints)
values(map.single_variable_constraints)
))
end

# Implementation of iterate: it should combine non-variablewise constraints,
# `SingleVariable` constraints and `VectorOfVariables` constraints.
function _iterate_vov(map::Map, elem_state=iterate(map.vector_of_variables_constraints))
if elem_state === nothing
return nothing
else
i, S = elem_state[1].first
bridge = elem_state[1].second
ci = MOI.ConstraintIndex{MOI.VectorOfVariables, S}(i)
return ci => bridge, (3, elem_state[2])
end
end
# Implementation of iterate: it should combine non-`SingleVariable` constraints and
# `SingleVariable` constraints.
function _iterate_sv(map::Map, elem_state=iterate(map.single_variable_constraints))
if elem_state === nothing
return _iterate_vov(map)
return nothing
else
i, S = elem_state[1].first
bridge = elem_state[1].second
ci = MOI.ConstraintIndex{MOI.SingleVariable, S}(i)
return ci => bridge, (2, elem_state[2])
end
end
function _index(index, F, S)
return MOI.ConstraintIndex{F, S}(index)
end
function _index(index, F::Type{MOI.VectorOfVariables}, S)
return MOI.ConstraintIndex{F, S}(-index)
end
function _iterate(map::Map, state=1)
while state ≤ length(map.bridges) && map.bridges[state] === nothing
state += 1
Expand All @@ -117,17 +96,15 @@ function _iterate(map::Map, state=1)
return _iterate_sv(map)
else
F, S = map.constraint_types[state]
return MOI.ConstraintIndex{F, S}(state) => map.bridges[state], (1, state + 1)
return _index(state, F, S) => map.bridges[state], (1, state + 1)
end
end
Base.iterate(map::Map) = _iterate(map)
function Base.iterate(map::Map, state)
if state[1] == 1
return _iterate(map, state[2])
elseif state[1] == 2
return _iterate_sv(map, iterate(map.single_variable_constraints, state[2]))
else
return _iterate_vov(map, iterate(map.vector_of_variables_constraints, state[2]))
return _iterate_sv(map, iterate(map.single_variable_constraints, state[2]))
end
end

Expand All @@ -149,14 +126,14 @@ were created with `add_key_for_bridge`.
"""
function keys_of_type end

function number_of_type(map::Map, C::Type{MOI.ConstraintIndex{F, S}}) where {F, S}
return count(i -> haskey(map, C(i)), eachindex(map.bridges))
function number_of_type(map::Map, ::Type{MOI.ConstraintIndex{F, S}}) where {F, S}
return count(i -> haskey(map, _index(i, F, S)), eachindex(map.bridges))
end
function keys_of_type(map::Map, C::Type{MOI.ConstraintIndex{F, S}}) where {F, S}
return Base.Iterators.Filter(
ci -> haskey(map, ci),
MOIU.LazyMap{C}(
i -> C(i), eachindex(map.bridges)))
i -> _index(i, F, S), eachindex(map.bridges)))
end
function number_of_type(map::Map, C::Type{MOI.ConstraintIndex{MOI.SingleVariable, S}}) where S
return count(key -> key[2] == S, keys(map.single_variable_constraints))
Expand All @@ -167,15 +144,6 @@ function keys_of_type(map::Map, C::Type{MOI.ConstraintIndex{MOI.SingleVariable,
Base.Iterators.Filter(key -> key[2] == S, keys(map.single_variable_constraints))
)
end
function number_of_type(map::Map, C::Type{MOI.ConstraintIndex{MOI.VectorOfVariables, S}}) where S
return count(key -> key[2] == S, keys(map.vector_of_variables_constraints))
end
function keys_of_type(map::Map, C::Type{MOI.ConstraintIndex{MOI.VectorOfVariables, S}}) where S
return MOIU.LazyMap{C}(
key -> C(key[1]),
Base.Iterators.Filter(key -> key[2] == S, keys(map.vector_of_variables_constraints))
)
end

"""
list_of_key_types(map::Map)
Expand All @@ -192,9 +160,6 @@ function list_of_key_types(map::Map)
for key in keys(map.single_variable_constraints)
push!(list, (MOI.SingleVariable, key[2]))
end
for key in keys(map.vector_of_variables_constraints)
push!(list, (MOI.VectorOfVariables, key[2]))
end
return list
end

Expand Down Expand Up @@ -222,8 +187,11 @@ Return the list of all keys that correspond to
"""
function vector_of_variables_constraints(map::Map)
return MOIU.LazyMap{MOI.ConstraintIndex{MOI.VectorOfVariables}}(
key -> MOI.ConstraintIndex{MOI.VectorOfVariables, key[2]}(key[1]),
keys(map.vector_of_variables_constraints)
i -> MOI.ConstraintIndex{map.constraint_types[i]...}(-i),
Base.Iterators.Filter(
i -> map.bridges[i] !== nothing && map.constraint_types[i][1] == MOI.VectorOfVariables,
eachindex(map.bridges)
)
)
end

Expand All @@ -238,8 +206,7 @@ operations in case variable bridges are not used.
"""
function has_bridges(map::Map)
return !isempty(map.bridges) ||
!isempty(map.single_variable_constraints) ||
!isempty(map.vector_of_variables_constraints)
!isempty(map.single_variable_constraints)
end


Expand All @@ -256,19 +223,13 @@ function add_key_for_bridge(map::Map, bridge::AbstractBridge,
func::MOI.AbstractFunction, set::MOI.AbstractSet)
push!(map.bridges, bridge)
push!(map.constraint_types, (typeof(func), typeof(set)))
return MOI.ConstraintIndex{typeof(func), typeof(set)}(length(map.bridges))
return _index(length(map.bridges), typeof(func), typeof(set))
end
function add_key_for_bridge(map::Map, bridge::AbstractBridge,
func::MOI.SingleVariable, set::MOI.AbstractScalarSet)
map.single_variable_constraints[(func.variable.value, typeof(set))] = bridge
return MOI.ConstraintIndex{MOI.SingleVariable, typeof(set)}(func.variable.value)
end
function add_key_for_bridge(map::Map, bridge::AbstractBridge,
func::MOI.VectorOfVariables, set::MOI.AbstractVectorSet)
index = first(func.variables).value
map.vector_of_variables_constraints[(index, typeof(set))] = bridge
return MOI.ConstraintIndex{MOI.VectorOfVariables, typeof(set)}(index)
end

"""
EmptyMap <: AbstractDict{MOI.ConstraintIndex, AbstractBridge}
Expand Down
13 changes: 11 additions & 2 deletions src/Bridges/Constraint/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ function bridges(bridge::SingleBridgeOptimizer)
end

MOIB.supports_constraint_bridges(::SingleBridgeOptimizer) = true
function MOIB.is_bridged(::SingleBridgeOptimizer, ::Type{<:MOI.AbstractSet})
return false
# If `BT` bridges `MOI.Reals` (such as `Constraint.FunctionizeBridge` bridge,
# without this method, it creates a `StackOverflow` with
# `is_bridged`, `supports_bridging_constrained_variable`
# and `supports_add_constrained_variables`.
MOIB.is_bridged(::SingleBridgeOptimizer, ::Type{MOI.Reals}) = false
function MOIB.is_bridged(b::SingleBridgeOptimizer, S::Type{<:MOI.AbstractSet})
return MOIB.supports_bridging_constrained_variable(b, S)
end
function MOIB.supports_bridging_constrained_variable(b::SingleBridgeOptimizer, S::Type{<:MOI.AbstractSet})
return MOIB.supports_bridging_constraint(b, MOIU.variable_function_type(S), S) && MOI.supports_add_constrained_variables(b, MOI.Reals)
end
function MOIB.supports_bridging_constraint(
::SingleBridgeOptimizer{BT}, F::Type{<:MOI.AbstractFunction},
Expand All @@ -45,3 +53,4 @@ function MOIB.bridge_type(::SingleBridgeOptimizer{BT},
::Type{<:MOI.AbstractSet}) where BT
return BT
end
MOIB.bridging_cost(::SingleBridgeOptimizer, args...) = 1.0
1 change: 1 addition & 0 deletions src/Bridges/Variable/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ function MOIB.bridge_type(::SingleBridgeOptimizer{BT},
::Type{<:MOI.AbstractSet}) where BT
return BT
end
MOIB.bridging_cost(::SingleBridgeOptimizer, args...) = 1.0
Loading