From a2040eb18430e10639665c5dabf043786a484d8e Mon Sep 17 00:00:00 2001 From: mcgrady00h <281130306@qq.com> Date: Thu, 30 Nov 2017 19:01:38 +0800 Subject: [PATCH 1/3] local change --- GTP/.test.py.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 GTP/.test.py.swp diff --git a/GTP/.test.py.swp b/GTP/.test.py.swp deleted file mode 100644 index b635af18e09fb58d34d0db6595b828ceb428bc48..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2TWB0r7{^a*Q#Cd9hQ$lY(JjetyP4g|b+a@=ySd`-Hp!+bRLwBiJ(-K@8s9*&_un(djg2k6AUO;`&|IF-`7&CKXeX)2Je%WNt z|9s~=|MShs2{{Ytxyb^}NNIxCErhr`?>+WI+ufveo)Dv~*=l9QYl(c#FtgEJI5Z{I zR5MEWcr##Xgf*5NtDc%|=%!Y6)x5fpn#rdeT{Rn3%T0~XPNf{yaHKka@TdVrn{Yrl z5IEpEi`|(H(%U2Zk9zEZ`|0hs9TDyb2ZRH{0pWmfKsX>A5Do|jgaiL02b}sg@&x9) zy*cOA=KHn{@68{@n{YrlARG`52nU1%!U5rca6mX991so&2ZRIHpaW`!kUaj_f8jbL zkN^L-{{FvoGa;XY55Y^o0tTpoIWPp0U_aOg65u|t9elBykoUpc;4QEQ&Vc8@aiD;C zkOF%_3=r^h2O&R!i{JwI7@P%b;6-o>JPfknF0dPPfbVW1u6^w&U&<4I~C*(cwGB^#M1PT}i_kdq-gg4+#a0Z+Jbx;IJ&<-x$ zK*+b?1Mmts4HPg7NF zmG8l2Z~>eHe*Ij7_5WW^S(9)Z=A^PIyxSKbU)n zj?05xJjDW$=Ojl<|p(|dQ3!2A&5CHXF@00QVfJD zwrbQ^#3(_GF_|~YXfS)SMSDx~zgU~iJ4da}OfX#R>S`evFFJmBF$U{26X9{7nC4c) z^cG@xwnk-I;*L`CM+pfMltYPI4l`WEG*>a@FWTml3G$eVFr=jjUznD{D{XR>rBRf7 zyx!cZ50h|DED#{1#XOjA0uP|TZzb=VTy8^qbSI{7zIB@NFssz1>e7@H9j0M z-yp`}2+vvyZa{$OKO?&J&FB1fx8`2v3mX z2@I})>t&u>7kO@7jAV?08&G%IhWNDl0E3%W6%ppe>!GLtg8~eJgS+%217&UN*rQ z3e_4ZUw7IX3E!3i8*;n@LXP)v^4(%^;dqprDRS|@QN4SLWMuI{Lss++v7z4Y%Ld3}1QsTyp9DwqzF{t|Ap6Rk|^ zw&hr5tEMb7yP;cNjtqxtTsR*!HQi)!4dE4Q;W$3j5=8Ub@Ci=ec#R4qG8ZDy`mrY85(chAxThUz^KPV+*%#44^- zHXro#BdWu)bk=3GXf4y843+z`eS`8K?MdUy{&Xgt_GDvv4YJm)`50Z+jqD;b%ftS~ XUnsiwb Date: Fri, 1 Dec 2017 01:37:55 +0800 Subject: [PATCH 2/3] compute score --- AlphaGo/__init__.py | 7 ++++ AlphaGo/game.py | 59 ++++++++++++++++++++++++++++++++-- AlphaGo/strategy.py | 2 ++ AlphaGo/test.py | 78 ++++++++++++++++++++++++--------------------- 4 files changed, 106 insertions(+), 40 deletions(-) create mode 100644 AlphaGo/__init__.py diff --git a/AlphaGo/__init__.py b/AlphaGo/__init__.py new file mode 100644 index 0000000..ae1e483 --- /dev/null +++ b/AlphaGo/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# $File: __init__.py +# $Date: Thu Nov 30 23:1446 2017 +0800 +# $Author: renyong15 © +# + diff --git a/AlphaGo/game.py b/AlphaGo/game.py index 192697a..88e5d17 100644 --- a/AlphaGo/game.py +++ b/AlphaGo/game.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim:fenc=utf-8 # $File: game.py -# $Date: Tue Nov 28 14:4726 2017 +0800 +# $Date: Fri Dec 01 01:3738 2017 +0800 # $Author: renyong15 © # from __future__ import print_function @@ -46,6 +46,17 @@ class Executor: return False, block return True, block + def _find_boarder(self, vertex): + block = [] + status = [False] * (self.game.size * self.game.size) + self._bfs(vertex, utils.EMPTY, block, status, False) + border = [] + for b in block: + for n in self._neighbor(b): + if not (n in block): + border.append(n) + return border + def _is_qi(self, color, vertex): nei = self._neighbor(vertex) for n in nei: @@ -134,14 +145,50 @@ class Executor: self.game.past.append(copy.copy(self.game.board)) return True + def _find_empty(self): + idx = [i for i,x in enumerate(self.game.board) if x == utils.EMPTY ][0] + return self.game._deflatten(idx) + + + + + def get_score(self): + ''' + return score from BLACK perspective. + ''' + _board = copy.copy(self.game.board) + while utils.EMPTY in self.game.board: + vertex = self._find_empty() + boarder = self._find_boarder(vertex) + boarder_color = set(map(lambda v: self.game.board[self.game._flatten(v)], boarder)) + if boarder_color == {utils.BLACK}: + self.game.board[self.game._flatten(vertex)] = utils.BLACK + elif boarder_color == {utils.WHITE}: + self.game.board[self.game._flatten(vertex)] = utils.WHITE + else: + self.game.board[self.game._flatten(vertex)] = utils.UNKNOWN + + score = 0 + for i in self.game.board: + if i == utils.BLACK: + score += 1 + elif i == utils.WHITE: + score -= 1 + score -= self.game.komi + + self.game.board = _board + return score + + class Game: def __init__(self, size=19, komi=6.5): self.size = size self.komi = 6.5 self.board = [utils.EMPTY] * (self.size * self.size) - self.strategy = strategy() - self.executor = Executor(game=self) + #self.strategy = strategy() + self.strategy = None + self.executor = Executor(game = self) self.history = [] self.past = deque(maxlen=8) for i in range(8): @@ -151,6 +198,12 @@ class Game: x, y = vertex return (y - 1) * self.size + (x - 1) + def _deflatten(self, idx): + x = idx % self.size + 1 + y = idx // self.size + 1 + return (x,y) + + def clear(self): self.board = [utils.EMPTY] * (self.size * self.size) diff --git a/AlphaGo/strategy.py b/AlphaGo/strategy.py index 99a8e4d..55e73c6 100644 --- a/AlphaGo/strategy.py +++ b/AlphaGo/strategy.py @@ -1,3 +1,5 @@ +import os,sys +sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) import numpy as np import utils import time diff --git a/AlphaGo/test.py b/AlphaGo/test.py index 59c5a26..66bf131 100644 --- a/AlphaGo/test.py +++ b/AlphaGo/test.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim:fenc=utf-8 # $File: test.py -# $Date: Tue Nov 28 14:4717 2017 +0800 +# $Date: Fri Dec 01 01:3722 2017 +0800 # $Author: renyong15 © # @@ -15,23 +15,24 @@ res = e.run_cmd('1 protocol_version') print(e.known_commands) print(res) -res = e.run_cmd('2 name') -print(res) -res = e.run_cmd('3 known_command quit') -print(res) +#res = e.run_cmd('2 name') +#print(res) -res = e.run_cmd('4 unknown_command quitagain') -print(res) +#res = e.run_cmd('3 known_command quit') +#print(res) -res = e.run_cmd('5 list_commands') -print(res) +#res = e.run_cmd('4 unknown_command quitagain') +#print(res) -res = e.run_cmd('6 komi 6') -print(res) +#res = e.run_cmd('5 list_commands') +#print(res) -res = e.run_cmd('7 play BLACK C3') -print(res) +#res = e.run_cmd('6 komi 6') +#print(res) + +#res = e.run_cmd('7 play BLACK C3') +#print(res) # res = e.run_cmd('play BLACK C4') # res = e.run_cmd('play BLACK C5') @@ -40,9 +41,9 @@ print(res) # print(res) -res = e.run_cmd('8 genmove WHITE') -print(res) -g.show_board() +#res = e.run_cmd('8 genmove WHITE') +#print(res) +#g.show_board() # res = e.run_cmd('8 genmove BLACK') # print(res) @@ -105,33 +106,33 @@ g.show_board() # print(res) # #g.show_board() # -# res = e.run_cmd('play BLACK P16') -# res = e.run_cmd('play BLACK P17') -# res = e.run_cmd('play BLACK P18') -# res = e.run_cmd('play BLACK P19') -# res = e.run_cmd('play BLACK Q16') -# res = e.run_cmd('play BLACK R16') -# res = e.run_cmd('play BLACK S16') -# -# res = e.run_cmd('play WHITE S18') -# res = e.run_cmd('play WHITE S17') -# res = e.run_cmd('play WHITE Q19') -# res = e.run_cmd('play WHITE Q18') -# res = e.run_cmd('play WHITE Q17') -# res = e.run_cmd('play WHITE R18') -# res = e.run_cmd('play WHITE R17') -# res = e.run_cmd('play BLACK S19') +res = e.run_cmd('play BLACK P16') +res = e.run_cmd('play BLACK P17') +res = e.run_cmd('play BLACK P18') +res = e.run_cmd('play BLACK P19') +res = e.run_cmd('play BLACK Q16') +res = e.run_cmd('play BLACK R16') +res = e.run_cmd('play BLACK S16') + +res = e.run_cmd('play WHITE S18') +res = e.run_cmd('play WHITE S17') +res = e.run_cmd('play WHITE Q19') +res = e.run_cmd('play WHITE Q18') +res = e.run_cmd('play WHITE Q17') +res = e.run_cmd('play WHITE R18') +res = e.run_cmd('play WHITE R17') +res = e.run_cmd('play BLACK S19') # print(res) # #g.show_board() # -# res = e.run_cmd('play WHITE R19') +res = e.run_cmd('play WHITE R19') # g.show_board() # -# res = e.run_cmd('play BLACK S19') +res = e.run_cmd('play BLACK S19') # print(res) # g.show_board() # -# res = e.run_cmd('play BLACK S19') +res = e.run_cmd('play BLACK S19') # print(res) # # @@ -153,7 +154,10 @@ g.show_board() # # res = e.run_cmd('play BLACK G19') # res = e.run_cmd('play BLACK G17') -# g.show_board() - +g.show_board() +res = e.run_cmd('play WHITE S18') +g.show_board() +res = g.executor.get_score() +print(res) From f6d691fa90cacb6efe057eaa2901c0d08a89cd26 Mon Sep 17 00:00:00 2001 From: mcgrady00h <281130306@qq.com> Date: Fri, 1 Dec 2017 01:38:11 +0800 Subject: [PATCH 3/3] add __init__.py --- __init__.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 __init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..5163bbe --- /dev/null +++ b/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# $File: __init__.py +# $Date: Thu Nov 30 23:1430 2017 +0800 +# $Author: renyong15 © +# +