Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy.linalg as npl
- import numpy as np
- import scipy.sparse as sp
- from sklearn.preprocessing import normalize
- def bigArray_to_playerArray(matrix):
- player_matrices = []
- for row in range(len(matrix)):
- diag_u = matrix[row]
- diag_l = [matrix[i][row] for i in range(len(diag_u))]
- diag_u = np.delete(diag_u,row)
- diag_l = np.delete(diag_l,row)
- for i in range(len(diag_u)):
- tot = diag_l[i] + diag_u[i]
- if tot != 0:
- diag_u[i] = diag_u[i] /tot
- diag_l[i] /= tot
- else:
- diag_l[i] = 0
- diag_u[i] = 0
- player = sp.diags([diag_u,diag_l],[1,-1]).todense()
- player_matrices.append(player)
- return player_matrices
- def clean_matrices(players):
- i,j,k = [0,0,0]
- diags = []
- for player in players:
- diag_u = np.diag(player,1)
- u_sum = sum(diag_u)
- diag_l = np.diag(player,-1)
- l_sum = sum(diag_l)
- diags.append(u_sum/(u_sum+l_sum))
- for player in players:
- diag_u = np.diag(player,1)
- diag_l = np.diag(player,-1)
- for index in range(len(diag_u)):
- if diag_u[index] == 0 and diag_l[index] == 0:
- k = index
- if index >= i:
- j = index + 1
- else:
- j = index
- print(i,j,k)
- p1win = (diags[i]+1-diags[j])/2
- player[0+k,1+k] = p1win
- player[1+k,0+k] = 1-p1win
- players[j][0+i,1+i] = 1-p1win
- players[j][1+i,0+i] = p1win
- #print(player)
- i += 1
- def ratiosToEigenvector(matrix):
- return npl.eig(matrix)
- def fill_matrix(matrix):
- ratios = []
- for i in range(len(matrix)):
- col = sum(matrix[:,i])
- row = sum(matrix[i,:])
- ratios.append(row/(col+row))
- for p1 in range(len(matrix)):
- for p2 in range(len(matrix)):
- if p1 != p2:
- if matrix[p1,p2] == 0 and matrix[p1,p2] == 0:
- p1win = (ratios[p1] + 1 - ratios[p2])/2
- matrix[p1,p2] = p1win
- matrix[p2,p1] = 1- p1win
- else:
- tot =matrix[p1,p2] + matrix[p2,p1]
- matrix[p1,p2] = matrix[p1,p2]/tot
- matrix[p2,p1] = matrix[p2,p1]/tot
- return matrix
- matrix =np.array([ [0,3,2,4],[1,0,1,1],[1,0,0,0],[3,1,0,0]],float)
- matrix2 = [[0,1.0,1,1],[0,0,1,1],[0,0,0,1],[0,0,0,0]]
- arr = fill_matrix(matrix)
- print(arr)
- print(ratiosToEigenvector(matrix)[1][:,0])
- """
- arr = bigArray_to_playerArray(matrix2)
- clean_matrices(arr)
- print(arr)
- for i in range(len(arr)):
- print("norm", npl.eigvals(normalize(arr[i])))
- print(npl.eigvals(arr[i]))
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement