# How to solve large sparse LPs in Gurobi

**URL:** <https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [July 16, 2019, 4:53am UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402 "2019-07-16T04:53:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)\
**Post date:** [July 16, 2019, 4:53am UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/1 "2019-07-16T04:53:09Z")

</div>

Hi,

I have a question about solving large sparse matrix LP problem. It really confused me for a long time.  
If I have a form like this:  
[![image](https://global.discourse-cdn.com/julialang/original/3X/0/8/0838f5a6bbac22246b48bb140f28db694c13cb09.png)](https://user-images.githubusercontent.com/52946708/61265723-8664de80-a75f-11e9-87a5-19e96805da51.png)  
I have all Fi data, which are in sparse form. How can I build a LP model to solve this problem faster? Any clue? Thank you!

---

<div class="post-metadata">

**Author:** ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)\
**Post date:** [July 16, 2019, 8:00am UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/2 "2019-07-16T08:00:15Z")

</div>

For example, if I have tr(AiX)=bi, i=1,2,…,100. Ai is 1000\*1000, Ai, X are symmetrix. How to build a LP model to solve this problem?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 16, 2019, 2:32pm UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/3 "2019-07-16T14:32:11Z")

</div>

First, welcome!

Second, please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757). It’s a lot easier to help if you can share a minimum working example showing what you have already tried.

Rather than exclusively using Gurobi, you should use JuMP to model this problem. Here is the documentation: [Introduction · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.2/), and here are some examples: [https://github.com/JuliaOpt/JuMP.jl/tree/release-0.19/examples](https://github.com/JuliaOpt/JuMP.jl/tree/release-0.19/examples).

---

<div class="post-metadata">

**Author:** ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)\
**Post date:** [July 16, 2019, 9:53pm UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/5 "2019-07-16T21:53:36Z")

</div>

Thank you, odow! Actually, I use Julia JuMP. I am confused about how to realize the constraint ci’Xci\>=0. X here is a large sparse matrix. The dimension of X is 1512\*1512.

```julia
Q=sparse(eigvecs(Y)) //Y is value of variable X
for i=1:1512
   if P[i]<=-1.0e-5 // P[i] is the ith eigenvalue of X
     @constraint(model, Q[:,i]'*X*Q[:,i]>=0) //(Q[:,i] is the ith eigenvector of X)
   end
end

```

When I run this code, I got this “Warning: The addition operator has been used on JuMP expressions a large number of times. This warning is safe to ignore but may indicate that model generation is slower than necessary. For performance reasons, you should not add expressions in a loop. Instead of x += , use append!(x,y) to modify x in place. Uf y is a single variable, you may also use push!(x, coef, y) in place of x += coef\*y”.

Anyway to avoid this? Thanks!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 16, 2019, 10:30pm UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/6 "2019-07-16T22:30:40Z")

</div>

It’s likely you want to create a semi-definite variable instead:  
[http://www.juliaopt.org/JuMP.jl/v0.19.2/variables/#Semidefinite-variables-1](http://www.juliaopt.org/JuMP.jl/v0.19.2/variables/#Semidefinite-variables-1)  
Alternatively, you can use  
[http://www.juliaopt.org/JuMP.jl/v0.19.2/constraints/#Semidefinite-constraints-1](http://www.juliaopt.org/JuMP.jl/v0.19.2/constraints/#Semidefinite-constraints-1)

---

<div class="post-metadata">

**Author:** ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)\
**Post date:** [July 17, 2019, 1:24am UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/7 "2019-07-17T01:24:15Z")

</div>

I am sorry. I know this is similar with SDP, but for some reason I have to use LP solver to solve this ci’Xci\>=0.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 17, 2019, 1:29pm UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/8 "2019-07-17T13:29:49Z")

</div>

If it doesn’t take too much time to build, you can ignore the warning. Otherwise, you need to write out the full scalarized expression:

```nohighlight
@constraint(model, sum(sum(Q[j,i] * X[j,k] for j in 1:N) * Q[k, i] for k in 1:N) >= 0)

```

---

<div class="post-metadata">

**Author:** ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)\
**Post date:** [July 17, 2019, 5:20pm UTC](https://discourse.julialang.org/t/how-to-solve-large-sparse-lps-in-gurobi/26402/9 "2019-07-17T17:20:00Z")

</div>

Thank you!!!
