-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmorality_metrics.py
86 lines (69 loc) · 2.48 KB
/
morality_metrics.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
"""
Morality metrics using the Axelrod library available at
https://github.com/marcharper/Axelrod
"""
import itertools
import numpy
import axelrod
from example_tournaments import axelrod_strategies, tscizzle_strategies, random_strategies, memoryone_strategies, run_tournament
def compute_metrics(strategies, noise=0):
results = run_tournament("tournament", strategies, noise=noise)
rows = []
for i in range(len(results.players)):
row = [str(results.players[i]), numpy.mean(results.normalised_scores[i]), numpy.mean(results.normalised_cooperation[i]), results.good_partner_rating[i], results.eigenjesus_rating[i], results.eigenmoses_rating[i]]
rows.append(row)
return rows
def print_rows(rows):
for row in rows:
print ",".join(map(str, row))
def wrap_html_tag(tag, data, attr=None):
attr_str = ""
if attr: # it's a dict
for k, v in attr.items():
attr_str += ' %s="%s"' % (k, v)
return '<%s%s>%s</%s>' % (tag, attr_str, data, tag)
def row_to_html(row, attr=None, header=False):
tag = "td"
if header:
tag = "th"
html = ""
for x in row:
if not isinstance(x, str):
x = round(x, 3)
html += wrap_html_tag(tag, x)
html = wrap_html_tag("tr", html, attr=attr) + "\n"
return html
def rows_to_html_table(rows, headers):
# Headers
header_html = row_to_html(headers, header=True)
html = wrap_html_tag("thead", header_html)
# Body
body_html = ""
for row, class_ in itertools.izip(rows, itertools.cycle(["", "alt"])):
body_html += row_to_html(row, {"class": class_})
body_html = wrap_html_tag("tbody", body_html)
html += body_html
# Wrap in a table tag
html = wrap_html_tag("table", html, {"class": "tablesorter"})
return html
def tft_strats():
strategies = [
axelrod.TitForTat(),
axelrod.Alternator(),
axelrod.CyclerCCD(),
axelrod.CyclerCCCD(),
axelrod.CyclerCCCCCD(),
axelrod.AntiCycler(),
axelrod.WinStayLoseShift(),
axelrod.FoolMeOnce()
]
return strategies
if __name__ == "__main__":
headers = ["Player Name", "Mean Score", "Cooperation Rate",
"Good Partner Rating", "EigenJesus", "EigenMoses"]
#strategies = tscizzle_strategies()
#strategies = axelrod_strategies(cheaters=False, meta=False)
#strategies = random_strategies()
strategies = tft_strats()
rows = compute_metrics(strategies)
print(rows_to_html_table(rows, headers))