-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathandOrGraphSearch.js
More file actions
36 lines (33 loc) · 915 Bytes
/
andOrGraphSearch.js
File metadata and controls
36 lines (33 loc) · 915 Bytes
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
var AndOrGraphSearch = function(){
this.orSearch = function(state,problem,path){
// If goal is reached then no more planning required
if(problem.goal_test(state))
return [];
// Return null if loops are found
for(var i = 0; i < path.length; i++)
if(path[i] == state)
return null;
var actions = problem.actions(state);
for (var i = 0; i < actions.length; i++){
var action = actions[i];
plan = this.andSearch(problem.results(state,action),
problem,
path.unshift(state));
if (plan.length > 0)
return plan.unshift(action);
}
return null;
};
this.andSearch = function(states,problem,path){
var plans = [];
for (var i = 0; i < states.length; i++){
plans[i] = this.orSearch(states[i],problem,path);
if(plans[i] == null)
return null;
}
return plans;
};
this.search = function(problem){
return this.orSearch(problem.INITIAL_STATE,problem,[]);
};
};