Skip to content

Commit 6f4eae6

Browse files
committed
initial
0 parents  commit 6f4eae6

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed

MinMaxClass.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
# coding: utf-8
3+
4+
# In[ ]:
5+
6+
class MinMaxNode:
7+
def __init__(self,label,rank=0):
8+
self.label = label
9+
self.rank = rank
10+
self.children= []
11+
12+
def get_label(self):
13+
return self.label
14+
15+
def get_children(self):
16+
return self.children
17+
def set_rank(self,newRank):
18+
self.rank = newRank
19+
def addChild(self,label):
20+
c = self.checkIfChildExists(label)
21+
if c==False:
22+
x=MinMaxNode(label)
23+
self.children.append(x)
24+
return x
25+
return c
26+
def checkIfChildExists(self,label):
27+
for c in self.children:
28+
if c.label==label:
29+
return c
30+
return False
31+
def __str__(self, level=0):
32+
if level%2==0:
33+
c="Attacker"
34+
else:
35+
c="Defender"
36+
ret = "\t"*level+repr(self.label)+"("+repr(self.rank)+")-"+c+"\n"
37+
for child in self.children:
38+
ret += child.__str__(level+1)
39+
ret+="\n"
40+
return ret
41+
42+
def __repr__(self):
43+
return '<tree node representation>'
44+
def isLeaf(self):
45+
if len(self.children)==0:
46+
return True
47+
return False
48+
49+
50+
# In[ ]:
51+
52+
class MinMaxTree:
53+
def __init__(self,label):
54+
self.root = MinMaxNode(label)
55+

MiniMaxAlgorithm.py

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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

Comments
 (0)