Skip to content

Commit

Permalink
add delete! and use proper get and set methods
Browse files Browse the repository at this point in the history
  • Loading branch information
laradicp committed Aug 13, 2021
1 parent 0182167 commit da2a964
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
21 changes: 8 additions & 13 deletions src/MOIwrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ function MOI.delete(model::Coluna.Optimizer, vi::MOI.VariableIndex)
MOI.modify(model, ci, MOI.ScalarCoefficientChange(vi, 0.0))
end
varid = getid(model.vars[vi])
orig_form = model.inner.original_formulation
delete!(orig_form.manager.vars, varid)
delete!(orig_form.buffer.var_buffer.added, varid)
delete!(get_original_formulation(model.inner), varid)
delete!(model.moi_varids, varid)
delete!(model.vars, vi)
delete!(model.env.varids, vi)
Expand All @@ -237,9 +235,9 @@ function MOI.modify(
model::Coluna.Optimizer, ::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
change::MathOptInterface.ScalarCoefficientChange{Float64}
)
var = model.vars[change.variable]
var.perendata.cost = change.new_coefficient
var.curdata.cost = change.new_coefficient
setperencost!(
get_original_formulation(model.inner), model.vars[change.variable], change.new_coefficient
)
return
end

Expand All @@ -257,7 +255,7 @@ function MOI.delete(
model::Coluna.Optimizer, ci::MOI.ConstraintIndex{F,S}
) where {F<:MOI.ScalarAffineFunction{Float64},S}
constrid = getid(model.constrs[ci])
orig_form = model.inner.original_formulation
orig_form = get_original_formulation(model.inner)
coefmatrix = getcoefmatrix(orig_form)
varids = VarId[]
for (varid, _) in @view coefmatrix[constrid, :]
Expand All @@ -266,8 +264,7 @@ function MOI.delete(
for varid in varids
coefmatrix[constrid, varid] = 0.0
end
delete!(orig_form.buffer.constr_buffer.added, constrid)
delete!(orig_form.manager.constrs, constrid)
delete!(orig_form, constrid)
delete!(model.constrs, ci)
return
end
Expand All @@ -276,9 +273,7 @@ function MOI.modify(
model::Coluna.Optimizer, ci::MOI.ConstraintIndex{F,S},
change::MOI.ScalarConstantChange{Float64}
) where {F<:MOI.ScalarAffineFunction{Float64},S}
constr = model.constrs[ci]
constr.perendata.rhs = change.new_constant
constr.curdata.rhs = change.new_constant
setperenrhs!(get_original_formulation(model.inner), model.constrs[ci], change.new_constant)
return
end

Expand All @@ -288,7 +283,7 @@ function MOI.modify(
) where {F<:MOI.ScalarAffineFunction{Float64},S}
varid = getid(model.vars[change.variable])
constrid = getid(model.constrs[ci])
getcoefmatrix(model.inner.original_formulation)[constrid, varid] = change.new_coefficient
getcoefmatrix(get_original_formulation(model.inner))[constrid, varid] = change.new_coefficient
return
end

Expand Down
2 changes: 1 addition & 1 deletion src/MathProg/MathProg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TimerOutputs
import ..Coluna # for NestedEnum (types.jl:210)
using ..ColunaBase

import Base: haskey, length, iterate, diff
import Base: haskey, length, iterate, diff, delete!

using DynamicSparseArrays, Logging, Printf

Expand Down
20 changes: 20 additions & 0 deletions src/MathProg/varconstr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,26 @@ function deactivate!(form::Formulation, f::Function)
return
end

## delete
"""
delete!(formulation, varconstrid)
delete!(formulation, varconstr)
Delete a variable or a constraint from a formulation.
"""
function Base.delete!(form::Formulation, varid::VarId)
delete!(form.manager.vars, varid)
delete!(form.buffer.var_buffer.added, varid)
return
end
Base.delete!(form::Formulation, var::Variable) = delete!(form, getid(var))

function Base.delete!(form::Formulation, constrid::ConstrId)
delete!(form.buffer.constr_buffer.added, constrid)
delete!(form.manager.constrs, constrid)
end
Base.delete!(form::Formulation, constr::Constraint) = delete!(form, getid(constr))

## explicit
"""
isexplicit(formulation, varconstr)
Expand Down

0 comments on commit da2a964

Please sign in to comment.