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

Commit

Permalink
Change the Implimentation of CSRMatrix to us SparseMatrixCSC (#101)
Browse files Browse the repository at this point in the history
* This changes the implementation of CSRMatrix to include the additional value at the end of row_pointers that signifies the end of that row

Remove Depreciation  add sparse conversion

Small Fix

Change CSRMatrix to Adjoint{Sparse}}

Cleanup

Cleanup comments

Comment cleanup

cleanup

Changed Adjoint to Transpose

Rebase to no CI for julia v 0.6 (#1)

* Update appveyor.yml

remove testing for v0.6 add v1.1

* Update .travis.yml

drop testing for v0.6 add v1.1

* change require to julia 0.7

* This changes the implementation of CSRMatrix to include the additional value at the end of row_pointers that signifies the end of that row

* Remove Depreciation  add sparse conversion

* Small Fix

* Change CSRMatrix to Adjoint{Sparse}}

* Cleanup

* Cleanup comments

* Comment cleanup

* cleanup

* Changed Adjoint to Transpose

Remove commented lines
Fix Collect statement
drop v0.7

* spelling
  • Loading branch information
ndinsmore authored and odow committed Apr 12, 2019
1 parent 4769deb commit 148334e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
40 changes: 19 additions & 21 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,29 @@ end
"""
CSRMatrix{T}
Matrix given in compressed sparse row (CSR) format.
`CSRMatrix` is analgous to the structure in Julia's `SparseMatrixCSC` but with
the rows and columns flipped. It contains three vectors:
- `row_pointers` is a vector pointing to the start of each row in
`columns` and `coefficients`;
- `columns` is a vector of column numbers; and
- `coefficients` is a vector of corresponding nonzero values.
Type alias for `Transpose{T,SparseMatrixCSC{T,Int}} where T`, which allows for the
efficient storage of compressed sparse row (CSR) sparse matrix
Please use the following interface to access this data in case model is changed in future
- `row_pointers(mat)` returns that sparse row_pointer vector see description
which has a length of rows+1
- `colvals` this is the column version of `rowvals(SparseMatrixCSC)`
- `row_nonzeros` this is the equivilent to `nonzeros(SparseMatrixCSC)`
"""

The length of `row_pointers` is the number of rows in the matrix.
const CSRMatrix{T} = Transpose{T,SparseMatrixCSC{T,Int}} where T

