# Specifying variable dimension in JuMP

**URL:** https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716
**Category:** Optimization (Mathematical)
**Tags:** jump, vector
**Created:** [July 18, 2023, 12:57am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716 "2023-07-18T00:57:19Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 12:57am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/1 "2023-07-18T00:57:19Z")

</div>

This might not be JuMP specific, but I’m trying to learn how to use JuMP for LPs.

Do I need to specify the size of the variable x? It should be a 2-d vector. If so, how?

My goal is to solve:  
$$\max\_x cx $$ subject to $$Ax \<= b, x\>=0$$.  
A is the identity matrix. c = [1,0], b = [1,1].

Below is a picture of my code and output of the `print model` command. When I print the model it outputs weird inequalities suggesting something is wrong. See screenshot:

 ![Screen Shot 2023-07-17 at 9.20.46 PM](https://global.discourse-cdn.com/julialang/original/3X/b/2/b21cc86710893f134e04b74802cf9c8bc33adbfb.png)

For example, ne of the inequalities has 0\<1, for example. I was expecting it to print something like the following for the constraints:

```julia
x_1 + 0*x_2 <= 1,
 0*x_1 + x_2 <= 1,
 x_1 >=0, x_2 >= 0 .

```

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 2:10am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/2 "2023-07-18T02:10:34Z")

</div>

OP edited - the following doesn’t apply anymore:  
~~Replacing `@constraint(model, A*x <= b)` with `@constraint(model, A*x .<= b)` should do the trick.~~

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 2:22am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/3 "2023-07-18T02:22:40Z")

</div>

Ok thanks that runs now, but it does appear to be quite what I want. See printed output below.  
Maybe I’m not properly specifying the vectors `b` and `c` or the matrix `A`? Or do I need to somehow specify the type of `x`?

 ![Screen Shot 2023-07-17 at 9.20.46 PM](https://global.discourse-cdn.com/julialang/original/3X/b/2/b21cc86710893f134e04b74802cf9c8bc33adbfb.png)

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 2:28am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/4 "2023-07-18T02:28:18Z")

</div>

I am not sure what your goal is here - so it is hard to evaluate what actually happens in the code versus your expectations.

Can you add some details?

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 2:30am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/5 "2023-07-18T02:30:17Z")

</div>

My goal is to solve $$\max\_x cx $$ subject to $$Ax \<= b, x\>=0$$

A is the identity matrix. c = [1,0], b = [1,1].

When it prints the model, the output is very weird (see above). One of the inequalities has 0\<1, for example. I was expecting it to print something like the following for the constraints:

```julia
x_1 + 0*x_2 <= 1,
 0*x_1 + x_2 <= 1,
 x_1 >=0, x_2 >= 0 .

```

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 2:49am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/6 "2023-07-18T02:49:29Z")

</div>

How about setting your variable in this way: `@variable(model, x[1:2] >= 0)` and reverting the constraint to `@constraint(model, A*x <= b)`?

I think this satisfies your constraints (`[x[1] - 1, x[2] - 1] ∈ MathOptInterface.Nonpositives(2)` is equivalent to your `x_1 + 0*x_2 <= 1, 0*x_1 + x_2 <= 1`).

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 2:57am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/7 "2023-07-18T02:57:50Z")

</div>

> [@algunion](#):
>
> How about setting your variable in this way: `@variable(model, x[1:2] >= 0)` and reverting the constraint to `@constraint(model, A*x <= b)`?

gives the error:  
`@constraint(model, $(Expr(:escape, :A)) * $(Expr(:escape, :x)) <= b)`: Unexpected vector in scalar constraint.

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 2:59am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/8 "2023-07-18T02:59:13Z")

</div>

This is the version that works on my end:

```julia
A = [1 0; 0 1]
b = [1;1]
c = [1,0]

model = Model(HiGHS.Optimizer)
set_silent(model)

@variable(model, x[1:2] >= 0)

@constraint(model, A*x <= b)

@objective(model, Max, sum(c .* x));

optimize!(model)

print(model)
#Subject to
# [x[1] - 1, x[2] - 1] ∈ MathOptInterface.Nonpositives(2)
# x[1] ≥ 0
# x[2] ≥ 0

```

Are you sure you didn’t change stuff on other parts? Please try to execute the above.

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 3:02am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/9 "2023-07-18T03:02:34Z")

</div>

Just copied your code. Still getting same error:  
`@constraint(model, $(Expr(:escape, :A)) * $(Expr(:escape, :x)) <= b)`: Unexpected vector in scalar constraint. Did you mean to use the dot comparison operators like .==, .\<=, and .\>= instead?

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 3:04am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/10 "2023-07-18T03:04:05Z")

</div>

