Kiri3L

Untitled

Dec 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.89 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), 1))
  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.     result **= (1 / len(vector))
  57.     return result
  58.  
  59.  
  60. class Table:
  61.     def __init__(self):
  62.         self.matrix = []
  63.         self.title = []
  64.         self.V = []
  65.         self.W = []
  66.         self.S = []
  67.         self.P = []
  68.         self.Lambda = 0
  69.         self.consistency_index = 0
  70.         self.consistency_relation = 0
  71.         self.SumV = 0
  72.  
  73.     def parse_table(self, filename):
  74.         self.matrix, self.title = parse_matrix2(filename)
  75.  
  76.     def calculate_V(self):
  77.         self.V = []
  78.         for i in range(len(self.matrix)):
  79.             self.V.append(round(geometric_mean(self.matrix[i]),2))
  80.  
  81.     def calculate_sum_V(self):
  82.         self.SumV = 0
  83.         for i in self.V:
  84.             self.SumV += i
  85.  
  86.     def normal_V(self):
  87.         self.W = []
  88.         for i in range(len(self.matrix)):
  89.             self.W.append(round(self.V[i] / self.SumV,2))
  90.  
  91.     def calculate_S(self):
  92.         self.S = []
  93.         for i in range(len(self.matrix)):
  94.             self.S.append(0)
  95.             for j in range(len(self.matrix)):
  96.                 self.S[-1] += self.matrix[j][i]
  97.  
  98.     def calculate_P(self):
  99.         self.P = []
  100.         for i in range(len(self.matrix)):
  101.             self.P.append(self.S[i] * self.W[i])
  102.  
  103.     def calculate_Lambda(self):
  104.         self.Lambda = 0
  105.         for p in self.P:
  106.             # p *= 100
  107.             # p = math.floor(p)
  108.             # p /= 100
  109.             self.Lambda += p
  110.  
  111.     def calculate_consistency_index(self):
  112.         n = len(self.matrix)
  113.         self.consistency_index = (self.Lambda - n) / (n - 1)
  114.  
  115.  
  116.     def calculate_consistency_relation(self):
  117.         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}
  118.         self.consistency_relation = self.consistency_index / random_consistency_index[len(self.matrix)]
  119.  
  120.     def print_table(self):
  121.         t = []
  122.         t.extend(self.matrix)
  123.         for i in range(len(self.matrix)):
  124.             t[i].append(self.V[i])
  125.             t[i].append(self.W[i])
  126.             t[i].append('')
  127.  
  128.         for i in range(len(self.matrix)):
  129.             t.insert(2 * i, 'split')
  130.         t.insert(0, self.title)
  131.         t[0].append('V')
  132.         t[0].append('W')
  133.         t[0].append('')
  134.         t.insert(0, 'split')
  135.         t.append('split')
  136.         s = []
  137.         p = []
  138.         for i in range(len(self.matrix)):
  139.             s.append('S{}'.format(i))
  140.             p.append('P{}'.format(i))
  141.         s.append('Summ V')
  142.         s.append('')
  143.         s.append('')
  144.         p.append('lambda')
  145.         p.append('')
  146.         p.append('')
  147.         t.append(s)
  148.         t.append('split')
  149.         t.append(self.S)
  150.         t[-1].append(self.SumV)
  151.         t[-1].append('')
  152.         t[-1].append('')
  153.         print_table(t, 2 * len(self.matrix) + 3, len(self.matrix) + 3)
  154.  
  155.  
  156. for i in range(1,6):
  157.     t = Table()
  158.     if i == 0:
  159.         t.parse_table('input.txt')
  160.     else:
  161.         t.parse_table('input{}.txt'.format(i))
  162.  
  163.     t.calculate_V()
  164.     t.calculate_sum_V()
  165.     t.normal_V()
  166.     t.calculate_S()
  167.     t.calculate_P()
  168.     t.calculate_Lambda()
  169.     t.calculate_consistency_index()
  170.     t.print_table()
  171.  
  172.  
  173.     t.calculate_consistency_relation()
  174.     print('\nLambda')
  175.     print(t.Lambda)
  176.     print('\nconsistancy_index')
  177.     print(t.consistency_index)
  178.     print(t.consistency_relation)
  179.     input("Продолжить?")
Advertisement
Add Comment
Please, Sign In to add comment