-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinimaxAgent.py
195 lines (163 loc) · 7.24 KB
/
MinimaxAgent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from Agent import *
from US_STATE import *
import copy
class MinimaxAgent(Agent):
def __init__(self, type, color):
super().__init__(type, color)
self.bonus_country = self.attack_country = self.defend_country = None
def takeTurn(self, countries):
#initialize state
#intitialState = Node(US_STATE.countries, self)
self.attack(countries)
def attack(self, state):
opponent = None
mp = {}
for c in state:
mp[c.id] = c
if c.owner == self:
c.ownerboolean = True
else:
c.ownerboolean = False
opponent = c.owner
newCountries = self.decision(state)
if newCountries == None:
return
self.countries.clear()
opponent.countries.clear()
for c in newCountries:
if c.ownerboolean is True:
mp[c.id].owner = self
self.countries.append(c)
else:
mp[c.id].owner = opponent
opponent.countries.append(mp[c.id])
mp[c.id].numOfTroops = c.numOfTroops
b = mp[self.bonus_country]
a = mp[self.attack_country]
d = mp[self.defend_country]
use_timer = False
if use_timer:
t = threading.Timer(3, self.setTroops, args=(
d, a, b, d.numOfTroops, a.numOfTroops,
b.numOfTroops))
t.start()
b.numOfTroops = str(b.numOfTroops) + ' B'
a.numOfTroops = str(a.numOfTroops) + 'A'
d.numOfTroops = str(d.numOfTroops) + "D"
def decision(self, state) -> [Country]:
(child, x) = self.maximize(state, 0, -1e9, 1e9)
return child
def heuristic(self, state: [Country]):
attacking = 0
defending = 0
for c in state:
if c.owner == self:
for n in c.neighbors:
if n.owner != self and c.numOfTroops - 1 > n.numOfTroops:
attacking += 1
else:
for n in c.neighbors:
if n.owner == self and c.numOfTroops - 1 <= n.numOfTroops:
defending += 1
return attacking+defending
def maximize(self, state, depth, alpha, beta) -> ([Country], int):
# evaluate heuristic of state
stateVal = self.heuristic(state)
if depth == 2:
return (state, stateVal)
(maxState, maxVal) = (None, -1e9)
bonus = self.calcBonusTroops()
done = False
for bonusCountry in state:
if done:
break
if bonusCountry.owner == self:
bonusCountry.numOfTroops += bonus
for c in state:
if done:
break
if c.owner == self:
for neighbor in c.neighbors:
if neighbor.owner != self and neighbor.numOfTroops < c.numOfTroops - 1:
attackerTroops = c.numOfTroops
defenderTroops = neighbor.numOfTroops
defenderOwner = neighbor.owner
defenderOwner.removeCountry(neighbor)
self.countries.append(neighbor)
neighbor.owner = self
neighbor.numOfTroops = attackerTroops - 1
c.numOfTroops = 1
neighbor.ownerboolean = True
(newstate, utility) = self.minimize(state, depth+1, alpha, beta)
if utility > maxVal:
maxVal = utility
maxState = copy.deepcopy(state)
self.attack_country = c.id
self.defend_country = neighbor.id
self.bonus_country = bonusCountry.id
c.numOfTroops = attackerTroops
neighbor.numOfTroops = defenderTroops
neighbor.owner = defenderOwner
neighbor.ownerboolean = False
self.removeCountry(neighbor)
defenderOwner.countries.append(neighbor)
if maxVal >= beta:
done = True
if done:
break
if maxVal > alpha:
alpha = maxVal
bonusCountry.numOfTroops -= bonus
return (maxState, maxVal)
def minimize(self, state, depth, alpha, beta) -> ([Country], int):
# evaluate heuristic of state
stateVal = self.heuristic(state)
if depth == 2:
return (state, stateVal)
(minState, minVal) = (None, 1e9)
bonus = self.calcBonusTroops()
done = False
for bonusCountry in state:
if done:
break
if bonusCountry.owner != self:
bonusCountry.numOfTroops += bonus
for c in state:
if done:
break
if c.owner != self:
for neighbor in c.neighbors:
if neighbor.owner == self and neighbor.numOfTroops < c.numOfTroops - 1:
attackerTroops = c.numOfTroops
defenderTroops = neighbor.numOfTroops
defenderOwner = neighbor.owner
defenderOwner.removeCountry(neighbor)
c.owner.countries.append(neighbor)
neighbor.owner = c.owner
neighbor.numOfTroops = attackerTroops - 1
c.numOfTroops = 1
neighbor.ownerboolean = False
(newstate, utility) = self.maximize(state, depth+1, alpha, beta)
if utility < minVal:
minVal = utility
minState = copy.deepcopy(state)
c.numOfTroops = attackerTroops
neighbor.numOfTroops = defenderTroops
neighbor.owner = defenderOwner
neighbor.ownerboolean = True
c.owner.removeCountry(neighbor)
defenderOwner.countries.append(neighbor)
if minVal <= alpha:
done = True
if done:
break
if minVal < beta:
beta = minVal
bonusCountry.numOfTroops -= bonus
return (minState, minVal)
def setTroops(self, d,a,b, dnum, anum, bnum):
d.numOfTroops = dnum
a.numOfTroops = anum
b.numOfTroops = bnum
def chooseCountryToAddTroops(self) -> Country:
pass