# Simplex implementation with A,b,c matrices

**URL:** <https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307>\
**Category:** Optimization (Mathematical)\
**Created:** [December 5, 2020, 4:13pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307 "2020-12-05T16:13:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [December 5, 2020, 4:13pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307/1 "2020-12-05T16:13:07Z")

</div>

Hello,

I would like to solve an LP

```julia
min cx
s.t. Ax = b

```

for which I have A, b and c all prepared. That is, I don’t want to go through a modeling phase. Is there a way to define a JuMP model directly with these components ?

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [December 5, 2020, 5:09pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307/2 "2020-12-05T17:09:58Z")

</div>

I suppose you also want to impose some more assumptions here, in particular something like x \>= 0, don’t you?

---

<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:** [December 5, 2020, 5:16pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307/3 "2020-12-05T17:16:18Z")

</div>

I think your best bet now would be to look at [https://github.com/jump-dev/MatrixOptInterface.jl](https://github.com/jump-dev/MatrixOptInterface.jl)

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [December 5, 2020, 5:50pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307/4 "2020-12-05T17:50:29Z")

</div>

You can do it with [https://github.com/JuliaSmoothOptimizers/QuadraticModels.jl](https://github.com/JuliaSmoothOptimizers/QuadraticModels.jl). Simply specify a zero Hessian (I opened an issue to add a convenience constructor for linear optimization). We currently have interfaces to Xpress ([https://github.com/JuliaSmoothOptimizers/QuadraticModelsXpress.jl](https://github.com/JuliaSmoothOptimizers/QuadraticModelsXpress.jl)), CPLEX ([https://github.com/JuliaSmoothOptimizers/QuadraticModelsCPLEX.jl](https://github.com/JuliaSmoothOptimizers/QuadraticModelsCPLEX.jl)) and Gurobi ([https://github.com/JuliaSmoothOptimizers/QuadraticModelsGurobi.jl](https://github.com/JuliaSmoothOptimizers/QuadraticModelsGurobi.jl)) with more coming. Only the barrier solvers are activated for now though. It should be easy to add the option to use simplex.

For an entirely free solver that is competitive with Gurobi, you can use RipQP ([https://github.com/JuliaSmoothOptimizers/RipQP.jl](https://github.com/JuliaSmoothOptimizers/RipQP.jl)). It’s also an interior-point method.

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [December 5, 2020, 5:56pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307/5 "2020-12-05T17:56:59Z")

</div>

Okay thank you all for your suggestions! I’ll have a look at all this.

---

<div class="post-metadata">

**Author:** ![JohnZ](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JohnZ](https://discourse.julialang.org/u/JohnZ)\
**Post date:** [December 5, 2020, 8:06pm UTC](https://discourse.julialang.org/t/simplex-implementation-with-a-b-c-matrices/51307/6 "2020-12-05T20:06:02Z")

</div>

For solving a model in the standard form, you don’t need a special syntax but you do need to know slack/surplus variables to interpret the solution. Here is how I would solve a LP in the standard form.

```julia
using JuMP, Clp
C =[-2.0,3.0,0.0, 0.0]
A=[1.0 1.0 1.0 0.0;
1.0 -1.0 0.0 1.0]
b= [4.0,6.0]
model = Model(Clp.Optimizer)
set_optimizer_attribute(model,"SolveType",1) # SolveType 1 is for primal simplex
@variable(model,x[1:4]>=0)
@objective(model,Min,C'*x)
@constraint(model,A*x.==b)
optimize!(model)
value.(x)      

```

Use `print(model)` to see the model you are solving:

```julia
julia> print(model)
Min -2 x[1] + 3 x[2]
Subject to
 x[1] + x[2] + x[3] == 4.0
 x[1] - x[2] + x[4] == 6.0
 x[1] >= 0.0
 x[2] >= 0.0
 x[3] >= 0.0
 x[4] >= 0.0

```

To define the same model in JuMP, I would use:

```julia
using JuMP, Clp
model = Model(Clp.Optimizer)
set_optimizer_attribute(model,"SolveType",1)
@variable(model,x[1:2]>=0)
@objective(model,Min,-2*x[1]+3*x[2])
@constraint(model,con_1,sum(x[i] for i in 1:2) <=4)
@constraint(model,con_2,x[1] - x[2] <=6)
optimize!(model)
value.(x)      

```

Now use `print(model)` and compare with the standard form

```julia
julia> print(model)
Min -2 x[1] + 3 x[2]
Subject to
 con_1 : x[1] + x[2] <= 4.0
 con_2 : x[1] - x[2] <= 6.0
 x[1] >= 0.0
 x[2] >= 0.0

```
