JuMP - variable depending on other variable

Hi,

I have a variable that is supposed to be a matrix. Its size should depend on another variable, which also should be optimized, or at least changed throughout the optimization process:

@variable(model, n,lower_bound = 0,integer = true)
@variable(model, NodeMatrix[1:20,1:n],lower_bound = 0,integer = true)

I get this result if I try to link the variables.

ERROR: MethodError: no method matching (::Colon)(::Int64, ::VariableRef)
Closest candidates are:
  Any(::T, ::Any, ::T) where T<:Real at range.jl:41
  Any(::A, ::Any, ::C) where {A<:Real, C<:Real} at range.jl:10
  Any(::T, ::Any, ::T) where T at range.jl:40
  ...
Stacktrace:
 [1] top-level scope at C:\Users\Keven\.julia\packages\JuMP\e0Uc2\src\macros.jl:91
 [2] top-level scope at REPL[6]:1

Based on the size of the second dimension of the “NodeMatrix”, my program yields different results.

Is there a way to link the variables?

Thanks :smiley:

Long story short: no :confused:

It is not possible to make the dimension of a decision variable depend on the value of another decision variable.
Without knowing more about your problem, it’s hard to say much more.

Here are some (very generic and likely inefficient) alternatives:

  • If you have a hard, upper bound on n, e.g., n <= N for some not-too-large integer N.

    • Introduce N binary variables x1, ..., xN, with the constraints x1 >= x2 >= ... >= xN,
      and write n = x1 + ... + xN.
    • Create a NodeMatrix[1:20, 1:N] variable
    • Add N big-M like constraints so that, if xk == 0, then the k-th column of NodeMatrix is set to zero. To do so, you need a known upper bound on the maximum value NodeMatrix can take (coefficient-wise).
  • Do a binary search on n, e.g., if the objective varies monotonically w.r.t n (or in a “convex” way)

1 Like

Thanks for your reply.

I guess I need to do some research to find another way to approach my problem.