Extra parameter in a callback function

587 views
Skip to first unread message

Laura Catalina Echeverri Guzmán

unread,
Sep 16, 2013, 1:00:31 PM9/16/13
to gur...@googlegroups.com
Hello everyone,

I'm writting a callback function for a model but I need to access to an object that is not in the model (it is not a variable neither a constraint, it is a matrix that is not used to set the model).  I see there are just two parameters allowed in a callback function, model and where; but, what if I need to access (in the callback function) to a matrix to set a new lazy constraint? How can I do it?

Best regards,
Laura Echeverri

Bjoern Simon

unread,
Sep 16, 2013, 2:43:55 PM9/16/13
to gur...@googlegroups.com
Hello Laura,

If the matrix is not part of the Gurobi model, then you cannot access it from Gurobi; hence you need to get/access it from another object outside Gurobi.

Regards,

Bjoern
--
 
---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Andy Jaworski

unread,
Sep 16, 2013, 5:29:20 PM9/16/13
to gur...@googlegroups.com
I am not sure is this is what you need, but you can auxiliary parameters to a Gurobi model.  Here is an example (I think you can find a similar example in Gurobi example codes) of my callback function:

def mycallback(model, where):
if where == GRB.callback.MIP:
time = model.cbGet(GRB.callback.RUNTIME)
objbst = model.cbGet(GRB.callback.MIP_OBJBST)
objbnd = model.cbGet(GRB.callback.MIP_OBJBND)
mipgap = objbst - objbnd
if mipgap <= model._gap and time > model._timelimit1:
print "termination 1"
model.terminate()
elif time > model._timelimit2:
print "termination 2"
model.terminate()

There are 2 different time limits and a gap parameters that have to be added to the model.

        m._timelimit1 = params1['timelimit1']
m._timelimit2 = params1['timelimit2']
m._gap = params1['mipgap']

Since I use this inside a Python function I pass the params1 dictionary to it from the outside.  It is simply

params1 = {"timelimit1": 300,
                 "timelimit2": 600,
                 "mipgap": 1}

I do not see why one of the parameters cannot be a matrix.

Hope this helps,
Andy

RobbyD

unread,
Sep 16, 2013, 8:24:12 PM9/16/13
to gur...@googlegroups.com
G'day Laura

From a .NET perspective this is an easy problem; in the declaration of the callback class, you simply pass the external data that you require access to in the callback routine. Eg:

Class MyCallback
    Inherits Gurobi.GRBCallback

    Private data1 As Object
    Private data2 As Integer

    Public Sub New(d1 As Object, d2 As Integer)
        Me.data1 = d1
        Me.data2 = d2
    End Sub

    Protected Overrides Sub Callback()

        Console.WriteLine(Me.data2)

    End Sub

End Class


I assume the other non-.NET options have something similar.
Cheers, Rob.

Laura Catalina Echeverri Guzmán

unread,
Sep 18, 2013, 11:35:37 AM9/18/13
to gur...@googlegroups.com
Thank you very much to all of you. 

In the end, as the matrix was defined outside the function callback, it behaved as a global variable for it, so I could access it, taking care to place the callback function in the right place.
Reply all
Reply to author
Forward
0 new messages