-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
178 lines (135 loc) · 4.17 KB
/
Search.java
File metadata and controls
178 lines (135 loc) · 4.17 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
174
175
176
177
178
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Search {
Problem problem;
public Search(Problem problem) { this.problem = problem; }
//Tree-search methods
public String BreadthFirstTreeSearch() {
return TreeSearch(new FrontierFIFO());
}
public String DepthFirstTreeSearch() {
return TreeSearch(new FrontierLIFO());
}
public String UniformCostTreeSearch() {
return TreeSearch(new FrontierPriorityQueue(new ComparatorG()));
}
public String GreedyBestFirstTreeSearch() {
return TreeSearch(new FrontierPriorityQueue(new ComparatorH(problem)));
}
public String AstarTreeSearch() {
return TreeSearch(new FrontierPriorityQueue(new ComparatorF(problem)));
}
//Graph-search methods
public String BreadthFirstGraphSearch() {
return GraphSearch(new FrontierFIFO());
}
public String DepthFirstGraphSearch() {
return GraphSearch(new FrontierLIFO());
}
public String UniformCostGraphSearch() {
return GraphSearch(new FrontierPriorityQueue(new ComparatorG()));
}
public String GreedyBestFirstGraphSearch() {
return GraphSearch(new FrontierPriorityQueue(new ComparatorH(problem)));
}
public String AstarGraphSearch() {
return GraphSearch(new FrontierPriorityQueue(new ComparatorF(problem)));
}
//Iterative deepening, tree-search and graph-search
public String IterativeDeepeningTreeSearch() {
//TODO
return null;
}
public String IterativeDeepeningGraphSearch() {
//TODO
return null;
}
//For statistics purposes
int cnt; //count expansions
List<Node> node_list; //store all nodes ever generated
Node initialNode; //initial node based on initial state
//
private String TreeSearch(Frontier frontier) {
cnt = 0;
node_list = new ArrayList<Node>();
initialNode = MakeNode(problem.initialState);
node_list.add( initialNode );
frontier.insert( initialNode );
while(true) {
if(frontier.isEmpty())
return null;
Node node = frontier.remove();
if( problem.goal_test(node.state) )
return Solution(node);
frontier.insertAll(Expand(node,problem));
cnt++;
}
}
private String GraphSearch(Frontier frontier) {
cnt = 0;
node_list = new ArrayList<Node>();
initialNode = MakeNode(problem.initialState);
node_list.add( initialNode );
Set<Object> explored = new HashSet<Object>(); //empty set
frontier.insert( initialNode );
while(true) {
if(frontier.isEmpty())
return null;
Node node = frontier.remove();
if( problem.goal_test(node.state) )
return Solution(node);
if( !explored.contains(node.state) ) {
explored.add(node.state);
frontier.insertAll(Expand(node,problem));
cnt++;
}
}
}
private String TreeSearchDepthLimited(Frontier frontier, int limit) {
//TODO
return null;
}
private String GraphSearchDepthLimited(Frontier frontier, int limit) {
//TODO
return null;
}
private Node MakeNode(Object state) {
Node node = new Node();
node.state = state;
node.parent_node = null;
node.path_cost = 0;
node.depth = 0;
return node;
}
private Set<Node> Expand(Node node, Problem problem) {
node.order = cnt;
Set<Node> successors = new HashSet<Node>(); //empty set
Set<Object> successor_states = problem.getSuccessors(node.state);
for(Object result : successor_states) {
Node s = new Node();
s.state = result;
s.parent_node = node;
s.path_cost = node.path_cost + problem.step_cost(node.state, result);
s.depth = node.depth + 1;
successors.add(s);
node_list.add( s );
}
return successors;
}
//Create a string to print solution.
private String Solution(Node node) {
String solution_str = "(cost=" + node.path_cost + ", expansions=" + cnt + ")\t";
Deque<Object> solution = new ArrayDeque<Object>();
do {
solution.push(node.state);
node = node.parent_node;
} while(node != null);
while(!solution.isEmpty())
solution_str += solution.pop() + " ";
return solution_str;
}
}