Files
@ 7ef3360afbe5
Branch filter:
Location: OneEye/src/tests/testEngine.py - annotation
7ef3360afbe5
2.7 KiB
text/x-python
StateBag.pushState: linking the states together
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 | f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 077600f0c5f8 077600f0c5f8 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 077600f0c5f8 077600f0c5f8 077600f0c5f8 077600f0c5f8 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 077600f0c5f8 077600f0c5f8 077600f0c5f8 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 077600f0c5f8 077600f0c5f8 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 077600f0c5f8 077600f0c5f8 077600f0c5f8 077600f0c5f8 077600f0c5f8 0cb3fbe06b5d 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 0cb3fbe06b5d 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 0cb3fbe06b5d 0cb3fbe06b5d 0cb3fbe06b5d 0cb3fbe06b5d f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 7ef3360afbe5 f9ab2070bd69 f9ab2070bd69 7ef3360afbe5 7ef3360afbe5 7ef3360afbe5 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 f9ab2070bd69 | import re
import os.path
import logging as log
from unittest import TestCase
import config as cfg
from util import BLACK as B,WHITE as W,EMPTY as _
from go.core import Go
from go.engine import SpecGo,Engine
from statebag import BoardState
_log=log.getLogger(__name__)
_log.setLevel(log.INFO)
_log.propagate=False
formatter=log.Formatter("%(asctime)s %(levelname)s: %(message)s",datefmt="%Y-%m-%d %H:%M:%S")
handler=log.FileHandler("/tmp/oneeye.log",mode="w")
handler.setFormatter(formatter)
_log.addHandler(handler)
def simpleLoadSgf(filename):
with open(filename) as f:
contents=f.read()
g=lambda m: tuple(ord(c)-ord('a') for c in reversed(m.group(1)))
return [g(m) for m in re.finditer(r"\b[BW]\[([a-z]{2})\]",contents)]
def listStates(moves):
g=Go()
res=[BoardState(g.board)]
for m in moves:
g.doMove(g.toMove,*m)
res.append(BoardState(g.board))
return res
class TestTransitions(TestCase):
def testBasic(self):
s1=BoardState([
[_,_,_],
[_,_,_],
[_,_,_]
])
s2=BoardState([
[_,_,_],
[_,B,_],
[_,_,_]
])
g=SpecGo(3)
eng=Engine(g)
eng.load(s1,s2-s1)
self.assertEqual(eng.dfs(s2,1),[(1,1,1)])
def testCapture(self):
s1=BoardState([
[_,W,_],
[W,B,_],
[_,W,_]
])
s2=BoardState([
[_,W,_],
[W,_,W],
[_,W,_]
])
g=SpecGo(3)
g.toMove=W
eng=Engine(g)
eng.load(s1,s2-s1)
self.assertEqual(eng.dfs(s2,1),[(W,1,2)])
def testMulti(self):
s1=BoardState([
[_,_,_],
[_,_,_],
[_,_,_]
])
s2=BoardState([
[_,_,_],
[_,B,W],
[_,_,_]
])
g=SpecGo(3)
eng=Engine(g)
eng.load(s1,s2-s1)
self.assertEqual(eng.dfs(s2,2),[(W,1,2),(B,1,1)])
def testSnapback(self):
s1=BoardState([
[B,B,B],
[B,_,B],
[B,W,B]
])
s2=BoardState([
[_,_,_],
[_,_,_],
[_,W,_]
])
g=SpecGo(3)
eng=Engine(g)
eng.load(s1,s2-s1)
self.assertEqual(eng.dfs(s2,2),[(W,2,1),(B,1,1)])
s1=BoardState([
[_,_,_],
[W,B,B],
[_,W,W]
])
s2=BoardState([
[_,_,_],
[W,B,B],
[_,W,_]
])
eng.load(s1,s2-s1)
self.assertEqual(eng.dfs(s2,2),[(W,2,1),(B,2,0)])
def testReal(self):
files=["O-Takao-20110106.sgf","Sakai-Iyama-20110110.sgf"]
g=SpecGo()
eng=Engine(g)
for f in files:
moves=simpleLoadSgf(os.path.join(cfg.srcDir,"tests/data",f))
states=listStates(moves)
for k in range(1,4):
toMove=B
for (i,(s1,s2)) in enumerate(zip(states,states[k:])):
diff=s2-s1
eng.load(s1,diff)
colorIn=W if i&1 else B
colorOut=colorIn if k&1 else 1-colorIn
seq=eng.iterativelyDeepen(s2,colorIn,colorOut)
msg="\n"+s1.exportDiff(s2)
self.assertIsNotNone(seq,msg)
self.assertLessEqual(len(seq),k,msg)
if len(seq)!=k: _log.warning("shorter than expected transition sequence:" + msg + "\n" + str(seq))
toMove*=-1
|