How to create my own function in gurobi with python, wich uses a variable as input.

90 views
Skip to first unread message

Hanzel Grillo Espinoza

unread,
Jun 21, 2016, 6:33:40 AM6/21/16
to Gurobi Optimization

Hello guys
I am trying to make a new way to solve a problem in supply chain, but I need to define a external function to calculate the price of the product. I am solving this directly in python.
My issue is that, I define a variable called price in the following way:

price = { }

for o in customerorder:
    for p in product:
            price[o, p] = m.addVar(prototype(duedate[o], harvestingtime[o, p], maximumshelflife[p]), name="price_{}_{}".format(o, p))

Where the definition of the function prototype(this is my own function) is the following:         


def prototype(duedate, harvestingtime, maximumshelflife):
   
t = duedate
h = harvtime
m = maximumshelflife
   
su = (a value defined by the user)
s2 = (a value defined by the user)
s1 = (a value defined by the user)
sl = (a value defined by the user)    
t1 = h + m*0.4
t2 = h + m*0.5       
t3 = h + m*0.7
t4 = h + m*0.8
t5 = h + m*0.9

 prot = 0

    try:
        if t < h:
            prot = su
        elif t >= h and t < t1:
            prot = su
        elif t >= t1 and t < t2:
            prot = (su*(t2-t)+s2*(t-t1))/(t2-t1)
        elif t >= t2 and t < t3:
            prot = s2
        elif t >= t3 and t < t4:
            prot = (s2*(t4-t)+s1*(t-t3))/(t4-t3)
        elif t >= t4 and t < t5:
            prot = s1
        elif t >= t5 and t < m:
            prot = (s1*(m-t)+sl*(t-t5))/(m-t5)
        elif t>=m:
            prot = 0
           
    except Exception:
        prot = 0
       
    return prot

Then my problem is that, this variable is always returning empty, error, or zero. It does not calculate anything.

Does anyone know how to handle an issue like this?

Thanks a lot in advance!

Hanzel Grillo Espinoza

unread,
Jun 21, 2016, 6:49:22 AM6/21/16
to Gurobi Optimization
I forgot to say that in this case the harvestingtime[o, p] is the pre-defined gurobi variable, and I think thats the reason the customized functions doesn't compute anything.

Ashley Thornton

unread,
Jun 22, 2016, 3:36:30 AM6/22/16
to Gurobi Optimization
I think you're misunderstanding the addVar function. It's not where you put expressions or values, it's literally just where you tell Gurobi that you're adding a variable to the problem. They take the form of:
    model.addVar(lb =<lower bound of the variable>,ub=<upper bound of the variable>, vtype=<Is it Continuous? Integer? Boolean?>, name=<what do you want to call it?>)
 
    then you can start adding constraints and defining your objective function USING the variables that you've added to the model. 

    I suggest you look at the example problems that are provided for guidance :) 

Hanzel Grillo Espinoza

unread,
Jun 22, 2016, 4:37:57 AM6/22/16
to Gurobi Optimization
Hello Ashley,
Yes I agree with you, but indeed I have already tried defining the variable as normal (as you suggest) but the result is the same.
My question is, how can I use what I described as protototype in a separated definition in order to calculate the value of the price?
Do you have any idea?

Ashley Thornton

unread,
Jun 22, 2016, 11:59:56 PM6/22/16
to Gurobi Optimization
For starters a description of what problem you're trying to solve would be helpful. 
Reply all
Reply to author
Forward
0 new messages