-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.py
113 lines (97 loc) · 4.61 KB
/
test.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
from dilemma import Game
from strategies import *
from collections import Counter
"""
If the machine learning algorithm don't care about future rewards. (decay == 0)
It's the non-iterated version of the game.
The best strategy is to defect (and win 1 or 5).
If the machine learning algorithm care about future rewards.
It's the iterated version of the game.
The best strategy is up to the opponant strategy.
"""
def ml_vs_titfortat_non_iterate():
print("Start a non-iterated game between MachineLearning and Titfortat")
prisoner_a = MachineLearning("A")
prisoner_a.qlearning.gamma = 0.0 # We don't care about future rewards
prisoner_b = Titfortat("B")
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print("last 10 moves: ", prisoner_a.actions[-10:])
print("ml values: ", prisoner_a.qlearning.values)
def ml_vs_titfortat_iterate():
print("Start an _iterated_ game between MachineLearning and Titfortat")
prisoner_a = MachineLearning("A")
prisoner_a.qlearning.gamma = 0.9 # We deeply care about future rewards
prisoner_b = Titfortat("B")
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print("last 10 moves: ", prisoner_a.actions[-10:])
def ml_vs_ml_non_iterate():
print("Start a non-iterated game between two MachineLearning bots")
prisoner_a = MachineLearning("A")
prisoner_a.qlearning.gamma = 0.0 # We don't care about future rewards
prisoner_b = MachineLearning("B")
prisoner_b.qlearning.gamma = 0.0 # We don't care about future rewards
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print("last 10 moves of a: ", prisoner_a.actions[-10:])
print("last 10 moves of b: ", prisoner_b.actions[-10:])
def ml_vs_ml_iterate():
print("Start an _iterated_ game between two MachineLearning bots")
prisoner_a = MachineLearning("A")
prisoner_a.qlearning.gamma = 0.9 # We care about future rewards
prisoner_b = MachineLearning("B")
prisoner_b.qlearning.gamma = 0.9 # We care about future rewards
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print("last 10 moves of a: ", prisoner_a.actions[-10:])
print("last 10 moves of b: ", prisoner_b.actions[-10:])
def deepqlearning_vs_titfortat_non_iterate():
print("Start a non-iterated game between DeepQLearning and Titfortat")
agent_a = DeepQLearnerAdaptater(2, 1, decay=0.0, learning_rate=0.02, scope="dvt_ni_a")
prisoner_a = MachineLearning("A", agent=agent_a)
prisoner_b = Titfortat("B")
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print('Last 1000 moves:')
print('prisoner_a:', Counter(prisoner_a.actions[-1000:]))
print('prisoner_b:', Counter(prisoner_b.actions[-1000:]))
def deepqlearning_vs_titfortat_iterate():
print("Start an iterated game between DeepQLearning and Titfortat")
agent_a = DeepQLearnerAdaptater(2, 1, decay=0.9, learning_rate=0.02, scope="dvt_i_a")
prisoner_a = MachineLearning("A", agent=agent_a)
prisoner_b = Titfortat("B")
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print('Last 1000 moves:')
print('prisoner_a:', Counter(prisoner_a.actions[-1000:]))
print('prisoner_b:', Counter(prisoner_b.actions[-1000:]))
def deepqlearning_vs_deepqlearning_non_iterate():
print("Start an non-iterated game between DeepQLearning and DeepQLearning")
agent_a = DeepQLearnerAdaptater(2, 1, decay=0.0, learning_rate=0.02, scope="dvd_ni_a")
prisoner_a = MachineLearning("A", agent=agent_a)
agent_b = DeepQLearnerAdaptater(2, 1, decay=0.0, learning_rate=0.02, scope="dvd_ni_b")
prisoner_b = MachineLearning("B", agent=agent_b)
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print('Last 1000 moves:')
print('prisoner_a:', Counter(prisoner_a.actions[-1000:]))
print('prisoner_b:', Counter(prisoner_b.actions[-1000:]))
def deepqlearning_vs_deepqlearning_iterate():
print("Start an _iterated_ game between DeepQLearning and Titfortat")
agent_a = DeepQLearnerAdaptater(2, 1, decay=0.9, learning_rate=0.02, scope="dvd_i_a")
prisoner_a = MachineLearning("A", agent=agent_a)
agent_b = DeepQLearnerAdaptater(2, 1, decay=0.9, learning_rate=0.02, scope="dvd_i_b")
prisoner_b = MachineLearning("B", agent=agent_b)
game = Game(prisoner_a, prisoner_b)
game.play(10000)
print('Last 1000 moves:')
print('prisoner_a:', Counter(prisoner_a.actions[-1000:]))
print('prisoner_b:', Counter(prisoner_b.actions[-1000:]))
def main():
deepqlearning_vs_titfortat_non_iterate()
deepqlearning_vs_titfortat_iterate()
deepqlearning_vs_deepqlearning_non_iterate()
deepqlearning_vs_deepqlearning_iterate()
if __name__ == '__main__':
main()