-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeatures.py
167 lines (140 loc) · 4.67 KB
/
features.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
"""Extract features from the history of play of a game theory match between
two players.
Potential additionally valuable features:
* Context responses
* Longest Streaks
* Has Cycles
* More history
"""
from collections import defaultdict
import numpy as np
import axelrod as axl
# mapping = {'C': 1, 'D': -1}
mapping = {'C': 1, 'D': 0}
def zeros_and_ones(h, round_num=-1):
"""Translate C/D history to binary."""
return list(map(lambda x: mapping[x], h[:round_num]))
# def cumulative_context_counts(h1, h2):
# """Extracts context counts for the finite state process underlying a game:
# CC, CD, DC, DD."""
#
# counts = []
# d = defaultdict(int)
# for i, (p1, p2) in enumerate(zip(h1, h2)):
# d[str(p1) + str(p2)] += 1
# counts.append((d['CC'], d['CD'], d['DC'], d['DD']))
# return counts
def cumulative_cooperations(h):
"""Computes the cumulative cooperations over the full history."""
coops = []
s = 0
for play in h:
if play == 'C':
s += 1
coops.append(s)
return coops
def cumulative_scores(h1, h2):
"""Computes the cumulative scores of each player."""
ss1, ss2 = [], []
game = axl.Game()
for p1, p2 in zip(h1, h2):
s1, s2 = game.score((p1, p2))
ss1.append(s1)
ss2.append(s2)
return np.cumsum(ss1), np.cumsum(ss2)
def starting_moves(h, round_num, depth):
base = [0] * (2 * depth)
for i in range(0, min(round_num, depth)):
if h[i] == 'C':
base[2 * i] = 1
base[2 * i + 1] = 0
else:
base[2 * i] = 0
base[2 * i + 1] = 1
return base
def trailing_moves(h, round_num, depth):
base = [0] * (2 * depth)
for i in range(0, min(round_num, depth)):
if h[round_num - i - 1] == 'C':
base[2 * i] = 1
base[2 * i + 1] = 0
else:
base[2 * i] = 0
base[2 * i + 1] = 1
return base
def num_features(starting=2, trailing=2, include_scores=False):
length = 5 + 2 * starting + 4 * trailing
if include_scores:
length += 2
return length
def extract_features_single(h1, h2, c1, c2, round_num, starting=2, trailing=2,
include_scores=False,
include_target=False):
if include_scores:
scores1, scores2 = cumulative_scores(h1, h2)
# h1 = zeros_and_ones(h1)
# h2 = zeros_and_ones(h2)
# Default is round_num, player1 coops and defects, player2 coops and defects
length = num_features(starting=starting, trailing=trailing,
include_scores=include_scores)
# Handle N=0
if round_num == 0:
row = [0] * length
if include_target:
row += [mapping[h2[0]]]
return row
i = round_num - 1
row = [
round_num,
c1, round_num - c1,
c2, round_num - c2
]
row += starting_moves(h2, round_num, starting)
row += trailing_moves(h1, round_num, trailing)
row += trailing_moves(h2, round_num, trailing)
if include_scores:
row += [scores1[i], scores2[i]]
if include_target:
row += [mapping[h2[round_num]]]
return row
def extract_features(h1, h2, starting=2, trailing=2, include_scores=False,
include_target=False):
coops = cumulative_cooperations(h1)
op_coops = cumulative_cooperations(h2)
# ccs = cumulative_context_counts(h1, h2)
# if include_scores:
# scores1, scores2 = cumulative_scores(h1, h2)
# h1 = zeros_and_ones(h1)
# h2 = zeros_and_ones(h2)
# Default is round_num, player1 coops and defects, player2 coops and defects
# length = num_features(starting=starting, trailing=trailing,
# include_scores=include_scores)
# # Handle N=0
# row = [0] * length
# if include_target:
# row += [h2[0]]
# yield row
# for round_num in range(1, len(h1)):
# i = round_num - 1
# row = [
# round_num,
# coops[i], round_num - coops[i],
# op_coops[i], round_num - op_coops[i]
# ]
# row += starting_moves(h2, round_num, 2)
# row += trailing_moves(h1, round_num, 2)
# row += trailing_moves(h2, round_num, 2)
# if include_scores:
# row += [scores1[i], scores2[i]]
# if include_target:
# row += [h2[1]]
# yield row
for round_num in range(0, len(h1)):
row = extract_features_single(
h1, h2,
coops[round_num-1], op_coops[round_num-1],
round_num,
starting=starting, trailing=trailing,
include_scores=include_scores,
include_target=include_target)
yield row