Search code examples
variablesoptimizationreferenceconstraintsjulia-jump

In JuMP How can we use VariableRef for mapping values?


I am trying to solve an optimisation problem in Julia Jump using Gurobi Pkg. I have created 2 matrices (A and B) that should be mapped using the 3rd one (Matrix C).

@variable(model, lb_C[i,j] <= C[i=1:m, j=1:n] <= ub_C[i,j], integer=true)
@variable(model, lb_X[i,j] <= A[i=1:m, j=1:n] <=  lb_X[i,j])
@variable(model, lb_X[i,j] <= B[i=1:m, j=1:n] <=  lb_X[i,j])

@constraint(model,[i in 1:m,j in 1:n],A[i,C[i,j]]==B[i,j],if C[i,j]!=0 end)

but I am getting error:

ArgumentError: invalid index: C[1,1] of type VariableRef

Can anyone one know how I can solve this? Thanks.


Solution

  • You cannot directly index variables like this.

    You need to formulate your problem as a mixed-integer linear program.

    One technique for this sort of thing are "Indicator constraints"

    # Add the constraint x == y if z == 1
    model = Model()
    @variable(model, x)
    @variable(model, y)
    @variable(model, z, Bin)
    @constraint(model, z => {x == y})
    

    The Mosek modeling cookbook has some good modeling tricks: