-
Notifications
You must be signed in to change notification settings - Fork 1
/
QL_pure.py
154 lines (119 loc) · 4.18 KB
/
QL_pure.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
from KuhnPoker import *
from treelib import Node, Tree
from CfrNode import CfrNode
from GameTree import GameTree
from matplotlib import pyplot as plt
import Utils
import math
class CFRtrainer:
def __init__(self):
self.playerOneTree = GameTree(CfrNode)
self.playerTwoTree = GameTree(CfrNode)
self.kuhn = KuhnPoker()
# def HasChild(self, parentId, childTag, tree):
# if(self.GetChildByTag(parentId, childTag, tree)):
# return True
#
# return False
#
# def GetChildByTag(self, parentId, childTag, tree):
# for childId in tree.children(parentId):
# childNode = tree[childId]
# if(childNode.tag == childTag):
# return childNode
#
# return None
def CFR(self, p0, p1):
curPlayer = self.kuhn.GetCurrentPlayer()
if(self.kuhn.IsTerminateState()):
return self.kuhn.GetPayoff(curPlayer)
curPlayerProb = p0 if curPlayer == Players.one else p1
tree = self.playerOneTree if curPlayer == Players.one else self.playerTwoTree
cfrNode = tree.GetOrCreateDataNode(self.kuhn, curPlayer)
strategy = cfrNode.GetStrategy(curPlayerProb)
util = [0.0] * NUM_ACTIONS
nodeUtil = 0
infosetStr = self.kuhn.GetInfoset(curPlayer)
infosetBackup = self.kuhn.SaveInfoSet()
#'1 | bet;bet;uplayed'
#'1 | bet;pas;uplayed'
# if(('1 | bet;bet' in infosetStr) and curPlayer == Players.one):
# g = 6
for action in range(NUM_ACTIONS):
self.kuhn.MakeAction(action)
if(curPlayer == Players.one):
util[action] = -self.CFR(p0 * strategy[action], p1)
else:
util[action] = -self.CFR(p0, p1 * strategy[action])
cfrNode.util[action] += util[action]
nodeUtil += strategy[action] * util[action]
self.kuhn.RestoreInfoSet(infosetBackup)
for action in range(NUM_ACTIONS):
regret = util[action] - nodeUtil
opProb = p1 if curPlayer == Players.one else p0
cfrNode.regretSum[action] += opProb * regret
#0445733333
return nodeUtil
def running_mean(self, x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / N
def Train(self):
util = 0
cnt = 0
# self.playerOneTree.GetOrCreateCFRNode(self.kuhn, Players.one)
# self.playerTwoTree.GetOrCreateCFRNode(self.kuhn, Players.one)
# while (self.kuhn.NewRound() != 1):
# util += self.CFR(1, 1)
# cnt += 1
# if(cnt % 10 == 0):
# print(util / cnt)
results = []
# utils = []
for i in range(1, 10000):
self.kuhn.NewRound()
curUtil = self.CFR(1, 1)
# utils.append(curUtil)
util += curUtil
if(cnt % 100 == 0):
results.append(util / i)
print("Avg util:", util / i)
# plt.plot(results)
# plt.show()
def CheckNash(self):
if (self.kuhn.IsPlayerOneCloseToNash(self.playerOneTree)):
print("Player one is in Nash")
else:
print("Player one is not in Nash")
if(self.kuhn.IsPlayerTwoCloseToNash(self.playerTwoTree)):
print("Player two is in Nash")
else:
print("Player two is not in Nash")
trainer = CFRtrainer()
trainer.Train()
#
print("Player one avg strategy:")
trainer.playerOneTree.PrintAvgStrategy()
print("Player one best resp strategy:")
trainer.playerOneTree.PrintBestResp()
print("Player Util regret strategy:")
trainer.playerOneTree.PrintUtilRegretStrategy()
print("Player Util strategy:")
trainer.playerOneTree.GetUtilStrategy()
print("Player Utils:")
trainer.playerOneTree.PrintUtils()
print("Player one regrets:")
trainer.playerOneTree.PrintRegrets()
#
#
# print("----------------------")
# print("Player two avg strategy:")
# trainer.playerTwoTree.PrintAvgStrategy()
# # print("Player two best resp strategy:")
# # trainer.playerTwoTree.PrintBestResp()
# # print("Player two regrets:")
# # trainer.playerTwoTree.PrintRegrets()
#
#
# print("Max dif: " , KuhnPoker.MaxDif)
# print("done")
#