-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_agent.py
56 lines (48 loc) · 1.7 KB
/
test_agent.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
import agent
import random
import mathmodel
class TestAgent(agent.Agent):
def __init__(self, level=0):
self.level = level
def pregame_place(self, numarmies, sim):
if self.level == 0:
c = random.choice(sim.owns[self])
return {c:numarmies}
locs = sim.owns[self]
adj_to_enemy = lambda c: len(filter(
lambda k: sim.countries[k] != self,
sim.edgelist[c]
)
) > 0
locs = filter(adj_to_enemy, locs)
c = random.choice(locs)
return {c:numarmies}
def attack(self, sim):
self.sim = sim
# find territory we own with most units
owned = sim.owns[self]
if len(owned) == 0:
# Uh... we don't own anything...
return None
top_owned = sorted(owned, key=self.army_size)
while len(top_owned) > 0:
src = top_owned.pop()
if self.army_size(src) < 2:
break
neighbors = sim.edgelist[src]
neighbors = filter(lambda c: sim.countries[c] != self, neighbors)
neighbors = sorted(neighbors, key=self.army_size)
if len(neighbors) == 0:
continue
dst = neighbors[0]
army2 = self.army_size(dst)
army1 = self.army_size(src) - 1
patch = sim.model.full_cdf(army1, army2)
chancetowin = mathmodel.integral2d(patch, lambda a1,a2: a1 > 0)
if chancetowin > 0.7:
return (src, dst, army1)
return None
def place_armies(self, numarmies, sim):
return self.pregame_place(numarmies, sim)
def army_size(self,c):
return self.sim.armies[c]