# JuMP/GLPK with equal upper and lower bounds

**URL:** <https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [September 6, 2019, 2:31pm UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469 "2019-09-06T14:31:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 6, 2019, 2:31pm UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/1 "2019-09-06T14:31:56Z")

</div>

Hello,

I’m solving a linear optimization problem with JuMP (0.19.2) using GLPK.jl (0.10.0) as the solver. I’m struggling with the situation where the lower and upper bounds of a variable are equal (and because it’s in a vector of inequalities, I can’t just simply remove the variable). Here is a MWE:

```julia
using JuMP #Call the modeling language
using GLPK # Call the solver 
probm = Model(with_optimizer(GLPK.Optimizer,))
 
@variable(probm, x[1:2])
xmax = [10., 0.]
@constraint(probm, 0. .<= x .<= xmax)
 
@objective(probm, Min, 0)

optimize!(probm)

```

The output is:

```julia
glp_simplex: row 2: lb = 0, ub = 0; incorrect bounds

```

I found related discussion on [GLPK mailing list](https://lists.gnu.org/archive/html/help-glpk/2011-07/msg00088.html), and a slightly similar thread here but [with Ipopt](https://discourse.julialang.org/t/fixing-vs-constraining-a-variable-in-jump/27609). However, I wonder if it’s possible to use GLPK here (with Clp it works). Maybe it’s possible to switch to using GLPK’s interior point solver, but I don’t know how. And maybe someone would have a better trick! Thanks for your help!

---

<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:** [September 6, 2019, 4:36pm UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/2 "2019-09-06T16:36:09Z")

</div>

So the issue is an `ScalarAffineFunction-in-Interval` constraint with identical bounds. There are multiple work-arounds:

```nohighlight
# Works
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, x == 1)  
optimize!(model)

# Works
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, 1 <= x <= 1) 
optimize!(model)

# Works
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, x)
@constraint(model, x == 1)
optimize!(model)

# Works
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, x)
@constraint(model, x >= 1)
@constraint(model, x <= 1)
optimize!(model)

# Doesn't work
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, x)
@constraint(model, 1 <= x <= 1)
optimize!(model)

```

Going forward, one option is to remove `ScalarAffineFunction-in-Interval` support from `GLPK.jl` and use a bridge to convert it into two constraints.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 9, 2019, 8:31am UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/3 "2019-09-09T08:31:31Z")

</div>

Thanks for the feedback. Among your propositions, I cannot take the ones with the `==` operator since I have vector bounds with some elements equal and some other unequals. However, splitting the constraint into two works (workaround 4), and using setting the variable bounds works as well (workaround 2).

I have one side question about the latter case, since I’m working with vectors. It seems I cannot set variable bounds using broadcasting notation:

```julia
xmax = [10., 0.]
@variable(model, 0 <= x[1:2] .<= xmax)

```

yields the error

```julia
In `@variable(model, 0 <= x[1:2] .<= xmax)`: Use the form lb <= ... <= ub.

Stacktrace:
 [1] error(::String, ::String) at ./error.jl:42
 [2] _macro_error(::Symbol, ::Tuple{Symbol,Expr}, ::String) at /home/pierre/.julia/packages/JuMP/ibcEh/src/macros.jl:1105
 [3] (::getfield(JuMP, Symbol("#_error#68")){Tuple{Symbol,Expr}})(::String) at /home/pierre/.julia/packages/JuMP/ibcEh/src/macros.jl:1341
 [4] parse_ternary_variable(::getfield(JuMP, Symbol("#_error#68")){Tuple{Symbol,Expr}}, ::JuMP._VariableInfoExpr, ::Val{:<=}, ::Int64, ::Val{:.<=}, ::Expr) at /home/pierre/.julia/packages/JuMP/ibcEh/src/macros.jl:1194
 [5] parse_variable(::Function, ::JuMP._VariableInfoExpr, ::Int64, ::Symbol, ::Expr, ::Symbol, ::Symbol) at /home/pierre/.julia/packages/JuMP/ibcEh/src/macros.jl:1199
 [6] @variable(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at /home/pierre/.julia/packages/JuMP/ibcEh/src/macros.jl:1376

```

Maybe this relates to [issue #1455 broadcasting a JuMParray](https://github.com/JuliaOpt/JuMP.jl/issues/1455)?

A working syntax is with using an explicit indexing variable:

```julia
@variable(model, 0 <= x[i=1:2] <= xmax[i])

```

Is it indeed the recommended way to define bounds on vector variables?

---

<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:** [September 9, 2019, 12:33pm UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/4 "2019-09-09T12:33:34Z")

</div>

Explicit indexing is the way to go. The error message could be improved in the previous case. Want to open a GitHub issue at JuMP.jl?

You could also open an issue at GLPK.jl to remove direct support for interval constraints.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 9, 2019, 2:34pm UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/5 "2019-09-09T14:34:43Z")

</div>

> [@odow](#):
>
> Explicit indexing is the way to go. The error message could be improved in the previous case. Want to open a GitHub issue at JuMP.jl?

Ok, here is the issue for JuMP error message:

> <https://github.com/jump-dev/JuMP.jl/issues/2056>
>
> Hello,
> 
> As suggested by @odow in \[this discussion\](https://discourse.julialang….org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/4), I report a possible enhancement of error message when one inadvertidly uses broadcasted vector bounds when creating a vector of JuMP variables.
> 
> Considering the following preamble code,
> \`\`\`julia
> using JuMP
> model = Model()
> xmax = \[10., 0.\]
> \`\`\`
> the correct way to have a variable \`x\<=xmax\` elementwise is by using an explicit indexing variable (which is \[documented\](http://www.juliaopt.org/JuMP.jl/stable/variables/#Variable-containers-1), but not exactly for this vector slicing usecase)
> 
> \`\`\`julia
> @variable(model, x\[i=1:2\] \<= xmax\[i\])
> \`\`\`
> 
> However, there is a wrong approach that someone can easily follow (at least I did -;)), using broadcasting on the comparison operator:
> \`\`\`julia
> @variable(model, x\[1:2\] .\<= xmax)
> \`\`\`
> which yields the surprising error message: 
> \`\`\`julia
> ERROR: LoadError: In \`@variable(model, x\[1:2\] .\<= xmax)\`: Unknown sense .\<=.
> \`\`\`
> 
> Also, in the case of two bounds, with the same mistake:
> \`\`\`julia
> @variable(model, 0 \<= x\[1:2\] .\<= xmax)
> \`\`\`
> the error message is different but also not helpful:
> \`\`\`julia
> ERROR: LoadError: In \`@variable(model, 0 \<= x\[1:2\] .\<= xmax)\`: Use the form lb \<= ... \<= ub.
> \`\`\`
> 
> Expected behavior: in an ideal world, the error message would point out to the correct solution (ex : "broadcasted constraints are not yet supported, use ... instead"). Alternatively, it may be worth supporting the broadcasted syntax, but I don't have enough background on JuMP to judge the difficulty of the task. This last idea may relate to the closed issue #1455, although it was about applying broadcasting on the \`set\_lower\_bound\` function not within the \`@variable\` macro.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 9, 2019, 2:57pm UTC](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds/28469/6 "2019-09-09T14:57:42Z")

</div>

> [@odow](#):
>
> You could also open an issue at GLPK.jl to remove direct support for interval constraints.

And here is the GLPK.jl issue:

> <https://github.com/jump-dev/GLPK.jl/issues/113>
>
> Hello,
> As a follow-up of this \[JuMP discussion\](https://discourse.julialang.org…/t/jump-glpk-with-equal-upper-and-lower-bounds), I'm reporting a corner case issue with \`ScalarAffineFunction-in-Interval\` constraints, when the lower and upper bounds are equal. Here is a MWE:
> 
> \`\`\`julia
> using JuMP
> using GLPK
> 
> model = Model(with\_optimizer(GLPK.Optimizer))
> 
> @variable(model, x\[1:2\])
> xmax = \[10., 0.\]
> @constraint(model, 0. .\<= x .\<= xmax)
> \`\`\`
> This is the output of the \`@constraint\` macro, with the two \`ScalarAffineFunction-in-Interval\` constraints:
> \`\`\`
> 2-element Array{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.Interval{Float64}},ScalarShape},1}:
> x\[1\] ∈ \[0.0, 10.0\]
> x\[2\] ∈ \[0.0, 0.0\] 
> \`\`\`
> 
> Now, when solving the model,
> \`\`\`julia
> @objective(model, Min, 0)
> optimize!(model)
> \`\`\`
> a GLPK error message is displayed (by the way, I'm not sure an actual Error is raised):
> \`\`\`
> glp\_simplex: row 2: lb = 0, ub = 0; incorrect bounds
> \`\`\`
> 
> As mentioned by @odow in the \[discussion\](https://discourse.julialang.org/t/jump-glpk-with-equal-upper-and-lower-bounds), there are workarounds (e.g.: splitting the interval constraint into two, or setting the bounds inside \`@variable\`), but if it were possible to have GLPK support this syntax like other solvers do (e.g. Clp), it would be nice.
> 
> Notice that there is a possibly related discussion on \[GLPK mailing-list\](https://lists.gnu.org/archive/html/help-glpk/2011-07/msg00088.html) “Can we set a row with equal upper and lower bound?”.
