# Array index which is an index of another array

**URL:** <https://discourse.julialang.org/t/array-index-which-is-an-index-of-another-array/37285>\
**Category:** Optimization (Mathematical)\
**Created:** [April 9, 2020, 4:05pm UTC](https://discourse.julialang.org/t/array-index-which-is-an-index-of-another-array/37285 "2020-04-09T16:05:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakub1998](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jakub1998](https://discourse.julialang.org/u/jakub1998)\
**Post date:** [April 9, 2020, 4:05pm UTC](https://discourse.julialang.org/t/array-index-which-is-an-index-of-another-array/37285/1 "2020-04-09T16:05:42Z")

</div>

I have to solve a problem with permutations. The function takes vector `a` with `n` elements as a parameter. I declare `b` as `@variable` - there should be the permutation `1:n` that gives the best result after finding the solution of a problem. The error appears when I want to create `@constraint` . I have to use `a[b[1]]` , so it takes the first element from vector which is a variable. It gives my error, that I can’t use type `VariableRef` as a index of an array. But how can I get around this when I have to use it?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 9, 2020, 4:13pm UTC](https://discourse.julialang.org/t/array-index-which-is-an-index-of-another-array/37285/2 "2020-04-09T16:13:27Z")

</div>

One option is to encode the permutation as a matrix; that’s what I did in TravelingSalesmanExact: [TravelingSalesmanExact.jl/TravelingSalesmanExact.jl at 61c0c350a11f091bb04dff8618d4c23885ed69c0 · ericphanson/TravelingSalesmanExact.jl · GitHub](https://github.com/ericphanson/TravelingSalesmanExact.jl/blob/61c0c350a11f091bb04dff8618d4c23885ed69c0/src/TravelingSalesmanExact.jl#L230). To permute a vector you would multiply the permutation matrix against the vector.

That uses binary variables, meaning it’s a mixed-integer problem and could be slow, but I don’t see how to encode a permutation without discrete constraints anyway.
