Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
"""
2
Hugely inspired by the Zero Determinant Implementations found in the Python-Axelrod library
3
4
http://axelrod.readthedocs.io/en/latest/reference/overview_of_strategies.html#zdgtft-2
5
https://github.com/Axelrod-Python/Axelrod/blob/master/axelrod/strategies/memoryone.py
6
"""
7
import random
8
9
C = 1
10
D = 0
11
12
class ZeroDeterminant():
13
"""It uses transition probabilities based on the last round of play by both players.
14
This strategy came in 1st in average score during a 2012 run of a tournament"""
15
16
probs = [1, 1/8., 1, 1/4.]
17
18
def __init__(self):
19
self.four_vector = dict(zip([(C,C),(C,D),(D,C),(D,D)], self.probs))
20
21
def step(self, history, round):
22
if round == 0:
23
action = C
24
else:
25
probC = self.four_vector[(history[self.order][round-1], history[self.order^1][round-1])]
26
action = int(random.random() <= probC)
27
return action
28
29
if __name__ == "__main__":
30
zd = ZeroDeterminant()
31
zd.order = 0
32
print zd.step([[0],[0]], 1)
33
print zd.step([[0],[1]], 1)
34
print zd.step([[1],[0]], 1)
35
print zd.step([[1],[1]], 1)
36
37