JKHA
JKHA

Reputation: 1896

Julia - JuMP constraints with array of tuples as indexes

The following is working in Julia JuMP:

@variable(m, δ[i=V, j=tildeV ; i != j && ŷ[i] < .5 && s[i,j] == sim_i[i]] >= 0)

While

Ω = [(i,j) for i in V, j in tildeV if i != j && ŷ[i] < .5 && s[i,j] == sim_i[i]]
@variable(m, δ[(i,j)=Ω] >= 0)

Results an error: ERROR: UndefVarError: i not defined

What am I doing wrong? I could not find in the documentation. I tried: δ[(i,j)...=Ω] and δ[(i,j)=Ω...]

You might want to use a simpler Ω for minimal example like

Ω = [(i,j) for i in 1:5, j in 1:5]

Upvotes: 0

Views: 234

Answers (1)

Oscar Dowson
Oscar Dowson

Reputation: 2574

These all work, so there must be a problem elsewhere in your code.

julia> S = [(1,2), (3,4)]
2-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (3, 4)

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[(i, j) = S])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, [(1, 2), (3, 4)]
And data, a 2-element Vector{VariableRef}:
 x[(1, 2)]
 x[(3, 4)]
julia> Ω = [(i,j) for i in 1:5, j in 1:5]
5×5 Matrix{Tuple{Int64, Int64}}:
 (1, 1)  (1, 2)  (1, 3)  (1, 4)  (1, 5)
 (2, 1)  (2, 2)  (2, 3)  (2, 4)  (2, 5)
 (3, 1)  (3, 2)  (3, 3)  (3, 4)  (3, 5)
 (4, 1)  (4, 2)  (4, 3)  (4, 4)  (4, 5)
 (5, 1)  (5, 2)  (5, 3)  (5, 4)  (5, 5)

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[(i, j) = Ω])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, [(1, 1) (1, 2) … (1, 4) (1, 5); (2, 1) (2, 2) … (2, 4) (2, 5); … ; (4, 1) (4, 2) … (4, 4) (4, 5); (5, 1) (5, 2) … (5, 4) (5, 5)]
And data, a 25-element Vector{VariableRef}:
 x[(1, 1)]
 x[(2, 1)]
 x[(3, 1)]
 x[(4, 1)]
 x[(5, 1)]
 x[(1, 2)]
 x[(2, 2)]
 x[(3, 2)]
 x[(4, 2)]
 x[(5, 2)]
 ⋮
 x[(2, 4)]
 x[(3, 4)]
 x[(4, 4)]
 x[(5, 4)]
 x[(1, 5)]
 x[(2, 5)]
 x[(3, 5)]
 x[(4, 5)]
 x[(5, 5)]

Upvotes: 2

Related Questions