-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfootball.py
90 lines (76 loc) · 2.8 KB
/
gfootball.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
import numpy as np
import market
from market import walker
def gfootball(env):
goals_grid = np.array([
[.2, .3, .1, .1],
[.1, .1, .1, .0],
[.0, .0, .0, .0],
[.0, .0, .0, .0],
])
class calculate(walker):
def len(expr):
grid = calculate(expr.coll)
return {
i: sum(grid[j, i - j] for j in xrange(0, i + 1))
for i in xrange(0, grid.shape[0])
}
def map(expr):
assert expr.fn == market.len
# TODO: Lift len into its own function.
grid = calculate(expr.coll)
return {
k: {i: sum(grid[k][j, i - j] for j in xrange(0, i + 1))
for i in xrange(0, grid[k].shape[0])}
for k in grid
}
def partition(expr):
# TODO: Support non-team partitioning.
assert expr.by == 'team'
grid = calculate(expr.coll)
# TODO: Use masked arrays and share the data.
home = np.zeros(grid.shape)
draw = np.zeros(grid.shape)
away = np.zeros(grid.shape)
for i in xrange(0, grid.shape[0]):
for j in xrange(0, grid.shape[1]):
if i > j:
home[i, j] = grid[i, j]
elif i == j:
draw[i, j] = grid[i, j]
else:
away[i, j] = grid[i, j]
return {('home',): home, ('home', 'away'): draw, ('away',): away}
# TODO: Does this make sense?
def max(expr):
grid = calculate(expr.coll)
return {k: sum(v.itervalues()) for k, v in grid.iteritems()}
def collection(expr):
return goals_grid
def nth(expr):
grid = calculate(expr.coll)
nth = np.zeros(grid.shape)
n = expr.n + 1
for i in xrange(0, n + 1):
nth[i, n - i] = grid[i, n - i]
nth *= 1 / np.sum(nth)
return nth
# HINT: This would explode for all goals after the first because
# the diagonal will have a value. The diagonals need to be
# converted into a home and an away value.
def attr(expr):
assert expr.attr == 'team'
grid = calculate(expr.elem)
# TODO: Use partition to do the heavy lifting.
home = 0
away = 0
for i in xrange(0, grid.shape[0]):
for j in xrange(0, grid.shape[1]):
if i > j:
home += grid[i, j]
elif j > i:
away += grid[i, j]
else:
assert grid[i, j] == 0
return {'home': home, 'away': away}
return calculate