|
| 1 | +## python Linear Programming visualization the process |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +# Define the constraints |
| 7 | +x1 = np.linspace(0, 10, 400) |
| 8 | + |
| 9 | +# Constraint 1: x1 + x2 <= 5 |
| 10 | +x2_1 = 5 - x1 |
| 11 | + |
| 12 | +# Constraint 2: 2x1 + x2 <= 8 |
| 13 | +x2_2 = 8 - 2*x1 |
| 14 | + |
| 15 | +# Feasibility boundary for x2 >= 0 |
| 16 | +x2_0 = np.maximum(0, np.minimum(x2_1, x2_2)) |
| 17 | + |
| 18 | +# Plot the constraints |
| 19 | +plt.figure(figsize=(8, 8)) |
| 20 | + |
| 21 | +# Plot the lines representing the constraints |
| 22 | +plt.plot(x1, x2_1, label=r'$x_1 + x_2 \leq 5$') |
| 23 | +plt.plot(x1, x2_2, label=r'$2x_1 + x_2 \leq 8$') |
| 24 | + |
| 25 | +# Shade the feasible region |
| 26 | +plt.fill_between(x1, 0, x2_0, where=(x2_0 >= 0), color='lightgrey', label='Feasible Region') |
| 27 | + |
| 28 | +# Mark corner points |
| 29 | +corner_points = [(0, 0), (0, 5), (4, 0), (2, 3)] |
| 30 | +for point in corner_points: |
| 31 | + plt.scatter(*point, color='red') |
| 32 | + plt.text(point[0] + 0.1, point[1] - 0.2, f'({point[0]}, {point[1]})', color='black') |
| 33 | + |
| 34 | +# Labels and title |
| 35 | +plt.xlim(0, 6) |
| 36 | +plt.ylim(0, 6) |
| 37 | +plt.xlabel('$x_1$') |
| 38 | +plt.ylabel('$x_2$') |
| 39 | +plt.title('Linear Programming Problem Visualization') |
| 40 | +plt.legend() |
| 41 | + |
| 42 | +# Show the plot |
| 43 | +plt.grid(True) |
| 44 | +plt.show() |
| 45 | + |
0 commit comments