|
| 1 | + |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# In[10]: |
| 5 | + |
| 6 | +#import packages |
| 7 | +import networkx as nx |
| 8 | +import random as rnd |
| 9 | +from MinMaxClass import MinMaxNode, MinMaxTree |
| 10 | +import math |
| 11 | + |
| 12 | + |
| 13 | +# In[2]: |
| 14 | + |
| 15 | +def makeTree(G,H,turn,node): |
| 16 | + if turn==0: #player: attacker |
| 17 | + infectedList=[] |
| 18 | + nodeAttribute=nx.get_node_attributes(G,"infected") #a dictionary of node attributes returned: {1:0, 2:0, 3:0, 4:0, 5:0} |
| 19 | + for e in nodeAttribute.keys(): |
| 20 | + if nodeAttribute[e]==-1: |
| 21 | + infectedList.append(e) |
| 22 | + for i in infectedList: |
| 23 | + for e in nx.all_neighbors(G,i): |
| 24 | + if nodeAttribute[e]==0: |
| 25 | + K=G.copy() |
| 26 | + nx.set_node_attributes(K,{e:{"infected":-1}}) |
| 27 | + x=node.addChild(e) |
| 28 | + makeTree(K,H,1,x) |
| 29 | + elif turn==1: #player: defender |
| 30 | + possibleList=[] |
| 31 | + nodeAttribute=nx.get_node_attributes(G,"infected") |
| 32 | + for e in nodeAttribute.keys(): |
| 33 | + if nodeAttribute[e]==0: |
| 34 | + possibleList.append(e) |
| 35 | + for i in possibleList: |
| 36 | + K=G.copy() |
| 37 | + nx.set_node_attributes(K,{i:{"infected":1}}) |
| 38 | + x=node.addChild(i) |
| 39 | + makeTree(K,H,0,x) |
| 40 | + |
| 41 | + |
| 42 | +# In[3]: |
| 43 | + |
| 44 | +#applying minimax algorithm (attacker is maximizing, defendor is minimizing) |
| 45 | +def minimax(node,turn,depth): |
| 46 | + global temp |
| 47 | + temp=temp+1 |
| 48 | + #when leaf node is reached, return the rank of the node |
| 49 | + if node.isLeaf() == True: |
| 50 | + node.set_rank(depth) |
| 51 | + return depth |
| 52 | + |
| 53 | + if turn==0: |
| 54 | + #attacker's turn |
| 55 | + max_rank = 0 |
| 56 | + for c in node.get_children(): |
| 57 | + label1 = c.get_label() |
| 58 | + tempA = minimax(c,1,depth+1) |
| 59 | + if(tempA > max_rank): |
| 60 | + max_node = c |
| 61 | + max_label = label1 |
| 62 | + max_rank = tempA |
| 63 | + node.set_rank(max_rank) |
| 64 | + return (max_rank) |
| 65 | + elif turn==1: |
| 66 | + min_rank = 1000000000000 |
| 67 | + for c in node.get_children(): |
| 68 | + label1 = c.get_label() |
| 69 | + tempD = minimax(c,0,depth+1) |
| 70 | + if(tempD < min_rank): |
| 71 | + max_node = c |
| 72 | + max_label = label1 |
| 73 | + min_rank = tempD |
| 74 | + node.set_rank(min_rank) |
| 75 | + return (min_rank) |
| 76 | + |
| 77 | + |
| 78 | +# In[4]: |
| 79 | + |
| 80 | +#applying minimax algorithm (attacker is maximizing, defendor is minimizing) |
| 81 | +def minimaxwithab(node,turn,depth,alpha,beta): |
| 82 | + #when leaf node is reached, return the rank of the node |
| 83 | + global temp1 |
| 84 | + temp1=temp1+1 |
| 85 | + if node.isLeaf() == True: |
| 86 | + node.set_rank(depth) |
| 87 | + return depth |
| 88 | + |
| 89 | + if turn==0: |
| 90 | + #attacker's turn |
| 91 | + max_rank = 0 |
| 92 | + for c in node.get_children(): |
| 93 | + label1 = c.get_label() |
| 94 | + tempA = minimaxwithab(c,1,depth+1,alpha, beta) |
| 95 | + if(tempA > max_rank): |
| 96 | + max_node = c |
| 97 | + max_label = label1 |
| 98 | + max_rank = tempA |
| 99 | + if(max_rank > alpha): |
| 100 | + alpha = max_rank |
| 101 | + if(beta <= alpha): |
| 102 | + break |
| 103 | + node.set_rank(max_rank) |
| 104 | + return (max_rank) |
| 105 | + elif turn==1: |
| 106 | + #defender's turn |
| 107 | + min_rank = 100000 |
| 108 | + for c in node.get_children(): |
| 109 | + label1 = c.get_label() |
| 110 | + tempD = minimaxwithab(c,0,depth+1, alpha, beta) |
| 111 | + if(tempD < min_rank): |
| 112 | + max_node = c |
| 113 | + max_label = label1 |
| 114 | + min_rank = tempD |
| 115 | + if(min_rank < beta): |
| 116 | + beta = min_rank |
| 117 | + if(beta <= alpha): |
| 118 | + break |
| 119 | + node.set_rank(min_rank) |
| 120 | + return (min_rank) |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +# In[5]: |
| 125 | + |
| 126 | +def getPrunedGraph(G,i): |
| 127 | + K=nx.Graph() |
| 128 | + queue=[i] |
| 129 | + K.add_node(i, depth=0) |
| 130 | + nx.set_node_attributes(K,{i:{"visited":1, "infected":-1}}) |
| 131 | + curr_count=0 |
| 132 | + curr_depth=0 |
| 133 | + for i in queue: |
| 134 | + if nx.get_node_attributes(K,"depth")[i]>curr_depth: |
| 135 | + if curr_count<=nx.get_node_attributes(K,"depth")[i]: |
| 136 | + break |
| 137 | + else: |
| 138 | + curr_count=0 |
| 139 | + curr_depth=nx.get_node_attributes(K,"depth")[i] |
| 140 | + child=nx.all_neighbors(G,i) |
| 141 | + for c in child: |
| 142 | + if not K.has_node(c): |
| 143 | + K.add_node(c,depth=nx.get_node_attributes(K,"depth")[i]+1,infected=nx.get_node_attributes(G,"infected")[c]) |
| 144 | + queue.append(c) |
| 145 | + curr_count=curr_count+1 |
| 146 | + K.add_edge(i,c) |
| 147 | + return K |
| 148 | + |
| 149 | + |
| 150 | +# In[15]: |
| 151 | + |
| 152 | +#make root of minimax |
| 153 | +for ratio in range(200,400,1): |
| 154 | + ratio=ratio/200 |
| 155 | + for num in range(10,500): |
| 156 | + |
| 157 | + graph_properties=[num,math.floor(num*ratio)] |
| 158 | + result=graph_properties |
| 159 | + for i in range(0,100): |
| 160 | + G=nx.gnm_random_graph(graph_properties[0],graph_properties[1], directed=True) |
| 161 | + for n in G.nodes(): |
| 162 | + G.node[n]['infected']=0 |
| 163 | + |
| 164 | + i=rnd.randint(0,len(G.nodes())-1) |
| 165 | + G.node[i]['infected'] = -1 |
| 166 | + G1=getPrunedGraph(G,i) |
| 167 | + k=nx.get_node_attributes(G,"infected") |
| 168 | + k1=nx.get_node_attributes(G1,"infected") |
| 169 | + for i in k.keys(): |
| 170 | + if k[i]==-1: |
| 171 | + label=i |
| 172 | + break |
| 173 | + temp=0 |
| 174 | + temp1=0 |
| 175 | + H = MinMaxTree(label) |
| 176 | + K= MinMaxTree(label) |
| 177 | + makeTree(G,H,1,H.root) |
| 178 | + makeTree(G,K,1,K.root) |
| 179 | + unpruned_minimax=minimax(H.root,1,1) |
| 180 | + unpruned_minimax_explored=temp |
| 181 | + unpruned_minimaxwithab=minimaxwithab(K.root,1,1,-1000000000,1000000) |
| 182 | + unpruned_minimaxwithab_explored=temp1 |
| 183 | + temp=0 |
| 184 | + temp1=0 |
| 185 | + H1 = MinMaxTree(label) |
| 186 | + K1 = MinMaxTree(label) |
| 187 | + makeTree(G1,H1,1,H1.root) |
| 188 | + makeTree(G1,K1,1,K1.root) |
| 189 | + pruned_minimax=minimax(H1.root,1,1) |
| 190 | + pruned_minimax_explored=temp |
| 191 | + pruned_minimaxwithab=minimaxwithab(K1.root,1,1,-1000000000,1000000) |
| 192 | + pruned_minimaxwithab_explored=temp1 |
| 193 | + |
| 194 | + result.append(unpruned_minimax) |
| 195 | + result.append(unpruned_minimax_explored) |
| 196 | + result.append(unpruned_minimaxwithab) |
| 197 | + result.append(unpruned_minimaxwithab_explored) |
| 198 | + result.append(pruned_minimax) |
| 199 | + result.append(pruned_minimax_explored) |
| 200 | + result.append(pruned_minimaxwithab) |
| 201 | + result.append(pruned_minimaxwithab_explored) |
| 202 | + with open("result.csv","a") as r: |
| 203 | + r.write(str(result)) |
| 204 | + r.write("\n") |
| 205 | + break |
| 206 | + break |
| 207 | + break |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +# In[ ]: |
| 213 | + |
| 214 | + |
| 215 | + |
0 commit comments