# How to pass value to cut generation function?

**URL:** <https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [January 3, 2019, 6:54am UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226 "2019-01-03T06:54:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)\
**Post date:** [January 3, 2019, 6:54am UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/1 "2019-01-03T06:54:16Z")

</div>

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,

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

```

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

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

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

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [January 3, 2019, 8:09am UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/2 "2019-01-03T08:09:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)\
**Post date:** [January 3, 2019, 8:28am UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/3 "2019-01-03T08:28:24Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [January 3, 2019, 9:09am UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/4 "2019-01-03T09:09:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)\
**Post date:** [January 3, 2019, 12:58pm UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/5 "2019-01-03T12:58:05Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)\
**Post date:** [January 3, 2019, 2:00pm UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/6 "2019-01-03T14:00:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [January 3, 2019, 2:37pm UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/7 "2019-01-03T14:37:38Z")

</div>

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

That’s because of lexical scoping (see the [Julia documentation](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)).

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

---

<div class="post-metadata">

**Author:** ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)\
**Post date:** [January 4, 2019, 4:20am UTC](https://discourse.julialang.org/t/how-to-pass-value-to-cut-generation-function/19226/8 "2019-01-04T04:20:00Z")

</div>

Thanks for the explanation!
