Array index which is an index of another array

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?

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

3 Likes