Commit 873b1b13 authored by Sylvain Arreckx's avatar Sylvain Arreckx
Browse files

Allow sparse entries

parent 0c9c0079
......@@ -4,4 +4,5 @@ authors = ["Some people"]
version = "0.1.0"
[deps]
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
using SparseArrays
"""
A bilevel linear optimization problem of the form:
```
......@@ -7,20 +9,20 @@ s.t. S x = b
```
"""
mutable struct CobraLP
S ::AbstractMatrix
b ::Array{Float64,1}
c ::Array{Float64,1}
lb ::Array{Float64,1}
ub ::Array{Float64,1}
S ::Union{AbstractMatrix, SparseMatrixCSC{Float64,Int64}}
b ::Union{Array{Float64,1}, SparseVector{Float64,Int64}}
c ::Union{Array{Float64,1}, SparseVector{Float64,Int64}}
lb ::Union{Array{Float64,1}, SparseVector{Float64,Int64}}
ub ::Union{Array{Float64,1}, SparseVector{Float64,Int64}}
rxns ::Array{String,1}
mets ::Array{String,1}
function CobraLP(
S ::AbstractMatrix,
b ::Array{Float64,1},
c ::Array{Float64,1},
lb ::Array{Float64,1},
ub ::Array{Float64,1},
S ::Union{AbstractMatrix, SparseMatrixCSC{Float64,Int64}},
b ::Union{Array{Float64,1}, SparseVector{Float64,Int64}},
c ::Union{Array{Float64,1}, SparseVector{Float64,Int64}},
lb ::Union{Array{Float64,1}, SparseVector{Float64,Int64}},
ub ::Union{Array{Float64,1}, SparseVector{Float64,Int64}},
rxns ::Array{String,1},
mets ::Array{String,1})
......@@ -35,13 +37,13 @@ mutable struct CobraLP
end
end
function addReaction(m::CobraLP, s::Array{Float64,1}, b)
function addReaction(m::CobraLP, s::Union{Array{Float64,1}, SparseVector{Float64,Int64}}, b)
newS = vcat(m.S, s')
newb = vcat(m.b, b)
return CobraLP(newS, newb, m.c, m.lb, m.ub, m.rxns, m.mets)
end
function addReactions(m::CobraLP, Sp::AbstractMatrix, b::Array{Float64,1})
function addReactions(m::CobraLP, Sp::Union{AbstractMatrix, SparseMatrixCSC{Float64,Int64}}, b::Union{Array{Float64,1}, SparseVector{Float64,Int64}})
newS = vcat(m.S, Sp)
newb = vcat(m.b, b)
return CobraLP(newS, newb, m.c, m.lb, m.ub, m.rxns, m.mets)
......
using Test
using SparseArrays
using ***REMOVED***
test_cobraLP() = CobraLP(zeros(4, 3),
......@@ -9,18 +10,44 @@ test_cobraLP() = CobraLP(zeros(4, 3),
["r1"],
["m1"])
test_sparseCobraLP() = CobraLP(sprand(4000, 3000, 0.5),
sprand(4000, 0.5),
sprand(3000, 0.5),
sprand(3000, 0.5),
sprand(3000, 0.5),
["r1"],
["m1"])
@testset "CobraLP type" begin
cp = test_cobraLP()
@test cp isa CobraLP
end
@testset "Add reaction" begin
@testset "Add reactions" begin
cp = test_cobraLP()
@test size(cp.S) == (4, 3)
cp = addReaction(cp, 2. * ones(3), 1.)
@test size(cp.S) == (5, 3)
cp = addReactions(cp, 2. * ones(1, 3), ones(1))
@test size(cp.S) == (6, 3)
cp = addReactions(cp, 2. * ones(10, 3), ones(10))
@test size(cp.S) == (16, 3)
cp = test_sparseCobraLP()
@test size(cp.S) == (4000, 3000)
cp = addReaction(cp, 2. * sprand(3000, 0.5), 1.)
@test size(cp.S) == (4001, 3000)
cp = addReactions(cp, 2. * sprand(1, 3000, 0.5), sprand(1, 0.5))
@test size(cp.S) == (4002, 3000)
cp = addReactions(cp, 2. * sprand(1000, 3000, 0.5), sprand(1000, 0.5))
@test size(cp.S) == (5002, 3000)
cp = test_sparseCobraLP()
@test size(cp.S) == (4000, 3000)
cp = addReaction(cp, 2. * ones(3000), 1.)
@test size(cp.S) == (4001, 3000)
cp = addReactions(cp, 2. * ones(1, 3000), ones(1))
@test size(cp.S) == (4002, 3000)
cp = addReactions(cp, 2. * ones(1000, 3000), ones(1000))
@test size(cp.S) == (5002, 3000)
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment