-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGreedyBestFirst.java
More file actions
82 lines (76 loc) · 2.29 KB
/
GreedyBestFirst.java
File metadata and controls
82 lines (76 loc) · 2.29 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
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;
public class GreedyBestFirst extends Search{
Comparator<Node> comparator = new NodeComparator();
PriorityQueue<Node> pq = new PriorityQueue<Node>(20, comparator);
public static void main(String[] args) throws IOException{
GreedyBestFirst gbf= new GreedyBestFirst();
gbf.readInput();
gbf.pq.add(gbf.startNode);
Node end = gbf.search();
gbf.findPath(end);
System.out.println("Path Cost: "+gbf.pathCost);
System.out.println("Nodes Expanded: "+gbf.nodesExpanded);
System.out.println("Max Depth: "+gbf.maxDepth);
System.out.println("Maximum size of the frontier: "+gbf.maxFrontierSize);
gbf.printMaze();
}
private Node search(){
while(!pq.isEmpty()){
if(pq.size()>maxFrontierSize)
maxFrontierSize = pq.size();
Node node = pq.poll();
nodesExpanded++;
int xPos = node.xPos, yPos = node.yPos;
if(maze.get(yPos).charAt(xPos)=='.')
return node;
if(maze.get(yPos).charAt(xPos+1)!='%'){
Node child = new Node(xPos+1,yPos,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos+1][yPos]!=1){
visited[xPos+1][yPos]=1;
pq.add(child);
}
}
if(maze.get(yPos-1).charAt(xPos)!='%'){
Node child = new Node(xPos,yPos-1,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos][yPos-1]!=1){
visited[xPos][yPos-1]=1;
pq.add(child);
}
}
if(maze.get(yPos+1).charAt(xPos)!='%'){
Node child = new Node(xPos,yPos+1,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos][yPos+1]!=1){
visited[xPos][yPos+1]=1;
pq.add(child);
}
}
if(maze.get(yPos).charAt(xPos-1)!='%'){
Node child = new Node(xPos-1,yPos,node,node.depth+1);
if(node.depth+1>maxDepth)
maxDepth = node.depth+1;
if(visited[xPos-1][yPos]!=1){
visited[xPos-1][yPos]=1;
pq.add(child);
}
}
}
return null;
}
class NodeComparator implements Comparator<Node>{
private int distanceToGoal(Node node){
return Math.abs(node.xPos-goalCol)+ Math.abs(node.yPos-goalRow);
}
@Override
public int compare(Node arg0, Node arg1) {
return distanceToGoal(arg0)-distanceToGoal(arg1);
}
}
}