# Using an integer-valued variable as the number of summands in JuMP?

**URL:** <https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303>\
**Category:** Optimization (Mathematical)\
**Created:** [August 9, 2017, 2:01pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303 "2017-08-09T14:01:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaoweiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoweiz/32/2049_2.png) [@xiaoweiz](https://discourse.julialang.org/u/xiaoweiz)\
**Post date:** [August 9, 2017, 2:01pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303/1 "2017-08-09T14:01:03Z")

</div>

I’m trying to reformulate a stochastic program (approximately) as a mixed-integer linear program. A particular constraint involves the summation of `N` i.i.d. random variables, where `N` is a decision variable itself, something like

```julia
m = Model()
@variable(m, N >= 0, Int)
@variable(m, x >= 0)
@constraint(m, x <= sum(rand() for i=1:N))

```

The above formulation does not work. I also tried to replace `N` with `getvalue(N)` in the above constraint, but the solution doesn’t seem to make sense. Can anyone share some thoughts on this?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 9, 2017, 2:16pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303/2 "2017-08-09T14:16:46Z")

</div>

Since you’re trying to formulate a mixed-integer LP, the key question is: if you remove all the integrality constraints, do you end up with an LP? In this case, no, because the constraint doesn’t even make sense for real-valued `N`.

---

<div class="post-metadata">

**Author:** ![xiaoweiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoweiz/32/2049_2.png) [@xiaoweiz](https://discourse.julialang.org/u/xiaoweiz)\
**Post date:** [August 9, 2017, 2:30pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303/3 "2017-08-09T14:30:12Z")

</div>

Good point. It may not be an linear program. But can I still model this type of constraint in JuMP?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 9, 2017, 3:23pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303/4 "2017-08-09T15:23:15Z")

</div>

Your application is not clear, but you cannot use a decision variable to specify the number of terms in a constraint!

If your goal from `sum(rand() for i=1:N)` is to get a random number from 1 to N, you can do the following:

```julia
@constraint(m, x <= rand()*N)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 9, 2017, 6:43pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303/5 "2017-08-09T18:43:19Z")

</div>

I don’t think you can model it the way you’ve written it, but there’s another formulation that should be equivalent. Rather than trying to use N as a decision variable, you can represent your summation by creating a new binary decision variable associated with each of your random variables. Then you can use those binary variables to encode whether each random variable is included in the summation:

```julia
using JuMP, Cbc

m = Model(solver=CbcSolver())

# The number of random variables
Nmax = 10

# The random variables themselves
x = randn(Nmax)

# The z variables select which x are included in the sum
@variable m z[1:Nmax] Bin
y = sum(x .* z)

# This constraint means that z must always take the form
# [1, 1, ..., 1, 0, 0, ...., 0]
# which is equivalent to saying y = sum(x[1:N]) for some N
for i in 2:Nmax
    @constraint m z[i] <= z[i - 1]
end

@objective m Min y

solve(m)
@show x getvalue(y) getvalue(z)

```

which gives:

```julia
x = [0.557752, -0.31448, 0.721013, -0.141906, -1.37589, -0.343218, 0.369579, 2.0714, 0.674477, -0.953159]
getvalue(y) = -0.8967336932327254
getvalue(z) = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![xiaoweiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoweiz/32/2049_2.png) [@xiaoweiz](https://discourse.julialang.org/u/xiaoweiz)\
**Post date:** [August 9, 2017, 11:45pm UTC](https://discourse.julialang.org/t/using-an-integer-valued-variable-as-the-number-of-summands-in-jump/5303/6 "2017-08-09T23:45:18Z")

</div>

Thanks a lot!
