Example #1
0
    def run(self, path):
        h = []
        count = 0

        self.startNode.h = self.getHScoreFromString(str(self.startNode.matrix))
        self.startNode.g = 0
        self.startNode.f = self.startNode.h

        heapq.heappush(h, self.startNode)

        while h:
            current = heapq.heappop(h)
            count = count + 1

            if (count <= self.maxLength):

                path.write(
                    str(current.f) + " " + str(current.g) + " " +
                    str(current.h) + " ")
                path.write(self.matrixToString(current.matrix) + "\n")

                if str(current.matrix) == self.expectedResult:
                    return current

                for i in range(0, self.size):
                    for j in range(0, self.size):
                        newMatrix = self.newBoard(i, j, current.matrix)
                        depth = current.depth + 1

                        serialMatrix = str(newMatrix)
                        n = Node(parent=current,
                                 matrix=newMatrix,
                                 move=chr(i + 65) + str(j),
                                 depth=depth)
                        n.exploratoryDepth = 0

                        n.h = self.getMinHWithExploratorySearch(
                            n, self.maxExploratoryDepth)
                        n.g = self.getG(n)
                        n.f = self.getF(n)

                        if serialMatrix in self.seen:
                            if self.seen[serialMatrix].f > n.f:
                                self.seen[
                                    serialMatrix].parentNode = n.parentNode
                                self.seen[serialMatrix].f = n.f
                                self.seen[serialMatrix].g = n.g
                                self.seen[serialMatrix].h = n.h
                                self.seen[serialMatrix].depth = depth
                        else:
                            heapq.heappush(h, n)
                            self.seen[serialMatrix] = n