# How do I extract the coefficients and RHS's of all constraints in a JuMP linear program? (Need for KKT)

**URL:** <https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, linear-programming\
**Created:** [May 1, 2020, 3:14am UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525 "2020-05-01T03:14:43Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [May 1, 2020, 3:14am UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/1 "2020-05-01T03:14:43Z")

</div>

I am building a bilevel problem and I need to construct the KKT conditions of a linear program written in JuMP. I am wondering if it is possible to extract all the coefficients and right-hand-sides (RHS) of the constraints built in JuMP? What I need are the `A, C, b`, and `d` from:

```julia-auto
Ax=b
Cx<=b

```

where `x` is the decision vector.

---

<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:** [May 1, 2020, 7:09am UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/2 "2020-05-01T07:09:30Z")

</div>

Have a look at [BilevelOptimization.jl](https://github.com/matbesancon/BilevelOptimization.jl), it might already have code to do what you want.

---

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [May 1, 2020, 1:30pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/3 "2020-05-01T13:30:32Z")

</div>

Thanks @leethargo I have not seen that package. It will be helpful in formulating my bilevel problem in JuMP. However, it appears that it requires as input what I am trying to extract from a JuMP model; i.e. what BilevelOptimizationl.jl refers to as the lower-level constraint matrix and the lower-level slack variable.

---

<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 1, 2020, 1:48pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/4 "2020-05-01T13:48:19Z")

</div>

See [Constraints · JuMP](https://www.juliaopt.org/JuMP.jl/stable/constraints/#Accessing-constraints-from-a-model-1).

JuMP doesn’t have an easy way to get matrices, because it does not represent the problem in a `Ax=b`-type standard form.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 1, 2020, 2:22pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/5 "2020-05-01T14:22:50Z")

</div>

After we merge [this pull request](https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/pull/47) and release it, you can do it with `NLPModelsJuMP.jl` and `NLPModels.jl`:

```julia
using JuMP, NLPModelsJuMP, NLPModels
model = Model()
@variable(model, x[1:3])
@variable(model, y[1:3])
@objective(model, Min, sum(x[i] * i - y[i] * 2^i for i = 1:3))
@constraint(model, [i=1:2], x[i+1] == x[i] + y[i])
@constraint(model, [i=1:3], x[i] + y[i] <= 1)

nlp = MathOptNLPModel(model) # NLPModelsJuMP enters here
x = zeros(nlp.meta.nvar)
grad(nlp, x) # = c = [1, 2, 3, -2, -4, -8]
jac(nlp, x) # = A = 5x6 12 entries
cons(nlp, x) # = g = zeros(5)
nlp.meta.lcon, nlp.meta.ucon # l <= Ax + g <= u = ([0,0,-Inf,-Inf,-Inf], [0, 0, 1, 1, 1])
# constraint indexes for each situation
# [1, 2], [], [3, 4, 5], []
nlp.meta.jfix, nlp.meta.jlow, nlp.meta.jupp, nlp.meta.jrng

```

Currently the JuMP model has to be nonlinear for it to work, but after the PR is merged (probably today), it should work with the example above.

cf. @dpo @amontoison

---

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [May 1, 2020, 2:52pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/6 "2020-05-01T14:52:32Z")

</div>

@abelsiqueira that looks like it will accomplish what I am looking for! I will watch [NLPModelsJuMP.jl](https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl) for the release.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 1, 2020, 10:46pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/7 "2020-05-01T22:46:50Z")

</div>

NLPModelsJuMP 0.6.3 is released, here’s a terminal recording showing the example above: [asciicast:326018 - asciinema](https://asciinema.org/a/326018)

---

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [May 3, 2020, 3:07pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/8 "2020-05-03T15:07:25Z")

</div>

This is awesome! For others I’ll add a [link to the NLPModels readme](https://github.com/JuliaSmoothOptimizers/NLPModels.jl#attributes) that helped me work with NLPModelsJuMP.

@abelsiqueira I have a couple questions one of which I will create a new thread for. The first one is rather simple though: `cons(nlp, x)` in your example returns a vector of zeros; the documentation says  
`cons(model, x): evaluate c(x), the vector of general constraints at x`  
I don’t understand what `cons` is returning, can you please explain it in terms of your example?

---

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [May 3, 2020, 3:39pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/9 "2020-05-03T15:39:59Z")

</div>

Here is my more in depth, related question: [How to map JuMP VariableRef to NLPModelMeta indices?](https://discourse.julialang.org/t/how-to-map-jump-variableref-to-nlpmodelmeta-indices/38681)

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 3, 2020, 7:37pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/10 "2020-05-03T19:37:22Z")

</div>

NLPModels assumes a model with constraints of the form `lcon <= c(x) <= ucon`. It could be that `c(x) = Ax`, and `lcon = ucon = b`, to signify `Ax = b`, but it could also be that `c(x) = Ax - b`, and `lcon = ucon = 0` for the same problem. `cons(nlp, x) = c(x)`.  
Maybe for NLPModelsJuMP, there is some internal assumption on which form is used. @amontoison, do you know?

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [May 3, 2020, 8:27pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/11 "2020-05-03T20:27:41Z")

</div>

Its still not released, but you can take a look at [https://github.com/joaquimg/BilevelJuMP.jl](https://github.com/joaquimg/BilevelJuMP.jl)  
There many examples in the test folder to build bilevel problems in JuMP.

---

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [May 3, 2020, 9:59pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/12 "2020-05-03T21:59:31Z")

</div>

Thanks @joaquimg this looks really useful (I looked through some of the [examples](https://github.com/joaquimg/BilevelJuMP.jl/blob/master/test/jump.jl)). Is [this comment](https://github.com/joaquimg/BilevelJuMP.jl/blob/d18bb4831d5bb600548899505d6aecc2014692a1/src/moi.jl#L204) a TODO reminder or have you implemented the KKT conditions for a linear lower level problem?

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [May 3, 2020, 10:22pm UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/13 "2020-05-03T22:22:38Z")

</div>

KKT conditions are implemented.  
Thing is, there are a few ways to do that, that TODO is just a list of the many I could remember, most of them are already implemented.  
If you want a MIP formulations You should use SOS1Mode, if you want a NLP formulation you should use ProductMode as in the tests.  
Once you add the package (via cloning) you should be able to easily copy and paste the examples and run models.  
Feel free to open issues.

---

<div class="post-metadata">

**Author:** ![amontoison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amontoison/32/218741_2.png) [@amontoison](https://discourse.julialang.org/u/amontoison)\
**Post date:** [May 4, 2020, 2:37am UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/14 "2020-05-04T02:37:37Z")

</div>

In NLPModelsJuMP, linear constraints (lcon \<= Ax + b \<= ucon) are transformed internally to (lcon - b \<= Ax \<= ucon - b). We will only store the jacobian A and we evaluate the constraints with a sparse matrix vector product A \* x.

---

<div class="post-metadata">

**Author:** ![amontoison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amontoison/32/218741_2.png) [@amontoison](https://discourse.julialang.org/u/amontoison)\
**Post date:** [May 4, 2020, 3:28am UTC](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/15 "2020-05-04T03:28:04Z")

</div>

With this internal modification, is you only have inequality constraints `≥` in your JuMP model, `nlp = MathOptNLPModel(model)` is the standard form of your LP and `SlackModel(model)` is the canonical form.
