예제 #1
0
    def explore(self, timeout: float):
        start = time.time()
        nextList = [self.head]
        i = 0

        for successors in nx.bfs_successors(self, self.head):
            if not successors.explored:
                nextList.append(successors)

        while len(nextList) > 0 and (time.time() - start < timeout):
            currentNode = nextList.pop(0)
            if currentNode.explored:
                pass
            for source in currentNode.endpoints:
                for target in currentNode.fullGraph.neighbors(source):
                    # create a new node by exploring it
                    newGraph = self.evolve_from(currentNode, source, target)
                    i += 1
                    if newGraph:
                        nextList.append(newGraph)
            currentNode.explored = True

        if len(nextList) != 0:
            print_err("timeout, made " + str(i))
        else:
            print_err("to the end of " + str(i))
예제 #2
0
 def claimRiver(self, punter: int, source: int, target: int):
     timeStart = time.time()
     self.discoveryGraph.head.fullGraph.claim(
         source, target)  # remove the claimed river from the main graph
     if punter == self.punter:  # if punter is player, evolve the scoringgraph
         self.discoveryGraph.claim(source, target)
         self.discoveryGraph.head.displayMove(source, target)
     print_err("claiming time = " + str(time.time() - timeStart))
예제 #3
0
 def eventIncoming(self, event: dict):
     # starts the timeout
     self.client.timeStart = time.time()
     for key, value in event.items():
         if key == u'map':
             self.setup_map(value, self.display)
         if key == u'move':
             # when received a move, apply it
             self.applyMove(value["moves"])
             # ask the next move to the model
             move = self.getNextMove()
             # check if move was found
             if move:
                 self.client.write(move)
             else:
                 print_err("did not find any move, passing")
                 self.client.write({"pass": {"punter": self.client.punter}})
             print_err("playing at :" + str(self.client.getTimeout()))
         if key == u'stop':
             # when received a stop, register the scores
             for punterScore in value["scores"]:
                 self.scores[punterScore["punter"]] = punterScore["score"]
                 print_err(str(self.scores))
             print_err("my Score : " + str(self.scores[self.client.punter]))
             return True
     return False
예제 #4
0
    def getBestMove(self):
        (bestScores, bestPathes) = nx.single_source_dijkstra(self,
                                                             self.head,
                                                             cutoff=10)
        maxLen = 0
        for bestPath in bestPathes.values():
            if maxLen < len(bestPath): maxLen = len(bestPath)
        print_err("max path len = " + str(maxLen))

        bestscoreitem = sorted([(key, value)
                                for (key, value) in bestScores.items()],
                               key=lambda x: x[1] / len(bestPathes[x[0]]))
        #bestscoreitem = sorted([(key, value) for (key, value) in bestscoreitem], key=lambda x:x[1])
        bestTarget = bestscoreitem[0]
        bestMove = None
        bestScore = self.head.score
        if len(bestPathes[bestTarget[0]]) > 1:
            bestfirstTarget = bestPathes[bestTarget[0]][1]
            bestScore = bestfirstTarget.score
            bestMove = self[self.head][bestfirstTarget]['move']
        return (bestMove, bestScore)
예제 #5
0
 def getNextMove(self):
     self.discoveryGraph.explore(
         self.client.timeout)  # explore discoveryGraph
     timeStart = time.time()
     (bestMove, bestScore
      ) = self.discoveryGraph.getBestMove()  # get the best move found
     print_err("getBestMoveTime = " + str(time.time() - timeStart))
     self.leftMoves -= 1  # update movesleft as we are returning the next move
     self.discoveryGraph.head.fullGraph.displayScore(
         self.client.title, bestScore,
         self.leftMoves)  # display score up to date
     if bestMove:
         bestMove = bestMove.copy()
         bestMove["claim"] = {
             "punter": self.client.punter,
             "source": bestMove["claim"][0],
             "target": bestMove["claim"][1]
         }  # set the move
     else:
         bestMove = {"pass": {"punter": self.client.punter}}
     printD(bestMove)
     return bestMove  # return the move