How to pass value to cut generation function?

Hi all,

I am trying to write a user defined cut generation process. I meet the following problem, can you guys help me on this?

Suppose I am solving an optimization problem, whose input is a matrix W.

I formulate the problem in JuMP and try to solve it with gurobi,

model = Model( solver = GurobiSolver(  ) )
@variable( model , x )
@constraint...

It is a integer programming problem, and I want to add some problem specific cuts.

function MyCutGeneration( cb )
xx = getvalue( x )
@usercut( cb , ... )

The value of variables in model can be accessed easily, but my cut generation process requires the original matrix W, which is not known to the cut generation function. Simply tries

function MyCutGeneration( cb , W )

does not work, it still says W is not defined. How to make the original matrix available to cut generation function?

Can anyone help me on this? Thanks in advanced!

Qm

The variables of the model are only accessible because they are probably defined in the scope where your function MyCutGeneration is defined. If you put a W variable there for your matrix, it should work just the same.

Thanks for the reply! I am sorry that I missed a line that MyCutGeneration function is passed to the gurobi model I defined earlier.

addcutcallback( model , MyCutGeneration )

Hence, in my understanding, the variables of model can be accessed because MyCutGeneration is called during solving model.

So are you suggesting I should create some dummy variables in model just to remember value of W? I think it would be a possible way, but I still wonding if there is a simpler way to do this? Also I am not sure if getting too much dummy variables will affect the performance of solver.

Qm

Hence, in my understanding, the variables of model can be accessed because MyCutGeneration is called during solving model .

I don’t think this is correct.

So are you suggesting I should create some dummy variables in model just to remember value of W ?

No, I did not mean to add variables to the optimization models, but a Julia variable to hold the values of the matrix W. If I understand your first comment correctly, the matrix W is actually used to build the model, so it should already be available in some form?

This discussion would be easier, if you could post your complete code or some minimal version of it.

Hi, the minimal version of my code is indeed what I have provided.

W = readfile from some sourse
model = Model( solver = GurobiSolver( ) )

@variable( model , x ) , and some other variables

@constraint( model ,  ) , all constraints

function MyCutGeneration( cb )
  xx = getvalue( x )
  m = Model( solver = GurobiSolver( ) )
  set up cut generation problem with W, xx into an optimization problem m
  status = solve( m )
  @usercut( model , ... )
end

addcutcallback( model , MyCutGeneration )
statusIP = solve( model )

Everything is in a single file, the same scope. I have not defined any structure or module.

Qm

Sry guys. It seems I made some mistakes, the above program could be correctly excuted.

I am still curious about how could MyCutGeneration function access the outside matrix W.

Because in my understanding, julia is just writting a formulation to solver, the actual solving process is in gurobi.

If julia only gives the formulated problem to gurobi, how can the cut generation process( which is excuted during gurobi solving the problem ) access a variable that is in original julia program?

Qm

I am still curious about how could MyCutGeneration function access the outside matrix W .

That’s because of lexical scoping (see the Julia documentation).

MyCutGeneration is simply a Julia function that can access the local variables of it’s surrounding scope.
Gurobi will actually call that very Julia function via the callback mechanism.

Thanks for the explanation!