Skip to content

Commit

Permalink
Bijection StorageUnit -> Record (#518)
Browse files Browse the repository at this point in the history
* Bijection StorageUnit -> Record

* address Ruslan's comments
  • Loading branch information
guimarqu authored Apr 27, 2021
1 parent 0797a5c commit b5795cb
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 129 deletions.
2 changes: 1 addition & 1 deletion src/Algorithm/Algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export AbstractOptimizationAlgorithm, TreeSearchAlgorithm, ColCutGenConquer, Col
EmptyInput

# Units
export PartialSolutionUnitPair, PreprocessingUnitPair
export PartialSolutionUnit, PreprocessingUnit

# Unit functions
export getstorageunit, add_to_solution!, add_to_localpartialsol!
Expand Down
2 changes: 1 addition & 1 deletion src/Algorithm/basic/cutcallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ end

function get_units_usage(algo::CutCallbacks, form::Formulation{Duty}
) where {Duty<:MathProg.AbstractFormDuty}
return [(form, MasterCutsUnitPair, READ_AND_WRITE)]
return [(form, MasterCutsUnit, READ_AND_WRITE)]
end

function run!(algo::CutCallbacks, env::Env, form::Formulation, input::CutCallbacksInput)
Expand Down
14 changes: 7 additions & 7 deletions src/Algorithm/basic/solveipform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ function get_units_usage(
# (deactivating artificial vars and enforcing integrality)
# are reverted before the end of the algorithm,
# so the state of the formulation remains the same
units_usage = Tuple{AbstractModel, UnitTypePair, UnitAccessMode}[]
push!(units_usage, (form, StaticVarConstrUnitPair, READ_ONLY))
units_usage = Tuple{AbstractModel, UnitType, UnitAccessMode}[]
push!(units_usage, (form, StaticVarConstrUnit, READ_ONLY))
if Duty <: MathProg.AbstractMasterDuty
push!(units_usage, (form, PartialSolutionUnitPair, READ_ONLY))
push!(units_usage, (form, MasterColumnsUnitPair, READ_ONLY))
push!(units_usage, (form, MasterBranchConstrsUnitPair, READ_ONLY))
push!(units_usage, (form, MasterCutsUnitPair, READ_ONLY))
push!(units_usage, (form, PartialSolutionUnit, READ_ONLY))
push!(units_usage, (form, MasterColumnsUnit, READ_ONLY))
push!(units_usage, (form, MasterBranchConstrsUnit, READ_ONLY))
push!(units_usage, (form, MasterCutsUnit, READ_ONLY))
end
return units_usage
end
Expand Down Expand Up @@ -68,7 +68,7 @@ function run!(algo::SolveIpForm, env::Env, form::Formulation, input::Optimizatio
partial_sol = nothing
partial_sol_value = 0.0
if isa(form, Formulation{MathProg.DwMaster})
partsolunit = getstorageunit(form, PartialSolutionUnitPair)
partsolunit = getstorageunit(form, PartialSolutionUnit)
partial_sol = get_primal_solution(partsolunit, form)
partial_sol_value = getvalue(partial_sol)
end
Expand Down
14 changes: 7 additions & 7 deletions src/Algorithm/basic/solvelpform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ function get_units_usage(
# we use units in the read only mode, as relaxing integrality
# is reverted before the end of the algorithm,
# so the state of the formulation remains the same
units_usage = Tuple{AbstractModel, UnitTypePair, UnitAccessMode}[]
push!(units_usage, (form, StaticVarConstrUnitPair, READ_ONLY))
units_usage = Tuple{AbstractModel, UnitType, UnitAccessMode}[]
push!(units_usage, (form, StaticVarConstrUnit, READ_ONLY))
if Duty <: MathProg.AbstractMasterDuty
push!(units_usage, (form, MasterColumnsUnitPair, READ_ONLY))
push!(units_usage, (form, MasterBranchConstrsUnitPair, READ_ONLY))
push!(units_usage, (form, MasterCutsUnitPair, READ_ONLY))
push!(units_usage, (form, MasterColumnsUnit, READ_ONLY))
push!(units_usage, (form, MasterBranchConstrsUnit, READ_ONLY))
push!(units_usage, (form, MasterCutsUnit, READ_ONLY))
end
if algo.consider_partial_solution
push!(units_usage, (form, PartialSolutionUnitPair, READ_ONLY))
push!(units_usage, (form, PartialSolutionUnit, READ_ONLY))
end
return units_usage
end
Expand Down Expand Up @@ -63,7 +63,7 @@ function run!(algo::SolveLpForm, env::Env, form::Formulation, input::Optimizatio
partial_sol = nothing
partial_sol_val = 0.0
if algo.consider_partial_solution
partsolunit = getstorageunit(form, PartialSolutionUnitPair)
partsolunit = getstorageunit(form, PartialSolutionUnit)
partial_sol = get_primal_solution(partsolunit, form)
partial_sol_val = getvalue(partial_sol)
end
Expand Down
12 changes: 6 additions & 6 deletions src/Algorithm/benders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ end
# to solve the subproblems

function get_units_usage(algo::BendersCutGeneration, reform::Reformulation)
units_usage = Tuple{AbstractModel, UnitTypePair, UnitAccessMode}[]
units_usage = Tuple{AbstractModel, UnitType, UnitAccessMode}[]
master = getmaster(reform)
push!(units_usage, (master, MasterCutsUnitPair, READ_AND_WRITE))
push!(units_usage, (master, MasterCutsUnit, READ_AND_WRITE))

# TO DO : everything else should be communicated by the child algorithms
push!(units_usage, (master, StaticVarConstrUnitPair, READ_ONLY))
push!(units_usage, (master, MasterBranchConstrsUnitPair, READ_ONLY))
push!(units_usage, (master, MasterColumnsUnitPair, READ_ONLY))
push!(units_usage, (master, StaticVarConstrUnit, READ_ONLY))
push!(units_usage, (master, MasterBranchConstrsUnit, READ_ONLY))
push!(units_usage, (master, MasterColumnsUnit, READ_ONLY))
for (id, spform) in get_benders_sep_sps(reform)
push!(units_usage, (spform, StaticVarConstrUnitPair, READ_ONLY))
push!(units_usage, (spform, StaticVarConstrUnit, READ_ONLY))
end
return units_usage
end
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/branching/varbranching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function generate_children(
)

units_to_restore = UnitsUsageDict(
(master, MasterBranchConstrsUnitPair) => READ_AND_WRITE
(master, MasterBranchConstrsUnit) => READ_AND_WRITE
#(master, BasisUnit) => READ_AND_WRITE) # not yet implemented
)

Expand Down Expand Up @@ -72,7 +72,7 @@ end
# VarBranchingRule does not have child algorithms

function get_units_usage(algo::VarBranchingRule, reform::Reformulation)
return [(getmaster(reform), MasterBranchConstrsUnitPair, READ_AND_WRITE)]
return [(getmaster(reform), MasterBranchConstrsUnit, READ_AND_WRITE)]
end

function run!(
Expand Down
12 changes: 6 additions & 6 deletions src/Algorithm/colgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ function get_child_algorithms(algo::ColumnGeneration, reform::Reformulation)
end

function get_units_usage(algo::ColumnGeneration, reform::Reformulation)
units_usage = Tuple{AbstractModel,UnitTypePair,UnitAccessMode}[]
units_usage = Tuple{AbstractModel,UnitType,UnitAccessMode}[]
master = getmaster(reform)
push!(units_usage, (master, MasterColumnsUnitPair, READ_AND_WRITE))
push!(units_usage, (master, PartialSolutionUnitPair, READ_ONLY))
push!(units_usage, (master, MasterColumnsUnit, READ_AND_WRITE))
push!(units_usage, (master, PartialSolutionUnit, READ_ONLY))
if stabilization_is_used(algo)
push!(units_usage, (master, ColGenStabilizationUnitPair, READ_AND_WRITE))
push!(units_usage, (master, ColGenStabilizationUnit, READ_AND_WRITE))
end
return units_usage
end
Expand Down Expand Up @@ -602,10 +602,10 @@ function cg_main_loop!(
iteration = 0
essential_cuts_separated = false

stabunit = (stabilization_is_used(algo) ? getstorageunit(masterform, ColGenStabilizationUnitPair)
stabunit = (stabilization_is_used(algo) ? getstorageunit(masterform, ColGenStabilizationUnit)
: ColGenStabilizationUnit(masterform) )

partsolunit = getstorageunit(masterform, PartialSolutionUnitPair)
partsolunit = getstorageunit(masterform, PartialSolutionUnit)
partial_solution = get_primal_solution(partsolunit, masterform)

init_stab_before_colgen_loop!(stabunit)
Expand Down
2 changes: 1 addition & 1 deletion src/Algorithm/colgenstabilization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ColunaBase.restore_from_record!(
return
end

const ColGenStabilizationUnitPair = (ColGenStabilizationUnit => ColGenStabRecord)
ColunaBase.record_type(::Type{ColGenStabilizationUnit}) = ColGenStabRecord

function init_stab_before_colgen_loop!(unit::ColGenStabilizationUnit)
unit.stabcenter = unit.basestabcenter
Expand Down
18 changes: 9 additions & 9 deletions src/Algorithm/data.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

function ColunaBase.getstorageunit(form::AbstractModel, pair)
storagecont = get(form.storagedict, pair, nothing)
storagecont = get(form.storage.units, pair, nothing)
storagecont === nothing && error("No storage unit for pair $pair in $(typeof(form)) with id $(getuid(form)).")
return storagecont.storage_unit
end

function store_records!(form::Formulation, records::RecordsVector)
storagedict = form.storagedict
for (FullType, storagecont) in storagedict
storagedict = form.storage.units
for (_, storagecont) in storagedict
recordid = store_record!(storagecont)
push!(records, storagecont => recordid)
end
return
end

function store_records!(reform::Reformulation, records::RecordsVector)
storagedict = reform.storagedict
for (FullType, storagecont) in storagedict
storagedict = reform.storage.units
for (_, storagecont) in storagedict
recordid = store_record!(storagecont)
push!(records, (storagecont, recordid))
end
Expand All @@ -39,15 +39,15 @@ function store_records!(reform::Reformulation)
end

function ColunaBase.check_records_participation(form::Formulation)
storagedict = form.storagedict
for (FullType, storagecont) in storagedict
storagedict = form.storage.units
for (_, storagecont) in storagedict
check_records_participation(storagecont)
end
end

function ColunaBase.check_records_participation(reform::Reformulation)
storagedict = reform.storagedict
for (FullType, storagecont) in storagedict
storagedict = reform.storage.units
for (_, storagecont) in storagedict
check_records_participation(storagecont)
end
check_records_participation(getmaster(reform))
Expand Down
Loading

0 comments on commit b5795cb

Please sign in to comment.