Advertisement
TheDoskz

SimplexMethod

May 20th, 2021
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def drw_table(table, c_basis, cj, basis, delta):
  4.     print(table)
  5.     print('c_basis', c_basis)
  6.     print('cj', cj)
  7.     print('basis', basis)
  8.     print('delta', delta)
  9.     print(f'p0:  {table.T[-1]}')
  10.     print('------------------------------------------------------')
  11.  
  12. def teta(p0, pi):
  13.     tmp = []
  14.     for i in range(len(pi)):
  15.         if pi[i] <= 0:
  16.             tmp.append(100000)
  17.             # print(p0[i])
  18.         else:
  19.             tmp.append(p0[i] / pi[i])
  20.     return list(sorted(enumerate(tmp), key=lambda x: x[1]))
  21.  
  22.  
  23. def minus1(tmp, n):
  24.     t = []
  25.     for i in tmp:
  26.         if i > n:
  27.             t.append(i - 1)
  28.         else:
  29.             t.append(i)
  30.     return np.array(t)
  31.  
  32. c = np.array([20, 13])  # коэф-ты целевой функции
  33.  
  34. a = np.array([[4, 3],  # коэф-ты левой части
  35.               [3, 4],
  36.               [4, 2],
  37.               [4, 3]])
  38.  
  39. limits=[False,False,False,False]
  40.  
  41. b = np.array([20, 20, 30, 20])  # коэф-ты правой части
  42.  
  43. bounds = np.array([[0, 59],
  44.                    [0, 42]])
  45.  
  46. if bounds[:,1].all() != 0:
  47.     a = np.concatenate((a, np.eye(bounds.shape[1])))
  48.     b = np.concatenate((b, bounds[:, 1]))
  49.  
  50.  
  51. E = np.eye(a.shape[0])
  52. E[:4] = E[:4] * -1
  53.  
  54. table = np.concatenate((a, E), axis=1)
  55.  
  56. table = np.concatenate((table, np.concatenate((np.eye(table.shape[0] - 2), [[0, 0, 0, 0], [0, 0, 0, 0]]))), axis=1)
  57.  
  58. table = np.concatenate((table, b.reshape(-1, 1)), axis=1)
  59.  
  60. c_basis = np.hstack((np.ones(table.shape[0] - 2), np.zeros(2)))
  61.  
  62. cj = np.hstack((np.hstack((np.zeros(8), np.ones(4))), [0]))
  63.  
  64. basis = np.array([8, 9, 10, 11, 6, 7])
  65. delta = np.dot(c_basis, table) - cj
  66.  
  67. drw_table(table, c_basis, cj, basis, delta)
  68.  
  69. while max(delta[:len(delta) - 1]) > 0:
  70.     index = max(enumerate(delta[:len(delta) - 1]), key=lambda x: x[1])[0]
  71.  
  72.     ind = teta(table.T[-1], table.T[index])
  73.     print("Введ столбец:   ", index)
  74.     print("Введ строка:   ", ind[0][0])
  75.     c_basis[ind[0][0]] = cj[index]
  76.  
  77.     table[ind[0][0]] = table[ind[0][0]] / table[ind[0][0], index]
  78.     for i in ind[1:]:
  79.         table[i[0]] = table[i[0]] - table[ind[0][0]] * table[i[0], index]
  80.  
  81.     if basis[ind[0][0]] > 7:
  82.         print(f'DELETE!:     {basis[ind[0][0]]}')
  83.         indx = basis[ind[0][0]]
  84.         table = np.delete(table, indx, axis=1)
  85.         cj = np.delete(cj, indx)
  86.         basis = minus1(basis, basis[ind[0][0]])
  87.  
  88.     basis[ind[0][0]] = index
  89.  
  90.     delta = np.dot(c_basis, table) - cj
  91.  
  92.     drw_table(table, c_basis, cj, basis, delta)
  93.  
  94. cj[0] = 20
  95. cj[1] = 13
  96. print(cj)
  97.  
  98. print(basis)
  99. c_basis = cj[basis]
  100. print(c_basis)
  101.  
  102. print(np.dot(c_basis, table) - cj)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement