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

Hello all,

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

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:

**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

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.

1 Like

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.

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:

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 https://ocw.mit.edu/courses/sloan-school-of-management/15-053-optimization-methods-in-management-science-spring-2013/tutorials/MIT15_053S13_tut09.pdf

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

Thanks.