Setting up an Minimizing the sum of absolute deviation linear programing problem in CPlex - linear-programming

I am new to CPLEx and trying to set up my first problem. What I want to do set up a LP to minimise the sum of absolute deviations. I have set up the below as a start (based on googling possibilities). This is only a single deviation. I thought I would get this to work and then add to ti. It loads ok but won't solve. Can anyone shed some light on where I need to go next?
Minimize
obj: y1pos + y1neg
Subject To
c1: x0 + x1 + x2 + x3 = 1
c2: y1pos - y1neg + 451320 x0 + 500870 x1 + 483425 x2 + 447330 x3 = 58999
Bounds
0 <= x0 <= 1
0 <= x1 <= 1
0 <= x2 <= 1
0 <= x3 <= 1
y1pos >= 0
y1neg <= 0
End

As Erwin Kalvelagen suggested, changing y1neg <= 0 to y1neg >= 0 was the answer since our our error factor in our constraint is y1pos - y1neg which we want to minimise.

Related

National competition in programming math problem

I encountered this problem practicing for an upcoming national competition. The problem goes as follows: You need to create a mixture of two ingredients being in relation to 1:1. You are given N different mixtures, each having its own weight Wi, and its relation in the mixture between the ingredients Mi, Ti (Each value, N, Wi, Mi, and Ti, will be less than 100). We need to find the biggest possible weight of the final mixture, keeping the relation to 1:1. We can take from each given mixture how much we want, we don't necessarily need to take the whole mixture, we can take some portion of it.
So with the given relation 1:1 in the final mixture, we know that we need to have an equal amount of weight from both ingredients possible. After that I need to know if I take K grams of some mixture, how much weight that is for ingredients A and B. So I came up with the following formula:
Let W be the weight in grams, and M and T be the relation between the ingredients respectively. If we want to take K (K <= W) grams we have the following:
Weight of ingredient A = M * (K / (M+T))
Weight of ingredient B = T * (K / (M+T))
#include <bits/stdc++.h>
using namespace std;
class state{
public:
int weight;
int A;
int B;
};
int n;
vector<state> arr;
double ans= 0;
void f(double weight_A, double weight_B, int idx){
if(weight_A == weight_B)
ans = max(ans, weight_A + weight_B);
if(idx >= n)
return;
int weight = arr[idx].weight, relA = arr[idx].A, relB = arr[idx].B;
for(int K = 0; K <= weight; K++){
f(weight_A + relA * (K * 1.0/(relA + relB)), weight_B + relB * (K * 1.0/(relA + relB)), idx+1);
}
}
int main(){
cin>>n;
for(int i = 0; i < n; i++){
state in;
cin>>in.weight>>in.A>>in.B;
arr.push_back(in);
}
f(0.0, 0.0, 0);
cout<<fixed<<setprecision(8);
cout<<ans<<endl;
}
The problem I encountered was that we don't necessarily need to take integer weights, some times to achieve the maximum possible weight of the final product we need to take decimal weights. Let's take a look at this example:
5
14 3 2
4 1 3
4 2 2
6 6 1
10 4 3
We have N = 5, and in each row are given 3 integers, Wi, Mi, and Ti. The weight of the ith mixture and its relation. My solution for this example gives 20.0000, and the correct solution for the above example is 20.85714286. Looking back my initial idea won't work because of the decimal numbers. I suppose there is some formula but I can't figure it out, can anyone help?
This is a Linear Programming problem, so you can solve it by constructing the problem in standard form, and then solve it with an optimization algorithm, like the simplex algorithm.
The objective is to maximize the quantity of medicine (from the original problem), that is the sum of quantities taken from each jar (I'll call the quantities x1, x2, ...).
The quantities are bounded to be lower than the weight Wi available in each jar.
The constraint is that the total amount of honey (first ingredient) is equal to the total amount of tahini (second ingredient). This would mean that:
sum(Mi/(Mi+Ti)*xi) = sum(Ti/(Mi+Ti)*xi)
You can take the second summation to the LHS and get:
sum((Mi-Ti)/(Mi+Ti)*xi) = 0
In order to get integer multipliers just multiply everything by the least common multiple of the denominators lcm(Mi+ti) and then divide by the gcd of the coefficients.
Using your example, the constraint would be:
(3-2)/(3+2) x1 + (1-3)/(1+3) x2 + (2-2)/(2+2) x3 + (6-1)/(6+1) x4 + (4-3)/(4+3) x5 = 0
that is
1/5 x1 -2/4 x2 + 0/4 x3 + 5/7 x4 + 1/7 x5 = 0
Multiply by the lcm(5,4,4,7,7)=140:
28 x1 -70 x2 + 0 x3 + 100 x4 + 20 x5 = 0
divide by 2:
14 x1 -35 x2 +0 x3 + 50 x4 + 10 x5 = 0
We are ready to solve the problem. Let's write it in CPLEX format:
maximize
quantity: x1 + x2 + x3 + x4 + x5
subject to
mix: 14 x1 -35 x2 +0 x3 + 50 x4 + 10 x5 = 0
bounds
x1 <= 14
x2 <= 4
x3 <= 4
x4 <= 6
x5 <= 10
end
Feed it to GLPK:
#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>
int main(void)
{
glp_prob *P;
P = glp_create_prob();
glp_read_lp(P, NULL, "problem.cplex");
glp_adv_basis(P, 0);
glp_simplex(P, NULL);
glp_print_sol(P, "output.txt");
glp_delete_prob(P);
return 0;
}
And the output is:
Problem:
Rows: 1
Columns: 5
Non-zeros: 4
Status: OPTIMAL
Objective: quantity = 20.85714286 (MAXimum)
No. Row name St Activity Lower bound Upper bound Marginal
------ ------------ -- ------------- ------------- ------------- -------------
1 mix NS 0 0 = 0.0714286
No. Column name St Activity Lower bound Upper bound Marginal
------ ------------ -- ------------- ------------- ------------- -------------
1 x1 B 2.85714 0 14
2 x2 NU 4 0 4 3.5
3 x3 NU 4 0 4 1
4 x4 NL 0 0 6 -2.57143
5 x5 NU 10 0 10 0.285714
Karush-Kuhn-Tucker optimality conditions:
KKT.PE: max.abs.err = 0.00e+00 on row 0
max.rel.err = 0.00e+00 on row 0
High quality
KKT.PB: max.abs.err = 0.00e+00 on row 0
max.rel.err = 0.00e+00 on row 0
High quality
KKT.DE: max.abs.err = 0.00e+00 on column 0
max.rel.err = 0.00e+00 on column 0
High quality
KKT.DB: max.abs.err = 0.00e+00 on row 0
max.rel.err = 0.00e+00 on row 0
High quality
End of output
Of course given your input you should construct the problem in memory and feed it to the simplex algorithm without going through a file. Additionally, there's no need to get integer coefficients, it was just to allow a nicer problem formulation.

LPSolve IDE cannot find solution

I have following problem that I try to solve with LPSolve IDE:
min: x1;
r_1: 1.08 - k <= x1;
r_2: -1.08 + k <= x1;
c_1: y1 + y2 + y3 = k;
c_2: 2.29 a1 y1 + 2.28 a2 y1 + 2.27 a3 y1 = 1;
c_3: 1.88 b1 y2 + 1.89 b2 y2 + 1.9 b3 y2 = 1;
c_4: 8.98 c1 y3 + 8.99 c2 y3 + 9.0 c3 y3 = 1;
c_14: a1+a2+a3=1;
c_15: b1+b2+b3=1;
c_16: c1+c2+c3=1;
bin a1,a2,a3,b1,b2,b3,c1,c2,c3;
Not sure why I get output from LPSolve as INFEASIBLE when I can use following param values to solve this:
a1=0, a2=1, a3=0
b1=0, b2=1, b3=0
c1=0, c2=1, c3=0
0 + 2.28 0.438596491 + 0 = 1
0 + 1.89 0.529100529 + 0 = 1
0 + 8.99 0.111234705 + 0 = 1
0.438596491 + 0.529100529 + 0.111234705 = 1.0789 (this is k)
1.08 - 1.0789 == 0.0011 <= x1
-1.08 + 1.0789 == -0.0011 <= x1
x1 = 0.0011
Am I formulating the problem in a wrong way, or doing something else wrong? If I relax that =1 constraint to >=1 there are some results, but I need it to be 1 (as it is in my solution).
Lpsolve is for linear models only. You have products of variables in the model such as 2.29 a1 y1. Lpsolve can not solve such quadratic models.
Too bad you don't get a good error message. I guess they never expected this input.
It is noted that products of binary and continuous variables can be linearized resulting in so-called big-M constraints (see link).
This is really a duplicate of lpsolve - unfeasible solution, but I have example of 1. Embarrassingly, this was an earlier question from the same poster!

Having negative value for non basic variable gives a non feasible solution in simplex method?

Objective function => x1 - 2x2
Subject to =>
x2 <= 5
x1 - x2 >= 2
x1 ,x2, x3 >= 0
Maximize?
convert to standard form :
Maximize -> -x1 + 2x2
Subject to ->
x2 <= 5
-x1 + x2 <= -2
convert to slack form :
Z = -x1 + 2x2
x3 = 5 - x2
x4 = -2 +x1 -x2
Basic solution (0,0,5,-2)
Can I found optimal solution in here? If not why?

SoPlex yields wrong answer

I have an LP in CPLEX LP format, in file LP.tmp
Maximize
obj: x0 + 2 x1 + 3 x2 + 4 x3 + 5 x4 + 7 x5
Subject To
c0: 1 x0 + 1 x1 + 1 x2 + 1 x3 + 1 x4 + 1 x5 + 1 x6 + 1 x7 + 1 x8 = 0
End
On this I call soplex -X -x -o0 -f0 -v3 LP.tmp
This is obviously unbounded, but calling Soplex gives me the answer (with some other lines).
SoPlex status : problem is solved [optimal]
Solving time (sec) : 0.00
Iterations : 0
Objective value : 0.00000000e+00
Primal solution (name, value):
All other variables are zero (within 1.0e-16). Solution has 0 nonzero entries.
Background: Originally, I had objective 0, but box constraints, and I always got infeasible. So I reduced everything, until I arrived at the above.
What am I doing wrong?
All variables are non-negative by default in the lp file format, see https://www.ibm.com/support/knowledgecenter/SSSA5P_12.5.0/ilog.odms.cplex.help/CPLEX/FileFormats/topics/LP.html
Therefore, your constraint fixes all variables to 0. As soon as you change the coefficient of any of the variables but x5 to -1 or add a bounds section where you define it to be free, e.g., x1 free, SoPlex claims unboundedness and provides a valid primal ray.
This model is not unbounded. There are implicit bounds of 0 on all variables, so the only feasible and hence optimal solution is the one SoPlex returns.
In the .lp data format, all variables are non-negative by default.

Linear index upper triangular matrix

If I have the upper triangular portion of a matrix, offset above the diagonal, stored as a linear array, how can the (i,j) indices of a matrix element be extracted from the linear index of the array?
For example, the linear array [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 is storage for the matrix
0 a0 a1 a2 a3
0 0 a4 a5 a6
0 0 0 a7 a8
0 0 0 0 a9
0 0 0 0 0
And we want to know the (i,j) index in the array corresponding to an offset in the linear matrix, without recursion.
A suitable result, k2ij(int k, int n) -> (int, int) would satisfy, for example
k2ij(k=0, n=5) = (0, 1)
k2ij(k=1, n=5) = (0, 2)
k2ij(k=2, n=5) = (0, 3)
k2ij(k=3, n=5) = (0, 4)
k2ij(k=4, n=5) = (1, 2)
k2ij(k=5, n=5) = (1, 3)
[etc]
The equations going from linear index to (i,j) index are
i = n - 2 - floor(sqrt(-8*k + 4*n*(n-1)-7)/2.0 - 0.5)
j = k + i + 1 - n*(n-1)/2 + (n-i)*((n-i)-1)/2
The inverse operation, from (i,j) index to linear index is
k = (n*(n-1)/2) - (n-i)*((n-i)-1)/2 + j - i - 1
Verify in Python with:
from numpy import triu_indices, sqrt
n = 10
for k in range(n*(n-1)/2):
i = n - 2 - int(sqrt(-8*k + 4*n*(n-1)-7)/2.0 - 0.5)
j = k + i + 1 - n*(n-1)/2 + (n-i)*((n-i)-1)/2
assert np.triu_indices(n, k=1)[0][k] == i
assert np.triu_indices(n, k=1)[1][k] == j
for i in range(n):
for j in range(i+1, n):
k = (n*(n-1)/2) - (n-i)*((n-i)-1)/2 + j - i - 1
assert triu_indices(n, k=1)[0][k] == i
assert triu_indices(n, k=1)[1][k] == j
First, let's renumber a[k] in opposite order. We'll get:
0 a9 a8 a7 a6
0 0 a5 a4 a3
0 0 0 a2 a1
0 0 0 0 a0
0 0 0 0 0
Then k2ij(k, n) will become k2ij(n - k, n).
Now, the question is, how to calculate k2ij(k, n) in this new matrix. The sequence 0, 2, 5, 9 (indices of diagonal elements) corresponds to triangular numbers (after subtracting 1): a[n - i, n + 1 - i] = Ti - 1. Ti = i * (i + 1)/2, so if we know Ti, it's easy to solve this equation and get i (see formula in the linked wiki article, section "Triangular roots and tests for triangular numbers"). If k + 1 is not exactly a triangular number, the formula will still give you the useful result: after rounding it down, you'll get the highest value of i, for which Ti <= k, this value of i corresponds to the row index (counting from bottom), in which a[k] is located. To get the column (counting from right), you should simply calculate the value of Ti and subtract it: j = k + 1 - Ti. To be clear, these are not exacly i and j from your problem, you need to "flip" them.
I didn't write the exact formula, but I hope that you got the idea, and it will now be trivial to find it after performing some boring but simple calculations.
The following is an implimentation in matlab, which can be easily transferred to another language, like C++. Here, we suppose the matrix has size m*m, ind is the index in the linear array. The only thing different is that here, we count the lower triangular part of the matrix column by column, which is analogus to your case (counting the upper triangular part row by row).
function z= ind2lTra (ind, m)
rvLinear = (m*(m-1))/2-ind;
k = floor( (sqrt(1+8*rvLinear)-1)/2 );
j= rvLinear - k*(k+1)/2;
z=[m-j, m-(k+1)];
For the records, this is the same function, but with one-based indexing, and in Julia:
function iuppert(k::Integer,n::Integer)
i = n - 1 - floor(Int,sqrt(-8*k + 4*n*(n-1) + 1)/2 - 0.5)
j = k + i + ( (n-i+1)*(n-i) - n*(n-1) )รท2
return i, j
end
Here is a more efficient formulation for k:
k = (2 * n - 3 - i) * i / 2 + j - 1
In python 2:
def k2ij(k, n):
rows = 0
for t, cols in enumerate(xrange(n - 1, -1, -1)):
rows += cols
if k in xrange(rows):
return (t, n - (rows - k))
return None
In python, the most efficient way is:
array_size= 3
# make indices using k argument if you want above the diagonal
u, v = np.triu_indices(n=array_size,k=1)
# assuming linear indices above the diagonal i.e. 0 means (0,1) and not (0,0)
linear_indices = [0,1]
ijs = [(i,j) for (i,j) in zip(u[linear_indices], v[linear_indices])]
ijs
#[(0, 1), (0, 2)]