Search algorithms for the 8-puzzle solution
include
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
:
17
mt19937_64
r
;
18
19
int
rnd
(
int
max) {
20
return
abs((
int
) r() % max);
21
}
22
23
public
:
24
MonteCarloSolver
() {
25
time_t result = time(nullptr);
26
r.seed((
unsigned
long
) std::localtime(&result));
27
}
28
29
LinkedList
<
GameState
*>
solve
(
Game
&
game
,
GameState
&
g0
) {
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
) {
35
LinkedList
<
GameState
*>
children
=
visit
(
only_one
,
false
);
36
only_one
=
children
.
remove
(
rnd
(
children
.
getSize
()));
37
38
while
(!
children
.
isEmpty
()) {
39
GameState
*
child
=
children
.
remove
(0);
40
delete
child
;
41
}
42
}
43
44
solved
=
true
;
45
return
endSearch
(
only_one
,
start
);
46
}
47
};
48
49
#
endif
MonteCarloSolver::MonteCarloSolver
MonteCarloSolver()
Definition:
MonteCarloSolver.hpp:24
MonteCarloSolver::rnd
int rnd(int max)
Definition:
MonteCarloSolver.hpp:19
Solver::solve
virtual LinkedList< GameState * > solve(Game &g, GameState &gs)=0
Explores the game tree in search of the goal state.
MonteCarloSolver::r
mt19937_64 r
Definition:
MonteCarloSolver.hpp:17
MonteCarloSolver::solve
LinkedList< GameState * > solve(Game &game, GameState &g0)
Explores the game tree in search of the goal state.
Definition:
MonteCarloSolver.hpp:29
Generated by
1.8.13