Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import re
- from Table_printer import print_table
- def parse_matrix(filename):
- f = open(filename)
- matrix = []
- title = []
- for line in f:
- if not line.startswith('#'):
- matrix.append([])
- line = line.replace('\n', '')
- line = re.split(r'\s+', line)
- title.append(line.pop(0))
- for i in range(len(line)):
- matrix[-1].append(int(line[i]))
- for i in range(1, len(matrix)):
- for j in range(i):
- matrix[i].insert(j, 1 / matrix[j][i])
- return (matrix, title)
- def parse_matrix2(filename):
- f = open(filename)
- matrix = []
- title = []
- for line in f:
- if not line.startswith('#'):
- matrix.append([])
- line = line.replace('\n', '')
- line = re.split(r'\s+', line)
- title.append(line.pop(0))
- for i in range(len(line)):
- if len(line[i]) == 0:
- break
- if '/' in line[i]:
- a, b = line[i].split('/')
- matrix[-1].append(round(int(a)/int(b), 1))
- else:
- matrix[-1].append(int(line[i]))
- return matrix, title
- #
- # def transitive_consistency(matrix):
- # for k in range(len(matrix)):
- # for i in range(len(matrix)):
- # for j in range(len(matrix)):
- # if matrix[i][j] > matrix[i][k] and matrix[j][k]
- def geometric_mean(vector):
- result = 1
- for i in vector:
- result *= i
- result **= (1 / len(vector))
- return result
- class Table:
- def __init__(self):
- self.matrix = []
- self.title = []
- self.V = []
- self.W = []
- self.S = []
- self.P = []
- self.Lambda = 0
- self.consistency_index = 0
- self.consistency_relation = 0
- self.SumV = 0
- def parse_table(self, filename):
- self.matrix, self.title = parse_matrix2(filename)
- def calculate_V(self):
- self.V = []
- for i in range(len(self.matrix)):
- self.V.append(round(geometric_mean(self.matrix[i]),2))
- def calculate_sum_V(self):
- self.SumV = 0
- for i in self.V:
- self.SumV += i
- def normal_V(self):
- self.W = []
- for i in range(len(self.matrix)):
- self.W.append(round(self.V[i] / self.SumV,2))
- def calculate_S(self):
- self.S = []
- for i in range(len(self.matrix)):
- self.S.append(0)
- for j in range(len(self.matrix)):
- self.S[-1] += self.matrix[j][i]
- def calculate_P(self):
- self.P = []
- for i in range(len(self.matrix)):
- self.P.append(self.S[i] * self.W[i])
- def calculate_Lambda(self):
- self.Lambda = 0
- for p in self.P:
- # p *= 100
- # p = math.floor(p)
- # p /= 100
- self.Lambda += p
- def calculate_consistency_index(self):
- n = len(self.matrix)
- self.consistency_index = (self.Lambda - n) / (n - 1)
- def calculate_consistency_relation(self):
- 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}
- self.consistency_relation = self.consistency_index / random_consistency_index[len(self.matrix)]
- def print_table(self):
- t = []
- t.extend(self.matrix)
- for i in range(len(self.matrix)):
- t[i].append(self.V[i])
- t[i].append(self.W[i])
- t[i].append('')
- for i in range(len(self.matrix)):
- t.insert(2 * i, 'split')
- t.insert(0, self.title)
- t[0].append('V')
- t[0].append('W')
- t[0].append('')
- t.insert(0, 'split')
- t.append('split')
- s = []
- p = []
- for i in range(len(self.matrix)):
- s.append('S{}'.format(i))
- p.append('P{}'.format(i))
- s.append('Summ V')
- s.append('')
- s.append('')
- p.append('lambda')
- p.append('')
- p.append('')
- t.append(s)
- t.append('split')
- t.append(self.S)
- t[-1].append(self.SumV)
- t[-1].append('')
- t[-1].append('')
- print_table(t, 2 * len(self.matrix) + 3, len(self.matrix) + 3)
- for i in range(1,6):
- t = Table()
- if i == 0:
- t.parse_table('input.txt')
- else:
- t.parse_table('input{}.txt'.format(i))
- t.calculate_V()
- t.calculate_sum_V()
- t.normal_V()
- t.calculate_S()
- t.calculate_P()
- t.calculate_Lambda()
- t.calculate_consistency_index()
- t.print_table()
- t.calculate_consistency_relation()
- print('\nLambda')
- print(t.Lambda)
- print('\nconsistancy_index')
- print(t.consistency_index)
- print(t.consistency_relation)
- input("Продолжить?")
Advertisement
Add Comment
Please, Sign In to add comment