You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Searches for a solution to the given problem by using depth-first-search or breadth-first-search. The default setting uses depth-first-search and returns the first solution found. To return more solutions, set maxSolutions to a higher value. Note, you can write your own solution algorithm by using the methods below.
60
+
Searches for a solution to the given problem by using depth-first-search, breadth-first-search, or A*. The default setting uses depth-first-search and returns the first solution found. To return more solutions, set maxSolutions to a higher value. To use A* search, provide a function for the "cost" parameter, using the format cost(state), to serve as a heuristic for A*. The function should return an integer, representing the cost of the given state. See [starcraft.js](https://github.com/primaryobjects/strips/blob/master/starcraft.js) for an example. The cost function may also be passed as the 3rd parameter (ie., solve(domain, problem, cost)). Note, you can also write your own solution algorithm by using the methods below.
61
61
62
62
#### getChildStates(domain, state)
63
63
@@ -164,11 +164,34 @@ Of course, the real power is in designing your own search algorithm using the st
164
164
165
165
### A* Search
166
166
167
-
You can implement your own A* search to find a solution. A* works by using a heuristic to guide it down the path of possible moves in the domain. In this manner, it is much faster than simple breadth-first or depth-first search. It will also find an optimal solution that contains the least number of steps.
167
+
A* search works by using a heuristic to guide it down the path of possible moves in the domain. In this manner, it is much faster than simple breadth-first or depth-first search. It will also find an optimal solution that contains the least number of steps.
168
168
169
-
Since the strips library exposes its internal methods, you can implement your own search algorithm. Here is an [example](https://github.com/primaryobjects/strips/blob/master/starcraft.js)of an A* search method for the [starcraft](https://github.com/primaryobjects/strips/blob/master/examples/starcraft/domain.txt)domain, to train a [marine](https://github.com/primaryobjects/strips/blob/master/examples/starcraft/marine.txt).
169
+
Strips comes with a built-in A* search algorithm that accepts your own cost function to use as a heuristic. See the section "methods" above. You simply write your own cost function that takes "state" as input and returns an integer as the resulting cost. Here is an [example](https://github.com/primaryobjects/strips/blob/master/starcraft.js)from the Starcraft [domain](https://github.com/primaryobjects/strips/blob/master/examples/starcraft/domain.txt) to train a [marine](https://github.com/primaryobjects/strips/blob/master/examples/starcraft/marine.txt):
170
170
171
-
The core idea to a custom search method is to use the strips methods isGoal() and getChildStates() to iterate through all states and actions. Once you have a list of child states, apply your heuristic to calculate a cost for each state. Then sort the states by cost so that A* can choose the next cheapest state to move to. You can see the details in the example.
171
+
```javascript
172
+
var solutions =strips.solve(domain, problem, cost);
173
+
174
+
functioncost(state) {
175
+
// This is our A* heuristic method to calculate the cost of a state.
176
+
// For Starcraft, the heuristic will be how many required buildings have been built. Subtract x from cost for each correct building, with 0 meaning all required buildings have been made and we're done.
177
+
var cost =10;
178
+
179
+
for (var i instate.actions) {
180
+
var action =state.actions[i].action;
181
+
182
+
if (action =='depot') {
183
+
cost -=5;
184
+
}
185
+
elseif (action =='barracks') {
186
+
cost -=5;
187
+
}
188
+
}
189
+
190
+
return cost;
191
+
}
192
+
```
193
+
194
+
You can also implement your own A* search to find a solution. Since the strips library exposes its internal methods, you can implement your own search algorithm. The core idea to a custom search method is to use the strips methods isGoal() and getChildStates() to iterate through all states and actions. Once you have a list of child states, apply your heuristic to calculate a cost for each state. Then sort the states by cost so that A* can choose the next cheapest state to move to. You can see the details in the solve() methods in strips.
// Find solution using A*, depth-first, or breadth-first search.
556
-
if(isDfs==null){
556
+
if(typeof(isDfs)=='function'&&!cost){
557
+
// Allow passing cost as 3rd parameter.
558
+
cost=isDfs;
559
+
}
560
+
elseif(isDfs==null){
561
+
// If no other option specified, use depth-first-search by default.
557
562
isDfs=true;
558
563
}
559
564
560
565
maxSolutions=maxSolutions||1;
561
566
567
+
if(cost&&typeof(cost)!='function'){
568
+
console.log('ERROR: parameter "cost" must be a function to serve as the A* algorithm heuristic. Method: solve(domain, problem, isDepthFirstSearch, cost, maxSolutions). Usage: solve(domain, problem), solve(domain, problem, false), solve(domain, problem, cost).');
0 commit comments