# JuMP - variable depending on other variable

**URL:** https://discourse.julialang.org/t/jump-variable-depending-on-other-variable/52529
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [December 28, 2020, 6:59pm UTC](https://discourse.julialang.org/t/jump-variable-depending-on-other-variable/52529 "2020-12-28T18:59:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![TheMEGuy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/themeguy/32/19862_2.png) [@TheMEGuy](https://discourse.julialang.org/u/TheMEGuy)
#### Post date: [December 28, 2020, 6:59pm UTC](https://discourse.julialang.org/t/jump-variable-depending-on-other-variable/52529/1 "2020-12-28T18:59:29Z")

</div>

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:

```julia
@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.

```julia
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 😃

---

<div class="post-metadata">

### Author: ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)
#### Post date: [December 28, 2020, 8:04pm UTC](https://discourse.julialang.org/t/jump-variable-depending-on-other-variable/52529/2 "2020-12-28T20:04:51Z")

</div>

Long story short: no 😕

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`.

- Do a binary search on `n`, e.g., if the objective varies monotonically w.r.t `n` (or in a “convex” way)

---

<div class="post-metadata">

### Author: ![TheMEGuy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/themeguy/32/19862_2.png) [@TheMEGuy](https://discourse.julialang.org/u/TheMEGuy)
#### Post date: [January 1, 2021, 11:15pm UTC](https://discourse.julialang.org/t/jump-variable-depending-on-other-variable/52529/3 "2021-01-01T23:15:03Z")

</div>

Thanks for your reply.

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