Skip to content

WIP: VectorAffineBridge #413

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

Closed
wants to merge 4 commits into from
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
5 changes: 4 additions & 1 deletion src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ include("singlebridgeoptimizer.jl")
include("lazybridgeoptimizer.jl")

# This is used by JuMP and removes the need to update JuMP everytime a bridge is added
MOIU.@model AllBridgedConstraints () (Interval,) (SecondOrderCone, RotatedSecondOrderCone, GeometricMeanCone, LogDetConeTriangle, RootDetConeTriangle) () () (ScalarAffineFunction,) (VectorOfVariables,) (VectorAffineFunction,)
MOIU.@model AllBridgedConstraints () (Interval,) (SecondOrderCone, RotatedSecondOrderCone, GeometricMeanCone, LogDetConeTriangle, RootDetConeTriangle, Zeros, Nonnegatives, Nonpositives) () () (ScalarAffineFunction,) (VectorOfVariables,) (VectorAffineFunction,)
"""
fullbridgeoptimizer(model::MOI.ModelLike, ::Type{T}) where T

Expand All @@ -43,6 +43,7 @@ function fullbridgeoptimizer(model::MOI.ModelLike, ::Type{T}) where T
addbridge!(bridgedmodel, MOIB.RSOCBridge{T})
addbridge!(bridgedmodel, MOIB.RSOCtoPSDCBridge{T})
addbridge!(bridgedmodel, MOIB.SOCtoPSDCBridge{T})
addbridge!(bridgedmodel, MOIB.VectorToScalarBridge{T})
bridgedmodel
end

Expand All @@ -58,5 +59,7 @@ include("detbridge.jl")
include("soctopsdbridge.jl")
@bridge SOCtoPSD SOCtoPSDCBridge () () (SecondOrderCone,) () () () (VectorOfVariables,) (VectorAffineFunction,)
@bridge RSOCtoPSD RSOCtoPSDCBridge () () (RotatedSecondOrderCone,) () () () (VectorOfVariables,) (VectorAffineFunction,)
include("vaftosafbridge.jl")
@bridge VectorToScalar VectorToScalarBridge () () (Zeros, Nonnegatives, Nonpositives) () () () () (VectorAffineFunction,)

end # module
148 changes: 148 additions & 0 deletions src/Bridges/vaftosafbridge.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const VectorToScalarBridgeSets = Union{MOI.Zeros,
MOI.Nonpositives,
MOI.Nonnegatives}
get_scalar_set(T, ::Type{MOI.Zeros}) = MOI.EqualTo{T}
get_scalar_set(T, ::Type{MOI.Nonpositives}) = MOI.LessThan{T}
get_scalar_set(T, ::Type{MOI.Nonnegatives}) = MOI.GreaterThan{T}

get_vector_set(::Type{MOI.EqualTo{T}}) where T = MOI.Zeros
get_vector_set(::Type{MOI.LessThan{T}}) where T = MOI.Nonpositives
get_vector_set(::Type{MOI.GreaterThan{T}}) where T = MOI.Nonnegatives

"""
VectorToScalarBridge{T, S}

The `VectorToScalarBridge` splits an `VectorAffineFunction` into a series of
`ScalarAffineFunction`s.

