Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions assignment2/Go2/gtp_connection_go2.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def safety_cmd(self, args):
except Exception as e:
self.respond('Error: {}'.format(str(e)))

def negamax(self, node, color, curtime, delta):
if int(time.time() - curtime) > delta:
def negamax(self, node, color, curtime, delta, α, β):
if (int(time.time() - curtime) > delta) or
print("score -- ", node.state.score(self.go_engine.komi))
return node.state.score(self.go_engine.komi)[0]

Expand All @@ -82,24 +82,31 @@ def negamax(self, node, color, curtime, delta):
# print(coord)
nodecopy.state.move(point, GoBoardUtil.color_to_int(color))
if color == "b":
v = -self.negamax(nodecopy, "w", curtime, delta)
v = -self.negamax(nodecopy, "w", curtime, delta, -β, -α) #alpha and beta switch with each negamax call
#print(v)
best = max(best, v)
else:
v = -self.negamax(nodecopy, "b", curtime, delta)
v = -self.negamax(nodecopy, "b", curtime, delta, -β, -α) #alpha and beta switch with each negamax call
best = max(best, v)
α = max( α, v ) #alpha-beta
if α ≥ β #alpha-beta
break #alpha-beta
#print("returning")
return best


def timelimit(self, args):
self.timelimit = args
self.respond("")

def solve(self, args):
# Create a copy of our current environment as the root of the tree.
root = node(self.board)
self.respond(self.negamax(root, GoBoardUtil.int_to_color(self.board.current_player), time.time(), self.timelimit))
α = float("-inf") #initialized to - infinity
β = float("inf") #initialized to +infinity
self.respond(self.negamax(root, GoBoardUtil.int_to_color(self.board.current_player), time.time(), self.timelimit), α, β) #alpha and beta added