I'm trying to model a problem in GLPK but it turned out to be non linear.
A simplified version of the model is written below. Basically it is a weighted average of a set of features of all enabled points substracting a cost associated to enabling those points, provided there are exactly P enabled points.
$\max {\sum_{i=1}^N { f_i \times e_i \times w_i } \over \sum_{i=1}^N { e_i \times w_i } } - \sum_{i=1}^N {c_i \times e_i}$
Subject to constraint $\sum_{i=1}^N e_i = P$
Where:
- e (enabled) is a vector of either 0 or 1 (decision variable)
- f (feature) is a vector of decimal numbers in [0,1] (precomputed)
- w (weight) is a vector of decimal numbers in [0,1] (precomputed)
- c (cost) is a vector of decimal numbers in [0,1] (precomputed)
The model is very simple so I'm guessing there probably is a way to linearize it or some workaround I'm not aware of.
So the questions are these.
- Is there a way to linearize this model so I can solve it with GLPK or similar?
- Do you know any (free) software I can use to solve or approximate it as a non linear model?
Here is a simple example:
N = 5
P = 2
f = (0.1, 0.6, 0.5, 0.5, 0.8)
w = (0.2, 0.3, 0.2, 0.2, 0.1)
c = (0.5, 0.5, 0.5, 0.3, 0.5)
In this scenario the result should be this:
e = (0, 0, 0, 1, 1)
This can be linearized but with some effort. The ratio $$y=\frac{\sum_i f_i w_i x_i}{\sum_i w_i x_i}$$ with $x_i \in \{0,1\}$ can be written as a (nonlinear) constraint: $$ y \sum_i w_i x_i = \sum_i f_i w_i x_i$$ where $y$ is an additional continuous variable. The non-linear expression $(y x_i)$ is a continuous variable times a binary variable. I assume $y\ge 0$. We can now linearize $z_i=y x_i$ as: $$\begin{align} &z_i \le M x_i\\ &z_i \le y \\& z_i \ge y - M(1-x_i)\\ &z_i \ge 0\end{align} $$ Here $M$ is an upper bound on $y$. We have $M=1$ because of the values $w_i$ and $f_i$ can assume.
So putting things together we have: $$\begin{align} \max\> & y - \sum_i c_i x_i\\ & \sum_i w_i z_i = \sum_i f_i w_i x_i\\ & 0 \le z_i \le x_i \\ & y-(1-x_i) \le z_i \le y \\ &y\ge 0\\ & x_i \in \{0,1\} \end{align}$$
I cannot replicate your stated optimal solution. Your solution has:
When I solve it, I get a better solution:
The objective for $x=[0,1,0,1,0]$ is $-0.24$ while my optimal $x=[0,0,0,1,1]$ gives an objective value of $-0.2$. (Assuming no typos in transcribing the problem and data).
A similar problem is formulated here.