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

clean setvarconstr! #331

Merged
merged 9 commits into from
May 15, 2020
Merged
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
9 changes: 7 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ makedocs(
),
pages = Any[
"Introduction" => "index.md",
"Getting started" => "user/start.md",
"Callbacks" => "user/callbacks.md"
"Manual" => Any[
"Getting started" => "user/start.md",
"Callbacks" => "user/callbacks.md"
],
"Reference" => Any[
"Formulation" => "dev/formulation.md"
]
]
)

Expand Down
43 changes: 43 additions & 0 deletions docs/src/dev/formulation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
```@meta
CurrentModule = Coluna.MathProg
DocTestSetup = quote
using Coluna.MathProg
end
```

# Formulation

## Attributes of variables and constraints

*Performance note* : use a variable or a constraint rather than its id.

```@docs
getperencost
getcurcost
setcurcost!
getperenlb
getcurlb
setcurlb!
getperenub
getcurub
setcurub!
getperenrhs
getcurrhs
setcurrhs!
getperenkind
getcurkind
setcurkind!
getperensense
getcursense
setcursense!
getperenincval
getcurincval
setcurincval!
isperenactive
iscuractive
activate!
deactivate!
isexplicit
getname
```

8 changes: 4 additions & 4 deletions src/Algorithm/benders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function update_benders_sp_slackvar_cost_for_ph2!(spform::Formulation)
setcurcost!(spform, var, 0.0)
setcurub!(spform, var, 0.0)
else
setcurcost!(spform, var, getperenecost(spform, var))
setcurcost!(spform, var, getperencost(spform, var))
end
end
return
Expand All @@ -96,7 +96,7 @@ end
function update_benders_sp_slackvar_cost_for_hyb_ph!(spform::Formulation)
for (varid, var) in getvars(spform)
iscuractive(spform, varid) || continue
setcurcost!(spform, var, getperenecost(spform, var))
setcurcost!(spform, var, getperencost(spform, var))
# TODO if previous phase is a pure phase 2, reset current ub
end
return
Expand All @@ -120,7 +120,7 @@ function update_benders_sp_problem!(
iscuractive(spform, varid) || continue
getduty(varid) <= BendSpSlackFirstStageVar || continue
haskey(master_primal_sol, varid) || continue
setcurub!(spform, var, getpereneub(spform, var) - master_primal_sol[varid])
setcurub!(spform, var, getperenub(spform, var) - master_primal_sol[varid])
end

if algo.option_use_reduced_cost
Expand Down Expand Up @@ -574,7 +574,7 @@ function bend_cutting_plane_main_loop!(
# TODO : replace with isinteger(master_primal_sol) # ISSUE 179
sol_integer = true
for (varid, val) in master_primal_sol
if getperenekind(masterform, varid) != Continuous
if getperenkind(masterform, varid) != Continuous
round_down_val = Float64(val, RoundDown)
round_up_val = Float64(val, RoundUp)

Expand Down
2 changes: 1 addition & 1 deletion src/Algorithm/branching/varbranching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function run!(
local_id = input.local_id
for (var_id, val) in input.solution
# Do not consider continuous variables as branching candidates
getperenekind(master, var_id) == Continuous && continue
getperenkind(master, var_id) == Continuous && continue
if !isinteger(val)
#description string is just the variable name
candidate = VarBranchingCandidate(getname(master, var_id), var_id)
Expand Down
18 changes: 8 additions & 10 deletions src/Algorithm/colgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ end
struct ReducedCostsVector
length::Int
varids::Vector{VarId}
perenecosts::Vector{Float64}
perencosts::Vector{Float64}
form::Vector{Formulation}
end

function ReducedCostsVector(varids::Vector{VarId}, form::Vector{Formulation})
len = length(varids)
perenecosts = zeros(Float64, len)
perencosts = zeros(Float64, len)
p = sortperm(varids)
permute!(varids, p)
permute!(form, p)
for i in 1:len
perenecosts[i] = getcurcost(getmaster(form[i]), varids[i])
perencosts[i] = getcurcost(getmaster(form[i]), varids[i])
end
return ReducedCostsVector(len, varids, perenecosts, form)
return ReducedCostsVector(len, varids, perencosts, form)
end

getoptstate(data::ColGenRuntimeData) = data.optstate
Expand Down Expand Up @@ -121,7 +121,7 @@ function set_ph2!(master::Formulation, data::ColGenRuntimeData)
if isanArtificialDuty(getduty(varid))
deactivate!(master, varid)
else
setcurcost!(master, varid, getperenecost(master, var))
setcurcost!(master, varid, getperencost(master, var))
end
end
set_lp_dual_bound!(data.optstate, DualBound(master))
Expand All @@ -134,7 +134,7 @@ function set_ph3!(master::Formulation, data::ColGenRuntimeData)
if isanArtificialDuty(getduty(varid))
activate!(master, varid)
else
setcurcost!(master, varid, getperenecost(master, var))
setcurcost!(master, varid, getperencost(master, var))
end
end
data.phase = 3
Expand All @@ -158,10 +158,8 @@ function insert_cols_in_master!(
ub = Inf
kind = Continuous
duty = MasterCol
sense = Positive
mc = setcol_from_sp_primalsol!(
masterform, spform, sol_id, name, duty; lb = lb, ub = ub,
kind = kind, sense = sense
masterform, spform, sol_id, name, duty; lb = lb, ub = ub, kind = kind
)
if data.phase == 1
setcurcost!(masterform, mc, 0.0)
Expand Down Expand Up @@ -274,7 +272,7 @@ end


function updatereducedcosts!(reform::Reformulation, redcostsvec::ReducedCostsVector, dualsol::DualSolution)
redcosts = deepcopy(redcostsvec.perenecosts)
redcosts = deepcopy(redcostsvec.perencosts)
master = getmaster(reform)
sign = getobjsense(master) == MinSense ? -1 : 1
matrix = getcoefmatrix(master)
Expand Down
20 changes: 10 additions & 10 deletions src/Algorithm/formstorages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function MasterBranchConstrsStorageState(form::Formulation, storage::EmptyStorag
state = MasterBranchConstrsStorageState(Dict{ConstrId, ConstrState}())
for (id, constr) in getconstrs(form)
if getduty(id) <= AbstractMasterBranchingConstr &&
iscuractive(form, constr) && iscurexplicit(form, constr)
iscuractive(form, constr) && isexplicit(form, constr)

constrstate = ConstrState(getcurrhs(form, constr))
state.constrs[id] = constrstate
Expand All @@ -86,7 +86,7 @@ function restorefromstate!(
)
@logmsg LogLevel(-2) "Restoring branching constraints"
for (id, constr) in getconstrs(form)
if getduty(id) <= AbstractMasterBranchingConstr && iscurexplicit(form, constr)
if getduty(id) <= AbstractMasterBranchingConstr && isexplicit(form, constr)
@logmsg LogLevel(-4) "Checking " getname(form, constr)
if haskey(state.constrs, id)
if !iscuractive(form, constr)
Expand Down Expand Up @@ -131,7 +131,7 @@ function MasterColumnsState(form::Formulation, storage::EmptyStorage)
state = MasterColumnsState(Dict{VarId, ConstrState}())
for (id, var) in getvars(form)
if getduty(id) <= MasterCol &&
iscuractive(form, var) && iscurexplicit(form, var)
iscuractive(form, var) && isexplicit(form, var)

varstate = VarState(getcurcost(form, var), getcurlb(form, var), getcurub(form, var))
state.cols[id] = varstate
Expand All @@ -145,7 +145,7 @@ function restorefromstate!(
)
@logmsg LogLevel(-2) "Restoring master columns"
for (id, var) in getvars(form)
if getduty(id) <= MasterCol && iscurexplicit(form, var)
if getduty(id) <= MasterCol && isexplicit(form, var)
@logmsg LogLevel(-4) "Checking " getname(form, var)
if haskey(state.cols, id)
if !iscuractive(form, var)
Expand Down Expand Up @@ -190,7 +190,7 @@ function MasterCutsState(form::Formulation, storage::EmptyStorage)
state = BranchingConstrsState(Dict{ConstrId, ConstrState}())
for (id, constr) in getconstrs(form)
if getduty(id) <= AbstractMasterCutConstr &&
iscuractive(form, constr) && iscurexplicit(form, constr)
iscuractive(form, constr) && isexplicit(form, constr)

constrstate = ConstrState(getcurrhs(form, constr))
state.cuts[id] = constrstate
Expand All @@ -204,7 +204,7 @@ function restorefromstate!(
)
@logmsg LogLevel(-2) "Storing master cuts"
for (id, constr) in getconstrs(form)
if getduty(id) <= AbstractMasterCutConstr && iscurexplicit(form, constr)
if getduty(id) <= AbstractMasterCutConstr && isexplicit(form, constr)
@logmsg LogLevel(-4) "Checking " getname(form, constr)
if haskey(state.cuts, id)
if !iscuractive(form, constr)
Expand Down Expand Up @@ -257,15 +257,15 @@ function StaticVarConstrStorageState(form::Formulation, storage::EmptyStorage)
for (id, constr) in getconstrs(form)
if !(getduty(id) <= AbstractMasterCutConstr) &&
!(getduty(id) <= AbstractMasterBranchingConstr) &&
iscuractive(form, constr) && iscurexplicit(form, constr)
iscuractive(form, constr) && isexplicit(form, constr)

constrstate = ConstrState(getcurrhs(form, constr))
state.constrs[id] = constrstate
end
end
for (id, var) in getvars(form)
if !(getduty(id) <= MasterCol) &&
iscuractive(form, var) && iscurexplicit(form, var)
iscuractive(form, var) && isexplicit(form, var)

varstate = VarState(getcurcost(form, var), getcurlb(form, var), getcurub(form, var))
state.vars[id] = varstate
Expand All @@ -280,7 +280,7 @@ function restorefromstate!(
@logmsg LogLevel(-2) "Restoring static vars and consts"
for (id, constr) in getconstrs(form)
if !(getduty(id) <= AbstractMasterCutConstr) &&
!(getduty(id) <= AbstractMasterBranchingConstr) && iscurexplicit(form, constr)
!(getduty(id) <= AbstractMasterBranchingConstr) && isexplicit(form, constr)
@logmsg LogLevel(-4) "Checking " getname(form, constr)
if haskey(state.constrs, id)
if !iscuractive(form, constr)
Expand All @@ -298,7 +298,7 @@ function restorefromstate!(
end
end
for (id, var) in getvars(form)
if !(getduty(id) <= MasterCol) && iscurexplicit(form, var)
if !(getduty(id) <= MasterCol) && isexplicit(form, var)
@logmsg LogLevel(-4) "Checking " getname(form, var)
if haskey(state.vars, id)
if !iscuractive(form, var)
Expand Down
10 changes: 5 additions & 5 deletions src/Algorithm/preprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function fix_local_partial_solution!(alg_data::PreprocessData)
for (varid, val) in sp_vars_vals
for (constrid, coef) in master_coef_matrix[:,varid]
iscuractive(master, constrid) || continue
iscurexplicit(master, constrid) || continue
isexplicit(master, constrid) || continue
setrhs!(master, constrid, getcurrhs(master, constrid) - val * coef)
push!(constrs_with_modified_rhs, getconstr(master, constrid))
end
Expand Down Expand Up @@ -239,7 +239,7 @@ function initconstraints!(
master_coef_matrix = getcoefmatrix(master)
for (constrid, constr) in getconstrs(master)
iscuractive(master, constrid) || continue
iscurexplicit(master, constrid) || continue
isexplicit(master, constrid) || continue
getduty(constrid) == MasterConvexityConstr || continue
initconstraint!(alg_data, constr, master)
push!(constrs_to_stack, (constr, master))
Expand All @@ -249,7 +249,7 @@ function initconstraints!(
for (spuid, spform) in get_dw_pricing_sps(alg_data.reformulation)
for (constrid, constr) in getconstrs(spform)
iscuractive(spform, constrid) || continue
iscurexplicit(spform, constrid) || continue
isexplicit(spform, constrid) || continue
initconstraint!(alg_data, constr, spform)
push!(constrs_to_stack, (constr, spform))
end
Expand Down Expand Up @@ -450,7 +450,7 @@ function update_lower_bound!(
coef_matrix = getcoefmatrix(form)
for (constrid, coef) in coef_matrix[:, varid]
iscuractive(form, constrid) || continue
iscurexplicit(form, constrid) || continue
isexplicit(form, constrid) || continue
status = false
if coef < 0
status = update_min_slack!(
Expand Down Expand Up @@ -521,7 +521,7 @@ function update_upper_bound!(
coef_matrix = getcoefmatrix(form)
for (constrid, coef) in coef_matrix[:, varid]
iscuractive(form, constrid) || continue
iscurexplicit(form, constrid) || continue
isexplicit(form, constrid) || continue
status = false
if coef > 0
status = update_min_slack!(
Expand Down
2 changes: 1 addition & 1 deletion src/MOIcallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function MOI.submit(

# setup variable
setup_var_id = [id for (id,v) in Iterators.filter(
v -> (iscuractive(form, v.first) && iscurexplicit(form, v.first) && getduty(v.first) <= DwSpSetupVar),
v -> (iscuractive(form, v.first) && isexplicit(form, v.first) && getduty(v.first) <= DwSpSetupVar),
getvars(form)
)][1]
push!(colunavarids, setup_var_id)
Expand Down
6 changes: 3 additions & 3 deletions src/MathProg/MOIinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function fill_primal_result!(form::Formulation, optimizer::MoiOptimizer,
solvars = Vector{VarId}()
solvals = Vector{Float64}()
for (id, var) in getvars(form)
iscuractive(form, id) && iscurexplicit(form, id) || continue
iscuractive(form, id) && isexplicit(form, id) || continue
moirec = getmoirecord(var)
moi_index = getindex(moirec)
kind = _getcolunakind(moirec)
Expand Down Expand Up @@ -226,7 +226,7 @@ function fill_dual_result!(form::Formulation, optimizer::MoiOptimizer,
# Getting dual bound is not stable in some solvers.
# Getting primal bound instead, which will work for lps
for (id, constr) in getconstrs(form)
iscuractive(form, id) && iscurexplicit(form, id) || continue
iscuractive(form, id) && isexplicit(form, id) || continue
moi_index = getindex(getmoirecord(constr))
val = MOI.get(inner, MOI.ConstraintDual(res_idx), moi_index)
val = round(val, digits = Coluna._params_.tol_digits)
Expand Down Expand Up @@ -262,7 +262,7 @@ function _getreducedcost(form::Formulation, optimizer::MoiOptimizer, var::Variab
"""
return nothing
end
if !iscuractive(form, var) || !iscurexplicit(form, var)
if !iscuractive(form, var) || !isexplicit(form, var)
varname = getname(form, var)
@warn """
Cannot retrieve reduced cost of variable $varname because the variable must be active and explicit.
Expand Down
13 changes: 6 additions & 7 deletions src/MathProg/MathProg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ include("vcids.jl")
include("variable.jl")
include("constraint.jl")
include("duties.jl")
include("varconstr.jl") # to rm
include("manager.jl")
include("bounds.jl")
include("solutions.jl")
include("buffer.jl")
include("formulation.jl")
include("new_varconstr.jl")
include("varconstr.jl")
include("optimizerwrappers.jl")
include("clone.jl")
include("reformulation.jl")
Expand Down Expand Up @@ -93,11 +92,11 @@ export isanArtificialDuty, isaStaticDuty, isaDynamicDuty, isanOriginalRepresenta

# Types and methods related to variables and constraints
export Variable, Constraint, VarId, ConstrId, VarMembership, ConstrMembership,
getperenecost, getcurcost, setcurcost!, getperenelb, getcurlb, setcurlb!,
getpereneub, getcurub, setcurub!, getperenerhs, getcurrhs, setcurrhs!, getperenesense,
getcursense, setcursense!, getperenekind, getcurkind, setcurkind!, getpereneincval,
getcurincval, setcurincval!, ispereneactive, iscuractive, activate!, deactivate!,
ispereneexplicit, iscurexplicit, setiscurexplicit!, getname, reset!, getreducedcost
getperencost, getcurcost, setcurcost!, getperenlb, getcurlb, setcurlb!,
getperenub, getcurub, setcurub!, getperenrhs, getcurrhs, setcurrhs!, getperensense,
getcursense, setcursense!, getperenkind, getcurkind, setcurkind!, getperenincval,
getcurincval, setcurincval!, isperenactive, iscuractive, activate!, deactivate!,
isexplicit, getname, reset!, getreducedcost

# Types & methods related to solutions & bounds
# Note : we should export only get methods for MoiResult (the solution is built in MathProg)
Expand Down
Loading