How to create a computed variable in python

217 views
Skip to first unread message

Hanzel Grillo Espinoza

unread,
Jun 15, 2016, 9:58:04 AM6/15/16
to Gurobi Optimization
Hello guys
I am creating a model that has just one decision variable (binary) called Y.
If I have another element in the model called S which is just the result of a computation of the binary variable with other input data, for example the input parameter of cost c. then S = c*Y.
How can I write this in my model in Python?
Specifically, should I define S as another variable of the model with the function addVar()? or how can I avoid that? I am thinking in a simpler resolution process for to the solver, since if I just define S as "something" calculated, it would avoid the model to generate values of the variable and then make them equal to c*Y. How can I do this in the code?
I guess it is a basic question, but I am beginner in the modelling directly in python.

Thanks a lot in advance.

Tobias Achterberg

unread,
Jun 15, 2016, 2:58:05 PM6/15/16
to gur...@googlegroups.com
There are basically two ways:

1. You do not use S in your Gurobi model but instead calculate it yourself after
Gurobi found an optimal value for your Y variable. Then, all this has nothing to
do with Gurobi---you would just use regular Python statements to calculate S.

2. You add a new variable using addVar() for S with infinite lower and upper
bound and add an equality constraint that states S = c*Y. Then, Gurobi will
calculate the value of S for you. Note that in terms of performance this
additional variable and constraint is most likely not an issue at all, because
the presolver of Gurobi (more precisely, the aggregator) will eliminate S and
the equation from the model and calculate the value of S in a post-processing
step, just like what you would have done manually in approach 1 above.

Hope this helps,

Tobias

Hanzel Grillo Espinoza

unread,
Jun 16, 2016, 9:33:31 AM6/16/16
to Gurobi Optimization
Thanks a lot Tobias
I think I will go with the second option then.
Greetings!
Reply all
Reply to author
Forward
0 new messages