def payoff_matrix( self, A_number, B_number, n, threads_number=None ): ## TODO dla symetrycznej gry wypełniamy tylko pół macieży symmetric = (A_number == B_number) self.A_symmetrized_strategies = divides(A_number, n) self.B_symmetrized_strategies = divides(B_number, n) matrix = np.zeros((self.A_symmetrized_strategies.shape[0], self.B_symmetrized_strategies.shape[0])) if (symmetric): args = ((i, j) for i in range(self.A_symmetrized_strategies.shape[0]) for j in range(i) ) ## Nie robimy przekątnej bo zawsze są na niej zera with concurrent.futures.ProcessPoolExecutor( max_workers=threads_number) as executor: for i, j, val in executor.map(self.payoff, args): matrix[i, j] = val matrix[j, i] = -val else: args = ((i, j) for i in range(self.A_symmetrized_strategies.shape[0]) for j in range(self.B_symmetrized_strategies.shape[0])) with concurrent.futures.ProcessPoolExecutor( max_workers=threads_number) as executor: for i, j, val in executor.map(self.payoff, args): matrix[i, j] = val return -matrix
def payoff_matrix_pd(matrix, A, B, n): A_symmetrized_strategies = divides(A, n) B_symmetrized_strategies = divides(B, n) columns_names = [] rows_names = [] for i in range(A_symmetrized_strategies.shape[0]): rows_names.append(str(A_symmetrized_strategies[i])) for i in range(B_symmetrized_strategies.shape[0]): columns_names.append(str(B_symmetrized_strategies[i])) df = pd.DataFrame(matrix, columns=columns_names, index=rows_names) return df
def get_strategies(A, B, n, res): rows_names = [] A_strategies = divides(A, n) for i in range(A_strategies.shape[0]): rows_names.append(str(A_strategies[i])) df1 = pd.DataFrame(res[0].transpose(), index=rows_names) df1 = df1.drop(df1[df1[0] < RESULT_PRECISION].index) rows_names = [] B_strategies = divides(B, n) for i in range(B_strategies.shape[0]): rows_names.append(str(B_strategies[i])) df2 = pd.DataFrame(res[1], index=rows_names) df2 = df2.drop(df2[df2[0] < RESULT_PRECISION].index) return df1, df2
def chopstic_row_solution_to_vector(resource_number, fields_number, solution): pure_strategies = divides(resource_number, fields_number) strategy = np.zeros((1, pure_strategies.shape[0])) for i in range(pure_strategies.shape[0]): for j in range(solution.shape[0]): if (str(pure_strategies[i]) == solution.index[j]): strategy[0, i] = solution.iloc[j][0] return strategy
def find_marginal_distribution(resource_number, fields_number, strategy): if (strategy.shape[0] == 1): strategy = strategy.reshape((strategy.shape[1], 1)) pure_strategies = divides(resource_number, fields_number) res = np.zeros((resource_number + 1)) for i in range(pure_strategies.shape[0]): if (strategy[i, 0] > 0): pure_strategy = pure_strategies[i, :] for j in range(pure_strategies.shape[1]): res[int(pure_strategy[j])] += strategy[i, 0] / fields_number values = [str(i) for i in range(res.shape[0])] res = pd.DataFrame(res.reshape((res.shape[0], 1)), index=values) res = res.drop(res[res[0] < RESULT_PRECISION].index) return res