Skip to content

Commit

Permalink
StorageContainer -> Storage & fix arg order for restore_from_records (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarqu authored Apr 15, 2021
1 parent 7664620 commit a4950f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Algorithm/branching/varbranching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function generate_children(
)

#adding the first branching constraints
restore_from_records!(copy_records(parent.recordids), units_to_restore)
restore_from_records!(units_to_restore, copy_records(parent.recordids))
TO.@timeit Coluna._to "Add branching constraint" begin
setconstr!(
master, string(
Expand All @@ -41,7 +41,7 @@ function generate_children(
child1 = Node(master, parent, child1description, store_records!(data))

#adding the second branching constraints
restore_from_records!(copy_records(parent.recordids), units_to_restore)
restore_from_records!(units_to_restore, copy_records(parent.recordids))
TO.@timeit Coluna._to "Add branching constraint" begin
setconstr!(
master, string(
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/conquer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

getnode(input::ConquerInput) = input.node

restore_from_records!(input::ConquerInput) = restore_from_records!(input.node.recordids, input.units_to_restore)
restore_from_records!(input::ConquerInput) = restore_from_records!(input.units_to_restore, input.node.recordids)

"""
AbstractConquerAlgorithm
Expand Down Expand Up @@ -237,7 +237,7 @@ function run!(algo::ColCutGenConquer, env::Env, data::ReformData, input::Conquer
end
end
end
ismanager(heur_algorithm) && restore_from_records!(recordids, input.units_to_restore)
ismanager(heur_algorithm) && restore_from_records!(input.units_to_restore, recordids)
end

if node_pruned
Expand Down
2 changes: 1 addition & 1 deletion src/Algorithm/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function initialize_storage_units!(data::AbstractData, algo::AbstractOptimizatio
for type_pair in type_pair_set
(StorageUnitType, RecordType) = type_pair
storagedict[type_pair] =
StorageContainer{ModelType, StorageUnitType, RecordType}(model)
Storage{ModelType, StorageUnitType, RecordType}(model)
end
end
end
44 changes: 22 additions & 22 deletions src/Algorithm/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ increaseparticipation!(essc::EmptyRecordContainer) = nothing
decreaseparticipation!(essc::EmptyRecordContainer) = nothing

"""
StorageContainer
Storage
This container keeps a storage unit and all records which have been
stored. It implements storing and restoring records of units in an
efficient way.
"""

mutable struct StorageContainer{M<:AbstractModel, S<:AbstractStorageUnit, SS<:AbstractRecord}
mutable struct Storage{M<:AbstractModel, S<:AbstractStorageUnit, SS<:AbstractRecord}
model::M
currecordcont::RecordContainer{SS}
maxrecordid::RecordId
Expand All @@ -189,25 +189,25 @@ mutable struct StorageContainer{M<:AbstractModel, S<:AbstractStorageUnit, SS<:Ab
recordsdict::Dict{RecordId, RecordContainer{SS}}
end

const RecordsVector = Vector{Pair{StorageContainer, RecordId}}
const RecordsVector = Vector{Pair{Storage, RecordId}}

const StorageDict = Dict{UnitTypePair, StorageContainer}
const StorageDict = Dict{UnitTypePair, Storage}

function StorageContainer{M,S,SS}(model::M) where {M,S,SS}
return StorageContainer{M,S,SS}(
function Storage{M,S,SS}(model::M) where {M,S,SS}
return Storage{M,S,SS}(
model, RecordContainer{SS}(1, 0), 1, S(model),
S => SS, Dict{RecordId, RecordContainer{SS}}()
)
end

getmodel(sc::StorageContainer) = sc.model
getcurrecordcont(sc::StorageContainer) = sc.currecordcont
getmaxrecordid(sc::StorageContainer) = sc.maxrecordid
getrecordsdict(sc::StorageContainer) = sc.recordsdict
getunit(sc::StorageContainer) = sc.storage_unit
gettypepair(sc::StorageContainer) = sc.typepair
getmodel(sc::Storage) = sc.model
getcurrecordcont(sc::Storage) = sc.currecordcont
getmaxrecordid(sc::Storage) = sc.maxrecordid
getrecordsdict(sc::Storage) = sc.recordsdict
getunit(sc::Storage) = sc.storage_unit
gettypepair(sc::Storage) = sc.typepair

function Base.show(io::IO, storagecont::StorageContainer)
function Base.show(io::IO, storagecont::Storage)
print(io, "unit (")
print(IOContext(io, :compact => true), getmodel(storagecont))
(StorageUnitType, RecordType) = gettypepair(storagecont)
Expand All @@ -216,7 +216,7 @@ function Base.show(io::IO, storagecont::StorageContainer)
end

function setcurstate!(
storagecont::StorageContainer{M,S,SS}, recordcont::RecordContainer{SS}
storagecont::Storage{M,S,SS}, recordcont::RecordContainer{SS}
) where {M,S,SS}
# we delete the current state container from the dictionary if necessary
currecordcont = getcurrecordcont(storagecont)
Expand All @@ -230,7 +230,7 @@ function setcurstate!(
end
end

function increaseparticipation!(storagecont::StorageContainer, recordid::RecordId)
function increaseparticipation!(storagecont::Storage, recordid::RecordId)
recordcont = getcurrecordcont(storagecont)
if (getrecordid(recordcont) == recordid)
increaseparticipation!(recordcont)
Expand All @@ -243,7 +243,7 @@ function increaseparticipation!(storagecont::StorageContainer, recordid::RecordI
end
end

function retrieve_from_recordsdict(storagecont::StorageContainer, recordid::RecordId)
function retrieve_from_recordsdict(storagecont::Storage, recordid::RecordId)
recordsdict = getrecordsdict(storagecont)
if !haskey(recordsdict, recordid)
error(string("State with id $recordid does not exist for ", storagecont))
Expand All @@ -257,7 +257,7 @@ function retrieve_from_recordsdict(storagecont::StorageContainer, recordid::Reco
end

function save_to_recordsdict!(
storagecont::StorageContainer{M,S,SS}, recordcont::RecordContainer{SS}
storagecont::Storage{M,S,SS}, recordcont::RecordContainer{SS}
) where {M,S,SS}
if getparticipation(recordcont) > 0 && stateisempty(recordcont)
state = SS(getmodel(storagecont), getunit(storagecont))
Expand All @@ -268,14 +268,14 @@ function save_to_recordsdict!(
end
end

function store_record!(storagecont::StorageContainer)::RecordId
function store_record!(storagecont::Storage)::RecordId
recordcont = getcurrecordcont(storagecont)
increaseparticipation!(recordcont)
return getrecordid(recordcont)
end

function restore_from_record!(
storagecont::StorageContainer{M,S,SS}, recordid::RecordId, mode::UnitAccessMode
storagecont::Storage{M,S,SS}, recordid::RecordId, mode::UnitAccessMode
) where {M,S,SS}
recordcont = getcurrecordcont(storagecont)
if getrecordid(recordcont) == recordid
Expand Down Expand Up @@ -319,14 +319,14 @@ end
# not used for the moment as it has impact on the code readability
# we keep this function for a while for the case when `restore_from_records!`
# happens to be a bottleneck
# function reserve_for_writing!(storagecont::StorageContainer{M,S,SS}) where {M,S,SS}
# function reserve_for_writing!(storagecont::Storage{M,S,SS}) where {M,S,SS}
# recordcont = getcurrecordcont(storagecont)
# save_to_recordsdict!(storagecont, recordcont)
# recordcont = RecordContainer{SS}(getmaxrecordid(storagecont) + 1, 0)
# setcurstate!(storagecont, recordcont)
# end

function restore_from_records!(records::RecordsVector, units_to_restore::UnitsUsageDict)
function restore_from_records!(units_to_restore::UnitsUsageDict, records::RecordsVector)
TO.@timeit Coluna._to "Restore/remove records" begin
for (storagecont, recordid) in records
mode = get(
Expand Down Expand Up @@ -358,7 +358,7 @@ function copy_records(records::RecordsVector)::RecordsVector
return recordscopy
end

function check_records_participation(storagecont::StorageContainer)
function check_records_participation(storagecont::Storage)
currecordcont = getcurrecordcont(storagecont)
if getparticipation(currecordcont) > 0
@warn string("Positive participation of state ", currecordcont)
Expand Down

0 comments on commit a4950f0

Please sign in to comment.