Search algorithms for the 8-puzzle solution
Public Member Functions | Protected Member Functions | Protected Attributes | Static Private Member Functions
Solver Class Referenceabstract

Base class for all 8-puzzle solvers. More...

#include <Solver.hpp>

Inheritance diagram for Solver:
Collaboration diagram for Solver:

Public Member Functions

double getSecondsToSolve () const
 
int getVisitedNodes () const
 
int getMaxDepth () const
 
int getSolutionDepth () const
 
bool isSolved ()
 
 Solver ()
 
virtual LinkedList< GameState * > solve (Game &g, GameState &gs)=0
 Explores the game tree in search of the goal state. More...
 
string to_string ()
 Generates a string containing useful information from the solver run. More...
 

Protected Member Functions

bool isVisited (GameState &g)
 Checks whether a node has already been visited by the solver. More...
 
LinkedList< GameState * > visit (GameState *current)
 
LinkedList< GameState * > visit (GameState *current, bool keepVisited)
 Visit a game state, adding it to the list of visited states and returning its valid child states. More...
 
LinkedList< GameState * > endSearch (GameState *currentGame, const clock_t start)
 End the search, generating the steps from the initial state to the goal. More...
 

Protected Attributes

OrderedList< GameState * > visited = OrderedList<GameState *>(compare)
 
int visitedNodes
 
int maxDepth
 
int solutionDepth
 
float secondsToSolve
 
bool solved
 

Static Private Member Functions

static int compare (GameState *a, GameState *b)
 

Detailed Description

Base class for all 8-puzzle solvers.

Author
Douglas De Rizzo Meneghetti (dougl.nosp@m.asri.nosp@m.zzom@.nosp@m.gmai.nosp@m.l.com)
Date
2017-6-22Base class for all 8-puzzle solvers.

Definition at line 17 of file Solver.hpp.

Constructor & Destructor Documentation

◆ Solver()

Solver::Solver ( )
inline

Definition at line 133 of file Solver.hpp.

133  {
134  visited = OrderedList<GameState *>(compare);
136  solved = false;
137  }
OrderedList< GameState * > visited
Definition: Solver.hpp:27
static int compare(GameState *a, GameState *b)
Definition: Solver.hpp:20
int maxDepth
Definition: Solver.hpp:30
int visitedNodes
Definition: Solver.hpp:30
int solutionDepth
Definition: Solver.hpp:30
bool solved
Definition: Solver.hpp:34
float secondsToSolve
Definition: Solver.hpp:31
Here is the call graph for this function:

Member Function Documentation

◆ compare()

static int Solver::compare ( GameState a,
GameState b 
)
inlinestaticprivate

Definition at line 20 of file Solver.hpp.

20  {
21  return *a < *b;
22  }
Here is the call graph for this function:

◆ endSearch()

LinkedList<GameState *> Solver::endSearch ( GameState currentGame,
const clock_t  start 
)
inlineprotected

End the search, generating the steps from the initial state to the goal.

Parameters
currentGamethe state containing the goal
startthe time the solve began, in order to calculate its time
Returns
list containing the steps from the initial state to the goal

Definition at line 91 of file Solver.hpp.

91  {
92  secondsToSolve = float(clock() - start) / CLOCKS_PER_SEC;
93 
94  LinkedList<GameState *> resultSteps;
95 
96  GameState *tmp = currentGame;
97  solutionDepth = currentGame->getDepth();
98 
99  // memoryless process don't work with a list of visited nodes, they
100  // increment this variable on the fly
101  visitedNodes = visitedNodes != 0 ? visitedNodes : visited.getSize();
102 
103  while (tmp != NULL) {
104  resultSteps.insert(tmp, resultSteps.getSize());
105  tmp = tmp->getParent();
106  }
107 
108  return resultSteps;
109  }
OrderedList< GameState * > visited
Definition: Solver.hpp:27
int visitedNodes
Definition: Solver.hpp:30
int solutionDepth
Definition: Solver.hpp:30
GameState * getParent() const
Definition: GameState.hpp:335
Describes a single state in the 8-puzzle.
Definition: GameState.hpp:18
float secondsToSolve
Definition: Solver.hpp:31
int getDepth() const
Definition: GameState.hpp:339
Here is the call graph for this function:

◆ getMaxDepth()

int Solver::getMaxDepth ( ) const
inline

Definition at line 121 of file Solver.hpp.

121  {
122  return maxDepth;
123  }
int maxDepth
Definition: Solver.hpp:30
Here is the call graph for this function:

◆ getSecondsToSolve()

double Solver::getSecondsToSolve ( ) const
inline

Definition at line 113 of file Solver.hpp.

113  {
114  return secondsToSolve;
115  }
float secondsToSolve
Definition: Solver.hpp:31
Here is the call graph for this function:

◆ getSolutionDepth()

int Solver::getSolutionDepth ( ) const
inline

Definition at line 125 of file Solver.hpp.

125  {
126  return solutionDepth;
127  }
int solutionDepth
Definition: Solver.hpp:30
Here is the call graph for this function:

◆ getVisitedNodes()

int Solver::getVisitedNodes ( ) const
inline

Definition at line 117 of file Solver.hpp.

117  {
118  return visitedNodes;
119  }
int visitedNodes
Definition: Solver.hpp:30
Here is the call graph for this function:

