Commit 10c22670 by ziho

test

parent 64b4d1ab
import os
from PK import pk
import time
import random
import re
def choose_game():
dir=os.listdir('comm')
random.shuffle(dir)
for d in dir:
res=re.match(r'(\d+)_(\d+)\.init',d)
if res:
black=res.group(1)
white=res.group(2)
name=black+'_'+white
try:
src='comm/'+d
dst='comm/'+name+'running'
os.rename(src,dst)
except:
continue
else:
return [black,white]
return None
def main():
time.sleep(random.uniform(3,6))
while not os.path.exists('stop.txt'):
if os.path.exists('pause.txt'):
time.sleep(10)
continue
game=choose_game()
if game:
winrate=pk(game[0],game[1])
name='_'.join([game[0],game[1],str(winrate)])+'.finish'
os.system('touch '+name)
else:
time.sleep(1)
if __name__ == '__main__':
main()
from elorating import EloRatingSystem
import numpy as np
import random
import os
import re
import time
elo=EloRatingSystem(recordSaveFile='gameRecord.txt') #初始化
def askYes(str):
ans=''
while True:
ans=input(str)
ans=ans.lower()
if ans=='y' or ans=='yes':
return True
elif ans=='n' or ans=='no':
return False
else:
print('please input <y|n>')
def main():
print('--------------ELO rating system---------------')
models=[str(i) for i in range(300)] #模型的名字
print('default models:range(0,300,1) or [0,299]')
elo.addModels(models) #添加模型
elo.summary() #查看现有模型
os.system('touch pause.txt') #通知client先不必行动
if not os.path.exists('comm'):
os.mkdir('comm')
if os.path.exists('stop.txt'):
os.remove('stop.txt')
if os.path.exists('gameRecord.txt'):
print('\n game records has found!')
if askYes('read it? if not,will delete it and create a new one <y/n> :'):
elo.readRecords('gameRecord.txt')
else:
os.remove('gameRecord.txt')
commandList=[r'play (\d+)','summary','stop']
print('commandList:\n'+'\n '.join(commandList))
ans=input('>>')
while True:
if ans=='stop':
#todo 保存记录?通知client退出?
break
elif ans=='summary':
elo.summary()
else:
matchRes=re.match(r'play (\d+)',ans)
if matchRes:
num=matchRes.group(1)
play(elo,num)
print('commandList:\n'+'\n '.join(commandList))
ans=input('>>')
def play(total_game):
max_game=500
complete_game=0
pause_flag='pause.txt'
if os.path.exists(pause_flag):
os.remove(pause_flag) #去掉上次的暂停标志
def choose():
#挑选比赛对手,选择分数相近的作为比赛对手
while True:
players=elo.getPlayers(sortByRank=True)
black=random.choice(players).name
matchList=elo.getNearPlayer(black)
if matchList:
white=random.choice(matchList)
return black,white
else:
continue #找不到对手,谁都打不过,或者谁都打不过
def create_game(num):
if num<=0:
return
i=0
while i<num:
black,white=choose()
name=black+'_'+white
if os.path.exists(name+'.init') or os.path.exists(name+'.running') or os.path.exists(name+'.complete'):
continue
os.system('touch '+name+'.init')
i+=1
def read():
#读取完成的比赛记录
count=0
dir=os.listdir('comm')
for d in dir:
res=re.match(r'(\d+)_(\d+)_(\d+)\.finish',d)
if res:
black=res.group(1)
white=res.group(2)
winrate=int(res.group(3))
elo.playgame(black,white,winrate)
os.remove(os.path.join('comm',d))
count+=1
return count
def exist_game():
count=0
dir=os.listdir('comm')
for d in dir:
res=re.match(r'(\d+)_(\d+)\.running',d)
if res:
count+=1
continue
res=re.match(r'(\d+)_(\d+)\.init',d)
if res:
count+=1
continue
return count
while complete_game<total_game:
complete_game+=read()
num=exist_game()
create_game(min(max_game-exist_game,total_game-complete_game-exist_game+10))
time.sleep(1)
os.system('touch pause.txt') #通知client先暂停行动
if __name__ == '__main__':
main()
\ No newline at end of file
import random
import time
#测试用pk函数
def pk(model1,model2,num_games=1):
time.sleep(random.uniform(8,12))
winrate=1-0.5**(abs(int(model1)-int(model2))/1) #假设新模型以一定概率战胜旧模型
res=random.choices([1,0.5,0],weights=[winrate,0,1-winrate],k=1)
if int(model1)>int(model2):
return res[0]
else:
return 1-res[0]
\ No newline at end of file
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