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

Do Not Merge Yet: Add MOI Disjunction Set Reformulation #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,33 @@ A type for using indicator constraint approach for linear disjunctive constraint
"""
struct Indicator <: AbstractReformulationMethod end

"""
MOIDisjunction <: AbstractReformulationMethod
A reformulation type for reformulating disjunctions into their MathOptInterface
set representations [`DisjunctionSet`](@ref) which are then added to the model.
"""
struct MOIDisjunction <: AbstractReformulationMethod end

"""
DisjunctionSet{S <: MOI.AbstractSet} <: MOI.AbstractVectorSet
A MathOptInterface set for representing disjunctions in the format vector of
functions in set to enable:
```julia
@constraint(model, [funcs...] in DisjunctionSet(n, idxs, sets))
```
where the vector of functions `funcs` is a flattened version of all the disjunct
constraint functions where the indicator variable is listed first, `n` is the
length of `[funcs...]`, `idxs` is a vector of the indices tracking where each
disjunct begins (i.e., it stores the indices of the indicator variables in
`[funcs...]`), and `sets` is a uses a vector of vectors structure for the MOI sets
that correspond to all the disjunct constraint functions.
"""
struct DisjunctionSet{S <: _MOI.AbstractSet} <: _MOI.AbstractVectorSet
dimension::Int
disjunct_indices::Vector{Int}
constraint_sets::Vector{Vector{S}}
end

################################################################################
# GDP Data
################################################################################
Expand Down
59 changes: 59 additions & 0 deletions src/moi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
################################################################################
# UTILITY METHODS
################################################################################
# Requred for extensions to MOI.AbstractVectorSet
# function _MOI.Utilities.set_dot(x::AbstractVector, y::AbstractVector, set::DisjunctionSet)
# return LinearAlgebra.dot(x, y) # TODO figure out what we should actually do here
# end

# TODO create a bridge for `DisjunctionSet`

# TODO create helper method to unpack DisjunctionSet at the MOI side of things

################################################################################
# REFRORMULATION METHODS
################################################################################
# Helper methods to handle recursively flattening the disjuncts
function _constr_set!(model, funcs, con::JuMP.AbstractConstraint)
append!(funcs, JuMP.jump_function(con))
return JuMP.moi_set(con)

Check warning on line 19 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L17-L19

Added lines #L17 - L19 were not covered by tests
end
function _constr_set!(model, funcs, con::Disjunction)
inner_funcs, set = _disjunction_to_set(model, con)
append!(funcs, inner_funcs)
return set

Check warning on line 24 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L21-L24

Added lines #L21 - L24 were not covered by tests
end

# Create the vectors needed for a disjunction vector constraint
function _disjunction_to_set(model::JuMP.Model, d::Disjunction)

Check warning on line 28 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L28

Added line #L28 was not covered by tests
# allocate memory for the storage vectors
num_disjuncts = length(d.indicators)
constr_mappings = _indicator_to_constraints(model)
num_constrs = sum(length(constr_mappings[lvref]) for lvref in d.indicators)
funcs = sizehint!(JuMP.AbstractJuMPScalar[], num_disjuncts + num_constrs)
sets = Vector{Vector{_MOI.AbstractSet}}(undef, num_disjuncts)
d_idxs = Vector{Int}(undef, num_disjuncts)

Check warning on line 35 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L30-L35

Added lines #L30 - L35 were not covered by tests
# iterate over the underlying disjuncts to fill in the storage vectors
for (i, lvref) in enumerate(d.indicators)
push!(funcs, _indicator_to_binary(model)[lvref])
d_idxs[i] = length(funcs)
crefs = constr_mappings(model)[lvref]
sets[i] = map(c -> _constr_set!(model, funcs, JuMP.constraint_object(c)), crefs)

Check warning on line 41 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L37-L41

Added lines #L37 - L41 were not covered by tests
end
# convert the `sets` type to be concrete if possible (TODO benchmark if this is worth it)
SetType = typeof(first(sets))
if SetType != Vector{_MOI.AbstractSet} && all(s -> s isa SetType, sets)
sets = convert(SetType, sets)

Check warning on line 46 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L44-L46

Added lines #L44 - L46 were not covered by tests
end
return funcs, DisjunctionSet(length(funcs), d_idxs, sets)

Check warning on line 48 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L48

Added line #L48 was not covered by tests
end

# Extend the disjunction reformulation
function reformulate_disjunction(

Check warning on line 52 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L52

Added line #L52 was not covered by tests
model::JuMP.Model,
d::Disjunction,
::MOIDisjunction
)
funcs, set = _disjunction_to_set(model, d)
return [JuMP.VectorConstraint(funcs, set, JuMP.VectorShape())]

Check warning on line 58 in src/moi.jl

View check run for this annotation

Codecov / codecov/patch

src/moi.jl#L57-L58

Added lines #L57 - L58 were not covered by tests
end
Loading