Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def drw_table(table, c_basis, cj, basis, delta):
- print(table)
- print('c_basis', c_basis)
- print('cj', cj)
- print('basis', basis)
- print('delta', delta)
- print(f'p0: {table.T[-1]}')
- print('------------------------------------------------------')
- def teta(p0, pi):
- tmp = []
- for i in range(len(pi)):
- if pi[i] <= 0:
- tmp.append(100000)
- # print(p0[i])
- else:
- tmp.append(p0[i] / pi[i])
- return list(sorted(enumerate(tmp), key=lambda x: x[1]))
- def minus1(tmp, n):
- t = []
- for i in tmp:
- if i > n:
- t.append(i - 1)
- else:
- t.append(i)
- return np.array(t)
- c = np.array([20, 13]) # коэф-ты целевой функции
- a = np.array([[4, 3], # коэф-ты левой части
- [3, 4],
- [4, 2],
- [4, 3]])
- limits=[False,False,False,False]
- b = np.array([20, 20, 30, 20]) # коэф-ты правой части
- bounds = np.array([[0, 59],
- [0, 42]])
- if bounds[:,1].all() != 0:
- a = np.concatenate((a, np.eye(bounds.shape[1])))
- b = np.concatenate((b, bounds[:, 1]))
- E = np.eye(a.shape[0])
- E[:4] = E[:4] * -1
- table = np.concatenate((a, E), axis=1)
- table = np.concatenate((table, np.concatenate((np.eye(table.shape[0] - 2), [[0, 0, 0, 0], [0, 0, 0, 0]]))), axis=1)
- table = np.concatenate((table, b.reshape(-1, 1)), axis=1)
- c_basis = np.hstack((np.ones(table.shape[0] - 2), np.zeros(2)))
- cj = np.hstack((np.hstack((np.zeros(8), np.ones(4))), [0]))
- basis = np.array([8, 9, 10, 11, 6, 7])
- delta = np.dot(c_basis, table) - cj
- drw_table(table, c_basis, cj, basis, delta)
- while max(delta[:len(delta) - 1]) > 0:
- index = max(enumerate(delta[:len(delta) - 1]), key=lambda x: x[1])[0]
- ind = teta(table.T[-1], table.T[index])
- print("Введ столбец: ", index)
- print("Введ строка: ", ind[0][0])
- c_basis[ind[0][0]] = cj[index]
- table[ind[0][0]] = table[ind[0][0]] / table[ind[0][0], index]
- for i in ind[1:]:
- table[i[0]] = table[i[0]] - table[ind[0][0]] * table[i[0], index]
- if basis[ind[0][0]] > 7:
- print(f'DELETE!: {basis[ind[0][0]]}')
- indx = basis[ind[0][0]]
- table = np.delete(table, indx, axis=1)
- cj = np.delete(cj, indx)
- basis = minus1(basis, basis[ind[0][0]])
- basis[ind[0][0]] = index
- delta = np.dot(c_basis, table) - cj
- drw_table(table, c_basis, cj, basis, delta)
- cj[0] = 20
- cj[1] = 13
- print(cj)
- print(basis)
- c_basis = cj[basis]
- print(c_basis)
- print(np.dot(c_basis, table) - cj)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement