Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import random
- def plot(x1, x2, point_x1, point_x2, color='blue', label=r'$x_1 + 3x_2 ≥ 3$', upper=1):
- plt.plot(x1, x2, c=color)
- plt.scatter(point_x1, point_x2, marker='o', c=color)
- if upper == 1:
- plt.fill_between(x1, x2, max(x2), color=color, alpha=0.15, label=label)
- else:
- plt.fill_between(x1, x2, min(x2), color=color, alpha=0.15, label=label)
- colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'white', 'orange', 'purple', 'pink', 'brown', 'gray']
- x1 = np.linspace(-100, 100, 1000)
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 + 7x_2 \rightarrow max$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- random_x1_f1 = random.uniform(-2, 100)
- random_x2_f1 = random.uniform(-2, 100)
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (264 - 3*x1) / 4
- x2 = f1(x1)
- print(f'(3*{round(random_x1_f1, 2)}) + (4*{round(random_x2_f1, 2)}) < 264')
- print(f'{round(3*random_x1_f1 + 4*random_x2_f1, 2)} < 264')
- print(f'{3*random_x1_f1 + 4*random_x2_f1 < 264}\n')
- if 3*random_x1_f1 + 4*random_x2_f1 < 264:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 88]), np.array([66, 0]), colors[0], label=r'$3x_1 + 4x_2 < 264$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, 100)
- plt.ylim(-3, 100)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 + 7x_2 \rightarrow max$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (264 - 3*x1) / 4
- x2 = f1(x1)
- print(f'(3*{round(random_x1_f1, 2)}) + (4*{round(random_x2_f1, 2)}) < 264')
- print(f'{round(3*random_x1_f1 + 4*random_x2_f1, 2)} < 264')
- print(f'{3*random_x1_f1 + 4*random_x2_f1 < 264}\n')
- if 3*random_x1_f1 + 4*random_x2_f1 < 264:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 88]), np.array([66, 0]), colors[0], label=r'$3x_1 + 4x_2 < 264$', upper=upper)
- random_x1_f2 = random.uniform(-2, 100)
- random_x2_f2 = random.uniform(-2, 100)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 140 - 2*x1
- x2 = f2(x1)
- print(f'(2*{round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) < 140')
- print(f'{round(2*random_x1_f2 + random_x2_f2, 2)} < 140')
- print(f'{2* random_x1_f2 + random_x2_f2 < 140}\n')
- if 2*random_x1_f2 + random_x2_f2 < 140:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([30, 70]), np.array([80, 0]), colors[1], label=r'$2x_1 + x_2 < 140$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, 100)
- plt.ylim(-3, 100)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 + 7x_2 \rightarrow max$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (264 - 3*x1) / 4
- x2 = f1(x1)
- print(f'(3*{round(random_x1_f1, 2)}) + (4*{round(random_x2_f1, 2)}) < 264')
- print(f'{round(3*random_x1_f1 + 4*random_x2_f1, 2)} < 264')
- print(f'{3*random_x1_f1 + 4*random_x2_f1 < 264}\n')
- if 3*random_x1_f1 + 4*random_x2_f1 < 264:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 88]), np.array([66, 0]), colors[0], label=r'$3x_1 + 4x_2 < 264$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 140 - 2*x1
- x2 = f2(x1)
- print(f'(2*{round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) < 140')
- print(f'{round(2*random_x1_f2 + random_x2_f2, 2)} < 140')
- print(f'{2* random_x1_f2 + random_x2_f2 < 140}\n')
- if 2*random_x1_f2 + random_x2_f2 < 140:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([30, 70]), np.array([80, 0]), colors[1], label=r'$2x_1 + x_2 < 140$', upper=upper)
- random_x1_f3 = random.uniform(-2, 100)
- random_x2_f3 = random.uniform(-2, 100)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- print(f'{round(random_x1_f3, 2)} < 68')
- print(f'{random_x1_f3 < 68}\n')
- x2 = np.copy(x1)
- plt.axvline(x=68, color=colors[2])
- plt.scatter(np.array([68, 68, random_x1_f3]), np.array([68, 0, random_x1_f3]), c=colors[2])
- plt.fill_betweenx([min(x2), max(x2)], min(x1), 68, color=colors[2], alpha=0.15, label=r'$x_1 < 68$')
- plt.grid()
- plt.legend()
- plt.xlim(-3, 100)
- plt.ylim(-3, 100)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 + 7x_2 \rightarrow max$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (264 - 3*x1) / 4
- x2 = f1(x1)
- print(f'(3*{round(random_x1_f1, 2)}) + (4*{round(random_x2_f1, 2)}) < 264')
- print(f'{round(3*random_x1_f1 + 4*random_x2_f1, 2)} < 264')
- print(f'{3*random_x1_f1 + 4*random_x2_f1 < 264}\n')
- if 3*random_x1_f1 + 4*random_x2_f1 < 264:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 88]), np.array([66, 0]), colors[0], label=r'$3x_1 + 4x_2 < 264$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 140 - 2*x1
- x2 = f2(x1)
- print(f'(2*{round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) < 140')
- print(f'{round(2*random_x1_f2 + random_x2_f2, 2)} < 140')
- print(f'{2* random_x1_f2 + random_x2_f2 < 140}\n')
- if 2*random_x1_f2 + random_x2_f2 < 140:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([30, 70]), np.array([80, 0]), colors[1], label=r'$2x_1 + x_2 < 140$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- print(f'{round(random_x1_f3, 2)} < 68')
- print(f'{random_x1_f3 < 68}\n')
- x2 = np.copy(x1)
- plt.axvline(x=68, color=colors[2])
- plt.scatter(np.array([68, 68, random_x1_f3]), np.array([68, 0, random_x1_f3]), c=colors[2])
- plt.fill_betweenx([min(x2), max(x2)], min(x1), 68, color=colors[2], alpha=0.15, label=r'$x_1 < 68$')
- random_x1_f4 = random.uniform(-2, 100)
- random_x2_f4 = random.uniform(-2, 100)
- print(f"Пробна точка: ({round(random_x1_f4, 2)}, {round(random_x2_f4, 2)})")
- x2 = np.array([150 / 5 for i in range(len(x1))])
- print(f'(0*{round(random_x1_f4, 2)}) + (5*{round(random_x2_f4, 2)}) < 150')
- print(f'{round(0*random_x1_f4 + 5*random_x2_f4, 2)} < 150')
- print(f'{0*random_x1_f4 + 5*random_x2_f4 < 150}\n')
- if 0*random_x1_f4 + 5*random_x2_f4 < 150:
- upper = 0
- else:
- if random_x2_f4 < 150/30:
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f4, random_x2_f4, c=colors[5])
- plot(x1, x2, np.array([0, 30]), np.array([30, 30]), colors[5], label=r'$5x_2 < 150$', upper=upper)
- plt.fill_between(x1, -1000, (x2), color=colors[5], alpha=0.2)
- plt.grid()
- plt.legend()
- plt.xlim(-3, 100)
- plt.ylim(-3, 100)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 + 7x_2 \rightarrow max$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (264 - 3*x1) / 4
- x2 = f1(x1)
- print(f'(3*{round(random_x1_f1, 2)}) + (4*{round(random_x2_f1, 2)}) < 264')
- print(f'{round(3*random_x1_f1 + 4*random_x2_f1, 2)} < 264')
- print(f'{3*random_x1_f1 + 4*random_x2_f1 < 264}\n')
- if 3*random_x1_f1 + 4*random_x2_f1 < 264:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 88]), np.array([66, 0]), colors[0], label=r'$3x_1 + 4x_2 < 264$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 140 - 2*x1
- x2 = f2(x1)
- print(f'(2*{round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) < 140')
- print(f'{round(2*random_x1_f2 + random_x2_f2, 2)} < 140')
- print(f'{2* random_x1_f2 + random_x2_f2 < 140}\n')
- if 2*random_x1_f2 + random_x2_f2 < 140:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([30, 70]), np.array([80, 0]), colors[1], label=r'$2x_1 + x_2 < 140$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- print(f'{round(random_x1_f3, 2)} < 68')
- print(f'{random_x1_f3 < 68}\n')
- x2 = np.copy(x1)
- plt.axvline(x=68, color=colors[2])
- plt.scatter(np.array([68, 68, random_x1_f3]), np.array([68, 0, random_x1_f3]), c=colors[2])
- plt.fill_betweenx([min(x2), max(x2)], min(x1), 68, color=colors[2], alpha=0.15, label=r'$x_1 < 68$')
- print(f"Пробна точка: ({round(random_x1_f4, 2)}, {round(random_x2_f4, 2)})")
- x2 = np.array([150 / 5 for i in range(len(x1))])
- print(f'(0*{round(random_x1_f4, 2)}) + (5*{round(random_x2_f4, 2)}) < 150')
- print(f'{round(0*random_x1_f4 + 5*random_x2_f4, 2)} < 150')
- print(f'{0*random_x1_f4 + 5*random_x2_f4 < 150}\n')
- if 0*random_x1_f4 + 5*random_x2_f4 < 150:
- upper = 0
- else:
- if random_x2_f4 < 150/30:
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f4, random_x2_f4, c=colors[5])
- plot(x1, x2, np.array([0, 30]), np.array([30, 30]), colors[5], label=r'$5x_2 < 150$', upper=upper)
- plt.fill_between(x1, -1000, (x2), color=colors[5], alpha=0.2)
- def f(x1):
- return -x1 / 7
- x2 = f(x1)
- plt.plot(x1, x2, c='black', lw=2)
- plt.scatter(np.array([0, 7]), np.array([0, -1]), c='black')
- plt.annotate('Вектор нормалі', xy=(1, 7), xycoords='data', xytext=(0.1, -4), textcoords='data', arrowprops=dict(facecolor='black'))
- plt.text(48, 30, f'max', fontsize=10, ha='right')
- plt.scatter(48, 30, c='red', edgecolors='black', s=50, label=r'$max(48, 30); L(max)=258$')
- plt.grid()
- plt.legend()
- plt.xlim(-3, 100)
- plt.ylim(-8, 100)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement