Skip to content
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

Fix index update after row/column deletion #53

Merged
merged 1 commit into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 33 additions & 17 deletions src/Core/problemData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,23 @@ function delete_constraint!(pb::ProblemData{Tv}, rind::Int) where{Tv}
deleteat!(pb.ucon, rind)

# Update columns
for j in pb.arows[rind].nzind
col = pb.acols[j]
k = searchsortedfirst(col.nzind, rind)
# Delete coefficient from column
deleteat!(col.nzind, k)
deleteat!(col.nzval, k)
# Update remaining indices
col.nzind[k:end] .-= 1
end
for (j, col) in enumerate(pb.acols)
# Search for row in that column
rg = searchsorted(col.nzind, rind)
if rg.start > length(col.nzind)
# Nothing to do
continue
else
if col.nzind[rg.start] == rind
# Delete row from column
deleteat!(col.nzind, rg.start)
deleteat!(col.nzval, rg.start)
end

# Decrement subsequent row indices
col.nzind[rg.start:end] .-= 1
end
end

# Delete row
deleteat!(pb.arows, rind)
Expand Down Expand Up @@ -375,14 +383,22 @@ function delete_variable!(pb::ProblemData{Tv}, cind::Int) where{Tv}
deleteat!(pb.uvar, cind)

# Update rows
for i in pb.acols[cind].nzind
row = pb.arows[i]
k = searchsortedfirst(row.nzind, cind)
# Delete coeff from row
deleteat!(row.nzind, k)
deleteat!(row.nzval, k)
# Update remaining coefficients
row.nzind[k:end] .-= 1
for (i, row) in enumerate(pb.arows)
# Search for column in that row
rg = searchsorted(row.nzind, cind)
if rg.start > length(row.nzind)
# Nothing to do
continue
else
if row.nzind[rg.start] == cind
# Column appears in row
deleteat!(row.nzind, rg.start)
deleteat!(row.nzval, rg.start)
end

# Decrement subsequent column indices
row.nzind[rg.start:end] .-= 1
end
end

# Delete column
Expand Down
51 changes: 50 additions & 1 deletion test/Core/problemData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,57 @@ function run_tests_pbdata(Tv::Type)

end

@testset "Modification" begin
@testset "Delete" begin
pb = TLP.ProblemData{Tv}("test")
#=
min x1 + 2 x2 + 3 x3
s.t. 1 ⩽ 1 * x1 ⩽ 10
2 ⩽ 2 * x2 ⩽ 20
3 ⩽ 3 * x3 ⩽ 30

11 ⩽ x1 ⩽ 110
22 ⩽ x2 ⩽ 220
33 ⩽ x3 ⩽ 330
=#

TLP.add_variable!(pb, Int[], Tv[], one(Tv), 11 * one(Tv), 110 * one(Tv), "x1")
TLP.add_variable!(pb, Int[], Tv[], 2 * one(Tv), 22 * one(Tv), 220 * one(Tv), "x2")
TLP.add_variable!(pb, Int[], Tv[], 3 * one(Tv), 33 * one(Tv), 330 * one(Tv), "x3")

TLP.add_constraint!(pb, [1], Tv.([1]), 1 * one(Tv), 10 * one(Tv), "row1")
TLP.add_constraint!(pb, [2], Tv.([2]), 2 * one(Tv), 20 * one(Tv), "row2")
TLP.add_constraint!(pb, [3], Tv.([3]), 3 * one(Tv), 30 * one(Tv), "row3")

# Delete row 1 and check remaining problem
TLP.delete_constraint!(pb, 1)

@test pb.ncon == 2
@test pb.nvar == 3
row2, row3 = pb.arows
@test pb.con_names == ["row2", "row3"]
@test pb.lcon == Tv.([2, 3])
@test pb.ucon == Tv.([20, 30])

@test row2.nzind == [2]
@test row2.nzval == [Tv(2)]

@test row3.nzind == [3]
@test row3.nzval == [Tv(3)]

# Delete variable 2
TLP.delete_variable!(pb, 2)
@test pb.ncon == 2
@test pb.nvar == 2
col1, col3 = pb.acols
@test pb.var_names == ["x1", "x3"]
@test pb.lvar == Tv.([11, 33])
@test pb.uvar == Tv.([110, 330])

@test col1.nzind == []
@test col1.nzval == Tv[]

@test col3.nzind == [2]
@test col3.nzval == [Tv(3)]
end

@testset "Queries" begin
Expand Down