Modeling Gini Coefficient
AnsweredHi there,
I have a list of linear expressions for which I'd like to model the Gini-coefficient, so I can add a constraint to my model ensuring equality. The Gini-Coefficient for unsorted quantities is computed according to following formula, where each x corresponds to an entry in the list:

I encountered some difficulties programming this in the Python-API since the formula includes both division and absolute value. Maybe one of you can help.
Best
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Murat,
It is possible to model your constraint. However, you will need to introduce a couple of auxiliary variables and constraints. Ultimately, what you have to model is the following
\[\begin{align*}
GUK \cdot 2\cdot n \cdot \sum_i x_i &= \sum_i \sum_j w_{ij}\\
w_{ij} &= |z_{ij}| \quad \forall i,j \\
z_{ij} &= x_i - x_j \quad \forall i,j
\end{align*}\]The first constraint is a reformulation of the division by a variable similar to what is described in How do I divide by a variable in Gurobi? Note that you have to make sure that \(2\cdot n \cdot \sum_i x_i \neq 0\) to make this reformulation mathematically valid. If \(GUK\) is an optimization variable, then, your model is very likely nonconvex and you have to set the NonConvex parameter to 2.
To model the absolute values, you have to use the AddGenConstrAbs method. The reason for the auxiliary constraints is that the AddGenConstrAbs method accepts only single variables as input and output meaning that it is not allowed to directly provide the \(x_i - x_j\) expression as input.
Note that you have to explicitly state that the auxiliary variables \(z_{ij}\) can be negative, because the default lower bound for variables in Gurobi is \(0\), i.e., you have to do something similar to
z = model.addVars(I,J,lb=-GRB.INFINITY,name="z")
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments