Ejemplo n.º 1
0
 def tilesDisplacedHeuristic(self, state):
     transform = Transform()
     currentPuzzleState = transform.convertStringToEightPuzzle(state)
     goalPuzzleState = transform.convertStringToEightPuzzle(
         self.goalState.puzzleState)
     h = 0
     for i in range(3):
         for j in range(3):
             if currentPuzzleState[i][j] != goalPuzzleState[i][j]:
                 h += 1
             if currentPuzzleState[i][j] == 0 and currentPuzzleState[i][
                     j] != goalPuzzleState[i][j] and isTileInclude == False:
                 h -= 1
     return h
Ejemplo n.º 2
0
    def manhattanHeuristic(self, state):
        transform = Transform()
        currentPuzzleState = transform.convertStringToEightPuzzle(state)
        goalPuzzleState = transform.convertStringToEightPuzzle(
            self.goalState.puzzleState)
        currentCoOrdinate = numpy.arange(18).reshape((9, 2))

        for i in range(3):
            for j in range(3):
                currentCoOrdinate[currentPuzzleState[i][j]][0] = i
                currentCoOrdinate[currentPuzzleState[i][j]][1] = j

        h = 0
        for i in range(3):
            for j in range(3):
                if goalPuzzleState[i][j] != 0:
                    h += abs(i - currentCoOrdinate[goalPuzzleState[i][j]][0]) + \
                         abs(j - currentCoOrdinate[goalPuzzleState[i][j]][1])
                if goalPuzzleState[i][j] == 0 and isTileInclude:
                    h += abs(i - currentCoOrdinate[goalPuzzleState[i][j]][0]) + \
                         abs(j - currentCoOrdinate[goalPuzzleState[i][j]][1])
        return h