def makeMove(self): self.states.append(self.playerIdx) legal = self.legal_plays(self.board) #all legal moves # states = [( (self.states, legal[0][i], legal[1][i]) ) for i in xrange(len(legal[0]))] states = [((legal[0][i], legal[1][i]), tuple(self.states)) for i in xrange(len(legal[0]))] print "self.states", self.states # print states begin, games = datetime.datetime.utcnow(), 0 '''Start MonteCarlo method''' while datetime.datetime.utcnow() - begin < self.max_time: self.random_game() games += 1 print "#random_game", games, datetime.datetime.utcnow() - begin print self.wins[self.playerIdx].get(tuple(self.states)) move = max( (self.wins[self.playerIdx].get(S,0) / self.plays[self.playerIdx].get(S,1), p) for p, S in states)[1] # move = max( # (self.wins[self.playerIdx].get(S,0) / self.plays[self.playerIdx].get(S,1), (x,y)) # for (S,x,y) in states)[1] # move = self.make_random_move() self.updatePoints(self.playerIdx, self.myMoves, move[0], move[1]) genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) print 'Current score: {0}'.format(self.get_score()) return move
def probe_score(self, moveNum, player, x, y): self.points[player][moveNum][0] = x self.points[player][moveNum][1] = y genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) scores = self.get_score() # undo self.points[player][moveNum][0] = -1 self.points[player][moveNum][1] = -1 genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) return scores
def probe_score(self, x, y): self.points[self.myIdx][self.myCnt][0] = x self.points[self.myIdx][self.myCnt][1] = y genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) scores = self.get_score() #undo self.points[self.myIdx][self.myCnt][0] = -1 self.points[self.myIdx][self.myCnt][1] = -1 genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) return scores[0]
def makeMove(self): """ move = self.make_random_move() """ simulation = MonteCarlo(self, myMoves=self.myMoves, oppMoves=self.oppMoves, numMoves=self.numMoves) print "{0} using greedy".format(self.name) move = simulation.greedy_play() self.updatePoints(self.playerIdx, self.myMoves, move[0], move[1]) genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) print "Current score: {0}".format(self.get_score()) return move
def thinkNextMove(self, board, deep, deepSet, points, Moves, OppMoves): if Moves==0 and OppMoves ==0: return (500,500) if deep > deepSet: genVoronoi.generate_voronoi_diagram(2, Moves, points, self.colors, None, 0, 0) scores = genVoronoi.get_scores(2) myScore = scores[self.playerIdx] oppScore = scores[self.oppIdx] return myScore legalMoves = self.legal_plays(board) if deep%2 == 1: randSampling = 10 else: randSampling = 10 randIdx = np.random.choice(len(legalMoves[0]), randSampling) tmp = [] for idx in randIdx: tmp.append((legalMoves[0][idx], legalMoves[1][idx])) legalMoves = tmp score = -sys.maxint - 1 bestMove = () for eachMove in legalMoves: # print deep, eachMove myBoard = np.copy(board) myPoints = np.copy(points) myMoves = copy.copy(Moves) myOppMoves = copy.copy(OppMoves) if deep%2 == 1: # it's me myPoints[self.playerIdx][myMoves][0] = eachMove[0] myPoints[self.playerIdx][myMoves][1] = eachMove[1] tmp_val = self.thinkNextMove(board, deep+1, deepSet, myPoints, myMoves+1, myOppMoves) board[eachMove[0]][eachMove[1]] = self.playerIdx else: myPoints[self.oppIdx][myOppMoves][0] = eachMove[0] myPoints[self.oppIdx][myOppMoves][1] = eachMove[1] tmp_val = self.thinkNextMove(board, deep+1, deepSet, myPoints, myMoves, myOppMoves+1) board[eachMove[0]][eachMove[1]] = self.oppIdx # tmp_val = self.thinkNextMove(board, deep, deepSet, myPoints, myMoves, myOppMoves) if not isinstance(tmp_val, tuple) and tmp_val > score: score = tmp_val bestMove = eachMove print "result in thinkNextMove" print score, bestMove, self.myMoves, deep if deep == 2: return score else: return bestMove
def winner(self, myMoves, myOppMoves, myPoints): # print "winner", myMoves, myOppMoves if myMoves == myOppMoves and myOppMoves == self.numMoves-1: genVoronoi.generate_voronoi_diagram(2, myMoves, myPoints, self.colors, None, 0, 0) scores = genVoronoi.get_scores(2) myScore = scores[self.playerIdx] oppScore = scores[self.oppIdx] print "myScore", myScore if myScore >= oppScore: return self.playerIdx else: return self.oppIdx else: return 0
def makeMove(self): # move = self.make_random_move() print "before thinkNextMove", self.myMoves move = self.thinkNextMove(self.board, 1, 2, self.points, self.myMoves, self.oppMoves) # if self.myMoves < self.numMoves-1: # move = self.thinkNextMove(self.board, 1, 2, self.points, self.myMoves, self.oppMoves) # elif self.myMoves < self.numMoves-1 and self.playerIdx == 0: # move = self.thinkNextMove(self.board, 1, 2, self.points, self.myMoves, self.oppMoves) # # move = self.findBestLastOne(self.board, 1, 2, self.points, self.myMoves, self.oppMoves) # else: # move = self.findBestLastOne(self.board, 1, 2, self.points, self.myMoves, self.oppMoves) print "makeMove", move self.updatePoints(self.playerIdx, self.myMoves, move[0], move[1]) genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) print 'Current score: {0}'.format(self.get_score()) return move
def makeMove(self): ''' move = self.make_random_move() ''' simulation = MonteCarlo(self, myMoves = self.myMoves, oppMoves = self.oppMoves, numMoves = self.numMoves) if self.playerIdx == 0: print 'Using Mixed: {0}'.format(self.name) move = simulation.mixed_play() else: print 'Using Minmax + MCTS: {0}'.format(self.name) move = simulation.minmax_and_mcts() self.updatePoints(self.playerIdx, self.myMoves, move[0], move[1]) genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0) print 'Current score: {0}'.format(self.get_score()) return move
def findBestLastOne(self, board, deep, deepSet, points, Moves, OppMoves): legalMoves = self.legal_plays(board) randIdx = np.random.choice(len(legalMoves[0]), 100) tmp = [] for idx in randIdx: tmp.append((legalMoves[0][idx], legalMoves[1][idx])) legalMoves = tmp score = -sys.maxint - 1 bestMove = () for eachMove in legalMoves: points[self.playerIdx][Moves+1][0] = eachMove[0] points[self.playerIdx][Moves+1][1] = eachMove[1] genVoronoi.generate_voronoi_diagram(2, Moves+1, points, self.colors, None, 0, 0) scores = genVoronoi.get_scores(2) myScore = scores[self.playerIdx] print "myScore", myScore, eachMove if score < myScore: score = myScore bestMove = eachMove points[self.playerIdx][Moves+1][0] = -1 points[self.playerIdx][Moves+1][1] = -1 return bestMove
if __name__ == '__main__': num_players, num_moves = tuple(int(n) for n in raw_input().split()) points = np.zeros([num_players, num_moves, 2], dtype=np.int) points.fill(-1) # Can put any garbage here. Not using this in the end. image = np.zeros([10, 10, 3], dtype=np.uint8) colors = np.zeros([num_players, 3], dtype=np.uint8) for p in xrange(num_players): player, actual_move_count = tuple(int(n) for n in raw_input().split()) for m in xrange(actual_move_count): x, y = tuple(int(n) for n in raw_input().split()) points[p, m, 0] = x points[p, m, 1] = y genVoronoi.init_cache() # Important to put GUIOn as false genVoronoi.generate_voronoi_diagram(num_players, num_moves, points, colors, image, 0, 1) scores, colors_index = genVoronoi.get_color_indices_and_scores(num_players, num_moves, points) for i in range(num_players): print scores[i] for i in range(1000): for j in range(1000): n = colors_index[i,j,0] print n, for k in range(1, n+1): print colors_index[i,j,k], print
def updateUI(self, move): if self.move == num_moves: return x_pos, y_pos = move self.points[self.current_player, self.move, 0] = x_pos self.points[self.current_player, self.move, 1] = y_pos t1 = time.time() genVoronoi.generate_voronoi_diagram( num_players, num_moves, self.points, self.colors, self.imagenp, 1, scale ) print "Time to set pixels", time.time() - t1 t2 = time.time() pixmap = QtGui.QPixmap.fromImage(self.image) self.lbl.setPixmap(pixmap) print self.points painter = QtGui.QPainter() painter.begin(self.lbl.pixmap()) for p in xrange(num_players): for m in xrange(num_moves): if self.points[p, m, 0] == -1: break painter.setBrush( QtGui.QColor( self.dark_colors[p][0], self.dark_colors[p][1], self.dark_colors[p][2] ) ) painter.drawEllipse( (self.points[p, m, 0] - 4*scale)/scale, (self.points[p, m, 1] - 4*scale)/scale, 4*scale, 4*scale, ) del painter print "Time to draw ellipses", time.time() - t2 scores = self.get_scores() for p in range(num_players): font = self.textlbl[p].font(); if (self.current_player+1)%num_players == p: font.setPointSize(24); font.setBold(True); else: font.setPointSize(18); self.textlbl[p].setFont(font); palette = self.textlbl[p].palette() role = self.textlbl[p].backgroundRole() color = QtGui.QColor( self.colors[p][0], self.colors[p][1], self.colors[p][2] ) palette.setColor(role, color) palette.setColor(self.textlbl[p].foregroundRole(), QtCore.Qt.white) self.textlbl[p].setPalette(palette) self.textlbl[p].setAlignment(QtCore.Qt.AlignCenter) self.textlbl[p].setText("Player "+str(p+1)+": "+str(scores[p])) self.textlbl[p].adjustSize() if self.current_player == num_players-1: self.move += 1 self.current_player = (self.current_player + 1) % num_players if self.move == num_moves: print self.get_scores()
def undoPoints(self, player, move, x, y): self.points[player][move][0] = -1 self.points[player][move][1] = -1 self.board[x][y] = -1 genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0)
def updateUI(self, player, move, move_num): if move_num == num_moves: return x_pos, y_pos = move self.points[player, move_num, 0] = x_pos self.points[player, move_num, 1] = y_pos t1 = time.time() genVoronoi.generate_voronoi_diagram( num_players, num_moves, self.points, self.colors, self.imagenp, self.GUIOn, scale ) print "Time to set pixels", time.time() - t1 if self.GUIOn: t2 = time.time() pixmap = QtGui.QPixmap.fromImage(self.image) self.lbl.setPixmap(pixmap) painter = QtGui.QPainter() painter.begin(self.lbl.pixmap()) for p in xrange(num_players): for m in xrange(num_moves): if self.points[p, m, 0] == -1: break painter.setBrush( QtGui.QColor( self.dark_colors[p][0], self.dark_colors[p][1], self.dark_colors[p][2] ) ) painter.drawEllipse( (self.points[p, m, 0] - 2*scale)/scale, (self.points[p, m, 1] - 2*scale)/scale, 4, 4, ) del painter print "Time to draw ellipses", time.time() - t2 scores = self.endMatch() for p in range(num_players): font = self.textlbl[p].font(); if (player+1)%num_players == p: font.setPointSize(32); font.setBold(True); else: font.setPointSize(18); self.textlbl[p].setFont(font); palette = self.textlbl[p].palette() role = self.textlbl[p].backgroundRole() color = QtGui.QColor( self.colors[p][0], self.colors[p][1], self.colors[p][2] ) palette.setColor(role, color) palette.setColor(self.textlbl[p].foregroundRole(), QtCore.Qt.white) self.textlbl[p].setPalette(palette) self.textlbl[p].setAlignment(QtCore.Qt.AlignCenter) self.textlbl[p].setText(self.player_names[p]+": "+str(scores[p])) self.textlbl[p].adjustSize() if self.delay - time.time() + t1 > 0: time.sleep(self.delay - time.time() + t1)
def updatePoints(self, player, move, x, y): self.points[player][move][0] = x self.points[player][move][1] = y genVoronoi.generate_voronoi_diagram(2, self.numMoves, self.points, self.colors, None, 0, 0)