-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add MOI methods: delete and modify #584
Changes from all commits
acb8530
875f42f
763be02
0182167
da2a964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,84 @@ function MOI.add_constraint( | |
return constrid | ||
end | ||
|
||
############################################################################################ | ||
# Delete and modify variable | ||
############################################################################################ | ||
function MOI.delete(model::Coluna.Optimizer, vi::MOI.VariableIndex) | ||
MOI.modify(model, MoiObjective(), MOI.ScalarCoefficientChange(vi, 0.0)) | ||
for (ci, _) in model.constrs_on_single_var_to_vars | ||
if ci.value == vi.value | ||
MOI.delete(model, ci) | ||
break | ||
end | ||
end | ||
for (ci, _) in model.constrs | ||
MOI.modify(model, ci, MOI.ScalarCoefficientChange(vi, 0.0)) | ||
end | ||
varid = getid(model.vars[vi]) | ||
delete!(get_original_formulation(model.inner), varid) | ||
delete!(model.moi_varids, varid) | ||
delete!(model.vars, vi) | ||
delete!(model.env.varids, vi) | ||
return | ||
end | ||
|
||
function MOI.modify( | ||
model::Coluna.Optimizer, ::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}, | ||
change::MathOptInterface.ScalarCoefficientChange{Float64} | ||
) | ||
setperencost!( | ||
get_original_formulation(model.inner), model.vars[change.variable], change.new_coefficient | ||
) | ||
return | ||
end | ||
|
||
############################################################################################ | ||
# Delete and modify constraint | ||
############################################################################################ | ||
# issue #583 | ||
# function MOI.delete( | ||
# model::Coluna.Optimizer, ci::MOI.ConstraintIndex{F,S} | ||
# ) where {F<:MOI.SingleVariable,S} | ||
# return | ||
# end | ||
|
||
function MOI.delete( | ||
model::Coluna.Optimizer, ci::MOI.ConstraintIndex{F,S} | ||
) where {F<:MOI.ScalarAffineFunction{Float64},S} | ||
constrid = getid(model.constrs[ci]) | ||
orig_form = get_original_formulation(model.inner) | ||
coefmatrix = getcoefmatrix(orig_form) | ||
varids = VarId[] | ||
for (varid, _) in @view coefmatrix[constrid, :] | ||
push!(varids, varid) | ||
end | ||
for varid in varids | ||
coefmatrix[constrid, varid] = 0.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if you do : for (varid, _) in @view coefmatrix[constrid, :]
coefmatrix[constrid, varid] = 0.0
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it enters in an undefined behavior because the size of the array that stores the coefficient matrix may change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, I got an error for accessing an invalid index. |
||
end | ||
delete!(orig_form, constrid) | ||
delete!(model.constrs, ci) | ||
return | ||
end | ||
|
||
function MOI.modify( | ||
model::Coluna.Optimizer, ci::MOI.ConstraintIndex{F,S}, | ||
change::MOI.ScalarConstantChange{Float64} | ||
) where {F<:MOI.ScalarAffineFunction{Float64},S} | ||
setperenrhs!(get_original_formulation(model.inner), model.constrs[ci], change.new_constant) | ||
return | ||
end | ||
|
||
function MOI.modify( | ||
model::Coluna.Optimizer, ci::MOI.ConstraintIndex{F,S}, | ||
change::MOI.ScalarCoefficientChange{Float64} | ||
) where {F<:MOI.ScalarAffineFunction{Float64},S} | ||
varid = getid(model.vars[change.variable]) | ||
constrid = getid(model.constrs[ci]) | ||
getcoefmatrix(get_original_formulation(model.inner))[constrid, varid] = change.new_coefficient | ||
return | ||
end | ||
|
||
############################################################################################ | ||
# Get variables | ||
############################################################################################ | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
end | ||||||||
Base.delete!(form::Formulation, constr::Constraint) = delete!(form, getid(constr)) | ||||||||
|
||||||||
## explicit | ||||||||
""" | ||||||||
isexplicit(formulation, varconstr) | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should change the coefficients in
MathProg.delete!
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah indeed