I am solving in R using GLPK a classical optimization problem:
min c*x
s.t. Ax<=b
Where x is my vector of variables, c is a vector of constants, A is the constraints matrix and b is the constraints vector.
Now my professor wants me to add the constraint that the positive terms in x must sum to 1 and the negative terms to -1.
To simplify things I though to implement it using the constraints
sum((x)_+) = 1
and
sum(x) = 0
I do not have much experience with R and I do not know which solvers allow me to write sum((x)_+) = 1 as a conditional constraint. I cannot use GLPK of course can someone help me to figure this out?
This is called a variable splitting technique. The formulation is
xplus[i] - xmin[i] = x[i]
xplus[i]*xmin[i] = 0 (make sure one of them is zero)
xplus[i], xmin[i] >= 0
Some solvers can handle this as is. GLPK is not one of them. Luckily the model can be linearized as:
xplus[i] - xmin[i] = x[i]
xplus[i] <= M*b
xmin[i] <= M*(1-b)
xplus[i], xmin[i] >= 0
b ∈ {0,1}
Here M
is bound on x: -M <= x <= M
. This is a constant. b[i]
is a binary variable.
Then your additional constraints are:
sum(i, xplus[i]) = 1
sum(i, xmin[i]) = 1
And, of course, you should talk to your professor if you don't understand the assignment.