-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathids.cc
More file actions
69 lines (59 loc) · 1.93 KB
/
ids.cc
File metadata and controls
69 lines (59 loc) · 1.93 KB
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
#include "ids.h"
#include <limits>
using namespace std;
IDS::IDS() {}
/*
* Depth-Limited Search
*
* Changes to the "common" algorithm:
*
* - Using a boolean return instead of a deque, as we don't need to retrieve the
* solution path, which has size == depth reached. However, even without
* push_fronts, there was no substantial performance gain (push_front is very
* well-optimized).
*
* - DLS takes as input a Node, and not a State -- this is because we can more
* easily check its parent node's state (we don't want to generate successor
* states that are equal to the current node's parent state).
*/
bool IDS::DLS(Node &node, int depth_limit) {
if (node.state.isGoal()) {
return true;
}
if (depth_limit > 0) {
this->expanded_node_count++;
vector<State> successors;
if (node.parent_state != nullptr)
successors = node.state.succ(node.parent_state);
else
successors = node.state.succ();
for (State &s : successors) {
Node n_ = Node(&node.state, s.action, s, node.cost);
bool solution = this->DLS(n_, depth_limit - 1);
if (solution) {
return true;
}
}
}
return false;
}
/*
* Iterative Deepening Search
*
* The idea is to perform a sequence of depth-limited searches with increasing
* depth limit. Each iteration "repeats the work" of all previous iterations, so
* it may be inneficient.
*/
optional<Search::Solution> IDS::run(State initial_state) {
this->start_clock = chrono::steady_clock::now();
this->start.makeRootNode(initial_state);
for (int depth_limit = 0; depth_limit < numeric_limits<int>::max();
depth_limit++) {
bool solution = this->DLS(this->start, depth_limit);
if (solution) {
this->end_clock = chrono::steady_clock::now();
return this->makeSolution(depth_limit);
}
}
return nullopt;
}