Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- from IPython.display import Latex
- import sympy as sp
- class DualityMethod:
- def __init__(self, A, sign, B, F, direction, rules):
- self.A = A
- self.sign = sign
- self.B = B
- self.F = F
- self.direction = direction
- self.rules = rules
- self.db = []
- self.new_A = None
- self.new_sign = None
- self.new_B = None
- self.new_F = None
- self.new_direction = None
- self.new_rules = None
- def beautiful_table(self, df):
- beaut_t = df.copy()
- beaut_t = beaut_t.map(lambda x: fr'${x}$')
- beaut_t = beaut_t.map(lambda x: fr'${x}$')
- beaut_t.columns = beaut_t.columns.map(lambda x: fr'${x}$')
- beaut_t.index = [' ' for i in range(len(beaut_t.index))]
- return beaut_t
- def display_system(self, A, sign, B, F, direction, rules, table_i=1):
- if table_i == 1:
- x = 'x'
- F_ = fr'$F(X) = '
- elif table_i == 2:
- x = 'y'
- F_ = fr'$L(Y) = '
- F_ += fr'{F[0]}x_1 '
- for i in range(1, len(F)):
- if F[i] < 0:
- F_ += fr' {F[i]}{x}_{i+1} '
- else:
- F_ += fr'+ {F[i]}{x}_{i+1} '
- F_ += fr'\rightarrow {direction} $'
- display(Latex(F_))
- equations = []
- for row in range(len(A)):
- equation = f'{A[row, 0]}{x}_{1} '
- for col in range(1, len(A[0])):
- if A[row, col] < 0:
- equation += f' {A[row, col]}{x}_{col+1} '
- else:
- equation += f'+ {A[row, col]}{x}_{col+1} '
- equation += f'& {sign[row]} {B[row]}'
- equations.append(equation)
- system = fr' \begin{{cases}} '
- for eq in equations:
- system += fr'{eq} \\ '
- system += fr'\end{{cases}} '
- system = fr'''{system}'''
- display(Latex(system))
- rule = fr'$ '
- for i in rules[0]:
- rule += fr'{x}_{i}'
- if i != rules[0][-1]:
- rule +=fr', '
- rule += fr'{rules[1]}'
- rule += fr'{rules[2]} $'
- display(Latex(rule))
- def fit(self):
- print(f'Вхідні дані:\n')
- print(f'A:\n{self.A}\n')
- print(f'sign:\n{self.sign}\n')
- print(f'B:\n{self.B}\n')
- print(f'F:\n{self.F}\n')
- print(f'direction:\n{self.direction}\n')
- print(f'rules:\n{self.rules}\n')
- self.display_system(self.A, self.sign, self.B, self.F, self.direction, self.rules)
- A_total = np.hstack((self.A, self.B.reshape(-1, 1)))
- row = [i for i in self.F]
- if self.direction == 'max': new_direction = sp.symbols('max')
- if self.direction == 'min': new_direction = sp.symbols('min')
- row.append(new_direction)
- A_total = np.vstack((A_total, row))
- columns_ = []
- for i in self.rules[0]:
- col = f'x_{i}'
- if i != self.rules[0][-1]:
- col += ','
- columns_.append(col)
- if self.rules[1] == '\leq': columns_.append(fr'$\geq \downarrow$')
- if self.rules[1] == '\geq': columns_.append(fr'$\leq \ \downarrow$')
- display(Latex(r'Process started:'))
- A_total_df = pd.DataFrame(A_total, columns=columns_, index=[' ' for i in range(len(A_total))])
- print(A_total_df)
- self.db.append(self.beautiful_table(A_total_df))
- A_t = A_total.T
- if str(new_direction) == 'max':
- new_direction = sp.symbols('min')
- elif str(new_direction) == 'min':
- new_direction = sp.symbols('max')
- A_t[-1, -1] = new_direction
- columns_ = []
- for i in range(1, len(self.A)+1):
- col = f'y_{i}'
- if i != len(self.A):
- col += ','
- columns_.append(col)
- if self.rules[1] == '\leq':
- columns_.append(fr'$\leq \downarrow$')
- new_s = '\leq'
- if self.rules[1] == '\geq':
- columns_.append(fr'$\geq \ \downarrow$')
- new_s = '\geq'
- A_t_df = pd.DataFrame(A_t, columns=columns_, index=[' ' for i in range(len(A_t))])
- print(A_t_df)
- self.db.append(self.beautiful_table(A_t_df))
- new_A = A_t[0:len(self.A[0]), 0:-1]
- new_B = A_t[0:len(self.A[0]), -1]
- new_sign = np.array([new_s for i in range(len(new_A))])
- new_F = A_t[-1, :-1]
- new_direction = A_t[-1, -1]
- new_rules = [[i+1 for i in range(len(new_A[0]))], new_s, 0]
- print(f'\nnew_A:\n{new_A}\n')
- print(f'new_sign:\n{new_sign}\n')
- print(f'new_B:\n{new_B}\n')
- print(f'new_F:\n{new_F}\n')
- print(f'new_direction:\n{new_direction}\n')
- print(f'new_rules:\n{new_rules}\n')
- self.new_A = new_A
- self.new_sign = new_sign
- self.new_B = new_B
- self.new_F = new_F
- self.new_direction = new_direction
- self.new_rules = new_rules
- self.display_system(new_A, new_sign, new_B, new_F, new_direction, new_rules, 2)
- return print(f'\nКількість таблиць - {len(self.db)}\n')
- F = np.array([1, 7])
- direction = 'max'
- A = np.array([
- [3, 4],
- [2, -1],
- [1, 0],
- [0, 5]
- ])
- sign = np.array(['\leq', '\leq', '\leq', '\leq'])
- B = np.array([264, 140, 68, 150])
- rules = ([[1, 2], '\geq', 0])
- solver = DualityMethod(A, sign, B, F, direction, rules)
- solver.fit()
- solver.db[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement