khushpatel2002/Optimization
0
1import numpy as np2import matplotlib.pyplot as plt3 4def plot_2d(C, A, B, initial_point):5 n_constraints = A.shape[0] # Get the number of constraints6 7 plt.figure(figsize=(8, 6))8 plt.xlim(0, 5) # Adjust based on your problem9 plt.ylim(0, 5) # Adjust based on your problem10 plt.xlabel('X1')11 plt.ylabel('X2')12 13 # Plot feasible region (constraints)14 x1 = np.linspace(0, 5, 100)15 16 for i in range(n_constraints):17 x2_i = (B[i] - A[i, 0] * x1) / A[i, 1] # Calculate x2 values for each constraint18 plt.plot(x1, x2_i, label=f'{A[i, 0]}*X1 + {A[i, 1]}*X2 <= {B[i]}')19 20 # Fill the feasible region21 min_x2 = np.minimum.reduce([(B[i] - A[i, 0] * x1) / A[i, 1] for i in range(n_constraints)])22 plt.fill_between(x1, min_x2, 0, where=(x1 >= 0) & (x1 <= 5), alpha=0.2)23 24 # Plot the initial feasible solution point25 plt.scatter(initial_point[0], initial_point[1], color='red', marker='o', label='Initial Point')26 27 plt.legend()28 plt.show()29 30# Example usage with variable-sized A and B:31C = np.array([2, 3]) # Objective function coefficients32A = np.array([[1, 1], # Constraint matrix33 [2, 1],34 [3, 1]]) # Add more rows for additional constraints35B = np.array([4, 5, 6]) # Right-hand side, add more values for additional constraints36initial_point = np.array([1.0, 3.0])37plot_2d(C, A, B, initial_point)38 