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

Abolish ElemDict #274

Merged
merged 5 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 2 additions & 12 deletions src/Containers/Containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ using DynamicSparseArrays

import ..Coluna

import DynamicSparseArrays
import Base
import Primes
import Printf
import Base: <=, setindex!, get, getindex, haskey, keys, values, iterate,
length, lastindex, filter, show, keys, copy, isapprox

# nestedenum.jl
export NestedEnum, @nestedenum, @exported_nestedenum
Expand All @@ -17,19 +15,11 @@ export NestedEnum, @nestedenum, @exported_nestedenum
export Bound, Solution,
getvalue, isbetter, diff, gap, printbounds, getbound, getsol, setvalue!

# members.jl
export MembersMatrix

# To be deleted :
export ElemDict,
MembersVector

export getelements, getelement, rows, cols, columns, getrecords

include("nestedenum.jl")
include("solsandbounds.jl")

# Following files will be deleted
include("elements.jl")
include("members.jl")

end
23 changes: 0 additions & 23 deletions src/Containers/elements.jl

This file was deleted.

72 changes: 1 addition & 71 deletions src/Containers/members.jl
Original file line number Diff line number Diff line change
@@ -1,73 +1,3 @@
abstract type AbstractMembersContainer end

mutable struct MembersVector{I,K,T} <: AbstractMembersContainer
elements::Dict{I,K} # holds a reference towards the container of elements (sorted by ID) to which we associate records
records::Dict{I,T} # holds the records associated to elements that are identified by their ID
end

"""
MembersVector{T}(elems::Dict{I,K})

Construct a `MembersVector` with indices of type `I`, elements of type `K`, and
records of type `T`.

The `MembersVector` maps each index to a tuple of element and record. This
structure must be use like a `Vector{I,T}`. If the user looks for an index
that that has an element associated but no record, `MembersVector` returns
`zeros(T)`.
"""
function MembersVector{T}(elems::Dict{I,K}) where {I,K,T}
return MembersVector{I,K,T}(elems, Dict{I,T}())
end

function MembersVector{T}(elems::ElemDict{I,K}) where {I,K,T}
return MembersVector{T}(elems.elements)
end


getrecords(vec::MembersVector) = vec.records


function Base.setindex!(vec::MembersVector{I,K,T}, val, id::I) where {I,K,T}
vec.records[id] = val
end

function Base.get(vec::MembersVector{I,K,T}, id::I, default) where {I,K,T}
Base.get(vec.records, id, default)
end

function Base.getindex(vec::MembersVector{I,K,MembersVector{J,L,T}}, id::I) where {I,J,K,L,T<:Number}
Base.get(vec, id, Nothing)
end

function Base.getindex(vec::MembersVector{I,K,T}, id::I) where {I,K,T<:Number}
Base.get(vec, id, zero(T))
end

function Base.haskey(vec::MembersVector{I,K,T}, id::I) where {I,K,T}
Base.haskey(vec.records, id)
end


function Base.Iterators.filter(f::Function, vec::MembersVector{I,K,T}) where {I,K,T}
return Base.Iterators.filter(
e -> f(vec.elements[e[1]]) && e[2] != zero(T), vec.records
)
end


Base.iterate(d::MembersVector) = iterate(d.records)
Base.iterate(d::MembersVector, state) = iterate(d.records, state)

function Base.show(io::IO, vec::MembersVector{I,J,K}) where {I,J,K}
print(io, "[")
for (id, val) in vec
print(io, " ", id, " => " , val, " ")
end
print(io, "]")
end

## New matrix
struct MembersMatrix{I,J,T}
cols_major::DynamicSparseArrays.MappedPackedCSC{I,J,T}
rows_major::DynamicSparseArrays.MappedPackedCSC{J,I,T}
Expand All @@ -89,4 +19,4 @@ end
function Base.getindex(m::MembersMatrix, row_id, col_id)
# TODO : check number of rows & cols
return m.cols_major[row_id, col_id]
end
end
2 changes: 1 addition & 1 deletion src/Containers/nestedenum.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
abstract type NestedEnum end

function <=(a::T, b::T) where {T <: NestedEnum}
function Base.:(<=)(a::T, b::T) where {T <: NestedEnum}
return a.value % b.value == 0
end

