-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaStar.java
105 lines (97 loc) · 3.31 KB
/
aStar.java
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
package src;
import java.util.ArrayList;
public class aStar {
private ArrayList<Node> frontier = new ArrayList<>();
private ArrayList<State> explored = new ArrayList<>();
private State state;
private Data data;
private Node target;
private int NUM = 1;
private double t = 0;
private Result result;
public aStar(Data d, String path) {
data = d;
state = new State(data.getROBOT(), data.getBUTTER());
t = System.nanoTime();
ASTAR();
t = System.nanoTime() - t;
if(frontier.isEmpty() && !target.isGoal(data.getMAP())){
result = new Result("cant pass the butter", null, -1, -1, t, path, explored.size(), NUM);
}
else {
ArrayList<String> answer = new ArrayList<>();
ArrayList<String> answerrev = new ArrayList<>();
Node n = target;
String solution = "";
while (n != null){
answer.add(n.getState().printMap(d.getMAP()));
solution += " " + n.getOrder();
n = n.getP();
}
char[] ans = solution.toCharArray();
solution = "";
for (int i = ans.length - 1; i >= 0; i--)
solution = solution + ans[i];
for (int i = answer.size()- 1; i >= 0; i--)
answerrev.add(answer.get(i));
result = new Result(solution, answerrev, target.getG(), target.getCutoff(), t, path, explored.size(), NUM);
}
data.output(result);
}
public void ASTAR(){
Node root = new Node(null, 0, 0, "", state);
frontier.add(root);
while (! frontier.isEmpty()){
Node node = minFrontier();
frontier.remove(node);
target = node;
if(node.isGoal(data.getMAP())){
return;
}
explored.add(node.getState());
for(Node child : node.successor(data.getMAP())){
NUM = NUM + 1;
if(!checkExplored(child)){
int i = checkFrontier(child);
if(i == -1){
frontier.add(child);
}
else{
if(frontier.get(i).getF(data.getPURCHASER()) > child.getF(data.getPURCHASER())){
frontier.remove(i);
frontier.add(child);
}
}
}
}
}
}
public boolean checkExplored(Node n){
for(int i = 0; i < explored.size(); i++){
if(n.getState().equals(explored.get(i)))
return true;
}
return false;
}
public int checkFrontier(Node n){
for(int i = 0; i < frontier.size(); i++){
if(n.getState().equals(frontier.get(i).getState()))
return i;
}
return -1;
}
public Node minFrontier(){
int j = 0;
int min = frontier.get(0).getF(data.getPURCHASER());
for (int i = 0; i < frontier.size(); i++){
if (min > frontier.get(i).getF(data.getPURCHASER())){
j = i;
min = frontier.get(i).getF(data.getPURCHASER());
}
}
return frontier.get(j);
}
public Result getResult() {
return result;
}
}