This struct is not a subtype of `AbstractSparseMatrix` as it is intended to be a
collection of the three vectors as they are required by solvers such as Gurobi.
It is not intended to be used for general computation.
"""
struct CSRMatrix{T}
row_pointers::Vector{Int}
columns::Vector{Int}
coefficients::Vector{T}
function CSRMatrix{T}(row_pointers, columns, coefficients) where T
@assert length(columns) == length(coefficients)
new(row_pointers, columns, coefficients)
end
function CSRMatrix{T}(row_pointers, columns, coefficients) where T
@assert length(columns) == length(coefficients)
@assert length(columns) + 1 == row_pointers[end]
transpose(SparseMatrixCSC{T,Int}(maximum(columns), length(row_pointers)-1, row_pointers, columns, coefficients))
end

# Move access to an interface so that if we want to change type definition we can.
colvals(mat::CSRMatrix) = rowvals(transpose(mat))
row_pointers(mat::CSRMatrix) = transpose(mat).colptr # This is the only way to get at colptrs without recreating it
row_nonzeros(mat::CSRMatrix) = nonzeros(transpose(mat))

#=
Below we add constraints.
=#
Expand Down
7 changes: 4 additions & 3 deletions src/constraints/scalaraffine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end
function add_linear_constraint(model::LinQuadOptimizer, func::Linear, set::IV)
columns = [get_column(model, term.variable_index) for term in func.terms]
coefficients = [term.coefficient for term in func.terms]
A = CSRMatrix{Float64}([1], columns, coefficients)
A = CSRMatrix{Float64}([1, length(coefficients) + 1], columns, coefficients)
add_ranged_constraints!(model, A, [set.lower], [set.upper])
end

Expand All @@ -45,7 +45,7 @@ function add_linear_constraint(model::LinQuadOptimizer, func::Linear, sense, rhs
end
columns = [get_column(model, term.variable_index) for term in func.terms]
coefficients = [term.coefficient for term in func.terms]
A = CSRMatrix{Float64}([1], columns, coefficients)
A = CSRMatrix{Float64}([1, length(coefficients) + 1], columns, coefficients)
add_linear_constraints!(model, A, [sense], [rhs - func.constant])
end

Expand Down Expand Up @@ -99,7 +99,7 @@ function functions_to_CSRMatrix(model::LinQuadOptimizer, functions::Vector{Linea
# row (i - 1), storing the result in r[i]. Then we perform a cumsum on r,
# storing the result back in r.
num_rows = length(functions)
row_pointers = fill(0, num_rows)
row_pointers = fill(0, num_rows+1)
row_pointers[1] = 1
non_zero_index = 0
for (row, func) in enumerate(functions)
Expand All @@ -117,6 +117,7 @@ function functions_to_CSRMatrix(model::LinQuadOptimizer, functions::Vector{Linea
end
end
cumsum!(row_pointers, row_pointers)
row_pointers[end] = length(coefficients)+1
return CSRMatrix{Float64}(row_pointers, columns, coefficients)
end

Expand Down
4 changes: 2 additions & 2 deletions src/constraints/vectoraffine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function add_linear_constraint(model::LinQuadOptimizer, func::VecLin, sense::Cch
# row (i - 1), storing the result in r[i]. Then we perform a cumsum on r,
# storing the result back in r.
num_rows = length(func.constants)
row_pointers = fill(0, num_rows)
row_pointers = fill(0, num_rows+1)
row_pointers[1] = 1
for term in func.terms
row = term.output_index
Expand All @@ -53,7 +53,7 @@ function add_linear_constraint(model::LinQuadOptimizer, func::VecLin, sense::Cch
end
end
cumsum!(row_pointers, row_pointers)

row_pointers[end]=length(coefficients)+1
A = CSRMatrix{Float64}(row_pointers, columns, coefficients)
add_linear_constraints!(model, A, fill(sense, num_rows), -func.constants)
end
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/vectorofvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function MOI.add_constraint(model::LinQuadOptimizer, func::VecVar, set::S) where
rows = get_number_linear_constraints(model)
num_vars = MOI.dimension(set)
add_linear_constraints!(model,
CSRMatrix{Float64}(collect(1:num_vars),
CSRMatrix{Float64}(collect(1:num_vars+1),
get_column.(Ref(model), func.variables),
ones(num_vars)),
fill(backend_type(model, set), num_vars),
Expand Down
8 changes: 2 additions & 6 deletions src/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,10 @@ function LQOI.add_linear_constraints!(instance::MockLinQuadOptimizer, A::CSRMatr
# quadind
append!(instance.l_rows,addedrows)

rowvec, colvec, coefvec = A.row_pointers, A.columns, A.coefficients
rowvec, colvec, coefvec = row_pointers(A), colvals(A), row_nonzeros(A)

rows = length(rhsvec)
cols = size(instance.inner.A)[2]
push!(rowvec,length(colvec)+1)

An = Array(SparseMatrixCSC(cols,rows,rowvec,colvec,coefvec)')

Expand All @@ -430,13 +429,10 @@ function LQOI.add_ranged_constraints!(instance::MockLinQuadOptimizer, A::CSRMatr
# quadind
append!(instance.l_rows,addedrows)

rowvec, colvec, coefvec = A.row_pointers, A.columns, A.coefficients
rowvec, colvec, coefvec = row_pointers(A), colvals(A), row_nonzeros(A)
rows = length(lowerbound)
cols = size(instance.inner.A)[2]
push!(rowvec,length(colvec)+1)

# An = Array(sparse(rowvec,colvec,coefvec,rows,cols))
# @show cols,rows,rowvec,colvec,coefvec
An = Array(SparseMatrixCSC(cols,rows,rowvec,colvec,coefvec)')

instance.inner.A = vcat(instance.inner.A,An)
Expand Down

0 comments on commit 148334e

Please sign in to comment.