◆ isSolved()

bool Solver::isSolved ( )
inline

Definition at line 129 of file Solver.hpp.

129  {
130  return solved;
131  }
bool solved
Definition: Solver.hpp:34
Here is the call graph for this function:

◆ isVisited()

bool Solver::isVisited ( GameState g)
inlineprotected

Checks whether a node has already been visited by the solver.

Parameters
gThe state to look for
Returns
true if g has been visited, otherwise false

Definition at line 39 of file Solver.hpp.

39  {
40  return visited.contains(&g);
41  }
OrderedList< GameState * > visited
Definition: Solver.hpp:27

◆ solve()

virtual LinkedList<GameState *> Solver::solve ( Game g,
GameState gs 
)
pure virtual

Explores the game tree in search of the goal state.

Exploration is done by applying one of the four valid actions of the 8-puzzle to intermediate, non-goal states until the goal state is reached.

Parameters
gA description of the game.
gsThe initial state of the game.
Returns
list containing all states explored, from the initial state to the goal state.

Implemented in HillClimbingSolver, GreedySolver, AStarSolver, DepthFirstSolver, MonteCarloSolver, and BreadthFirstSolver.

Here is the caller graph for this function:

◆ to_string()

string Solver::to_string ( )
inline

Generates a string containing useful information from the solver run.

Returns
string representation of the state-space exploration

Definition at line 149 of file Solver.hpp.

149  {
150  return string(
151  "\t\t\t\tSeconds: ").append(std::to_string(getSecondsToSolve())).append(
152  "\n\t\t Solution depth: ").append(
153  std::to_string(getSolutionDepth())).append("\n\t Max depth explored: ").
154  append(
155  std::to_string(getMaxDepth())).append("\nNumber of visited nodes: ").append(
156  std::to_string(getVisitedNodes())).append("\n");
157  }
int getVisitedNodes() const
Definition: Solver.hpp:117
double getSecondsToSolve() const
Definition: Solver.hpp:113
int getSolutionDepth() const
Definition: Solver.hpp:125
int getMaxDepth() const
Definition: Solver.hpp:121

◆ visit() [1/2]

LinkedList<GameState *> Solver::visit ( GameState current)
inlineprotected

Definition at line 43 of file Solver.hpp.

43  {
44  return visit(current, true);
45  }
LinkedList< GameState * > visit(GameState *current)
Definition: Solver.hpp:43
Here is the call graph for this function:

◆ visit() [2/2]

LinkedList<GameState *> Solver::visit ( GameState current,
bool  keepVisited 
)
inlineprotected

Visit a game state, adding it to the list of visited states and returning its valid child states.

Parameters
currentthe state to be visited
Returns
a list of the valid child states. A valid child state is the result state of applying a valid action to the current state. Only states that have not been visited before by the solver are returned.

Definition at line 50 of file Solver.hpp.

50  {
51  LinkedList<GameState *> children;
52  GameAction actions[4]{RIGHT, DOWN, LEFT, UP};
53 
54  // add current state to the list of visited states
55  if (keepVisited)
56  visited.insert(current);
57 
58  // keep a record of the maximum depth the solver has explored
59  if (current->getDepth() > maxDepth) maxDepth = current->getDepth();
60 
61  // iterate through the 4 actions
62  for (int i = 0; i < 4; i ++) {
63  // check if the action is valid
64  if (current->isValid(actions[i])) {
65  // generate a child state from the valid action
66  // and add it to the return list, only if the
67  // child node has not been visited by the solver before
68  GameState *newState = new GameState(current, actions[i]);
69 
70  if (! isVisited(*newState)) children.insert(newState);
71  else delete newState;
72  }
73  }
74 
75  // throws an exception in case all actions give valid states,
76  // since at least one state was already visited
77  if (keepVisited && (children.getSize() == 4) && (current->getDepth() > 0)) {
78  throw invalid_argument("Duplicate states being visited.");
79  }
80 
81  return children;
82  }
GameAction
Actions allowed in the 8-puzzle and its variants.
Definition: GameAction.hpp:11
OrderedList< GameState * > visited
Definition: Solver.hpp:27
bool isVisited(GameState &g)
Checks whether a node has already been visited by the solver.
Definition: Solver.hpp:39
bool isValid(GameAction action)
Checks whether an action is valid for this state.
Definition: GameState.hpp:200
int maxDepth
Definition: Solver.hpp:30
Describes a single state in the 8-puzzle.
Definition: GameState.hpp:18
int getDepth() const
Definition: GameState.hpp:339
Here is the call graph for this function:

Field Documentation

◆ maxDepth

int Solver::maxDepth
protected

Definition at line 30 of file Solver.hpp.

◆ secondsToSolve

float Solver::secondsToSolve
protected

Definition at line 31 of file Solver.hpp.

◆ solutionDepth

int Solver::solutionDepth
protected

Definition at line 30 of file Solver.hpp.

◆ solved

bool Solver::solved
protected

Definition at line 34 of file Solver.hpp.

◆ visited

OrderedList<GameState *> Solver::visited = OrderedList<GameState *>(compare)
protected

Definition at line 27 of file Solver.hpp.

◆ visitedNodes

int Solver::visitedNodes
protected

Definition at line 30 of file Solver.hpp.


The documentation for this class was generated from the following file: