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] Basis record/restore #927

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions src/Algorithm/basic/solvelpform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function get_units_usage(
push!(units_usage, (form, MasterColumnsUnit, READ_ONLY))
push!(units_usage, (form, MasterBranchConstrsUnit, READ_ONLY))
push!(units_usage, (form, MasterCutsUnit, READ_ONLY))
push!(units_usage, (form, MasterBasisUnit, READ_ONLY))
end
return units_usage
end
Expand Down
1 change: 1 addition & 0 deletions src/Algorithm/colgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ function get_units_usage(algo::ColumnGeneration, reform::Reformulation)
units_usage = Tuple{AbstractModel,UnitType,UnitPermission}[]
master = getmaster(reform)
push!(units_usage, (master, MasterColumnsUnit, READ_AND_WRITE))
push!(units_usage, (master, MasterBasisUnit, READ_AND_WRITE))
#push!(units_usage, (master, PartialSolutionUnit, READ_ONLY))
if stabilization_is_used(algo)
#push!(units_usage, (master, ColGenStabilizationUnit, READ_AND_WRITE))
Expand Down
2 changes: 2 additions & 0 deletions src/Algorithm/conquer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ end

function run!(algo::ColCutGenConquer, env::Env, reform::Reformulation, input::AbstractConquerInput)
ctx = new_context(type_of_context(algo), algo, reform, input)
node = get_node(input)
restore_from_records!(get_units_to_restore(input), TreeSearch.get_records(node))
guimarqu marked this conversation as resolved.
Show resolved Hide resolved
return run_colcutgen_conquer!(ctx, env, reform, input)
end

Expand Down
36 changes: 36 additions & 0 deletions src/Algorithm/formstorages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,42 @@ function ClB.restore_from_record!(
end


####
####
struct MasterBasisUnit <: AbstractRecordUnit end

struct MasterBasisRecord <: AbstractRecord
basis::Union{Nothing,MathProg.Basis}
end

struct MasterBasisKey <: AbstractStorageUnitKey end

key_from_storage_unit_type(::Type{MasterBasisUnit}) = MasterBasisKey()
record_type_from_key(::MasterBasisKey) = MasterBasisRecord

ClB.storage_unit(::Type{MasterBasisUnit}, _) = MasterBasisUnit()

function ClB.record(::Type{MasterBasisRecord}, id::Int, form::Formulation, unit::MasterBasisUnit)
optimizer = getoptimizer(form, 1)
basis = MathProg.get_basis(form, optimizer)
return MasterBasisRecord(basis)
end

ClB.record_type(::Type{MasterBasisUnit}) = MasterBasisRecord
ClB.storage_unit_type(::Type{MasterBasisRecord}) = MasterBasisUnit

apply_set_basis!(form, optimizer::MoiOptimizer, basis) = MathProg.set_basis!(form, optimizer, basis)
apply_set_basis!(form, _, basis) = nothing

function ClB.restore_from_record!(
form::Formulation, ::MasterBasisUnit, state::MasterBasisRecord
)
optimizer = getoptimizer(form, 1)
if !isnothing(state.basis)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@artalvpes sync solver here

apply_set_basis!(form, optimizer, state.basis)
end
return
end

##### UNCOVERED CODE BELOW #####

Expand Down
54 changes: 54 additions & 0 deletions src/MathProg/MOIinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,60 @@ function get_dual_solutions(form::F, optimizer::MoiOptimizer) where {F <: Formul
return solutions
end


struct Basis
vars::Dict{VarId,MOI.BasisStatusCode}
constrs::Dict{ConstrId,MOI.BasisStatusCode}
end

function get_basis(form::F, optimizer::MoiOptimizer) where {F <: Formulation}
inner = getinner(optimizer)
nb_primal_sols = MOI.get(inner, MOI.ResultCount())
if nb_primal_sols <= 0 || MOI.get(inner, MOI.PrimalStatus(1)) != MOI.FEASIBLE_POINT
return nothing
end

vars = Dict{VarId,MOI.BasisStatusCode}()
for (varid, var) in getvars(form)
iscuractive(form, varid) && isexplicit(form, varid) || continue
moi_index = getindex(getmoirecord(var))
if moi_index.value != -1
vars[varid] = MOI.get(inner, MOI.VariableBasisStatus(), moi_index)
end
end

constrs = Dict{ConstrId,MOI.BasisStatusCode}()
for (constrid, constr) in getconstrs(form)
iscuractive(form, constrid) && isexplicit(form, constrid) || continue
moi_index = getindex(getmoirecord(constr))
if moi_index.value != -1
constrs[constrid] = MOI.get(inner, MOI.ConstraintBasisStatus(), moi_index)
end
end
return Basis(vars, constrs)
end

function set_basis!(form::F, optimizer::MoiOptimizer, basis::Basis) where {F <: Formulation}
inner = getinner(optimizer)
for (varid, var) in getvars(form)
iscuractive(form, varid) && isexplicit(form, varid) || continue
moi_index = getindex(getmoirecord(var))
var_basis = get(basis.vars, varid, nothing)
if !isnothing(var_basis)
MOI.set(inner, MOI.VariableBasisStatus(), moi_index, var_basis)
end
end
for (constrid, constr) in getconstrs(form)
iscuractive(form, constrid) && isexplicit(form, constrid) || continue
moi_index = getindex(getmoirecord(constr))
constr_basis = get(basis.constrs, constrid, nothing)
if !isnothing(constr_basis)
MOI.set(inner, MOI.ConstraintBasisStatus(), moi_index, constr_basis)
end
end
return
end

function get_dual_infeasibility_certificate(form::F, optimizer::MoiOptimizer) where {F <: Formulation}
inner = getinner(optimizer)
nb_certificates = MOI.get(inner, MOI.ResultCount())
Expand Down