-
Notifications
You must be signed in to change notification settings - Fork 1
/
IDS.java
93 lines (87 loc) · 2.73 KB
/
IDS.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
package src;
import java.util.ArrayList;
import java.util.Stack;
public class IDS {
private boolean goal = false;
private Node target;
private State state;
private Data data;
private int num = 0;
private int NUM = 0;
private double t = 0;
private int maxdepth;
private String path = "";
private Result result;
public IDS(Data data, int maxDepth, String p) {
this.data = data;
state = new State(data.getROBOT(), data.getBUTTER());
Node n = new Node(null, 0, 0, "", state);
target = n;
goal = n.isGoal(data.getMAP());
t = System.nanoTime();
maxdepth = maxDepth;
runDeepSearch();
t = System.nanoTime() - t;
path = p;
if(goal == false){
result = new Result("cant pass the butter", null, -1, -1, t, path, num, NUM);
}
else {
ArrayList<String> answer = new ArrayList<>();
ArrayList<String> answerrev = new ArrayList<>();
Node nn = target;
String solution = "";
while (nn != null) {
answer.add(nn.getState().printMap(data.getMAP()));
solution += " " + nn.getOrder();
nn = nn.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.getCutoff(), target.getCutoff(), t, path, num, NUM);
}
data.output(result);
}
public void runDeepSearch(){
int depth = 0;
while (!goal && depth < maxdepth){
Node root = new Node(null, 0, 0, "", state);
DFS(root, depth);
depth++;
}
}
public void DFS(Node root, int depth){
num = 1;
NUM = 1;
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.empty()){
Node node = stack.pop();
if(node.isGoal(data.getMAP())){
goal = true;
target = node;
return;
}
int c = node.getCutoff();
if(c < depth){
num = num + 1;
for (Node n: node.successor(data.getMAP())){
NUM = NUM + 1;
if (n.isGoal(data.getMAP())){
goal = true;
target = n;
return;
}
stack.push(n);
}
}
}
}
public Result getResult() {
return result;
}
}