Skip to content

Commit

Permalink
getname (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarqu authored Jan 27, 2020
1 parent 8663cf9 commit 157a3c1
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 55 deletions.
7 changes: 4 additions & 3 deletions src/Algorithm/branching/varbranching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function generate_children(

@logmsg LogLevel(-1) string(
"Chosen branching variable : ",
getname(getvar(getmaster(reform), candidate.var_id)), ". With value ",
getname(getmaster(reform), candidate.var_id), ". With value ",
lhs, "."
)

Expand Down Expand Up @@ -51,13 +51,14 @@ function gen_candidates_for_orig_sol(
rule::VarBranchingRule, reform::Reformulation, sol::PrimalSolution{Sense},
max_nb_candidates::Int64, local_id::Int64, criterion::SelectionCriterion
) where Sense
master = getmaster(reform)
groups = Vector{BranchingGroup}()
for (var_id, val) in sol
# Do not consider continuous variables as branching candidates
getperenekind(getmaster(reform), var_id) == Continuous && continue
getperenekind(master, var_id) == Continuous && continue
if !isinteger(val)
#description string is just the variable name
candidate = VarBranchingCandidate(getname(getvar(reform.master, var_id)), var_id)
candidate = VarBranchingCandidate(getname(master, var_id), var_id)
local_id += 1
push!(groups, BranchingGroup(candidate, local_id, val))
end
Expand Down
12 changes: 6 additions & 6 deletions src/Algorithm/node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ getdepth(b::Branch) = b.depth

function show(io::IO, branch::Branch, form::Formulation)
for (id, coeff) in branch.var_coeffs
print(io, " + ", coeff, " ", getname(getelem(form, id)))
print(io, " + ", coeff, " ", getname(form, id))
end
if branch.sense == Greater
print(io, " >= ")
Expand Down Expand Up @@ -220,15 +220,15 @@ end
function apply_data!(form::Formulation, var::Variable, var_state::VarState)
# Bounds
if getcurlb(form, var) != var_state.lb || getcurub(form, var) != var_state.ub
@logmsg LogLevel(-2) string("Reseting bounds of variable ", getname(var))
@logmsg LogLevel(-2) string("Reseting bounds of variable ", getname(form, var))
setcurlb!(form, var, var_state.lb)
setcurub!(form, var, var_state.ub)
@logmsg LogLevel(-3) string("New lower bound is ", getcurlb(form, var))
@logmsg LogLevel(-3) string("New upper bound is ", getcurub(form, var))
end
# Cost
if getcurcost(form, var) != var_state.cost
@logmsg LogLevel(-2) string("Reseting cost of variable ", getname(var))
@logmsg LogLevel(-2) string("Reseting cost of variable ", getname(form, var))
setcurcost!(form, var, var_state.cost)
@logmsg LogLevel(-3) string("New cost is ", getcurcost(form, var))
end
Expand All @@ -238,7 +238,7 @@ end
function apply_data!(form::Formulation, constr::Constraint, constr_state::ConstrState)
# Rhs
if getcurrhs(form, constr) != constr_state.rhs
@logmsg LogLevel(-2) string("Reseting rhs of constraint ", getname(constr))
@logmsg LogLevel(-2) string("Reseting rhs of constraint ", getname(form, constr))
setcurrhs!(form, constr, constr_state.rhs)
@logmsg LogLevel(-3) string("New rhs is ", getcurrhs(form, constr))
end
Expand All @@ -255,9 +255,9 @@ end

function reset_var_constr!(form::Formulation, active_var_constrs, var_constrs_in_formulation)
for (id, vc) in var_constrs_in_formulation
@logmsg LogLevel(-4) "Checking " getname(vc)
@logmsg LogLevel(-4) "Checking " getname(form, vc)
# vc should NOT be active but is active in formulation
if !haskey(active_var_constrs, id) && getcurisactive(form,vc)
if !haskey(active_var_constrs, id) && getcurisactive(form, vc)
@logmsg LogLevel(-4) "Deactivating"
deactivate!(form, id)
continue
Expand Down
6 changes: 3 additions & 3 deletions src/Algorithm/preprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function update_lower_bound!(
end
end
alg_data.printing && println(
"updating lb of var ", getname(var), " from ", cur_lb, " to ",
"updating lb of var ", getname(form, var), " from ", cur_lb, " to ",
new_lb, " duty ", getduty(var)
)
setcurlb!(form, var, new_lb)
Expand Down Expand Up @@ -480,7 +480,7 @@ function update_upper_bound!(
end
if alg_data.printing
println(
"updating ub of var ", getname(var), " from ", cur_ub,
"updating ub of var ", getname(form, var), " from ", cur_ub,
" to ", new_ub, " duty ", getduty(var)
)
end
Expand Down Expand Up @@ -605,7 +605,7 @@ function propagation!(alg_data::PreprocessData)
alg_data.constr_in_stack[getid(constr)] = false

if alg_data.printing
println("constr ", getname(constr), " ", typeof(constr), " popped")
println("constr ", getname(form, constr), " ", typeof(constr), " popped")
println(
"rhs ", getcurrhs(form, constr), " max: ",
alg_data.cur_max_slack[getid(constr)], " min: ",
Expand Down
8 changes: 4 additions & 4 deletions src/MathProg/MOIinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function add_to_optimizer!(form::Formulation, var::Variable)
if getcurkind(form, var) != Binary
enforce_bounds_in_optimizer!(form, var)
end
MOI.set(inner, MOI.VariableName(), moi_index, getname(var))
MOI.set(inner, MOI.VariableName(), moi_index, getname(form, var))
return
end

Expand All @@ -144,7 +144,7 @@ function add_to_optimizer!(
)
moirecord = getmoirecord(constr)
setindex!(moirecord, moi_constr)
MOI.set(inner, MOI.ConstraintName(), moi_constr, getname(constr))
MOI.set(inner, MOI.ConstraintName(), moi_constr, getname(form, constr))
return
end

Expand Down Expand Up @@ -195,7 +195,7 @@ function fill_primal_result!(optimizer::MoiOptimizer,
moi_index = getindex(getmoirecord(var))
val = MOI.get(inner, MOI.VariablePrimal(res_idx), moi_index)
if val > 0.000001 || val < - 0.000001 # todo use a tolerance
@logmsg LogLevel(-4) string("Var ", getname(var), " = ", val)
@logmsg LogLevel(-4) string("Var ", var.name , " = ", val)
sol[id] = val
end
end
Expand Down Expand Up @@ -225,7 +225,7 @@ function fill_dual_result!(optimizer::MoiOptimizer,
moi_index = getindex(getmoirecord(constr))
val = MOI.get(inner, MOI.ConstraintDual(res_idx), moi_index)
if val > 0.000001 || val < - 0.000001 # todo use a tolerance
@logmsg LogLevel(-4) string("Constr ", getname(constr), " = ", val)
@logmsg LogLevel(-4) string("Constr ", constr.name, " = ", val)
sol[id] = val
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/MathProg/MathProg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export no_optimizer_builder, set_original_formulation!, create_origvars!,
get_lp_primal_bound, update!, get_lp_primal_sol,
get_benders_sep_sp, convert_status, getduty, getbestdualsol, update_lp_dual_sol!,
projection_is_possible, proj_cols_on_rep, get_lp_dual_bound,
getname, computereducedrhs,
computereducedrhs,
unsafe_getbestprimalsol, getcost,
getconstrcounter, setprimaldualbendspsol!,
set_lp_primal_bound!, _active_, update_ip_primal_bound!, getprimaldwspsolmatrix, _active_explicit_,
Expand Down Expand Up @@ -116,6 +116,7 @@ export getperenecost,
getpereneisexplicit,
getcurisexplicit,
setcurisexplicit!,
getname,
reset!

# Translation methods
Expand Down
4 changes: 2 additions & 2 deletions src/MathProg/clone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function clonevar!(originform::Formulation,
assignedform::Formulation,
var::Variable,
duty::Duty{Variable};
name::String = getname(var),
name::String = getname(originform, var),
cost::Float64 = getperenecost(originform, var),
lb::Float64 = getperenelb(originform, var),
ub::Float64 = getpereneub(originform, var),
Expand All @@ -26,7 +26,7 @@ function cloneconstr!(originform::Formulation,
assignedform::Formulation,
constr::Constraint,
duty::Duty{Constraint};
name::String = getname(constr),
name::String = getname(originform, constr),
rhs::Float64 = getperenerhs(originform, constr),
kind::ConstrKind = getperenekind(originform, constr),
sense::ConstrSense = getperenesense(originform, constr),
Expand Down
6 changes: 3 additions & 3 deletions src/MathProg/decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function create_local_art_vars!(masterform::Formulation)
)
for (constr_id, constr) in getconstrs(masterform)
var = setvar!(
masterform, string("local_art_of_", getname(constr)),
masterform, string("local_art_of_", getname(masterform, constr)),
MasterArtVar;
cost = (getobjsense(masterform) == MinSense ? 10000.0 : -10000.0),
lb = 0.0, ub = Inf, kind = Continuous, sense = Positive
Expand Down Expand Up @@ -287,7 +287,7 @@ function create_side_vars_constrs!(
getvars(spform)
)))[1]

name = "η[$(split(getname(nu_var), "[")[end])"
name = "η[$(split(getname(spform, nu_var), "[")[end])"
setvar!(
masterform, name, MasterBendSecondStageCostVar; cost = 1.0,
lb = getperenelb(spform, nu_var),
Expand Down Expand Up @@ -320,7 +320,7 @@ function instantiate_orig_vars!(
for (id, var) in vars
duty, explicit = _dutyexpofbendmastvar(var, annotations, origform)
if duty == MasterBendFirstStageVar
name = "μ[$(split(getname(var), "[")[end])"
name = "μ[$(split(getname(origform, var), "[")[end])"
mu = setvar!(
spform,
name,
Expand Down
26 changes: 13 additions & 13 deletions src/MathProg/formulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function enforce_integrality!(form::Formulation)
getcurkind(form, v) == Integ && continue
getcurkind(form, v) == Binary && continue
if (getduty(v) == MasterCol || getperenekind(form, v) != Continuous)
@logmsg LogLevel(-3) string("Setting kind of var ", getname(v), " to Integer")
@logmsg LogLevel(-3) string("Setting kind of var ", getname(form, v), " to Integer")
setcurkind!(form, v, Integ)
end
end
Expand All @@ -484,7 +484,7 @@ function relax_integrality!(form::Formulation)
getvars(form)
)
getcurkind(form, v) == Continuous && continue
@logmsg LogLevel(-3) string("Setting kind of var ", getname(v), " to continuous")
@logmsg LogLevel(-3) string("Setting kind of var ", getname(form, v), " to continuous")
setcurkind!(form, v, Continuous)
end
return
Expand Down Expand Up @@ -525,7 +525,7 @@ function _setmembers!(form::Formulation, constr::Constraint, members::VarMembers
# This adds the column to the convexity constraints automatically
# since the setup variable is in the sp solution and it has a
# a coefficient of 1.0 in the convexity constraints
@logmsg LogLevel(-2) string("Setting members of constraint ", getname(constr))
@logmsg LogLevel(-2) string("Setting members of constraint ", getname(form, constr))
coef_matrix = getcoefmatrix(form)
constr_id = getid(constr)
@logmsg LogLevel(-4) "Members are : ", members
Expand All @@ -534,14 +534,14 @@ function _setmembers!(form::Formulation, constr::Constraint, members::VarMembers
# Add coef for its own variables
var = getvar(form, var_id)
coef_matrix[constr_id, var_id] = var_coeff
@logmsg LogLevel(-4) string("Adding variable ", getname(var), " with coeff ", var_coeff)
@logmsg LogLevel(-4) string("Adding variable ", getname(form, var), " with coeff ", var_coeff)

if getduty(var) <= MasterRepPricingVar || getduty(var) <= MasterRepPricingSetupVar
# then for all columns having its own variables
assigned_form_uid = getassignedformuid(var_id)
spform = get_dw_pricing_sps(form.parent_formulation)[assigned_form_uid]
for (col_id, col_coeff) in getprimalsolmatrix(spform)[var_id,:]
@logmsg LogLevel(-4) string("Adding column ", getname(getvar(form, col_id)), " with coeff ", col_coeff * var_coeff)
@logmsg LogLevel(-4) string("Adding column ", getname(form, col_id), " with coeff ", col_coeff * var_coeff)
coef_matrix[constr_id, col_id] = col_coeff * var_coeff
end
end
Expand All @@ -555,7 +555,7 @@ function _setmembers!(form::Formulation, constr::Constraint, members::AbstractDi
# This adds the column to the convexity constraints automatically
# since the setup variable is in the sp solution and it has a
# a coefficient of 1.0 in the convexity constraints
@logmsg LogLevel(-2) string("Setting members of constraint ", getname(constr))
@logmsg LogLevel(-2) string("Setting members of constraint ", getname(form, constr))
coef_matrix = getcoefmatrix(form)
constr_id = getid(constr)
@logmsg LogLevel(-4) "Members are : ", members
Expand All @@ -564,14 +564,14 @@ function _setmembers!(form::Formulation, constr::Constraint, members::AbstractDi
# Add coef for its own variables
var = getvar(form, var_id)
coef_matrix[constr_id, var_id] = var_coeff
@logmsg LogLevel(-4) string("Adding variable ", getname(var), " with coeff ", var_coeff)
@logmsg LogLevel(-4) string("Adding variable ", getname(form, var), " with coeff ", var_coeff)

if getduty(var) <= MasterRepPricingVar || getduty(var) <= MasterRepPricingSetupVar
# then for all columns having its own variables
assigned_form_uid = getassignedformuid(var_id)
spform = get_dw_pricing_sps(form.parent_formulation)[assigned_form_uid]
for (col_id, col_coeff) in getprimalsolmatrix(spform)[var_id,:]
@logmsg LogLevel(-4) string("Adding column ", getname(getvar(form, col_id)), " with coeff ", col_coeff * var_coeff)
@logmsg LogLevel(-4) string("Adding column ", getname(form, col_id), " with coeff ", col_coeff * var_coeff)
coef_matrix[constr_id, col_id] = col_coeff * var_coeff
end
end
Expand All @@ -593,7 +593,7 @@ function remove_from_optimizer!(ids::Set{Id{T}}, form::Formulation) where {
T <: AbstractVarConstr}
for id in ids
vc = getelem(form, id)
@logmsg LogLevel(-3) string("Removing varconstr of name ", getname(vc))
@logmsg LogLevel(-3) string("Removing varconstr of name ", getname(form, vc))
remove_from_optimizer!(form.optimizer, vc)
end
return
Expand Down Expand Up @@ -679,7 +679,7 @@ function _show_obj_fun(io::IO, form::Formulation)
getvars(form))
ids = sort!(collect(keys(vars)), by = getsortuid)
for id in ids
name = getname(vars[id])
name = getname(form, vars[id])
cost = getcurcost(form, id)
op = (cost < 0.0) ? "-" : "+"
print(io, op, " ", abs(cost), " ", name, " ")
Expand All @@ -691,12 +691,12 @@ end
function _show_constraint(io::IO, form::Formulation, constr_id::ConstrId,
members::VarMembership)
constr = getconstr(form, constr_id)
print(io, getname(constr), " : ")
print(io, getname(form, constr), " : ")
ids = sort!(collect(keys(members)), by = getsortuid)
for id in ids
coeff = members[id]
var = getvar(form, id)
name = getname(var)
name = getname(form, var)
op = (coeff < 0.0) ? "-" : "+"
print(io, op, " ", abs(coeff), " ", name, " ")
end
Expand Down Expand Up @@ -728,7 +728,7 @@ function _show_constraints(io::IO , form::Formulation)
end

function _show_variable(io::IO, form::Formulation, var::Variable)
name = getname(var)
name = getname(form, var)
lb = getcurlb(form, var)
ub = getcurub(form, var)
t = getcurkind(form, var)
Expand Down
9 changes: 8 additions & 1 deletion src/MathProg/new_varconstr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,14 @@ function setcurisexplicit!(form::Formulation, constrid::ConstrId, is_explicit::B
end
setcurisexplicit!(form::Formulation, constr::Constraint, is_explicit::Bool) = setcurisexplicit!(form, getid(constr), is_explicit)


## name
"""
todo
"""
getname(form::Formulation, varid::VarId) = getvar(form, varid).name
getname(form::Formulation, var::Variable) = var.name
getname(form::Formulation, constrid::ConstrId) = getconstr(form, constrid).name
getname(form::Formulation, constr::Constraint) = constr.name

# Reset

Expand Down
18 changes: 9 additions & 9 deletions src/MathProg/optimizerwrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ function sync_solver!(optimizer::MoiOptimizer, f::Formulation)
# Add vars
for id in buffer.var_buffer.added
v = getvar(f, id)
@logmsg LogLevel(-4) string("Adding variable ", getname(v))
@logmsg LogLevel(-4) string("Adding variable ", getname(f, v))
add_to_optimizer!(f, v)
end

# Add constrs
for id in buffer.constr_buffer.added
c = getconstr(f, id)
@logmsg LogLevel(-4) string("Adding constraint ", getname(c))
@logmsg LogLevel(-4) string("Adding constraint ", getname(f, c))
add_to_optimizer!(
f, c, filter(
vc -> getcurisactive(f, vc) && getcurisexplicit(f, vc),
Expand All @@ -128,24 +128,24 @@ function sync_solver!(optimizer::MoiOptimizer, f::Formulation)
# Update variable bounds
for id in buffer.changed_bound
(id in buffer.var_buffer.added || id in buffer.var_buffer.removed) && continue
@logmsg LogLevel(-4) "Changing bounds of variable " getname(getvar(f,id))
@logmsg LogLevel(-5) string("New lower bound is ", getcurlb(getvar(f,id)))
@logmsg LogLevel(-5) string("New upper bound is ", getcurub(getvar(f,id)))
@logmsg LogLevel(-4) "Changing bounds of variable " getname(f,id)
@logmsg LogLevel(-5) string("New lower bound is ", getcurlb(f,id))
@logmsg LogLevel(-5) string("New upper bound is ", getcurub(f,id))
update_bounds_in_optimizer!(f, getvar(f, id))
end

# Update variable kind
for id in buffer.changed_kind
(id in buffer.var_buffer.added || id in buffer.var_buffer.removed) && continue
@logmsg LogLevel(-2) "Changing kind of variable " getname(getvar(f,id))
@logmsg LogLevel(-3) string("New kind is ", getcurkind(f, getvar(f,id)))
@logmsg LogLevel(-2) "Changing kind of variable " getname(f,id)
@logmsg LogLevel(-3) string("New kind is ", getcurkind(f,id))
enforce_kind_in_optimizer!(f, getvar(f,id))
end

# Update constraint rhs
for id in buffer.changed_rhs
(id in buffer.constr_buffer.added || id in buffer.constr_buffer.removed) && continue
@logmsg LogLevel(-2) "Changing rhs of constraint " getname(getconstr(f,id))
@logmsg LogLevel(-2) "Changing rhs of constraint " getname(f,id)
@logmsg LogLevel(-3) string("New rhs is ", getcurrhs(f,id))
update_constr_rhs_in_optimizer!(f, getconstr(f, id))
end
Expand All @@ -169,7 +169,7 @@ function sync_solver!(optimizer::MoiOptimizer, f::Formulation)
(c_id in buffer.constr_buffer.removed || v_id in buffer.var_buffer.removed) && continue
c = getconstr(f, c_id)
v = getvar(f, v_id)
@logmsg LogLevel(-2) string("Setting matrix coefficient: (", getname(c), ",", getname(v), ") = ", coeff)
@logmsg LogLevel(-2) string("Setting matrix coefficient: (", getname(f, c), ",", getname(f, v), ") = ", coeff)
# @logmsg LogLevel(1) string("Setting matrix coefficient: (", getname(c), ",", getname(v), ") = ", coeff)
update_constr_member_in_optimizer!(optimizer, c, v, coeff)
end
Expand Down
2 changes: 1 addition & 1 deletion src/MathProg/reformulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function find_owner_formulation(reform::Reformulation, vc::AbstractVarConstr)
for (formid, spform) in get_dw_pricing_sps(reform)
vc_belongs_to_formulation(spform, vc) && return spform
end
@error(string("VC ", getname(vc), " does not belong to any problem in reformulation"))
@error(string("VC ", vc.name, " does not belong to any problem in reformulation"))
end

function deactivate!(reform::Reformulation, id::Id)
Expand Down
Loading

0 comments on commit 157a3c1

Please sign in to comment.