> [@algunion](#):
>
> I think this satisfies your constraints (`[x[1] - 1, x[2] - 1] ∈ MathOptInterface.Nonpositives(2)` is equivalent to your `x_1 + 0*x_2 <= 1, 0*x_1 + x_2 <= 1`).

This seems to work ok I guess:

 ![Screen Shot 2023-07-17 at 9.58.57 PM](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c8ed6157217ae95600305a1eae57862bbfe50f0.png)

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 3:05am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/11 "2023-07-18T03:05:52Z")

</div>

Ok I think this is what I want:

 ![Screen Shot 2023-07-17 at 10.05.07 PM](https://global.discourse-cdn.com/julialang/original/3X/e/0/e054fba1d87231b9babb9149e8174935428e2cef.png)

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 3:07am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/12 "2023-07-18T03:07:20Z")

</div>

Thanks for all this help, but this seems awful difficult. Are you an expert in JuMP? If not, maybe I should wait for someone with specialization in this package?

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 3:08am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/13 "2023-07-18T03:08:43Z")

</div>

You seem to work in a Jupyter notebook there - you might get into spaghetti issues with the cells: please make sure you are executing the `model = Model(HiGHS.Optimizer)` before running the other operations on the model.

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 3:12am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/14 "2023-07-18T03:12:40Z")

</div>

Yeah I’m in Jupyter notebook. I executed that line first. I also tried restarting the kernel to make sure no other cell is messing with things. I still get same error with the matrix approach (i.e. without specifying the components of x explicitly).

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 3:20am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/15 "2023-07-18T03:20:09Z")

</div>

Here you have my vs-code naive execution - it just works:

Anyway - to answer your question: I cannot call myself an expert on JuMP - so if you need a contribution at that level for your specific problem, I think it is best to leave this to other users that might be more qualified.

However, for reference, here is the solution I proposed (which you seem to have issues in executing without getting an error):

```julia
A = [1 0; 0 1]
b = [1;1]
c = [1,0]

model = Model(HiGHS.Optimizer)
set_silent(model)

@variable(model, x[1:2] >= 0)

@constraint(model, A*x <= b)

@objective(model, Max, sum(c .* x));

optimize!(model)

print(model)
#Subject to
# [x[1] - 1, x[2] - 1] ∈ MathOptInterface.Nonpositives(2)
# x[1] ≥ 0
# x[2] ≥ 0

```

I wish you luck.

P. S. I see that you actually added `[x[1] - 1, x[2] - 1] ∈ MathOptInterface.Nonpositives(2)` to your constraint: please note that I indicated that as an output that satisfied your stated expectations, not instructed actually to build a constraint from that. The actual instruction was above that paragraph:

> [@algunion](#):
>
> How about setting your variable in this way: `@variable(model, x[1:2] >= 0)` and reverting the constraint to `@constraint(model, A*x <= b)`?

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [July 18, 2023, 3:24am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/16 "2023-07-18T03:24:35Z")

</div>

Thank you! Maybe I’ll try it in VScode and see how it goes. I’m probably just doing something stupid somewhere.

---

<div class="post-metadata">

### Author: ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)
#### Post date: [July 18, 2023, 7:39am UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/17 "2023-07-18T07:39:02Z")

</div>

I was intrigued by what’s happening, so here is my two cents. First case:

```julia
@variable(model, x >= 0) ###Creates a scalar variable x
@constraint(model, A*x .<= b)

```

JuMP doesn’t know that `x` is supposed to be a vector. If x is a scalar, then `A*x`=[x 0;0 x]. Using `A*x.<=b` gives a column-wise comparisons: [x;0]\<=[1;1] and [0;x]\<=[1;1], which, in turn, give the element-wise scalar constraints x\<=1, 0\<=1, 0\<=1, x\<=1.

Telling JuMP that x is a vector

```julia
@variable(model, x[1:2] >= 0) ###Creates a vector variable x with x[1] and x[2]

```

If x is a vector of size 2x1, then `A*x` is also a vector of size 2x1. And whereas both `A*x<=b` and `A*x.<=b` give mathematically the same constraints, numerically they can differ, see: [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Vectorized-constraints).

Using `@constraint(model, A*x .<= b)` we get four separate scalar constraints (two from the matrix multiplication, two from the definition of x):  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/7/37c3e84d90e91d5c2819da8b705f19c8a825e55d.png)  
Using `@constraint(model, A*x <= b)` we get one vector constraint from the matrix multiplication and two scalar constraints from the definition of x:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/4/d4fe985c190aa2bc461a56fcc86b7be108fffbc0.png)

---

<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 18, 2023, 1:20pm UTC](https://discourse.julialang.org/t/specifying-variable-dimension-in-jump/101716/18 "2023-07-18T13:20:32Z")

</div>

If you’re getting the escape error, its because you’re using an older version of JuMP.

Update your packages, or use the .\>= syntax.
