Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import numpy.linalg as npl
  2. import numpy as np
  3. import scipy.sparse as sp
  4. from sklearn.preprocessing import normalize
  5. def bigArray_to_playerArray(matrix):
  6.   player_matrices = []
  7.   for row in range(len(matrix)):
  8.     diag_u = matrix[row]
  9.     diag_l = [matrix[i][row] for i in range(len(diag_u))]
  10.    
  11.     diag_u = np.delete(diag_u,row)
  12.    
  13.     diag_l = np.delete(diag_l,row)
  14.    
  15.     for i in range(len(diag_u)):
  16.       tot = diag_l[i] + diag_u[i]
  17.       if tot != 0:
  18.         diag_u[i] = diag_u[i] /tot
  19.         diag_l[i] /= tot
  20.       else:
  21.         diag_l[i] = 0
  22.         diag_u[i] = 0
  23.     player = sp.diags([diag_u,diag_l],[1,-1]).todense()
  24.     player_matrices.append(player)
  25.   return player_matrices
  26.  
  27. def clean_matrices(players):
  28.   i,j,k = [0,0,0]
  29.   diags = []
  30.   for player in players:
  31.     diag_u = np.diag(player,1)
  32.     u_sum = sum(diag_u)
  33.     diag_l = np.diag(player,-1)
  34.     l_sum = sum(diag_l)
  35.     diags.append(u_sum/(u_sum+l_sum))
  36.   for player in players:
  37.     diag_u = np.diag(player,1)
  38.     diag_l = np.diag(player,-1)
  39.     for index in range(len(diag_u)):
  40.       if diag_u[index] == 0 and diag_l[index] == 0:
  41.         k = index
  42.         if index >= i:
  43.           j = index + 1
  44.          
  45.         else:
  46.           j = index
  47.         print(i,j,k)
  48.         p1win = (diags[i]+1-diags[j])/2
  49.         player[0+k,1+k] = p1win
  50.         player[1+k,0+k] = 1-p1win
  51.         players[j][0+i,1+i] = 1-p1win
  52.         players[j][1+i,0+i] = p1win
  53.     #print(player)  
  54.     i += 1
  55.  
  56. def ratiosToEigenvector(matrix):
  57.   return npl.eig(matrix)
  58. def fill_matrix(matrix):
  59.   ratios = []
  60.   for i in range(len(matrix)):
  61.     col = sum(matrix[:,i])
  62.     row = sum(matrix[i,:])
  63.     ratios.append(row/(col+row))
  64.   for p1 in range(len(matrix)):
  65.     for p2 in range(len(matrix)):
  66.       if p1 != p2:
  67.         if matrix[p1,p2] == 0 and matrix[p1,p2] == 0:
  68.           p1win = (ratios[p1] + 1 - ratios[p2])/2
  69.           matrix[p1,p2] = p1win
  70.           matrix[p2,p1] = 1- p1win
  71.         else:
  72.           tot =matrix[p1,p2] +  matrix[p2,p1]
  73.           matrix[p1,p2] = matrix[p1,p2]/tot
  74.           matrix[p2,p1] = matrix[p2,p1]/tot
  75.   return matrix
  76.  
  77. matrix =np.array([ [0,3,2,4],[1,0,1,1],[1,0,0,0],[3,1,0,0]],float)
  78. matrix2 = [[0,1.0,1,1],[0,0,1,1],[0,0,0,1],[0,0,0,0]]
  79.  
  80. arr = fill_matrix(matrix)
  81. print(arr)
  82. print(ratiosToEigenvector(matrix)[1][:,0])
  83. """
  84. arr = bigArray_to_playerArray(matrix2)
  85. clean_matrices(arr)
  86. print(arr)
  87. for i in range(len(arr)):
  88.  print("norm", npl.eigvals(normalize(arr[i])))
  89.  print(npl.eigvals(arr[i]))
  90. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement