Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

WIP: fix issues identified by MOI #354 #10

Merged
merged 6 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
56 changes: 56 additions & 0 deletions src/LinQuadOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,62 @@ ConstraintMapping() = ConstraintMapping(
Dict{VVCI{SOS1}, Int}(),
Dict{VVCI{SOS2}, Int}()
)

"""
shift_references_after_delete_affine!(m, row)

This function updates all of the references in `m`
after we have deleted row `row` in the affine constraint matrix.
"""
function shift_references_after_delete_affine!(m, row)
for scalar_affine in [
cmap(m).less_than,
cmap(m).greater_than,
cmap(m).equal_to,
cmap(m).interval
]
for (key, val) in scalar_affine
if val > row
scalar_affine[key] -= 1
end
end
end

for vector_affine in [
cmap(m).vv_nonnegatives,
cmap(m).vv_nonpositives,
cmap(m).vv_zeros
]
for (key, vals) in vector_affine
for (i, val) in enumerate(vals)
if val > row
vector_affine[key][i] -= 1
end
end
end
end
end

"""
shift_references_after_delete_quadratic!(m, row)

This function updates all of the references in `m`
after we have deleted row `row` in the quadratic constraint matrix.
"""
function shift_references_after_delete_quadratic!(m, row)
for scalar_quadratic in [
cmap(m).q_less_than,
cmap(m).q_greater_than,
cmap(m).q_equal_to
]
for (key, val) in scalar_quadratic
if val > row
scalar_quadratic[key] -= 1
end
end
end
end

function Base.isempty(map::ConstraintMapping)

ret = true
Expand Down
21 changes: 12 additions & 9 deletions src/constraints/scalaraffine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::V
coefficients = Vector{Float64}(nnz) # corresponding non-zeros
i = 1
for (fi, f) in enumerate(func)
row_starts[fi] = cnt
row_starts[fi] = i
for (var, coef) in zip(f.variables, f.coefficients)
column_indices[i] = getcol(m, var)
coefficients[i] = coef
Expand Down Expand Up @@ -129,6 +129,13 @@ function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::LCI{S}) where S <:
S(rhs+m.constraint_constant[m[c]])
end

MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{LCI{IV}}) = false
# TODO(odow): get constraint sets for ranged constraints.
# function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::LCI{IV})
# ???
# IV(lowerbound+m.constraint_constant[m[c]], upperbound + m.constraint_constant[m[c]])
# end

#=
Constraint function of Linear function
=#
Expand Down Expand Up @@ -169,7 +176,7 @@ end
Delete a linear constraint
=#

MOI.candelete(m::LinQuadOptimizer, c::LCI{<: LinSets}) = true
MOI.candelete(m::LinQuadOptimizer, c::LCI{<: LinSets}) = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::LCI{<: LinSets})
deleteconstraintname!(m, c)
dict = constrdict(m, c)
Expand All @@ -178,13 +185,9 @@ function MOI.delete!(m::LinQuadOptimizer, c::LCI{<: LinSets})
deleteat!(m.constraint_primal_solution, row)
deleteat!(m.constraint_dual_solution, row)
deleteat!(m.constraint_constant, row)
deleteref!(m, row, c)
end
function deleteref!(m::LinQuadOptimizer, row::Int, ref::LCI{<: LinSets})
deleteref!(cmap(m).less_than, row, ref)
deleteref!(cmap(m).greater_than, row, ref)
deleteref!(cmap(m).equal_to, row, ref)
deleteref!(cmap(m).interval, row, ref)
# shift all the other references
shift_references_after_delete_affine!(m, row)
delete!(dict, c)
end

#=
Expand Down
14 changes: 14 additions & 0 deletions src/constraints/scalarquadratic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ function reduceduplicates(rowi::Vector{T}, coli::Vector{T}, vals::Vector{S}) whe
end
ri, ci, vi
end


MOI.candelete(m::LinQuadOptimizer, c::QCI{<: LinSets}) = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::QCI{<: LinSets})
deleteconstraintname!(m, c)
dict = constrdict(m, c)
row = dict[c]
delete_quadratic_constraints!(m, row, row)
deleteat!(m.qconstraint_primal_solution, row)
deleteat!(m.qconstraint_dual_solution, row)
# shift all the other references
shift_references_after_delete_quadratic!(m, row)
delete!(dict, c)
end
10 changes: 5 additions & 5 deletions src/constraints/singlevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::S) where S <: L
end

