Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Assignment: Write a Python Program to solve Linear Programming Problem using
- #Simplex Method
- import numpy as np
- def simplex_method(A, b, c):
- m, n = A.shape
- # Create the initial tableau
- tableau = np.hstack([A, np.eye(m), b.reshape(-1, 1)])
- tableau = np.vstack([tableau, np.concatenate([c, np.zeros(m + 1)])])
- while True:
- # Find the pivot column
- pivot_col = np.argmin(tableau[-1, :-1])
- # If all elements in the last row are non-negative, optimal solution found
- if np.all(tableau[-1, :-1] >= 0):
- break
- # Find the pivot row
- ratios = tableau[:-1, -1] / tableau[:-1, pivot_col]
- pivot_row = np.argmin(ratios)
- # Perform pivot operation
- tableau[pivot_row, :] /= tableau[pivot_row, pivot_col]
- for i in range(m + 1):
- if i != pivot_row:
- tableau[i, :] -= tableau[i, pivot_col] * tableau[pivot_row, :]
- return tableau[-1, -1], tableau[-1, :-1]
- A = np.array([[2, 1], [1, 2]])
- b = np.array([4, 3])
- c = np.array([-3, -5])
- optimal_value, optimal_solution = simplex_method(A, b, c)
- print("Optimal value:", optimal_value)
- print("Optimal solution:", optimal_solution)
- #Output
- Optimal value: 8.333333333333334
- Optimal solution: [0. 0. 0.33333333 2.33333333]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement