5.3 KiB
5.3 KiB
None
<html>
<head>
</head>
</html>
Make sure you run this at the begining
In [1]:
import numpy as np
np.set_printoptions(suppress=True)
from scipy.optimize import linprog
Feasible Example¶
\begin{align}
\max f=4x_1+x_2 \\
\text{s.t.} -x_1 + 2x_2 &\leq 4 \\
2x_1 + 3x_2 &\leq 12 \\
x_1 - x_2 &\leq 3 \\
\text{and } x_1, x_2 \geq 0
\end{align}
In [2]:
c = [-4, -1, 0, 0, 0]
A = [[-1, 2, 0, 0, 0], [2, 3, 0, 0, 0], [1, -1, 0, 0, 0]]
b = [4, 12, 3]
x0_bounds = (0, None)
x1_bounds = (0, None)
x2_bounds = (None, None)
x3_bounds = (None, None)
x4_bounds = (None, None)
In [3]:
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds])
print(res)
In [ ]:
Infeasible Example¶
\begin{align}
\min f = x_1 - 2x_2 + 3x_3 \\
s.t. -2x_1 + x_2 + 3x_3 & = 2 \\
3x_1 + 3x_2 + 4x_3 & = 1 \\
\end{align}
In [4]:
c = [-1, 2, -3]
A = [[-2, 1, 3], [3, 3, 4]]
b = [2, 1]
x0_bounds = (0, None)
x1_bounds = (0, None)
x2_bounds = (0, None)
In [5]:
res = linprog(c, A_eq=A, b_eq=b, bounds=[x0_bounds, x1_bounds, x2_bounds,])
print(res)
In [ ]:
Assignment
\begin{align}
\min f = 3x_1 - x_2 \\
s.t. 2x_1 + x_2 & \geq 2 \\
x_1 + 3x_2 & \leq 3 \\
x_2 & \leq 4 \\
\text{and } x_1, x_2 \geq 0
\end{align}
In [6]:
c = [-3, 1]
A = [[-2, -1], [1, 3], [0, 1]]
b = [-2, 3, 4]
x0_bounds = (0, None)
x1_bounds = (0, None)
In [7]:
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds])
print(res)
In [ ]: