-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomata.py
80 lines (66 loc) · 2.06 KB
/
Automata.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
class Automaton:
PAYOFF_TABLE = [[(3, 3), (0, 4)],
[(4, 0), (1, 1)]]
def __init__(self, current, payoff, table, initial):
self.current = current
self.payoff = payoff
self.table = table
self.initial = initial
def interact(self, other, r):
"""
the sum of pay-offs for the two respective automata over all rounds
:param other: Automaton
:param r: rounds
:return: (Automaton)
"""
c1 = self.current
y1 = self.payoff
t1 = self.table
c2 = other.current
y2 = other.payoff
t2 = other.table
for i in range(0, r):
(p1, p2) = self.PAYOFF_TABLE[c1][c2]
c1 = t1[c1][c2]
y1 = y1 + p1
c2 = t2[c2][c1]
y2 = y2 + p2
self.current = c1
self.payoff = y1
other.current = c2
other.payoff = y2
return (self, other)
def jump(self, input, delta):
self.current = self.table[self.current][input]
self.payoff = self.payoff + delta
def pay(self):
"""
reset the historic payoff
:return: float
"""
return self.payoff
def clone(self):
"""
reset payoff and current state to initial strategy
:return: Automaton
"""
return Automaton(self.current, 0, self.table, self.initial)
def reset(self):
"""
reset the historic payoff
:return: Automation
"""
return Automaton(self.current, 0, self.table, self.initial)
def compute_payoffs(self, other_current):
"""
:param other_current: Natural
:return: [Automaton]
"""
return self.PAYOFF_TABLE[self.current][other_current]
def __eq__(self, other):
if not isinstance(other, Automaton):
return False
else:
return (self.current == other.current and self.payoff ==
other.payoff and self.initial == other.initial and
self.table == other.table)