-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathida.txt
216 lines (179 loc) · 6.75 KB
/
ida.txt
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Stack;
public class IDS {
public static int temp = 0;
public static void search(State initialState) {
System.out.println("IDS...");
int depth = 0;
IDSResult result;
while(true) {
temp = 0;
System.out.print("currDepth: "+ depth + "\t");
result = tree_search(initialState, depth);
System.out.println(result);
if(result!= IDSResult.cutOff) {
return;
}
depth++;
}
}
/*
Iterative deepening and depth-limited tree-like search. Iterative deepening repeatedly
applies depth-limited search with increasing limits. It returns one of three different
types of values: either a solution node; or failure, when it has exhausted all nodes and proved
there is no solution at any depth; or cutoff , to mean there might be a solution at a deeper depth
than `. This is a tree-like search algorithm that does not keep track of reached states, and thus
uses much less memory than best-first search, but runs the risk of visiting the same state multiple
times on different paths. Also, if the IS-CYCLE check does not check all cycles, then
the algorithm may get caught in a loop.
*/
public static IDSResult graph_search(State initialState, int limit){
Stack<State> frontier = new Stack<State>();
frontier.add(initialState);
Hashtable<String, Boolean> explored = new Hashtable<>();
IDSResult result = IDSResult.failure;
while (!frontier.isEmpty()){
State tempState = frontier.pop();
if(isGoal(tempState)) {
result(tempState);
result = IDSResult.Node;
return result;
}
if(tempState.depth>limit) {
result = IDSResult.cutOff;
}
else {
explored.put(tempState.hash(),true);
ArrayList<State> children = tempState.successor();
for(int i = 0;i<children.size();i++){
if(!explored.containsKey(children.get(i).hash())) {
frontier.push(children.get(i));
}
}
}
}
return result;
}
//works very slowly
public static IDSResult tree_search(State initialState, int limit){
//Stack<State> frontier = new Stack<State>();
Hashtable<String, Boolean> currentTree = new Hashtable<>();
IDSResult result = IDSResult.failure;
if(isGoal(initialState)) {
result(initialState);
result = IDSResult.Node;
return result;
}
//frontier.add(initialState);
result = recursive(initialState, currentTree, limit);
return result;
}
public static IDSResult recursive(State state, Hashtable<String, Boolean> currentTree, int limit) {
//System.out.println(currentTree.size());
//State tempState = frontier.pop();
IDSResult result0 = IDSResult.failure;
if(state.depth>limit) {
//do not put state in currentTree if its above limit
return IDSResult.cutOff;
}
currentTree.put(state.hash(), true);
ArrayList<State> children = state.successor();
for(int i = 0; i < children.size(); i++) {
// redundant state
if(!(currentTree.containsKey(children.get(i).hash()))) {
if (isGoal(children.get(i))) {
result(children.get(i));
System.out.println("currentTree size: "+currentTree.size());
return IDSResult.Node;
}
//frontier.push(children.get(i));
result0 = recursive(children.get(i), currentTree, limit);
if(result0== IDSResult.Node) return result0;
}
}
currentTree.remove(state.hash());
return result0;
}
public static boolean isRedundant(State state) {
boolean result = false;
State parent = state.getParentState();
//System.out.println("CHECKING... ("+state.hash()+")");
int limit = 5;
int depth = 0;
while(parent!=null&&depth<limit) {
if(parent.hash().equals(state.hash())) {
//System.out.println("Parent of redundant: " + parent.hash());
result=true;
break;
}
parent = parent.getParentState();
depth++;
}
return result;
}
private static boolean isGoal(State state){
for (int i = 0; i < state.getGraph().size(); i++) {
if(state.getGraph().getNode(i).getColor() == Color.Red
|| state.getGraph().getNode(i).getColor() == Color.Black){
return false;
}
}
return true;
}
private static void result(State state){
Stack<State> states = new Stack<State>();
while (true){
states.push(state);
if(state.getParentState() == null){
break;
}
else {
state = state.getParentState();
}
}
int ans = states.size();
try {
FileWriter myWriter = new FileWriter("IdsResult.txt");
System.out.println("initial state : ");
while (!states.empty()){
State tempState = states.pop();
if(tempState.getSelectedNodeId() != -1) {
System.out.println("selected id : " + tempState.getSelectedNodeId()
+ "\t depth of current state: "+tempState.depth);
}
tempState.getGraph().print();
myWriter.write(tempState.getSelectedNodeId()+" ,");
myWriter.write(tempState.outputGenerator()+"\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
System.out.println("SIZE: "+ ans);
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
enum IDSResult {
failure {
@Override
public String toString() {
return "failure";
}
},
cutOff {
@Override
public String toString() {
return "cutOff";
}
},
Node {
@Override
public String toString() {
return "Found Node";
}
};
}