-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.js
More file actions
173 lines (157 loc) · 4.57 KB
/
solver.js
File metadata and controls
173 lines (157 loc) · 4.57 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
goog.provide('soko.Solver');
goog.require('soko.Heap');
goog.require('soko.Level');
goog.require('soko.State');
goog.require('soko.Queue');
goog.require('soko.heuristic.NullHeuristic');
goog.scope(function() {
/**
* Statistics pulled from the solver.
*
* @typedef {{
* nodesVisited: (number|string),
* finalQueueSize: (number|string),
* elapsedTime: (number|string),
* solutionLength: (number|string)
* }}
*/
soko.SolverStats;
/**
* A node with extra information used in the search tree. Keeps
* track of distance travelled so far (g), heuristic value (h),
* an optional location of the pusher (for condensed representation),
* the state, and a parent.
*
* @typedef {{
* f: (undefined|number),
* g: number,
* h: number,
* pusher: soko.types.GridPoint,
* id: soko.StateId,
* state: !soko.State,
* parent: soko.SolverNode
* }}
*/
soko.SolverNode;
/**
* @param {function (new:soko.heuristic.Heuristic, !soko.Level)=} opt_heuristic heuristicType
* @param {function (new:soko.HeapInterface)=} opt_heapType heap type
* @param {boolean=} opt_condensed whether to use condensed moves
* @constructor
*/
soko.Solver = function(opt_heuristic, opt_heapType, opt_condensed) {
/** @private {function (new:soko.heuristic.Heuristic, !soko.Level)} */
this.heuristicType_ = opt_heuristic || soko.heuristic.NullHeuristic;
/** @private {function (new:soko.HeapInterface)} */
this.heapType_ = opt_heapType || soko.Queue;
/** @private {boolean} */
this.condensed_ = opt_condensed || false;
/** @type {boolean} */
this.print = false;
/** @type {soko.SolverStats} */
this.solverStats = {'elapsedTime': '', 'solutionLength': '', 'finalQueueSize': '', 'nodesVisited': ''};
};
var Solver = soko.Solver;
/**
* @param {!soko.Level} level
* @param {!soko.State} state
* @return {!Array.<soko.State>}
*/
Solver.prototype.solve = function(level, state) {
var solution = [];
var startTime = Date.now();
var heuristic = new this.heuristicType_(level);
var node = {
'state': state,
'g': 0,
'h': heuristic.evaluate(state),
'parent': null,
'id': state.id()
};
var Q = new this.heapType_();
var numVisited = 0;
var visited = {};
var getNeighbors = level.getNeighbors.bind(level);
var invalidMap = new soko.heuristic.InvalidMap(level);
if (this.condensed_) {
getNeighbors = level.getNeighborsCondensed.bind(level);
}
Q.push(node, node.h);
while (!Q.empty()) {
var top = Q.pop();
node = top.value;
if (this.print && numVisited % 5000 == 0) {
console.log(top.score + ' ' + numVisited + ' ' + Q.size());
}
if (numVisited % 20000 == 0) {
var elapsedTime = (Date.now() - startTime) / 1000.0;
if (elapsedTime > 120) break;
}
visited[node.id] = true;
numVisited++;
if (level.isGoal(node.state)) {
solution = this.backtrack_(level, node);
break;
}
var neighbors = getNeighbors(node.state);
for (var i = 0, length = neighbors.length; i < length; ++i) {
var id = neighbors[i][1].id();
if (visited[id]) continue;
// This is a very important pruning step for the BFS and heuristics
// that were not finding invalid states.
if (invalidMap.isInvalid(neighbors[i][1])) continue;
var neighState = /** @type {!soko.State} */(neighbors[i][1]);
var g = node.g + /** @type {number} */(neighbors[i][0]);
var h = heuristic.evaluate(neighState);
var f = g + h;
var child = {
'state': neighState,
'pusher': neighbors[i][2],
'g': g,
'h': h,
'parent': node,
'id': id
};
if (Q.exists(child)) {
Q.updateIfBetter(child, f);
} else {
Q.push(child, f);
}
}
}
var duration = (Date.now() - startTime) / 1000.0;
if (this.print) console.log('numVisited:' + numVisited + ' duration:' + duration);
this.solverStats = {
'solutionLength': solution.length,
'elapsedTime': duration,
'finalQueueSize': Q.size(),
'nodesVisited': numVisited
};
return solution;
};
/** @private */
Solver.prototype.backtrack_ = function(level, node) {
var solution = [];
if (this.print) {
console.log(node);
}
do {
var parent = node.parent;
if (parent != null && node.pusher) {
var path = level.computeShortestPath(parent.state, node.pusher);
solution.push(node.state);
for (var i = 0; i < path.length - 1; i++) {
var state = new soko.State(path[i], parent.state.boxes);
solution.push(state);
}
} else {
solution.push(node.state);
}
node = node.parent;
} while (node != null);
if (this.print) {
console.log(solution.length);
}
return solution;
};
});