Skip to content

Commit

Permalink
bug in strong branching issue #242
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarqu committed Jan 25, 2020
1 parent 6078bb2 commit 050e91a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
11 changes: 10 additions & 1 deletion src/Containers/members.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,18 @@ function MembersMatrix{I,J,T}() where {I,J,T}
)
end

function Base.setindex!(m::MembersMatrix, val, row_id, col_id)
m.cols_major[row_id, col_id] = val
m.rows_major[col_id, row_id] = val
m
end

function Base.getindex(m::MembersMatrix, row_id, col_id)
# TODO : check number of rows & cols
return m.cols_major[row_id, col_id]
end

######## DELETE BELOW THIS LINE #########

struct OldMembersMatrix{I,K,J,L,T} <: AbstractMembersContainer
#matrix_csc::DynamicSparseArrays.MappedPackedCSC{}
Expand All @@ -162,7 +172,6 @@ struct OldMembersMatrix{I,K,J,L,T} <: AbstractMembersContainer
rows::MembersVector{J,L,MembersVector{I,K,T}} # to rm
end


"""
OldMembersMatrix{T}(columns_elems::Dict{I,K}, rows_elems::Dict{J,L})
Expand Down
30 changes: 16 additions & 14 deletions src/MathProg/MOIinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ function set_obj_sense!(optimizer::MoiOptimizer, ::Type{<:MinSense})
return
end

function compute_moi_terms(members::VarMembership)
return [
MOI.ScalarAffineTerm{Float64}(
coef, getindex(getmoirecord(getelements(members)[id]))
) for (id, coef) in members
]
end

function update_bounds_in_optimizer!(form::Formulation, var::Variable)
optimizer = getoptimizer(form)
inner = getinner(optimizer)
Expand Down Expand Up @@ -132,16 +124,26 @@ function add_to_optimizer!(form::Formulation, var::Variable)
return
end

function add_to_optimizer!(
form::Formulation, constr::Constraint, members::VarMembership
)
function add_to_optimizer!(form::Formulation, constr::Constraint)
constr_id = getid(constr)

inner = getinner(getoptimizer(form))
terms = compute_moi_terms(members)
f = MOI.ScalarAffineFunction(terms, 0.0)

matrix = getcoefmatrix(form)
terms = MOI.ScalarAffineTerm{Float64}[]
for (varid, coeff) in matrix[constr_id, :]
if getcurisactive(form, varid) && getcurisexplicit(form, varid)
moi_id = getindex(getmoirecord(getvar(form, varid)))
push!(terms, MOI.ScalarAffineTerm{Float64}(coeff, moi_id))
end
end

lhs = MOI.ScalarAffineFunction(terms, 0.0)
moi_set = convert_coluna_sense_to_moi(getcursense(form, constr))
moi_constr = MOI.add_constraint(
inner, f, moi_set(getcurrhs(form, constr))
inner, lhs, moi_set(getcurrhs(form, constr))
)

moirecord = getmoirecord(constr)
setindex!(moirecord, moi_constr)
MOI.set(inner, MOI.ConstraintName(), moi_constr, getname(constr))
Expand Down
24 changes: 8 additions & 16 deletions src/MathProg/formulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,40 +688,32 @@ function _show_obj_fun(io::IO, form::Formulation)
return
end

function _show_constraint(io::IO, form::Formulation, constr_id::ConstrId,
members::VarMembership)
function _show_constraint(io::IO, form::Formulation, constr_id::ConstrId)
constr = getconstr(form, constr_id)
print(io, getname(constr), " : ")
ids = sort!(collect(keys(members)), by = getsortuid)
for id in ids
coeff = members[id]
var = getvar(form, id)
for (varid, coeff) in getcoefmatrix(form)[constr_id, :]
var = getvar(form, constr_id)
name = getname(var)
op = (coeff < 0.0) ? "-" : "+"
print(io, op, " ", abs(coeff), " ", name, " ")
end
op = "<="
if getcursense(form, constr) == Equal
op = "=="
elseif getcursense(form, constr) == Greater
op = ">="
else
op = "<="
end
print(io, " ", op, " ", getcurrhs(form, constr))
println(io, " (", getduty(constr), getid(constr), " | ", getcurisexplicit(form,constr) ,")")
return
end

function _show_constraints(io::IO , form::Formulation)
# constrs = filter(
# _explicit_, rows(getcoefmatrix(form))
# )
constrs = rows(getcoefmatrix(form))
constrs = getconstrs(form)
ids = sort!(collect(keys(constrs)), by = getsortuid)
for id in ids
constr = getconstr(form, id)
if getcurisactive(form,constr)
_show_constraint(io, form, id, constrs[id])
for constr_id in ids
if getcurisactive(form, constr_id)
_show_constraint(io, form, constr_id)
end
end
return
Expand Down
3 changes: 3 additions & 0 deletions src/MathProg/manager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const VarVarMatrix = OldMembersMatrix{VarId,Variable,VarId,Variable,Float64}

const ConstrVarMatrix = MembersMatrix{ConstrId,VarId,Float64}

# Define the semaphore of the dynamic sparse matrix using MathProg.Id as index
DynamicSparseArrays.semaphore_key(::Type{I}) where {I <: Id} = zero(I)

const ConstrConstrMatrix = OldMembersMatrix{ConstrId,Constraint,ConstrId,Constraint,Float64}
const PrimalSolution{S} = Solution{Primal, S, Id{Variable}, Float64}
const DualSolution{S} = Solution{Dual, S, Id{Constraint}, Float64}
Expand Down
13 changes: 4 additions & 9 deletions src/MathProg/optimizerwrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,10 @@ function sync_solver!(optimizer::MoiOptimizer, f::Formulation)
end

# Add constrs
for id in buffer.constr_buffer.added
c = getconstr(f, id)
@logmsg LogLevel(-4) string("Adding constraint ", getname(c))
add_to_optimizer!(
f, c, filter(
vc -> getcurisactive(f, vc) && getcurisexplicit(f, vc),
matrix[id, :]
)
)
for constr_id in buffer.constr_buffer.added
constr = getconstr(f, constr_id)
@logmsg LogLevel(-4) string("Adding constraint ", getname(constr))
add_to_optimizer!(f, constr)
end

# Update variable costs
Expand Down

0 comments on commit 050e91a

Please sign in to comment.