Search code examples
juliajulia-jump

Julia JuMP Error when initializing variable: MethodError: no method matching (::Colon)(::Int64, ::Vector{Vector{Int64}})


I have a problem in Julia JuMP and I would really appreciate some help. I am currently trying to initialize a variable that I called X.

X represents the quantity of demand element e satisfied from production in block t at Machine n.

If I look into the model formulation I can see that n is element of N, e is element of E(n) and t is element of T(e).

we have 2 Machines so N = 2.

E(n), and T(e) should look something like this:

E(n) is set of demand elements e that can be produced on Machine 1 or 2

En = [[1, 2, 3, 4, 5, 6, 7, 8, 9], #Machine 1
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,...]] #Machine 2

T(e) represents the production periods for each of the demand elements

Te = [[1,2,3,4,5,6,7,8], # demand element 1
      [1,2,3,4,5,6,7,8], # 2
      [1,2,3,4,5,6,7,8], # 3
      [1,2,3,4,5,6,7,8], # etc.
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8],
      [1,2,3,4,5,6,7,8,9,10,11,12],
      [1,2,3,4,5,6,7,8,9,10,11,12],
      ...
      ...
      ...]]

Now I want to initialize X as follows:

**@variable(MO, X[1:N, 1:En[1:N]], 1:Te[1:E] >= 0);**

But I unfortunately get the error code: MethodError: no method matching (::Colon)(::Int64, ::Vector{Vector{Int64}})

I unfortunately don't have too much experience with Julia so I would be really grateful for any help or hints how I can properly initialize X without getting this error.

I tried different ways to define the ranges of n,e and t, but I always get errors.


Solution

  • @variable(MO, X[1:N, 1:En[1:N]], 1:Te[1:E] >= 0)

    You cannot write 1:En[1:N] because En[1:N] is a vector.

    julia> x = [1, 2, 3]
    3-element Vector{Int64}:
     1
     2
     3
    
    julia> 1:x
    ERROR: MethodError: no method matching (::Colon)(::Int64, ::Vector{Int64})
    Closest candidates are:
    

    You also have some brackets in the wrong place. Did you mean instead:

    @variable(MO, X[n=1:N, e=En[n], Te[e]] >= 0)

    Here's an example:

    julia> N = 2
    2
    
    julia> En = [ [1, 2], [1, 2, 3] ]
    2-element Vector{Vector{Int64}}:
     [1, 2]
     [1, 2, 3]
    
    julia> Te = [ [1], [1,2], [1,2,3,4] ]
    3-element Vector{Vector{Int64}}:
     [1]
     [1, 2]
     [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[n=1:N, e=En[n], Te[e]] >= 0)
    JuMP.Containers.SparseAxisArray{VariableRef, 3, Tuple{Int64, Int64, Int64}} with 10 entries:
      [1, 1, 1]  =  X[1,1,1]
      [1, 2, 1]  =  X[1,2,1]
      [1, 2, 2]  =  X[1,2,2]
      [2, 1, 1]  =  X[2,1,1]
      [2, 2, 1]  =  X[2,2,1]
      [2, 2, 2]  =  X[2,2,2]
      [2, 3, 1]  =  X[2,3,1]
      [2, 3, 2]  =  X[2,3,2]
      [2, 3, 3]  =  X[2,3,3]
      [2, 3, 4]  =  X[2,3,4]
    
    julia> X[2, 3, 2]
    X[2,3,2]