Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import numpy as np
- import matplotlib.pyplot as plt
- def print_teams(teams, players, self_players, squad_size=4):
- teams -= 1
- players -= self_players
- new_array = np.zeros(teams*squad_size, dtype=int)
- ones_array = np.copy(new_array)
- ones_array[0] = 1
- # create diagonal matrix
- for i in range(1, teams*squad_size):
- new_array[i] = 1
- ones_array = np.vstack((ones_array, new_array))
- new_array[i] = 0
- cache_array = ones_array[0] + ones_array[1:]
- for row in range(1, teams*squad_size):
- new_array = ones_array[row] + ones_array[row+1:]
- cache_array = np.vstack((cache_array,new_array))
- caches_dic = {"2": cache_array}
- for p in range(3, players+1):
- 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
- caches_dic[str(p)] = cache_array_new
- for row in range(1, caches_dic[str(p-1)].shape[0]): # all the other rows, from 2nd on, are defined in for-loop
- # determine number of zeroes after the last 1
- num_zeroes = 0
- for row_element in caches_dic[str(p-1)][row]:
- num_zeroes += 1
- if row_element == 1:
- num_zeroes = 0
- if num_zeroes == 0:
- continue # continue with the next row if there are 0 zeroes after the last 1
- new_array = caches_dic[str(p-1)][row] + ones_array[teams*squad_size - num_zeroes:]
- caches_dic[str(p)] = np.vstack((caches_dic[str(p)],new_array))
- # convert rows to team combinations
- nrow, ncol = caches_dic[str(players)].shape
- caches_dic[str(players)] = np.reshape(caches_dic[str(players)], (nrow*ncol//squad_size, squad_size) )
- # slice into nrow 2D arrays --> 3D shape
- caches_dic[str(players)] = np.reshape(caches_dic[str(players)], (nrow, teams, squad_size) )
- # start filtering out combinations with teams with zero players
- combinations_array = np.copy(caches_dic[str(players)])
- deletion_list = [] # compare (time-wise) with arrays instead of list
- TwoD_array_number = -1
- for TwoD_array in combinations_array:
- TwoD_array_number += 1
- for row in TwoD_array:
- row_sum = np.sum(row)
- if row_sum == 0:
- deletion_list.append(TwoD_array_number)
- combinations_array = np.delete(combinations_array, obj=deletion_list, axis=0)
- # 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)
- first_row_list = []
- for i in range(teams):
- row_sum = np.sum(combinations_array[0][i])
- first_row_list.append(row_sum)
- combinations_array_sums = np.array([first_row_list])
- for TwoD_array in combinations_array[1:]: # since the first 2D array sum list has been included already
- row_list = []
- for row in TwoD_array:
- row_sum = np.sum(row)
- row_list.append(row_sum)
- row_array = np.array([row_list])
- combinations_array_sums = np.vstack((combinations_array_sums, row_array))
- # convert all sum rows to sets and count the number
- sorted_list = []
- counter_dic = {}
- for row in combinations_array_sums:
- sorted_row = sorted(row)
- if sorted_row not in sorted_list:
- sorted_list.append(sorted_row)
- counter_dic[str(sorted_row)] = 1
- else:
- counter_dic[str(sorted_row)] += 1
- ## PLOT ##
- figsize = (20,10)
- fontsize = figsize[0]
- fig, ax = plt.subplots(figsize=figsize)
- labels = counter_dic.keys()
- comparisons = np.array(list(counter_dic.values()))
- ## show as percentage
- values_sum = np.sum(comparisons)
- comparisons_perc = [round(x/values_sum*100,1) for x in comparisons]
- ##
- bar_number = len(comparisons_perc)
- index = np.arange(bar_number)
- bar_width = 1/bar_number
- plt.bar(index, comparisons_perc, bar_width, color="green")
- plt.xticks(index, labels) # labels get centered
- index = 0
- for p in ax.patches: # add height label above bins
- width = p.get_width()
- height = p.get_height()
- x, y = p.get_xy()
- ax.annotate(f"{height}% ({comparisons[index]})", (x + width/2, y + height*1.02), ha='center', fontsize=fontsize)
- index += 1
- ax.set_xlabel("Possible team combinations", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
- ax.set_ylabel("Percentage (Counts)", labelpad = 10, fontsize=9/5*fontsize, fontweight="bold")
- ax.tick_params(labelsize=fontsize, length=4/5*fontsize)
- title_dic = {
- 1: "Solos",
- 2: "Duos",
- 3: "Trios",
- 4: "Squads"
- }
- plt.title(f"Bar Chart for {teams} enemy teams (mode: {title_dic[squad_size]})", pad = 15, fontsize=round(9/5*fontsize), fontweight="bold")
- plt.show()
- print_teams(6,15,1,squad_size=4)
Add Comment
Please, Sign In to add comment