Expand Down
4 changes: 2 additions & 2 deletions src/MathProg/decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function create_side_vars_constrs!(
coefmatrix = getcoefmatrix(masterform)
for (spuid, spform) in get_dw_pricing_sps(masterform.parent_formulation)
ann = get(annotations, spform)
setupvars = filter(var -> getduty(getid(var)) == DwSpSetupVar, getvars(spform))
setupvars = filter(v -> getduty(v.first) == DwSpSetupVar, getvars(spform))
@assert length(setupvars) == 1
setupvar = collect(values(setupvars))[1]
clonevar!(origform, masterform, spform, setupvar, MasterRepPricingSetupVar, is_explicit = false)
Expand Down Expand Up @@ -271,7 +271,7 @@ function create_side_vars_constrs!(

for (spuid, spform) in get_benders_sep_sps(masterform.parent_formulation)
nu_var = collect(values(filter(
var -> getduty(getid(var)) == BendSpSlackSecondStageCostVar,
v -> getduty(v.first) == BendSpSlackSecondStageCostVar,
getvars(spform)
)))[1]

Expand Down
9 changes: 5 additions & 4 deletions src/MathProg/formulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ _reset_buffer!(form::Formulation) = form.buffer = FormulationBuffer()
Buffers the matrix modification in `form.buffer` to be sent to `form.optimizer` right before next call to optimize!.
"""
set_matrix_coeff!(
form::Formulation, varid::Id{Variable}, constrid::Id{Constraint}, new_coeff::Float64
form::Formulation, varid::VarId, constrid::ConstrId, new_coeff::Float64
) = set_matrix_coeff!(form.buffer, varid, constrid, new_coeff)

"Creates a `Variable` according to the parameters passed and adds it to `Formulation` `form`."
Expand Down Expand Up @@ -246,11 +246,12 @@ function setcol_from_sp_primalsol!(

master_coef_matrix = getcoefmatrix(masterform)
sp_sol = getprimalsolmatrix(spform)[:,sol_id]
members = MembersVector{Float64}(getconstrs(masterform))
members = ConstrMembership()

for (sp_var_id, sp_var_val) in sp_sol
for (master_constrid, sp_var_coef) in master_coef_matrix[:,sp_var_id]
members[master_constrid] += sp_var_val * sp_var_coef
val = get(members, master_constrid, 0.0)
members[master_constrid] = val + sp_var_val * sp_var_coef
end
end

Expand Down Expand Up @@ -492,7 +493,7 @@ end

function _show_obj_fun(io::IO, form::Formulation)
print(io, getobjsense(form), " ")
vars = filter(var -> iscurexplicit(form, var), getvars(form))
vars = filter(v -> iscurexplicit(form, v.first), getvars(form))
ids = sort!(collect(keys(vars)), by = getsortuid)
for id in ids
name = getname(form, vars[id])
Expand Down
12 changes: 6 additions & 6 deletions src/MathProg/manager.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const DynSparseVector{I} = DynamicSparseArrays.PackedMemoryArray{I, Float64}

const VarDict = ElemDict{Id{Variable}, Variable}
const VarDataDict = Dict{Id{Variable}, VarData}
const ConstrDict = ElemDict{Id{Constraint}, Constraint}
const ConstrDataDict = Dict{Id{Constraint}, ConstrData}
const VarMembership = MembersVector{VarId,Variable,Float64}
const ConstrMembership = MembersVector{ConstrId,Constraint,Float64}
const VarDict = Dict{VarId, Variable}
const VarDataDict = Dict{VarId, VarData}
const ConstrDict = Dict{ConstrId, Constraint}
const ConstrDataDict = Dict{ConstrId, ConstrData}
const VarMembership = Dict{VarId,Float64}
const ConstrMembership = Dict{ConstrId,Float64}
const ConstrConstrMatrix = MembersMatrix{ConstrId,ConstrId,Float64}
const ConstrVarMatrix = MembersMatrix{ConstrId,VarId,Float64}
const VarVarMatrix = MembersMatrix{VarId,VarId,Float64}
Expand Down
2 changes: 1 addition & 1 deletion src/MathProg/optimizerwrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function retrieve_result(form::Formulation, optimizer::MoiOptimizer)
fill_primal_result!(form, optimizer, result)
fill_dual_result!(
optimizer, result, filter(
constr -> iscuractive(form, constr) && iscurexplicit(form, constr),
c -> iscuractive(form, c.first) && iscurexplicit(form, c.first),
getconstrs(form)
)
)
Expand Down
4 changes: 2 additions & 2 deletions test/pricing_callback_tests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function mycallback(form::CL.Formulation)
vars = [v for (id,v) in Iterators.filter(
v -> (CL.iscuractive(form,v) && CL.iscurexplicit(form,v)),
v -> (CL.iscuractive(form,v.first) && CL.iscurexplicit(form,v.first)),
CL.getvars(form)
)]
constr = [c for (id,c) in Iterators.filter(
c -> (CL.iscuractive(form,c) && CL.iscurexplicit(form,c)),
c -> (CL.iscuractive(form,c.first) && CL.iscurexplicit(form,c.first)),
CL.getconstrs(form))][1]
matrix = CL.getcoefmatrix(form)
m = JuMP.Model(GLPK.Optimizer)
Expand Down