Advertisement
999ms

Untitled

Oct 20th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. from sympy import *
  2. init_printing()
  3.  
  4. A = Matrix([[6,  3,  -2,  4,  1,  0],
  5.             [18,  2,  1,  3,  0,  1]])
  6. c = Matrix([[0, 4,  2,  -1,  0,  0]])
  7.  
  8. def is_non_negative_col0(m):
  9.     return sum(1 for val in m[:,0] if val < 0) == 0
  10.  
  11. # поиск единичного базиса
  12. def find_basis(m):
  13.     rows, cols = m.shape
  14.     basis = [None]*rows
  15.     for j in range(cols):
  16.         l_j = list(m[:,j])
  17.         if l_j.count(1) == 1 and l_j.count(0) == rows - 1:
  18.             i = l_j.index(1) # индекс строки с единицей
  19.             basis[i] = j
  20.     return basis
  21.  
  22. def simplex(m,c):
  23.     if not is_non_negative_col0(m):
  24.         m = m * (-1)
  25.    
  26.     all_names = 'A0 A1 A2 A3 A4 A5'.split()
  27.    
  28.    
  29.     basis = find_basis(m)
  30.     bases_names = Matrix([all_names[i] for i in basis])
  31.     basis_coeffs = Matrix([c[i] for i in basis])
  32.     m01 = bases_names.row_join(basis_coeffs)
  33.    
  34.     rows, cols = m.shape
  35.     deltas = []
  36.     for j in range(cols):
  37.         x = c[j] - m[:,j].dot(basis_coeffs)
  38.         deltas.append(x)
  39.        
  40.     # строка заголовок
  41.     m_title = Matrix([["Базис", "C_баз"]+all_names])
  42.     # основные строки
  43.     m_rows = m01.row_join(m)
  44.     # строка оценок
  45.     m_delta = Matrix([["z","z"]+deltas])
  46.     # итоговая таблица
  47.     m_all = m_title.col_join(m_rows).col_join(m_delta)
  48.    
  49.     rows, cols = A.shape
  50.     while any(x > 0 for x in deltas[1:]): # or deltas[1:].count(0) > rows
  51.    
  52.         for j in range(1, cols):
  53.             if deltas[j] > 0 and all(x <= 0 for x in A[:,j]):
  54.                 print("Целевая функция не ограничена!")
  55.                 break
  56.         # ведущий элемент
  57.         max = deltas[1]
  58.         for j in deltas:
  59.             if max <j:
  60.                 max = j
  61.             j+=1
  62.         j1 = deltas.index(max)
  63.    
  64.         print(A[0,0],A[0,j1])
  65.         z = A[0,0]/A[0,j1]
  66.         print(type(z))
  67.         print(z)
  68.         k=1
  69.  
  70.         i1=0
  71.         while k<rows:
  72.             m = A[k,0]/A[k,j]
  73.             if not comp(z, m):
  74.                 z = A[k,0] / A[k,j]
  75.                 i1 = k
  76.             k+=1
  77.         j_cur = j1
  78.         i_cur = i1
  79.         A_pivot = A[i_cur,j_cur]
  80.         for i in range(rows):
  81.             if i == i_cur:
  82.                 continue
  83.             A.row_op(i, lambda var, j : (var*A_pivot-A[i_cur,j]*A[i,j_cur])/A_pivot)
  84.         A.row_op(i_cur, lambda var, j : var/A_pivot)
  85.    
  86.         basis = find_basis(A)
  87.         bases_names = Matrix([all_names[i] for i in basis])
  88.         basis_coeffs = Matrix([c[i] for i in basis])
  89.         A01 = bases_names.row_join(basis_coeffs)
  90.  
  91.         deltas = []
  92.         for j in range(A.shape[1]):
  93.             x = c[0,j] - A[:,j].dot(basis_coeffs)
  94.             deltas.append(x)
  95.        
  96.         A_title = Matrix([["Базис", "C_баз"]+all_names])
  97.         A_rows = A01.row_join(A)
  98.         A_delta = Matrix([["z","z"]+deltas])
  99.         A_all = A_title.col_join(A_rows).col_join(A_delta)
  100.         if deltas[1:].count(0) > rows:
  101.             if input("Продолжить решение? Y/N ") == "N":
  102.                 break
  103.  
  104. simplex(A,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement