-
Notifications
You must be signed in to change notification settings - Fork 2
/
secret_agent.py
80 lines (64 loc) · 2.29 KB
/
secret_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import agent
import random
import mathmodel
import graph_funcs
class SecretAgent(agent.Agent):
def __init__(self):
self.mm = mathmodel.MathModel()
def preferred_ids(self, num):
return ["Secret Agent Man #%02d" % num]
def pregame_place(self, numarmies, sim):
#c = random.choice(sim.owns[self])
#return {c:numarmies}
owned = sim.owns[self]
stronghold = max(graph_funcs.regions(owned, sim.edgelist), key=len)
c = random.choice(stronghold)
return {c:numarmies}
def attack(self, sim):
self.sim = sim
owned = sim.owns[self]
if len(owned) == 0:
# Uh... we don't own anything...
return None
# Find the largest cluster of countries that we own
stronghold = max(graph_funcs.regions(owned, sim.edgelist), key=len)
scorez = []
for country in graph_funcs.inner_border(stronghold, sim.edgelist):
# We don't have enough dudes to attack from this country
# Retreat!!!!
if self.army_size(country) < 2:
continue
# Retrieve a list of neighboring countries
neighbors = sim.edgelist[country]
neighbors = filter(lambda c: sim.countries[c] != self, neighbors)
neighbors = sorted(neighbors, key=lambda i: sim.armies[i])
if len(neighbors) == 0:
continue
for neighbor in neighbors:
#patch = sim.model.full_cdf()
#chancetowin = mathmodel.integral2d(patch, lambda a1,a2: a1 > 0)
# LOL there's a function for that LOL
score = self.mm.chance_to_win(sim.armies[country], sim.armies[neighbor])
scorez += [(country, neighbor, score)]
# print country,neighbors
# print scorez
#raw_input()
if len(scorez) > 0:
victim = sorted(scorez, key = lambda i: i[2]).pop()
return (victim[0], victim[1], sim.armies[victim[0]] - 1)
# print "Done"
#raw_input()
return None
def place_armies(self, numarmies, sim):
#return self.pregame_place(numarmies, sim) # STUPID
owned = sim.owns[self]
stronghold = max(graph_funcs.regions(owned, sim.edgelist), key=len)
stronghold = graph_funcs.inner_border(stronghold, sim.edgelist)
def summator(x):
edges = filter(lambda i: sim.countries[i] != self, sim.edgelist[x])
return sum(map(lambda i: sim.armies[i], edges))
threatened = map(lambda i: (i,summator(i)), stronghold)
coolest = max(threatened, key=lambda i: i[1])
return {coolest[0]: numarmies}
def army_size(self,c):
return self.sim.armies[c]