forked from tseip/fourinarow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathninarow_bfs.h
109 lines (94 loc) · 3.27 KB
/
ninarow_bfs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef NINAROW_BFS_INCLUDED
#define NINAROW_BFS_INCLUDED
#include <memory>
#include "ninarow_heuristic.h"
#include "searches.h"
namespace NInARow {
/**
* Represents a best-first-search algorithm with customized stopping conditions
* (with precisely the same behavior as Bas's original implementation).
*/
template <class Heuristic>
class NInARowBestFirstSearch
: public Search<Heuristic, BFSNode<typename Heuristic::BoardT>> {
public:
/**
* Constructor.
*
* @param heuristic The heuristic used to evaluated the board at each
* position.
* @param board The board position to search from.
*/
NInARowBestFirstSearch(std::shared_ptr<Heuristic> heuristic,
const typename Heuristic::BoardT& board)
: Search<Heuristic, BFSNode<typename Heuristic::BoardT>>(heuristic,
board),
best_move(),
num_repetitions(0),
iterations(0) {}
~NInARowBestFirstSearch() = default;
/**
* @note Due to limitations of SWIG's inheritance system, we have to redefine
* the public interface of any classes we expect to directly subclass in
* Python. They can simply be defined as pure pass-throughs.
*
* @{
*/
virtual bool advance_search() override {
return Search<Heuristic,
BFSNode<typename Heuristic::BoardT>>::advance_search();
}
virtual std::shared_ptr<Node<typename Heuristic::BoardT>> get_tree()
override {
return Search<Heuristic, BFSNode<typename Heuristic::BoardT>>::get_tree();
}
/**
* @}
*/
/**
* @return The number of iterations the current search has performed.
*/
std::size_t get_iterations() const { return iterations; }
/**
* @return The number of times the current search has returned the same
* move as the best consecutively.
*/
std::size_t get_num_repetitions() const { return num_repetitions; }
protected:
bool stopping_conditions(
std::shared_ptr<Heuristic> heuristic,
const typename Heuristic::BoardT& board) const override {
return iterations >= (std::size_t(1.0 / heuristic->get_gamma()) + 1) ||
num_repetitions >= heuristic->get_stopping_thresh() ||
Search<Heuristic, BFSNode<typename Heuristic::BoardT>>::
stopping_conditions(heuristic, board);
}
void on_node_expansion(
std::shared_ptr<BFSNode<typename Heuristic::BoardT>> /*expanded_node*/,
std::shared_ptr<Heuristic> heuristic,
const typename Heuristic::BoardT& /*board*/) override {
typename Heuristic::BoardT::MoveT old_best_move = best_move;
best_move = this->root->get_best_move();
old_best_move.board_position == best_move.board_position
? ++num_repetitions
: num_repetitions = 0;
++iterations;
return;
}
protected:
/**
* The current best known move.
*/
typename Heuristic::BoardT::MoveT best_move;
/**
* The number of times the current search has returned the same
* move as the best consecutively.
*/
std::size_t num_repetitions;
/**
* The number of iterations the current search has performed.
*/
std::size_t iterations;
};
} // namespace NInARow
#endif // NINAROW_BFS_INCLUDED