Scipy
The main method to solve linear programming problem in python is given by the following command
scipy.optimize.linprog(c,
A_ub=None,
b_ub=None,
A_eq=None,
b_eq=None,
bounds=None,
method='revised simplex',
callback=None,
options=None,
x0=None)
Where the value of each argument is given by comparing the linear programming with the following linear programn
Example¶
c = [-1, 4]
A = [[-3, 1],
[1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='simplex')
message: The problem is unbounded. (HiGHS Status 10: model_status is Unbounded; primal_status is At upper bound)
success: False
status: 3
fun: None
x: None
nit: 3
lower: residual: None
marginals: None
upper: residual: None
marginals: None
eqlin: residual: None
marginals: None
ineqlin: residual: None
marginals: None
1. Example 1.a¶
Now let's try to solve the following problem from the assignment
We have to use the following command to solve this problem
message: Optimization terminated successfully. (HiGHS Status 7: Optimal)
success: True
status: 0
fun: -18.0
x: [ 0.000e+00 8.000e+00 5.000e-01 0.000e+00]
nit: 4
lower: residual: [ 0.000e+00 8.000e+00 5.000e-01 0.000e+00]
marginals: [ 0.000e+00 0.000e+00 0.000e+00 1.000e+00]
upper: residual: [ inf inf inf inf]
marginals: [ 0.000e+00 0.000e+00 0.000e+00 0.000e+00]
eqlin: residual: []
marginals: []
ineqlin: residual: [ 5.000e+00 0.000e+00 0.000e+00]
marginals: [-0.000e+00 -1.500e+00 -5.000e-01]
mip_node_count: 0
mip_dual_bound: 0.0
mip_gap: 0.0
We can use a small function to extract the important information and print in a line as follows.
The value of optimal is \(-18\) at \((0,8,1/2,0)\)
2. Example 1.b¶
We can solve the second assignment
The value of optimal is \(-5\) at \((0,5,0)\)
3. Example 1.c¶
We can solve the second assignment
The value of optimal is \(21/2\) at \((0,3/4,9/2)\)
4. Example 2.a¶
We can solve the second assignment
The value of optimal is \(6\) at \((2,0)\)
5. Example 5.a¶
We can solve the second assignment
The value of optimal is \(10\) at \((0,5/2)\)
6. Example 4¶
Consider the following linear programming problem
The value of optimal is \(17/4\) at \((6755399441055743/9007199254740992,4503599627370497/4503599627370496)\)
7. Example 6.b¶
Show that the following problem has unbounded objective
'The problem is unbounded. (HiGHS Status 10: model_status is Unbounded; primal_status is At upper bound)'
8. Example 3.b¶
Consider the following problem, in the phase I, the artificial variable didn't leave but assumes the value \(0\), hence we can remove it and continue with phase II
The value of optimal is \(4\) at \((0,2,0)\)
Last modified on: 2023-01-06 22:22:45