Search algorithms for the 8-puzzle solution
AStarSolver.hpp
Go to the documentation of this file.
1 /**
2  * @author Douglas De Rizzo Meneghetti (douglasrizzom@gmail.com)
3  * @date 2017-6-24
4  * @brief 8-puzzle exploration based on the A* search algorithm
5  */
6 
7 #ifndef SEARCH_ASTARSOLVER_HPP
8 #define SEARCH_ASTARSOLVER_HPP
9 
10 #include "Solver.hpp"
11 #include "Heuristic.hpp"
12 #include "OrderedList.hpp"
13 
14 //! 8-puzzle exploration based on the A* search algorithm
15 class AStarSolver : public Solver {
16  private:
17 
19 
20  public:
21  //! \param h the heuristic to be used by A*
22  explicit AStarSolver(Heuristic *h) : heuristic(h) {
23  }
24 
26  delete heuristic;
27  }
28 
29  //! Compares two states using the available heuristic.
30  //! A* takes into consideration the sum of Manhattan distances between pieces in the
31  //! current state to their goal state and the number of steps taken to reach the
32  //! current state. The state that has the lowest sum of both of these values is
33  //! considered a more promising exploration candidate.
34  //! \param a
35  //! \param b
36  //! \return 1 if h(a) + f(a) < h(b) + f(b), -1 if h(a) + f(a) > h(b) + f(b), 0 if h(a) + f(a) = h(b) + f(b)
37  static int compare(GameState *a, GameState *b) {
38  Manhattan h;
39 
40  if (h.calc(*a) + a->getDepth() < h.calc(*b) + b->getDepth()) return 1;
41 
42  if (h.calc(*a) + a->getDepth() > h.calc(*b) + b->getDepth()) return - 1;
43 
44  return 0;
45  }
46 
48  //big improvement: insert states in an OrderedList based on their heuristic value
50 
51  const clock_t start = clock();
52  expanded.insert(&g0);
53 
54  while (! expanded.isEmpty()) {
55  // need only get the first state in the list, since they are ordered! SO FAST
57 
58  // has the goal state been found?
59  if (*game.getGoal() == *currentGame) {
60  solved = true;
61  return endSearch(currentGame, start);
62  }
63 
64  // otherwise, expand children and insert them in the ordered
65  // list in the position where they belong
67  while (! children.isEmpty()) {
69  }
70  }
71 
72  throw invalid_argument("This game is unsolvable!");
73  }
74 };
75 
76 #endif // SEARCH_BESTFIRSTSOLVER_HPP
Implementation of the Manhattan distance for the 8-puzzle.
Definition: Heuristic.hpp:23
virtual LinkedList< GameState * > solve(Game &g, GameState &gs)=0
Explores the game tree in search of the goal state.
AStarSolver(Heuristic *h)
Definition: AStarSolver.hpp:22
LinkedList< GameState * > solve(Game &game, GameState &g0)
Explores the game tree in search of the goal state.
Definition: AStarSolver.hpp:47
int calc(GameState &currentState)
Calculates the heuristic value for a state.
Definition: Heuristic.hpp:26
Heuristic * heuristic
Definition: AStarSolver.hpp:18
Describes a single state in the 8-puzzle.
Definition: GameState.hpp:18
Abtract class for heuristic functions.
Definition: Heuristic.hpp:13
int getDepth() const
Definition: GameState.hpp:339
static int compare(GameState *a, GameState *b)
Compares two states using the available heuristic.
Definition: AStarSolver.hpp:37