Kiri3L

Untitled

Dec 24th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.08 KB | None | 0 0
  1. import math
  2. import re
  3. from Table_printer import print_table
  4.  
  5.  
  6. def parse_matrix(filename):
  7.     f = open(filename)
  8.     matrix = []
  9.     title = []
  10.     for line in f:
  11.         if not line.startswith('#'):
  12.             matrix.append([])
  13.             line = line.replace('\n', '')
  14.             line = re.split(r'\s+', line)
  15.             title.append(line.pop(0))
  16.             for i in range(len(line)):
  17.                 matrix[-1].append(int(line[i]))
  18.     for i in range(1, len(matrix)):
  19.         for j in range(i):
  20.             matrix[i].insert(j, 1 / matrix[j][i])
  21.     return (matrix, title)
  22.  
  23.  
  24. def parse_matrix2(filename):
  25.     f = open(filename)
  26.     matrix = []
  27.     title = []
  28.     for line in f:
  29.         if not line.startswith('#'):
  30.             matrix.append([])
  31.             line = line.replace('\n', '')
  32.             line = re.split(r'\s+', line)
  33.             title.append(line.pop(0))
  34.             for i in range(len(line)):
  35.                 if len(line[i]) == 0:
  36.                     break
  37.                 if '/' in line[i]:
  38.                     a, b = line[i].split('/')
  39.                     matrix[-1].append(round(int(a)/int(b), 2))
  40.                 else:
  41.                     matrix[-1].append(int(line[i]))
  42.     return matrix, title
  43.  
  44. #
  45. # def transitive_consistency(matrix):
  46. #     for k in range(len(matrix)):
  47. #         for i in range(len(matrix)):
  48. #             for j in range(len(matrix)):
  49. #                 if matrix[i][j] > matrix[i][k] and matrix[j][k]
  50.  
  51.  
  52. def geometric_mean(vector):
  53.     result = 1
  54.     for i in vector:
  55.         result *= i
  56.     print(len(vector))
  57.     result **= (1 / len(vector))
  58.     return result
  59.  
  60.  
  61. class Table:
  62.     def __init__(self):
  63.         self.matrix = []
  64.         self.title = []
  65.         self.V = []
  66.         self.W = []
  67.         self.S = []
  68.         self.P = []
  69.         self.Lambda = 0
  70.         self.consistency_index = 0
  71.         self.consistency_relation = 0
  72.         self.SumV = 0
  73.  
  74.     def parse_table(self, filename):
  75.         self.matrix, self.title = parse_matrix2(filename)
  76.  
  77.     def calculate_V(self):
  78.         self.V = []
  79.         for i in range(len(self.matrix)):
  80.             self.V.append(round(geometric_mean(self.matrix[i]),2))
  81.  
  82.     def calculate_sum_V(self):
  83.         self.SumV = 0
  84.         for i in self.V:
  85.             self.SumV += i
  86.  
  87.     def normal_V(self, lvl):
  88.         self.W = []
  89.         for i in range(len(self.matrix)):
  90.             self.W.append(round(self.V[i] / self.SumV,2))
  91.             print('W{}{} = {} / {} = {}'.format(lvl, i, self.V[i], self.SumV, self.W[-1]))
  92.  
  93.     def calculate_S(self):
  94.         self.S = []
  95.         for i in range(len(self.matrix)):
  96.             self.S.append(0)
  97.             print('S{} = '.format(i),end='')
  98.             for j in range(len(self.matrix)):
  99.                 if i != len(self.matrix):
  100.                     print('{} + '.format(self.matrix[j][i]), end='')
  101.                 else:
  102.                     print('{} ='.format(self.matrix[j][i]), end='')
  103.                 self.S[-1] += self.matrix[j][i]
  104.             print(self.S[-1])
  105.  
  106.     def calculate_P(self, lvl):
  107.         self.P = []
  108.         for i in range(len(self.matrix)):
  109.             print('P{} = S{} * W{}{} = {} * {} = {}'.format(i, i, lvl, i, self.S[i], self.W[i], round(self.S[i]*self.W[i], 2)))
  110.             self.P.append(round(self.S[i] * self.W[i], 2))
  111.  
  112.     def calculate_Lambda(self):
  113.         self.Lambda = 0
  114.         for p in self.P:
  115.             # p *= 100
  116.             # p = math.floor(p)
  117.             # p /= 100
  118.             self.Lambda += p
  119.  
  120.     def calculate_consistency_index(self):
  121.         n = len(self.matrix)
  122.         self.consistency_index = (self.Lambda - n) / (n - 1)
  123.  
  124.  
  125.     def calculate_consistency_relation(self):
  126.         random_consistency_index = {1: 0, 2: 0, 3: 5.8, 4: 0.9, 5: 1.12, 6: 1.24, 7: 1.32, 8: 1.41, 9: 1.45, 10: 1.49}
  127.         self.consistency_relation = self.consistency_index / random_consistency_index[len(self.matrix)]
  128.  
  129.     def print_table(self):
  130.         t = []
  131.         t.extend(self.matrix)
  132.         for i in range(len(self.matrix)):
  133.             t[i].append(self.V[i])
  134.             t[i].append(self.W[i])
  135.             t[i].append('')
  136.  
  137.         for i in range(len(self.matrix)):
  138.             t.insert(2 * i, 'split')
  139.         t.insert(0, self.title)
  140.         t[0].append('V')
  141.         t[0].append('W')
  142.         t[0].append('')
  143.         t.insert(0, 'split')
  144.         t.append('split')
  145.         s = []
  146.         p = []
  147.         for i in range(len(self.matrix)):
  148.             s.append('S{}'.format(i))
  149.             p.append('P{}'.format(i))
  150.         s.append('Summ V')
  151.         s.append('')
  152.         s.append('')
  153.         p.append('lambda')
  154.         p.append('')
  155.         p.append('')
  156.         t.append(s)
  157.         t.append('split')
  158.         t.append(self.S)
  159.         t[-1].append(self.SumV)
  160.         t[-1].append('')
  161.         t[-1].append('')
  162.         print_table(t, 2 * len(self.matrix) + 3, len(self.matrix) + 3)
  163.  
  164.  
  165. def alternative_synthesis(table_list):
  166.     wi = 0.0
  167.     w2i = []
  168.     w3ky = []
  169.     c = 0
  170.     for t in table_list:
  171.         if c == 0:
  172.             w2i = t.W.copy()
  173.             print(w2i)
  174.             c += 1
  175.         else:
  176.             w3ky = t.W.copy()
  177.             print(w3ky)
  178.             c += 1
  179.     for i in range(len(w2i)):
  180.         wi += w2i[i] * w3ky[i]
  181.         print('w:' + str(i + 1))
  182.         print(wi)
  183.  
  184.  
  185. table_list = []
  186. for i in range(6):
  187.     t = Table()
  188.     if i == 0:
  189.         t.parse_table('input.txt')
  190.     else:
  191.         t.parse_table('input{}.txt'.format(i))
  192.  
  193.     t.calculate_V()
  194.     t.calculate_sum_V()
  195.  
  196.  
  197.     if i == 0:
  198.         t.normal_V(2)
  199.         t.calculate_S()
  200.         t.calculate_P(2)
  201.     else:
  202.         t.normal_V(2)
  203.         t.calculate_S()
  204.         t.calculate_P(3)
  205.     t.calculate_Lambda()
  206.     t.calculate_consistency_index()
  207.     t.print_table()
  208.  
  209.     t.calculate_consistency_relation()
  210.     print('\nLambda')
  211.     print(t.Lambda)
  212.     print('\nconsistancy_index')
  213.     print(t.consistency_index)
  214.     print(t.consistency_relation)
  215.     table_list.append(t)
  216.     print('\nСинтез Альтернатив')
  217.     input("Продолжить?")
  218.  
  219. alternative_synthesis(table_list)
Advertisement
Add Comment
Please, Sign In to add comment