From 4f05f76225e7337b4606b81a0792c6472dad3a5d Mon Sep 17 00:00:00 2001 From: Jonathan David Armfield Date: Mon, 26 Feb 2018 23:31:34 -0700 Subject: [PATCH] Update gtp_connection_go2.py added alpha beta --- assignment2/Go2/gtp_connection_go2.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/assignment2/Go2/gtp_connection_go2.py b/assignment2/Go2/gtp_connection_go2.py index e9cd326..a2692ed 100644 --- a/assignment2/Go2/gtp_connection_go2.py +++ b/assignment2/Go2/gtp_connection_go2.py @@ -56,7 +56,7 @@ def safety_cmd(self, args): except Exception as e: self.respond('Error: {}'.format(str(e))) - def negamax(self, pnode, color, curtime, delta): + def negamax(self, pnode, color, curtime, delta, α, β): if int(time.time() - curtime) > delta: #If the timelimit is passed, just return the heuristic value of the move return pnode.state.score(self.go_engine.komi) @@ -88,11 +88,13 @@ def negamax(self, pnode, color, curtime, delta): return pnode.state.score(self.go_engine.komi) if color == "b": - moved, score = self.negamax(nodecopy, "w", curtime, delta) + moved, score = self.negamax(nodecopy, "w", curtime, delta, -β, -α) else: - moved, score = self.negamax(nodecopy, "b", curtime, delta) - + moved, score = self.negamax(nodecopy, "b", curtime, delta, -β, -α) + α = max( α, v ) #alpha-beta + if α ≥ β #alpha-beta + break #alpha-beta #best = max(best, score) if(best < -score): best = -score @@ -113,10 +115,14 @@ def timelimit(self, args): def solve(self, args): # Create a copy of our current environment as the root of the tree. root = node(self.board) + α = float("-inf") #initialized to - infinity + β = float("inf") #initialized to +infinity negamaxResult = self.negamax(root, GoBoardUtil.int_to_color(self.board.current_player), time.time(), - self.timelimit + self.timelimit, + α, + β ) #print(negamaxResult) self.respond("{0} {1}".format(GoBoardUtil.int_to_color(negamaxResult[0]), ""))