# Define a variable that depends on a previously defined variable in JuMP

**URL:** https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572
**Category:** New to Julia
**Tags:** jump
**Created:** [November 1, 2019, 1:56am UTC](https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572 "2019-11-01T01:56:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jayzyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayzyu/32/10782_2.png) [@Jayzyu](https://discourse.julialang.org/u/Jayzyu)
#### Post date: [November 1, 2019, 1:56am UTC](https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572/1 "2019-11-01T01:56:01Z")

</div>

Hello all,

I am trying to define a variable that is dependent on a previously defined variable. A close example is as follows:

```julia
N=10
nodes = 1:N
model=Model()
@variable(model,is_not_damaged_nodes[i in nodes], Bin)
@variable(model,ω[i in nodes[is_not_damaged_nodes.==0] ],Bin)

```

The error message is:

```julia
**ERROR: MethodError: no method matching iterate(::JuMP.JuMPArray{Variable,1,Tuple{UnitRange{Int64}}})**
Closest candidates are:
  iterate(::Core.SimpleVector) at essentials.jl:589
  iterate(::Core.SimpleVector, ::Any) at essentials.jl:589
  iterate(::ExponentialBackOff) at error.jl:171
  ...
Stacktrace:
 [1] copyto!(::Array{Variable,1}, ::JuMP.JuMPArray{Variable,1,Tuple{UnitRange{Int64}}}) at .\abstractarray.jl:646
 [2] _collect(::UnitRange{Int64}, ::JuMP.JuMPArray{Variable,1,Tuple{UnitRange{Int64}}}, ::Base.HasEltype, ::Base.HasLength) at .\array.jl:563
 [3] collect(::JuMP.JuMPArray{Variable,1,Tuple{UnitRange{Int64}}}) at .\array.jl:557
 [4] broadcastable(::JuMP.JuMPArray{Variable,1,Tuple{UnitRange{Int64}}}) at .\broadcast.jl:617
 [5] broadcasted(::Function, ::JuMP.JuMPArray{Variable,1,Tuple{UnitRange{Int64}}}, ::Int64) at .\broadcast.jl:1171
 [6] top-level scope at C:\Users\yuj5\.juliapro\JuliaPro_v1.0.5-1\packages\JuMP\I7whV\src\macros.jl:231

```

Could you give some suggestions to fix the bug? Thanks a lot!

JZ

---

<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: [November 1, 2019, 2:21am UTC](https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572/2 "2019-11-01T02:21:47Z")

</div>

You can’t define variables that depend on other variables like this.

What is the outcome you are trying to achieve? There’s probably a better way to do it.

---

<div class="post-metadata">

### Author: ![Jayzyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayzyu/32/10782_2.png) [@Jayzyu](https://discourse.julialang.org/u/Jayzyu)
#### Post date: [November 1, 2019, 3:27pm UTC](https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572/3 "2019-11-01T15:27:11Z")

</div>

Thank you for pointing out the error!

I am trying to formulate a network recovery problem. A part of this problem is to define a variable `ω` indicating whether or not damaged nodes in a network will be selected to be restored. Therefore, the number of `ω` is dependent on the number of damaged nodes, which is a random variable as well due to the uncertainty around the disruption.

---

<div class="post-metadata">

### Author: ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)
#### Post date: [November 5, 2019, 12:48am UTC](https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572/4 "2019-11-05T00:48:38Z")

</div>

One option depending on the rest of your model if you are willing to solve an integer program is to use a disjunctive constraint. Define Omega for all nodes and then add two constraints. The following pseudo code illustrates the idea:

```julia
Omega[i] <= M*(1-is_damaged_node[i])
Omega[i] >= -M*(1-is_damaged_node[i])

is_damaged_node[i] is binary for all I

```

And the variable M is chosen to be big enough that when `is_damaged_node[i] == 0` the variable `Omega[i]`is unconstrained and is otherwise set to zero.

Reference: despite the silly illustrations, the following presentation does work through several examples [Tutorials | Optimization Methods in Management Science | Sloan School of Management | MIT OpenCourseWare](https://ocw.mit.edu/courses/sloan-school-of-management/15-053-optimization-methods-in-management-science-spring-2013/tutorials/MIT15_053S13_tut09.pdf)

---

<div class="post-metadata">

### Author: ![Jayzyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayzyu/32/10782_2.png) [@Jayzyu](https://discourse.julialang.org/u/Jayzyu)
#### Post date: [November 5, 2019, 2:00am UTC](https://discourse.julialang.org/t/define-a-variable-that-depends-on-a-previously-defined-variable-in-jump/30572/5 "2019-11-05T02:00:28Z")

</div>

As Tom in the funny but wonderful illustration said, “It is a little tricky, but I like it.”

Thanks.
