OreganoHauch

Team combinations

Nov 22nd, 2021 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import math
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. def print_teams(teams, players, self_players, squad_size=4):
  6.  
  7. teams -= 1
  8. players -= self_players
  9.  
  10. new_array = np.zeros(teams*squad_size, dtype=int)
  11. ones_array = np.copy(new_array)
  12. ones_array[0] = 1
  13.  
  14. # create diagonal matrix
  15. for i in range(1, teams*squad_size):
  16. new_array[i] = 1
  17. ones_array = np.vstack((ones_array, new_array))
  18. new_array[i] = 0
  19.  
  20. cache_array = ones_array[0] + ones_array[1:]
  21. for row in range(1, teams*squad_size):
  22. new_array = ones_array[row] + ones_array[row+1:]
  23. cache_array = np.vstack((cache_array,new_array))
  24.  
  25. caches_dic = {"2": cache_array}
  26.  
  27. for p in range(3, players+1):
  28. cache_array_new = caches_dic[str(p-1)][0] + ones_array[p-1:] # new array with p times 1 in the beginning of each row is preredefined
  29. caches_dic[str(p)] = cache_array_new
  30.  
  31. for row in range(1, caches_dic[str(p-1)].shape[0]): # all the other rows, from 2nd on, are defined in for-loop
  32.  
  33.  
  34. # determine number of zeroes after the last 1
  35. num_zeroes = 0
  36. for row_element in caches_dic[str(p-1)][row]:
  37. num_zeroes += 1
  38. if row_element == 1:
  39. num_zeroes = 0
  40. if num_zeroes == 0:
  41. continue # continue with the next row if there are 0 zeroes after the last 1
  42.  
  43. new_array = caches_dic[str(p-1)][row] + ones_array[teams*squad_size - num_zeroes:]
  44.  
  45. caches_dic[str(p)] = np.vstack((caches_dic[str(p)],new_array))
  46.  
  47. # convert rows to team combinations
  48. nrow, ncol = caches_dic[str(players)].shape
  49. caches_dic[str(players)] = np.reshape(caches_dic[str(players)], (nrow*ncol//squad_size, squad_size) )
  50.  
  51. # slice into nrow 2D arrays --> 3D shape
  52. caches_dic[str(players)] = np.reshape(caches_dic[str(players)], (nrow, teams, squad_size) )
  53.  
  54. # start filtering out combinations with teams with zero players
  55. combinations_array = np.copy(caches_dic[str(players)])
  56. deletion_list = [] # compare (time-wise) with arrays instead of list
  57. TwoD_array_number = -1
  58. for TwoD_array in combinations_array:
  59. TwoD_array_number += 1
  60. for row in TwoD_array:
  61. row_sum = np.sum(row)
  62. if row_sum == 0:
  63. deletion_list.append(TwoD_array_number)
  64. combinations_array = np.delete(combinations_array, obj=deletion_list, axis=0)
  65.  
  66. # convert 3D array to 2D array (each combination is one row, where each number of the row represents the sum of the Ones-Zeroes row)
  67. first_row_list = []
  68. for i in range(teams):
  69. row_sum = np.sum(combinations_array[0][i])
  70. first_row_list.append(row_sum)
  71. combinations_array_sums = np.array([first_row_list])
  72. for TwoD_array in combinations_array[1:]: # since the first 2D array sum list has been included already
  73. row_list = []
  74. for row in TwoD_array:
  75. row_sum = np.sum(row)
  76. row_list.append(row_sum)
  77. row_array = np.array([row_list])
  78. combinations_array_sums = np.vstack((combinations_array_sums, row_array))
  79.  
  80. # convert all sum rows to sets and count the number
  81. sorted_list = []
  82. counter_dic = {}
  83. for row in combinations_array_sums:
  84. sorted_row = sorted(row)
  85. if sorted_row not in sorted_list:
  86. sorted_list.append(sorted_row)
  87. counter_dic[str(sorted_row)] = 1
  88. else:
  89. counter_dic[str(sorted_row)] += 1
  90.  
  91. ## PLOT ##
  92. figsize = (20,10)
  93. fontsize = figsize[0]
  94. fig, ax = plt.subplots(figsize=figsize)
  95. labels = counter_dic.keys()
  96. comparisons = np.array(list(counter_dic.values()))
  97. ## show as percentage
  98. values_sum = np.sum(comparisons)
  99. comparisons_perc = [round(x/values_sum*100,1) for x in comparisons]
  100. ##
  101. bar_number = len(comparisons_perc)
  102. index = np.arange(bar_number)
  103. bar_width = 1/bar_number
  104. plt.bar(index, comparisons_perc, bar_width, color="green")
  105. plt.xticks(index, labels) # labels get centered
  106. index = 0
  107. for p in ax.patches: # add height label above bins
  108. width = p.get_width()
  109. height = p.get_height()
  110. x, y = p.get_xy()
  111. ax.annotate(f"{height}% ({comparisons[index]})", (x + width/2, y + height*1.02), ha='center', fontsize=fontsize)
  112. index += 1
  113. ax.set_xlabel("Possible team combinations", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
  114. ax.set_ylabel("Percentage (Counts)", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
  115. ax.tick_params(labelsize=fontsize, length=4/5*fontsize)
  116. title_dic = {
  117. 1: "Solos",
  118. 2: "Duos",
  119. 3: "Trios",
  120. 4: "Squads"
  121. }
  122. plt.title(f"Bar Chart for {teams} enemy teams (mode: {title_dic[squad_size]})", pad = 15, fontsize=round(9/5*fontsize), fontweight="bold")
  123. plt.show()
  124.  
  125. print_teams(6,15,1,squad_size=4)
Add Comment
Please, Sign In to add comment