Formulating objective functions with Python

668 views
Skip to first unread message

Putra Manggala

unread,
Mar 30, 2010, 1:37:27 PM3/30/10
to gur...@googlegroups.com
Hi,

I can't seem to wrap my head around an objective function formulation using Python, to be solved using Gurobi. My decision variables are binary and I would like to maximize the value of choosing those variables while penalizing the objective function when the amount of variables chosen (value equal to 1) is far from a certain constant. For example, letting x_i be the 0-1 decision variable,

max (sum_i v_i*x_i) - penalty * |(sum_i x_i) - constant|

where v_i is the value of choosing variable i and I am reducing the objective function by penalty times the distance between the number of chosen variables and a constant.

using model.addVar, I have no problem formulating the first sum, but after that, should I be making a new variable to encapsulate the whole penalty expression? I also have trouble thinking about formulating the absolute expression, as I'm not sure how to say "if sum_ix_i>=constant" when formulating using addVar.

Maybe I'm lacking some knowledge in using another way of formulating the constraints, as far as I know, I can't formulate objective functions using linear expressions.

Thanks for your help,
Putra


Greg Glockner

unread,
Mar 31, 2010, 1:54:14 AM3/31/10
to gur...@googlegroups.com
The objective coefficients are expressed using the Obj attribute on the variables. If you want to define the objective via an expression, then introduce a new variable z. Set the Obj attribute to 1 on the new variable z and 0 on all other variables. Then define a new constraint where z equals your objective expression.

Mike Jung

unread,
Mar 31, 2010, 8:07:09 AM3/31/10
to Gurobi Optimization
Hi Putra,

you could model this by adding an additional continuous slack
variable, e.g. z, and then do the following:

max (sum_i v_i*x_i) - penalty*z

subject to
z >= sum_i x_i - constant
z >= - ( sum_i x_i - constant )

Since z has negative objective function coeficent, it will be as small
as possible and by limiting it from below by the positive and negative
value you get z >= | sum_i x_i - constant |
Together this should work the way you want to model this...
(It is a well known standard approach to model absolute values)

Best regards,
Michael Jung

Reply all
Reply to author
Forward
0 new messages