Search algorithms for the 8-puzzle solution
MonteCarloSolver.hpp
Go to the documentation of this file.
1 /**
2  * @author Douglas De Rizzo Meneghetti (douglasrizzom@gmail.com)
3  * @date 2017-7-7
4  * @brief Memoryless solver that randomly searches the state space.
5  */
6 
7 #ifndef SEARCH_MONTECARLOSOLVER_HPP
8 #define SEARCH_MONTECARLOSOLVER_HPP
9 
10 #include <OrderedList.hpp>
11 #include "Solver.hpp"
12 #include "Heuristic.hpp"
13 
14 //! Memoryless solver that randomly searches the state space.
15 class MonteCarloSolver : public Solver {
16  private:
18 
19  int rnd(int max) {
20  return abs((int) r() % max);
21  }
22 
23  public:
25  time_t result = time(nullptr);
26  r.seed((unsigned long) std::localtime(&result));
27  }
28 
30  // this algorithm has no memory, so no data structure to keep expanded states
31  GameState *only_one = &g0;
32  const clock_t start = clock();
33 
34  while (*game.getGoal() != *only_one) {
37 
38  while (! children.isEmpty()) {
40  delete child;
41  }
42  }
43 
44  solved = true;
45  return endSearch(only_one, start);
46  }
47 };
48 
49 #endif
virtual LinkedList< GameState * > solve(Game &g, GameState &gs)=0
Explores the game tree in search of the goal state.
LinkedList< GameState * > solve(Game &game, GameState &g0)
Explores the game tree in search of the goal state.