# delete constraint
MOI.candelete(m::LinQuadOptimizer, c::SVCI{S}) where S <: LinSets = true
MOI.candelete(m::LinQuadOptimizer, c::SVCI{S}) where S <: LinSets = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::SVCI{S}) where S <: LinSets
deleteconstraintname!(m, c)
dict = constrdict(m, c)
Expand Down Expand Up @@ -116,7 +116,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.ZeroOne)
ref
end

MOI.candelete(m::LinQuadOptimizer, c::SVCI{MOI.ZeroOne}) = true
MOI.candelete(m::LinQuadOptimizer, c::SVCI{MOI.ZeroOne}) = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.ZeroOne})
deleteconstraintname!(m, c)
dict = constrdict(m, c)
Expand All @@ -134,7 +134,7 @@ MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{SVCI{MOI.ZeroOne}})
MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{MOI.ZeroOne}) = MOI.ZeroOne()

MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintFunction, ::Type{SVCI{MOI.ZeroOne}}) = true
MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{MOI.ZeroOne}) = m[c]
MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{MOI.ZeroOne}) = SinVar(m[c][1])

#=
Integer constraints
Expand All @@ -150,6 +150,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.Integer)
ref
end

MOI.candelete(m::LinQuadOptimizer, c::SVCI{MOI.Integer}) = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.Integer})
deleteconstraintname!(m, c)
dict = constrdict(m, c)
Expand All @@ -160,10 +161,9 @@ function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.Integer})
make_problem_type_continuous(m)
end
end
MOI.candelete(m::LinQuadOptimizer, c::SVCI{MOI.Integer}) = true

MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{SVCI{MOI.Integer}}) = true
MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{MOI.Integer}) = MOI.Integer()

MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{MOI.Integer}) = m[c]
MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintFunction, ::Type{SVCI{MOI.Integer}}) = true
MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{MOI.Integer}) = SinVar(m[c])
16 changes: 16 additions & 0 deletions src/constraints/vectoraffine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ function MOI.modifyconstraint!(m::LinQuadOptimizer, ref::VLCI{<: VecLinSets}, ch
end
end

MOI.candelete(m::LinQuadOptimizer, c::VLCI{<:VecLinSets}) = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::VLCI{<:VecLinSets})
deleteconstraintname!(m, c)
dict = constrdict(m, c)
for row in copy(dict[c]) # put a copy here because we modify in the loop
delete_linear_constraints!(m, row, row)
deleteat!(m.constraint_primal_solution, row)
deleteat!(m.constraint_dual_solution, row)
deleteat!(m.constraint_constant, row)
# shift all the other references
shift_references_after_delete_affine!(m, row)
end
delete!(dict, c)
end


#=
Constraint set of Linear function
=#
Expand Down
16 changes: 15 additions & 1 deletion src/constraints/vectorofvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::VecVar, set::S) where S <
return ref
end

MOI.candelete(m::LinQuadOptimizer, c::VVCI{S}) where S <: VecLinSets = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::VVCI{S}) where S <: VecLinSets
deleteconstraintname!(m, c)
dict = constrdict(m, c)
for row in copy(dict[c])
delete_linear_constraints!(m, row, row)
deleteat!(m.constraint_primal_solution, row)
deleteat!(m.constraint_dual_solution, row)
deleteat!(m.constraint_constant, row)
# shift all the other references
shift_references_after_delete_affine!(m, row)
end
delete!(dict, c)
end

#=
Get constraint set of vector variable bound
Expand Down Expand Up @@ -73,7 +87,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::VecVar, sos::S) where S <: U
ref
end

MOI.candelete(m::LinQuadOptimizer, c::VVCI{<:Union{SOS1, SOS2}}) = true
MOI.candelete(m::LinQuadOptimizer, c::VVCI{<:Union{SOS1, SOS2}}) = MOI.isvalid(m, c)
function MOI.delete!(m::LinQuadOptimizer, c::VVCI{<:Union{SOS1, SOS2}})
deleteconstraintname!(m, c)
dict = constrdict(m, c)
Expand Down
8 changes: 8 additions & 0 deletions src/solver_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ the model `m`.
function delete_linear_constraints! end
@deprecate lqs_delrows! delete_linear_constraints!

"""
delete_quadratic_constraints!(m, start_row::Int, end_row::Int)::Void

Delete the quadratic constraints `start_row`, `start_row+1`, ..., `end_row` from
the model `m`.
"""
function delete_quadratic_constraints! end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joaquimg I added this method to the API

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Are there MOI tests for deleting quadratics?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


"""
lqs_chgctype(m, cols::Vector{Int}, types):Void

Expand Down