I'm using scipy.optimize.linprog library to calculate the minimization using the simplex method. I have two cases where I am getting error:
"ValueError: Phase 1 of the simplex method failed to find a feasible solution. The pseudo-objective function evaluates to 3.1e-12 which exceeds the required tolerance of 1e-12 for a solution to be considered 'close enough' to zero to be a basic solution. Consider increasing the tolerance to be greater than 3.1e-12. If this tolerance is unacceptably large the problem may be infeasible. ".
Maybe someone will find where the problem is.
Minimaze: 45x1 + 54x2 + 42x3 + 36x4
Subject to: x1 + x2 + x3 + x4 = 1600
30x1 + 60x2 + 70x3 + 80x4 = 100000
30x1 + 40x2 + 0x3 + 20x4 = 30000
The code I wrote:
A = np.array([[-30, -60, -70, -80], [-30, -40, 0, -20], [-1, -1, -1, -1]])
b = np.array([-100000, -30000, -1600])
c = np.array([45, 54, 42, 36])
res = linprog(c, A_eq=A, b_eq=b, bounds=(0, None))
Here is the second examples:
Minimize: 100x1 + 50x2 + 100x3
Subject to: x1 + x2 + x3 = 3000
28x1 + 14x2 + 10x3 <= 42000
10x1 + 12x2 + 6x3 <= 24000
30x1 + 20x2 + 30x3 >= 75000
10x1 + 10x2 + 15x3 >= 36000
Here is the code:
A_ub = np.array([[28, 14, 10], [10, 12, 6], [-30, -20, -30], [-10, -10, -15]])
b_ub = np.array([42000, 24000, -75000, -36000])
A_eq = np.array([[1, 1, 1]])
b_eq = np.array([3000])
c = np.array([100, 50, 200])
res = linprog(c, A_ub, b_ub, A_eq, b_eq, bounds=(0, None))
print('Optimal value:', res.fun, '\nX:', res.x)
I checked the system and solutions are indeed feasible. After reading this post, it seems that there are floating point issues in linprog
, clearly a problem of the method. Seems that passing method='interior-point'
improves the algorithm.
It worked for me in both cases
Case 1:
res = linprog(c, A_eq=A, b_eq=b, method='interior-point')
print('Optimal value:', res.fun, '\nX:', res.x)
>> Optimal value: 64090.8624935836
X: [4.90908724e+02 1.50821194e-05 3.45454303e+02 7.63635788e+02]
Case 2:
res = linprog(c, A_ub, b_ub, A_eq, b_eq, bounds=(0, None), method='interior-point')
print('Optimal value:', res.fun, '\nX:', res.x)
#output:
>> Optimal value: 449999.99988966336
X: [ 377.22836393 748.5144238 1874.25721154]