# Read LP files

**URL:** https://discourse.julialang.org/t/read-lp-files/6877
**Category:** Optimization (Mathematical)
**Created:** [November 4, 2017, 4:14pm UTC](https://discourse.julialang.org/t/read-lp-files/6877 "2017-11-04T16:14:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Andrey\_Stroil](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Andrey\_Stroil](https://discourse.julialang.org/u/Andrey_Stroil)
#### Post date: [November 4, 2017, 4:14pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/1 "2017-11-04T16:14:56Z")

</div>

Hello, I have a huge stochastic optimization model and JuMP is not fast enough to deal with it. I want to write the lp model in a file and then solve it.

Does JuMP have a method like read\_LP(“file.lp”)? Or is there a better approach to my problem?

Thanks a lot

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [November 5, 2017, 4:27pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/2 "2017-11-05T16:27:07Z")

</div>

Without looking at the docs: I’m guessing that JuMP has no such method, but the individual solver backends probably have it.

---

<div class="post-metadata">

### Author: ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)
#### Post date: [November 5, 2017, 10:57pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/3 "2017-11-05T22:57:50Z")

</div>

A quick search of the documentation doesn’t show a `readLP` function. `JuMP` is primarily a domain-specific language for _defining_ an optimization problem. If you are already writing the relevant `.lp` file, it would probably be more efficient to feed this directly into the solver and then read the solution into Julia. I’ve gone full-circle before, specifying a linear program in `JuMP`, using `writeLP` to get the `.lp` file, then solving using `CPLEX` on a computer that didn’t have Julia installed, and then reading in the resulting `.sol`, which is just an XML file for postprocessing and visualization.

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://discourse.julialang.org/u/Luiz)
#### Post date: [November 17, 2017, 8:40pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/4 "2017-11-17T20:40:15Z")

</div>

Try building your model without JuMP.

Use linprog() method

[http://mathprogbasejl.readthedocs.io/en/latest/linprog.html?highlight=MathProgBase%20](http://mathprogbasejl.readthedocs.io/en/latest/linprog.html?highlight=MathProgBase%20)

---

<div class="post-metadata">

### Author: ![andreas-ernst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreas-ernst/32/2874_2.png) [@andreas-ernst](https://discourse.julialang.org/u/andreas-ernst)
#### Post date: [December 8, 2017, 5:53am UTC](https://discourse.julialang.org/t/read-lp-files/6877/5 "2017-12-08T05:53:13Z")

</div>

The fowllowing should do the trick. Though there is no guarantee that it will be faster than creating the model in JuMP.

```julia
using CPLEX
import MathProgBase
const MPB=MathProgBase # shorthand for long module name
"This function takes an optional solver and loades a model from an LP file "
function loadLP(filename,solver=CplexSolver())
    model=buildlp([-1,0],[2 1],'<',1.5, solver) # create dummy model with correct solver
    MPB.loadproblem!(model,filename) # load what we actually want
    return model
end
lp = loadLP("file.lp")
MPB.optimize!(lp)

```

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [December 9, 2017, 10:50pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/6 "2017-12-09T22:50:53Z")

</div>

The correct way to generate an empty MPB model in this context is `MPB.LinearQuadraticModel(solver)`.

---

<div class="post-metadata">

### Author: ![CanLi1](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@CanLi1](https://discourse.julialang.org/u/CanLi1)
#### Post date: [May 9, 2019, 1:30am UTC](https://discourse.julialang.org/t/read-lp-files/6877/7 "2019-05-09T01:30:11Z")

</div>

Hi folks,

I’m trying to do the same thing of reading LP files. Thanks to your posts, I can read an LP file as a CplexMathProgModel. However, I would like to have a dictionary that maps the variables in the CplexMathProgModel to the variables in the LP file. I don’t think the variables in CplexMathProgModel have names. I wonder if you know some workaround that can get this done.

Thanks in advance.

Regards,  
Can

---

<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: [May 9, 2019, 3:31pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/8 "2019-05-09T15:31:21Z")

</div>

Check out [https://github.com/odow/LPWriter.jl](https://github.com/odow/LPWriter.jl). I haven’t tested on Julia 1.0 though. It might need some changes.

You can read and write MPS files using [https://github.com/odow/MathOptFormat.jl](https://github.com/odow/MathOptFormat.jl), but you can only write LP files, not read. There’s an open issue to finish the LP migration [https://github.com/odow/MathOptFormat.jl/issues/32](https://github.com/odow/MathOptFormat.jl/issues/32)

If you want to help, either updating LPWriter to Julia 1 or moving the LP reader to MathOptFormat would be a great contribution.

---

<div class="post-metadata">

### Author: ![jamalzadeh1400](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jamalzadeh1400](https://discourse.julialang.org/u/jamalzadeh1400)
#### Post date: [June 7, 2022, 6:47pm UTC](https://discourse.julialang.org/t/read-lp-files/6877/9 "2022-06-07T18:47:21Z")

</div>

JuMP has a function to write your CPLEX or Gurobi model in lp or mps format (not limited to):  
write\_to\_file(model, “filename.lp”)
