Commit eaa8837a by ziho

c

parent f5952a37
...@@ -4,8 +4,12 @@ import time ...@@ -4,8 +4,12 @@ import time
import random import random
import re import re
commdir='.' #共享目录
def cd(path):
return os.path.join(commdir,path)
def choose_game(): def choose_game():
dir=os.listdir('comm') dir=os.listdir(cd('comm'))
random.shuffle(dir) random.shuffle(dir)
for d in dir: for d in dir:
res=re.match(r'(\d+)_(\d+)\.init',d) res=re.match(r'(\d+)_(\d+)\.init',d)
...@@ -14,8 +18,8 @@ def choose_game(): ...@@ -14,8 +18,8 @@ def choose_game():
white=res.group(2) white=res.group(2)
name=black+'_'+white name=black+'_'+white
try: try:
src='comm/'+d src=cd('comm/'+d)
dst='comm/'+name+'.running' dst=cd('comm/'+name+'.running')
os.rename(src,dst) os.rename(src,dst)
except: except:
continue continue
...@@ -25,16 +29,16 @@ def choose_game(): ...@@ -25,16 +29,16 @@ def choose_game():
def main(): def main():
time.sleep(random.uniform(3,6)) time.sleep(random.uniform(3,6))
while not os.path.exists('stop.txt'): while not os.path.exists(cd('stop.txt')):
if os.path.exists('pause.txt'): if os.path.exists(cd('pause.txt')):
time.sleep(10) time.sleep(10)
continue continue
game=choose_game() game=choose_game()
if game: if game:
winrate=pk(game[0],game[1]) winrate=pk(game[0],game[1])
name='comm/'+'_'.join([game[0],game[1],str(winrate)])+'.finish' name='comm/'+'_'.join([game[0],game[1],str(winrate)])+'.finish'
os.system('touch '+name) os.system('touch '+cd(name))
os.remove('comm/'+game[0]+'_'+game[1]+'.running') os.remove(cd('comm/'+game[0]+'_'+game[1]+'.running'))
else: else:
time.sleep(1) time.sleep(1)
......
...@@ -5,7 +5,11 @@ import os ...@@ -5,7 +5,11 @@ import os
import re import re
import time import time
elo=EloRatingSystem(recordSaveFile='gameRecord.txt') #初始化 commdir='.' #共享目录
def cd(path):
return os.path.join(commdir,path)
elo=EloRatingSystem(recordSaveFile=cd('gameRecord.txt')) #初始化
def askYes(str): def askYes(str):
ans='' ans=''
...@@ -22,40 +26,47 @@ def askYes(str): ...@@ -22,40 +26,47 @@ def askYes(str):
def main(): def main():
print('--------------ELO rating system---------------') print('--------------ELO rating system---------------')
if os.path.exists(cd('models.txt')):
print('read models from models.txt')
with open(cd('models.txt'),'r') as f:
lines=f.readlines()
models=lines
else:
models=[str(i) for i in range(300)] #模型的名字 models=[str(i) for i in range(300)] #模型的名字
print('default models:range(0,300,1) or [0,299]') print('default models:range(0,300,1) or [0,299]')
elo.addModels(models) #添加模型 elo.addModels(models) #添加模型
elo.summary() #查看现有模型 elo.summary() #查看现有模型
os.system('touch pause.txt') #通知client先不必行动 os.system('touch '+cd('pause.txt')) #通知client先不必行动
if not os.path.exists('comm'): if not os.path.exists(cd('comm')):
os.mkdir('comm') os.mkdir(cd('comm'))
if os.path.exists('stop.txt'): if os.path.exists(cd('stop.txt')):
os.remove('stop.txt') os.remove(cd('stop.txt'))
if os.path.exists('gameRecord.txt'): if os.path.exists(cd('gameRecord.txt')):
print('\n game records has found!') print('\n game records has found!')
if askYes('read it? if not,will delete it and create a new one <y/n> :'): if askYes('read it? if not,will delete it and create a new one <y/n> :'):
elo.readRecords('gameRecord.txt') elo.readRecords(cd('gameRecord.txt'))
else: else:
os.remove('gameRecord.txt') os.remove(cd('gameRecord.txt'))
commandList=[r'play (\d+)','summary','stop'] commandList=[r'play (\d+)','summary','stop','save']
print('commandList:\n'+'\n '.join(commandList)) print('commandList:\n '+'\n '.join(commandList))
ans=input('>>') ans=input('>>')
while True: while True:
if ans=='stop': if ans=='stop':
#todo 保存记录?通知client退出? os.system('touch '+cd('stop.txt'))
os.system('touch stop.txt')
break break
elif ans=='summary': elif ans=='summary':
elo.summary() elo.summary()
elif ans=='save':
elo.save(saveFile=cd('output.txt'),sortByRank=False)
else: else:
matchRes=re.match(r'play (\d+)',ans) matchRes=re.match(r'play (\d+)',ans)
if matchRes: if matchRes:
num=matchRes.group(1) num=matchRes.group(1)
play(int(num)) play(int(num))
print('commandList:\n'+'\n '.join(commandList)) print('commandList:\n '+'\n '.join(commandList))
ans=input('>>') ans=input('>>')
...@@ -63,8 +74,8 @@ def play(total_game): ...@@ -63,8 +74,8 @@ def play(total_game):
max_game=500 max_game=500
complete_game=0 complete_game=0
pause_flag='pause.txt' pause_flag='pause.txt'
if os.path.exists(pause_flag): if os.path.exists(cd(pause_flag)):
os.remove(pause_flag) #去掉上次的暂停标志 os.remove(cd(pause_flag)) #去掉上次的暂停标志
def choose(): def choose():
#挑选比赛对手,选择分数相近的作为比赛对手 #挑选比赛对手,选择分数相近的作为比赛对手
...@@ -85,9 +96,9 @@ def play(total_game): ...@@ -85,9 +96,9 @@ def play(total_game):
while i<num: while i<num:
black,white=choose() black,white=choose()
name=black+'_'+white name=black+'_'+white
if os.path.exists('comm/'+name+'.init') or os.path.exists('comm/'+name+'.running') or os.path.exists('comm/'+name+'.complete'): if os.path.exists(cd('comm/'+name+'.init')) or os.path.exists(cd('comm/'+name+'.running')) or os.path.exists(cd('comm/'+name+'.complete')):
continue continue
os.system('touch comm/'+name+'.init') os.system('touch '+cd('comm/'+name+'.init'))
i+=1 i+=1
def read(): def read():
...@@ -101,7 +112,7 @@ def play(total_game): ...@@ -101,7 +112,7 @@ def play(total_game):
white=res.group(2) white=res.group(2)
winrate=int(res.group(3)) winrate=int(res.group(3))
elo.playgame(black,white,winrate) elo.playgame(black,white,winrate)
os.remove(os.path.join('comm',d)) os.remove(cd(os.path.join('comm',d)))
count+=1 count+=1
return count return count
...@@ -128,7 +139,7 @@ def play(total_game): ...@@ -128,7 +139,7 @@ def play(total_game):
os.system('touch pause.txt') #通知client先暂停行动 os.system('touch '+cd('pause.txt')) #通知client先暂停行动
......
import os
import time
for i in range(200):
os.system('nohup python ELO_client.py &')
time.sleep(0.1)
\ No newline at end of file
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
import numpy as np import numpy as np
import random import random
import os
def main(): def main():
models=[str(i) for i in range (0,50000,1000)] models=[str(i) for i in range (0,50000,1000)]
...@@ -188,10 +189,20 @@ class EloRatingSystem: ...@@ -188,10 +189,20 @@ class EloRatingSystem:
return p.rating return p.rating
players.sort(key=sortKey,reverse=True) players.sort(key=sortKey,reverse=True)
print('{:<10s}{:<10s}{:<10s}{:<10s}{:<10s}'.format('model','rating','games','wins','losses')) print('{:<10s}{:<10s}{:<10s}{:<10s}{:<10s}'.format('model','rating','games','wins','losses'))
for p in players: for p in players:
print('{:<10s}{:<10.2f}{:<10d}{:<10d}{:<10d}'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses)) print('{:<10s}{:<10.2f}{:<10d}{:<10d}{:<10d}'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses))
def save(self,saveFile='elo_rating_output.txt',sortByRank=False):
players=self.getPlayers(sortByRank)
with open(saveFile,'w') as f:
f.write('{:<10s}{:<10s}{:<10s}{:<10s}{:<10s}\n'.format('model','rating','games','wins','losses'))
for p in players:
f.write('{:<10s}{:<10.2f}{:<10d}{:<10d}{:<10d}\n'.format(str(p.name),p.rating,p.num_game,p.wins,p.losses))
def getPlayers(self,sortByRank=False): def getPlayers(self,sortByRank=False):
#默认按模型排序,加入选项可以按分数排序 #默认按模型排序,加入选项可以按分数排序
players=list(self.players.values()) players=list(self.players.values())
......
0
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
\ No newline at end of file
#### 运行
**1、**在models.txt写入所要加入的模型名称
​ pk函数应能用这个名字来标识对应的模型(测试用的PK.py仅能识别纯数字名字)
**2、**
​ 共享目录请到ELo_server.py和ELO_client.py中修改,默认是'.'
​ 所有的文件都会存到这个目录
**3、**
启动server,
``` bash
python ELo_server.py
```
将有一些提示信息,如果有之前的比赛记录会提示是否读取
然后会提示可用的命令,可以等client开启后再接着下一步
```
>>play 5000 将开启5000盘随机的比赛
>>summary 将输出现有模型情况
>>stop 输入stop后,所有的client都会自动结束
>>save 保存和summary相同的信息到文件里
```
**4、**启动client
我是这么开的
```
import os
import time
for i in range(200):
os.system('nohup python ELO_client.py &')
time.sleep(0.1)
```
**5、**server端会输出一些信息,play结束后可以用summary查看结果
#### 基本类 #### 基本类
``` python ``` python
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment