-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdockworkerrobot.js
47 lines (41 loc) · 1.79 KB
/
dockworkerrobot.js
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
var strips = require('strips');
strips.verbose = true;
// Load the domain and problem.
strips.load('./examples/dockworkerrobot/domain.txt', './examples/dockworkerrobot/problem.txt', function(domain, problem) {
// Use A* search to run the problem against the domain.
var solutions = strips.solve(domain, problem, cost);
// Display solution.
var solution = solutions[0];
console.log('- Solution found in ' + solution.steps + ' steps!');
for (var i = 0; i < solution.path.length; i++) {
console.log((i + 1) + '. ' + solution.path[i]);
}
});
function cost(state) {
// This is our A* heuristic method to calculate the cost of a state.
var cost = 120;
for (var i in state.actions) {
var action = state.actions[i].action;
if (action == 'in') {
if (state.actions[i].parameters.indexOf('ca') != -1 && state.actions[i].parameters.indexOf('p2') != -1) {
cost -= 20;
}
else if (state.actions[i].parameters.indexOf('cb') != -1 && state.actions[i].parameters.indexOf('q2') != -1) {
cost -= 20;
}
else if (state.actions[i].parameters.indexOf('cc') != -1 && state.actions[i].parameters.indexOf('p2') != -1) {
cost -= 20;
}
else if (state.actions[i].parameters.indexOf('cd') != -1 && state.actions[i].parameters.indexOf('q2') != -1) {
cost -= 20;
}
else if (state.actions[i].parameters.indexOf('ce') != -1 && state.actions[i].parameters.indexOf('q2') != -1) {
cost -= 20;
}
else if (state.actions[i].parameters.indexOf('cf') != -1 && state.actions[i].parameters.indexOf('q2') != -1) {
cost -= 20;
}
}
}
return cost;
}