From 2cbe44dcb07ca93c8b60397dc2542435e017de88 Mon Sep 17 00:00:00 2001 From: Liyao Zhu <l.zhu34@student.unimelb.edu.au> Date: Wed, 24 Apr 2019 04:49:30 +1000 Subject: [PATCH] stacked bar of alpha added simplified select for well-mixed graph --- game.py | 58 +++++++++++++++++++++++++++++++++++++++++++------------- graph.py | 55 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/game.py b/game.py index b613031..47360f6 100644 --- a/game.py +++ b/game.py @@ -172,20 +172,19 @@ def rep(repeat, N=100, R=1, K=99, P=0, Actions=[0, 0.2, 0.4, 0.6, 0.8], I=1000, return data - def averageOfLast(data, Actions, r=0, lastIterations=100 ): sum = 0 - action_counter = {} + action_counter = {action:0 for action in Actions} for i in range(-1, -lastIterations-1, -1): sum += np.sum(data[i, r] * Actions) - for a in Actions: - action_counter[a] += data[i, r, a]/lastIterations + for a in range(len(Actions)): + action_counter[Actions[a]] += data[i, r, a]/lastIterations return (sum/100, action_counter) -def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1): +def graph_kp3d(Actions, Klist=[2, 4], Plist=[0.2, 0.5, 0.8], repeat=1): K = Klist P = Plist @@ -194,7 +193,8 @@ def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1): for k in range(len(K)): for p in range(len(P)): data = rep(repeat, K=K[k], P=P[p]) # Specify other params by adding here or change default of rep - meanA[k][p] = averageOfLast(data, Actions, lastIterations=100) # Doing the first round only -- for now + meanA[k][p] = averageOfLast(data, Actions, lastIterations=100)[0] # Doing the first round only -- for now + print("k, p, mean",k,p,meanA[k][p]) P, K = np.meshgrid(P, K) @@ -208,6 +208,35 @@ def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1): plt.show() +def stackBar_alpha(r, Actions, alphaList, repeat=1): # Plotting the data for round r + + A = len(Actions) + p = [] + count = np.zeros((A, len(alphaList))) # of each action in each iter + ind = np.arange(len(alphaList)) + width = 0.3 + + for al in range(len(alphaList)): + data = rep(repeat, Actions=Actions, alpha=alphaList[al]) + action_counter = averageOfLast(data, Actions, r, 100)[1] + for a in range(A): + count[a, al] = action_counter[Actions[a]] + base = 0 + + for a in range(A): + p.append(plt.bar(ind, count[a], width, bottom=base, color=str(0.9 - 0.9 * Actions[a]))) + base += count[a] + + plt.ylabel('Number of Actions') + plt.xlabel('Alpha, the loss fraction') + plt.title('Average Number of Actions in Round ' + str(r+1)) + plt.xticks(ind, alphaList) + # plt.yticks(np.arange(0, 81, 10)) + plt.legend(tuple([p[x][0] for x in range(A)][::-1]), tuple(Actions[::-1]), loc='lower left') + plt.show() + + + def main(): # Read-in or Define Parameters @@ -226,19 +255,22 @@ def main(): Graph1: Number of Actions of Round r (start by 0) by Iteration """ # Repeat game and get the averaged data - RepeatTimes = 30 - data = rep(RepeatTimes, N, R, K, P, Actions, I, RF, alpha) - - for r in range(R): - stackPlot(data, r, Actions, I, "Fully-Mixed Graph") + # RepeatTimes = 30 + # data = rep(RepeatTimes, N, R, K, P, Actions, I, RF, alpha) + # + # for r in range(R): + # stackPlot(data, r, Actions, I, "Fully-Mixed Graph") """ Graph2: Average contribution by K, P """ - graph_kp3d(Actions) - + # graph_kp3d(Actions) + """ + Graph3: Actions by different alpha value + """ + stackBar_alpha(0, Actions, alphaList=[0, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]) if __name__ == '__main__': main() \ No newline at end of file diff --git a/graph.py b/graph.py index b9c6e6a..59e0277 100644 --- a/graph.py +++ b/graph.py @@ -65,28 +65,39 @@ class Graph: select = [] selectedNodes = {i: 0 for i in range(self.N)} - while edges: # Loop when edges is not empty - i, j = edges[np.random.randint(0, len(edges))] - # print("selected nodes:", i, j) - select.append((i, j)) - nodes.remove(i) - nodes.remove(j) - selectedNodes[i] += 1 - selectedNodes[j] += 1 - # print("Remaining nodes:", nodes) - edges = [(a, b) for (a, b) in edges if (a != i) and (a != j) - and (b != i) and (b != j)] - # print("after removal", edges) - - while nodes: - v = nodes.pop(np.random.randint(0, len(nodes))) - v_edges = [(i, j) for (i, j) in self.edges if i == v or j == v] - i, j = v_edges[np.random.randint(len(v_edges))] - select.append((i, j)) - selectedNodes[i] += 1 - selectedNodes[j] += 1 - - # print("Number of each nodes selected:", selectedNodes) + if self.K == self.N - 1: #Well-mixed graph + permutation = np.random.permutation(self.N) + selectedNodes = {i: 1 for i in range(self.N)} + if self.N % 2 == 1: + extraNode = np.random.randint(0, self.N) + while extraNode == permutation[self.N - 1]: + extraNode = np.random.randint(0, self.N) + np.append(permutation, extraNode) + selectedNodes[extraNode] += 1 + select = permutation.reshape((int(len(permutation)/2), 2)) + else: + while edges: # Loop when edges is not empty + i, j = edges[np.random.randint(0, len(edges))] + # print("selected nodes:", i, j) + select.append((i, j)) + nodes.remove(i) + nodes.remove(j) + selectedNodes[i] += 1 + selectedNodes[j] += 1 + # print("Remaining nodes:", nodes) + edges = [(a, b) for (a, b) in edges if (a != i) and (a != j) + and (b != i) and (b != j)] + # print("after removal", edges) + + while nodes: + v = nodes.pop(np.random.randint(0, len(nodes))) + v_edges = [(i, j) for (i, j) in self.edges if i == v or j == v] + i, j = v_edges[np.random.randint(len(v_edges))] + select.append((i, j)) + selectedNodes[i] += 1 + selectedNodes[j] += 1 + + # print("Number of each nodes selected:", selectedNodes) self.selectedNodes = selectedNodes return select -- GitLab