Advertisement
mirosh111000

Двоїстий метод

Mar 23rd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.75 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. from IPython.display import Latex
  4. import sympy as sp
  5.  
  6. class DualityMethod:
  7.     def __init__(self, A, sign, B, F, direction, rules):
  8.         self.A = A
  9.         self.sign = sign
  10.         self.B = B
  11.         self.F = F
  12.         self.direction = direction
  13.         self.rules = rules
  14.         self.db = []
  15.         self.new_A = None
  16.         self.new_sign = None
  17.         self.new_B = None
  18.         self.new_F = None
  19.         self.new_direction = None
  20.         self.new_rules = None
  21.  
  22.        
  23.     def beautiful_table(self, df):
  24.         beaut_t = df.copy()
  25.         beaut_t = beaut_t.map(lambda x: fr'${x}$')
  26.         beaut_t = beaut_t.map(lambda x: fr'${x}$')
  27.         beaut_t.columns = beaut_t.columns.map(lambda x: fr'${x}$')
  28.         beaut_t.index = [' ' for i in range(len(beaut_t.index))]
  29.        
  30.         return beaut_t
  31.        
  32.        
  33.     def display_system(self, A, sign, B, F, direction, rules, table_i=1):
  34.        
  35.        
  36.        
  37.        
  38.        
  39.         if table_i == 1:
  40.             x = 'x'
  41.             F_ = fr'$F(X) = '
  42.         elif table_i == 2:
  43.             x = 'y'
  44.             F_ = fr'$L(Y) = '
  45.            
  46.         F_ += fr'{F[0]}x_1 '
  47.         for i in range(1, len(F)):
  48.             if F[i] < 0:
  49.                 F_ += fr' {F[i]}{x}_{i+1} '
  50.             else:
  51.                 F_ += fr'+ {F[i]}{x}_{i+1} '
  52.         F_ += fr'\rightarrow {direction} $'
  53.        
  54.         display(Latex(F_))
  55.  
  56.         equations = []
  57.         for row in range(len(A)):
  58.             equation = f'{A[row, 0]}{x}_{1} '
  59.             for col in range(1, len(A[0])):
  60.                 if A[row, col] < 0:
  61.                     equation += f' {A[row, col]}{x}_{col+1} '
  62.                 else:
  63.                     equation += f'+ {A[row, col]}{x}_{col+1} '
  64.             equation += f'& {sign[row]} {B[row]}'
  65.             equations.append(equation)
  66.  
  67.         system = fr' \begin{{cases}} '
  68.         for eq in equations:
  69.             system += fr'{eq} \\ '
  70.         system += fr'\end{{cases}} '
  71.         system = fr'''{system}'''
  72.         display(Latex(system))
  73.        
  74.        
  75.         rule = fr'$ '
  76.         for i in rules[0]:
  77.             rule += fr'{x}_{i}'
  78.             if i != rules[0][-1]:
  79.                 rule +=fr', '
  80.         rule += fr'{rules[1]}'
  81.         rule += fr'{rules[2]} $'
  82.         display(Latex(rule))
  83.  
  84.     def fit(self):
  85.        
  86.         print(f'Вхідні дані:\n')
  87.        
  88.         print(f'A:\n{self.A}\n')
  89.         print(f'sign:\n{self.sign}\n')
  90.         print(f'B:\n{self.B}\n')
  91.         print(f'F:\n{self.F}\n')
  92.         print(f'direction:\n{self.direction}\n')
  93.         print(f'rules:\n{self.rules}\n')
  94.        
  95.        
  96.         self.display_system(self.A, self.sign, self.B, self.F, self.direction, self.rules)
  97.         A_total = np.hstack((self.A, self.B.reshape(-1, 1)))
  98.         row = [i for i in self.F]
  99.         if self.direction == 'max': new_direction = sp.symbols('max')
  100.         if self.direction == 'min': new_direction = sp.symbols('min')
  101.         row.append(new_direction)
  102.         A_total = np.vstack((A_total, row))
  103.        
  104.         columns_ = []
  105.         for i in self.rules[0]:
  106.             col = f'x_{i}'
  107.             if i != self.rules[0][-1]:
  108.                 col += ','
  109.             columns_.append(col)
  110.        
  111.         if self.rules[1] == '\leq': columns_.append(fr'$\geq \downarrow$')
  112.         if self.rules[1] == '\geq': columns_.append(fr'$\leq \ \downarrow$')
  113.            
  114.         display(Latex(r'Process started:'))
  115.         A_total_df = pd.DataFrame(A_total, columns=columns_, index=[' ' for i in range(len(A_total))])
  116.         print(A_total_df)
  117.         self.db.append(self.beautiful_table(A_total_df))
  118.    
  119.         A_t = A_total.T
  120.         if str(new_direction) == 'max':
  121.             new_direction = sp.symbols('min')
  122.         elif str(new_direction) == 'min':
  123.             new_direction = sp.symbols('max')
  124.         A_t[-1, -1] = new_direction
  125.        
  126.         columns_ = []
  127.         for i in range(1, len(self.A)+1):
  128.             col = f'y_{i}'
  129.             if i != len(self.A):
  130.                 col += ','
  131.             columns_.append(col)
  132.            
  133.         if self.rules[1] == '\leq':
  134.             columns_.append(fr'$\leq \downarrow$')
  135.             new_s = '\leq'
  136.         if self.rules[1] == '\geq':
  137.             columns_.append(fr'$\geq \ \downarrow$')
  138.             new_s = '\geq'
  139.            
  140.         A_t_df = pd.DataFrame(A_t, columns=columns_, index=[' ' for i in range(len(A_t))])
  141.         print(A_t_df)
  142.         self.db.append(self.beautiful_table(A_t_df))
  143.        
  144.         new_A = A_t[0:len(self.A[0]), 0:-1]
  145.         new_B = A_t[0:len(self.A[0]), -1]
  146.         new_sign = np.array([new_s for i in range(len(new_A))])
  147.         new_F = A_t[-1, :-1]
  148.         new_direction = A_t[-1, -1]
  149.         new_rules = [[i+1 for i in range(len(new_A[0]))], new_s, 0]
  150.         print(f'\nnew_A:\n{new_A}\n')
  151.         print(f'new_sign:\n{new_sign}\n')
  152.         print(f'new_B:\n{new_B}\n')
  153.         print(f'new_F:\n{new_F}\n')
  154.         print(f'new_direction:\n{new_direction}\n')
  155.         print(f'new_rules:\n{new_rules}\n')
  156.         self.new_A = new_A
  157.         self.new_sign = new_sign
  158.         self.new_B = new_B
  159.         self.new_F = new_F
  160.         self.new_direction = new_direction
  161.         self.new_rules = new_rules
  162.        
  163.         self.display_system(new_A, new_sign, new_B, new_F, new_direction, new_rules, 2)
  164.        
  165.         return print(f'\nКількість таблиць - {len(self.db)}\n')
  166.  
  167.  
  168. F = np.array([1, 7])
  169. direction = 'max'
  170.  
  171. A = np.array([
  172.     [3, 4],
  173.     [2, -1],
  174.     [1, 0],
  175.     [0, 5]
  176. ])
  177. sign = np.array(['\leq', '\leq', '\leq', '\leq'])
  178. B = np.array([264, 140, 68, 150])
  179.  
  180. rules = ([[1, 2], '\geq', 0])
  181.  
  182. solver = DualityMethod(A, sign, B, F, direction, rules)
  183. solver.fit()
  184. solver.db[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement