-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_Star.java
More file actions
107 lines (95 loc) · 3.46 KB
/
Copy pathA_Star.java
File metadata and controls
107 lines (95 loc) · 3.46 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
import java.util.HashMap;
public class A_Star {
private HashMap<String,Node> openNodes = new HashMap<String,Node>();
private HashMap<String,String> closedNodes = new HashMap<String,String>();
private String expansion;
private Labyrinth lab;
private int sx;
private int sy;
private int g1x;
private int g1y;
private int g2x;
private int g2y;
private Heuristic h;
public A_Star(Labyrinth lab,int sx,int sy,int g1x,int g1y,int g2x,int g2y){
this.lab=lab;
this.sx =sx;
this.sy=sy;
this.g1x=g1x;
this.g1y=g1y;
this.g2x=g2x;
this.g2y=g2y;
h = new Heuristic(g1x, g1y, g2x, g2y);
}
public void startA_Star(){
Node s= new Node(lab, sx, sy, null, Name(sx, sy),h.findCost(sx, sy));
openNodes.put(s.getName(),s);
expansion= s.getName();
createTree(s);
doTheA_Star(s);
}
public void doTheA_Star(Node prev){
double cost=prev.getTotalCost();
Node next = prev;
while(openNodes.size()>0){
for(String key : openNodes.keySet()){
Node cur = openNodes.get(key);
if(cost==prev.getTotalCost()){
if(next==prev){
cost = cur.getTotalCost();
next = cur;
}
}
else if(cost>cur.getTotalCost()){
cost = cur.getTotalCost();
next = cur;
}
}
expansion += " " + next.getName();
createTree(next);
doTheA_Star(next);
}
}
public void createTree(Node a){
closedNodes.put(a.getName(),a.getPath());
openNodes.remove(a.getName());
int nRows = a.getNeighbourRows();
for(int i=0;i<nRows;i++){
int x = a.getNeighbourX(i);
int y = a.getNeighbourY(i);
Node cur = new Node(lab, x, y, a, Name(x, y),h.findCost(x, y));
if(cur.getName().equals(Name(g1x,g1y))){
openNodes.clear();
expansion += " " + cur.getName();
printPath("G1", cur);
return;
}
else if(cur.getName().equals(Name(g2x,g2y))){
openNodes.clear();
expansion += " " + cur.getName();
printPath("G2", cur);
return;
}
if(!closedNodes.containsKey(Name(x, y))){
if(!openNodes.containsKey(Name(x, y))){
openNodes.put(Name(x, y), cur);
}
else{
Node prev = openNodes.get(Name(x, y));
if(cur.getTotalCost() < prev.getTotalCost()){
openNodes.replace(Name(x, y),cur,prev);
}
}
}
}
return;
}
private void printPath(String end, Node a) {
System.out.println("The algorithm expanded on:"+ expansion);
System.out.println("Path = S:"+a.getPath()+":"+end+"\n"+"Cost of Path = "+a.getPathCost());
return;
}
public String Name(int x, int y){
return ("("+x+","+y+")");
}
}