Supported sets are:
- `MOI.Zeros` =>`MOI.EqualTo{T}`
- `MOI.Nonnegatives` =>`MOI.GreaterThan{T}`
- `MOI.Nonpositives` =>`MOI.LessThan{T}`
"""
struct VectorToScalarBridge{T, S} <: AbstractBridge
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to have

struct VectorToScalarBridge{T, F, S} <: AbstractBridge
    constriants::Vector{CI{F, S}}
end

where F can also be, e.g. SingleVariable and ScalarQuadraticFunction.
You can leave it for future work as the tools needed are being added in #455

constraints::Vector{CI{MOI.ScalarAffineFunction{T}, S}}
end
function VectorToScalarBridge{T, ScalarSet}(model,
func::MOI.VectorAffineFunction{T},
set::VectorSet) where
{T, ScalarSet, VectorSet}
@assert ScalarSet == get_scalar_set(T, VectorSet)
sets = [ScalarSet(-constant) for constant in func.constants]
@assert MOI.dimension(set) == length(sets)
functions = [MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}[], zero(T))
for i in 1:MOI.dimension(set)]
Copy link
Member

Choose a reason for hiding this comment

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

Cosmetic/Subjective: I would indent to start for just past the [

Copy link
Contributor

Choose a reason for hiding this comment

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

Ha ... if it was up to me, anything other than 4 space indents would be a jailable offense

Copy link
Member

Choose a reason for hiding this comment

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

indenting is where coder can express their artistic inner-self :D
More seriously, I was confused when reading the line as I thought it was a block (e.g. like for, begin, if, ...) instead of a line-break.
I usually do 4-spaces for blocks and for line-breaks I align with parentheses, brackets, ...

for term in func.terms
scalar_function = functions[term.output_index]
push!(scalar_function.terms, term.scalar_term)
end
Copy link
Member

Choose a reason for hiding this comment

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

These lines are equivalent to functions = MOIU.eachscalar(func)[1:MOI.dimension(set)]. The advantage of this is that it could also work with VectorQuadraticFunction without the need to do any change in the bridge

constraint_indices = MOI.addconstraints!(model, functions, sets)
return VectorToScalarBridge(constraint_indices)
end

function MOI.supportsconstraint(::Type{VectorToScalarBridge{T, ScalarSet}},
::Type{MOI.VectorAffineFunction{T}},
::Type{VectorSet}) where {T, ScalarSet, VectorSet}
return get_scalar_set(T, VectorSet) == ScalarSet
end

function addedconstrainttypes(::Type{VectorToScalarBridge{T, ScalarSet}},
::Type{MOI.VectorAffineFunction{T}},
::Type{VectorSet}) where {T, ScalarSet, VectorSet}
return [(MOI.ScalarAffineFunction{T}, ScalarSet)]
end

function concrete_bridge_type(::Type{<:VectorToScalarBridge},
::Type{<:MOI.VectorAffineFunction{T}},
::Type{S}) where {T, S<:VectorToScalarBridgeSets}
scalar_set = get_scalar_set(T, S)
return VectorToScalarBridge{T, scalar_set}
end


function MOI.canget(bridge::VectorToScalarBridge{T, S},
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, S}) where {T, S}
return true
end
function MOI.get(bridge::VectorToScalarBridge{T, S},
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, S}) where {T, S}
return length(bridge.constraints)
end

function MOI.canget(bridge::VectorToScalarBridge{T, S},
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T}, S}) where {T, S}
return true
end
function MOI.get(bridge::VectorToScalarBridge{T, S},
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T}, S}) where {T, S}
return bridge.constraints
end

function MOI.delete!(model::MOI.ModelLike, bridge::VectorToScalarBridge)
MOI.delete!.(Ref(model), bridge.constraints)
end

function MOI.canget(model::MOI.ModelLike, attribute::MOI.ConstraintDual,
::Type{VectorToScalarBridge{T, S}}) where {T, S}
return MOI.canget(model, attribute, CI{MOI.ScalarAffineFunction{T}, S})
end
function MOI.get(model::MOI.ModelLike, attribute::MOI.ConstraintDual,
bridge::VectorToScalarBridge)
return MOI.get.(Ref(model), Ref(attribute), bridge.constraints)
end

function MOI.canget(model::MOI.ModelLike, attribute::MOI.ConstraintPrimal,
::Type{VectorToScalarBridge{T, S}}) where {T, S}
return MOI.canget(model, attribute, CI{MOI.ScalarAffineFunction{T}, S}) &&
MOI.canget(model, MOI.ConstraintSet(), CI{MOI.ScalarAffineFunction{T}, S})
end
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintPrimal, bridge::VectorToScalarBridge)
primals = fill(0.0, length(bridge.constraints))
Copy link
Member

Choose a reason for hiding this comment

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

zeros(T, length(bridge.constraints))

for (i, index) in enumerate(bridge.constraints)
primal = MOI.get(model, MOI.ConstraintPrimal(), index)
set = MOI.get(model, MOI.ConstraintSet(), index)
set_constant = MOIU.getconstant(set)
primals[i] = primal - set_constant
end
return primals
end

function MOI.modify!(model::MOI.ModelLike, bridge::VectorToScalarBridge{T, S},
change::MOI.VectorConstantChange{Float64}) where {T, S}
@assert length(bridge.constraints) == length(change.new_constant)
for (index, coef) in zip(bridge.constraints, change.new_constant)
MOI.set!(model, MOI.ConstraintSet(), index, S(-coef))
end
end

function MOI.modify!(model::MOI.ModelLike, bridge::VectorToScalarBridge{T, S},
change::MOI.MultirowChange{T}) where {T, S}
@assert length(bridge.constraints) == length(change.new_coefficients)
variable = change.variable
for (row, coef) in change.new_coefficients
index = bridge.constraints[row]
MOI.modify!(model, index, MOI.ScalarCoefficientChange(variable, coef))
end
end

MOI.canget(::MOI.ModelLike, ::MOI.ConstraintSet, ::Type{<:VectorToScalarBridge}) = true
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintSet,
bridge::VectorToScalarBridge{T, S}) where {T, S}
return get_vector_set(S)
end

MOI.canget(::MOI.ModelLike, ::MOI.ConstraintFunction, ::Type{<:VectorToScalarBridge}) = true
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintFunction,
bridge::VectorToScalarBridge{T, S}) where {T, S}
terms = VectorAffineTerm{T}[]
Copy link
Member

Choose a reason for hiding this comment

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

moivcat can be helpful here. Using moivcat will help adding support for quadratic functions

constants = T[]
for (row, index) in enumerate(bridge.constraints)
foo = MOI.get(model, MOI.ConstraintFunction(), index)
for term in foo.terms
push!(terms, MOI.VectorAffineTerm(row, term))
end
scalar_set = MOI.get(model, MOI.ConstraintSet())::S
push!(constants, foo.constant + MOIU.getconstant(scalar_set))
end
return VectorAffineFunction(terms, constants)
end
1 change: 1 addition & 0 deletions test/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,5 @@ end
(MOI.VectorAffineFunction{Float64}, MOI.GeometricMeanCone, 0),
(MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle, 0)))
end

end