-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
406 lines (333 loc) · 9.24 KB
/
Node.java
File metadata and controls
406 lines (333 loc) · 9.24 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import static java.awt.BorderLayout.*;
public class Node implements Comparable<Node>{
public static int global_index=1;
private final Node father;
private int curr_cost;
// private final int index;
private final Operation last_op;
public final int pos_x;
public final int pos_y;
private boolean out;
private int creation_time;
public static final Operation[] CLOCKWISE = {Operation.EAST, Operation.SOUTHEAST,Operation.SOUTH,
Operation.SOUTHWEST, Operation.WEST, Operation.NORTHWEST,Operation.NORTH,Operation.NORTHEAST
};
public static final Operation[] COUNTER_CLOCKWISE = {Operation.EAST,Operation.NORTHEAST,Operation.NORTH,
Operation.NORTHWEST,Operation.WEST,Operation.SOUTHWEST,Operation.SOUTH,Operation.SOUTHEAST
};
@Override
public int compareTo(Node o) {
//manhattan heuristic
// int this_h_cost = Algorithm.manhattan_cost(this,Ex1.finish_x,Ex1.finish_y);
// int o_h_cost = Algorithm.manhattan_cost(o,Ex1.finish_x,Ex1.finish_y);
// return Integer.compare(this_h_cost+this.curr_cost,o_h_cost+o.curr_cost);
// euclidean heuristic
// double this_h_cost = Algorithm.euclidean_cost(this,Ex1.finish_x,Ex1.finish_y);
// double o_h_cost = Algorithm.euclidean_cost(o,Ex1.finish_x,Ex1.finish_y);
// return Double.compare(this_h_cost+this.curr_cost,o_h_cost+o.curr_cost);
//diagonal heuristic
int this_h_cost = Algorithm.diagonal_cost(this,Ex1.finish_x,Ex1.finish_y);
int o_h_cost = Algorithm.diagonal_cost(o,Ex1.finish_x,Ex1.finish_y);
int ans = Integer.compare(this_h_cost*this.curr_cost,o_h_cost+o.curr_cost);
if(ans == 0)
{
if(this.creation_time < o.creation_time)
{
if(Ex1.new_first)
{
return 1;
}
else
{
return -1;
}
}
else
{
if(!Ex1.new_first)
{
return -1;
}
else
{
return 1;
}
}
}
return ans;
//new try
// double this_h_cost = Algorithm.heuristic(this,Ex1.finish_x,Ex1.finish_y);
// double o_h_cost = Algorithm.heuristic(o,Ex1.finish_x,Ex1.finish_y);
// return Double.compare(this_h_cost+this.curr_cost,o_h_cost+o.curr_cost);
//no heuristic
// return Integer.compare(this.curr_cost,o.curr_cost);
}
enum Operation {
NORTH,NORTHEAST,EAST,SOUTHEAST,SOUTH,SOUTHWEST,WEST,NORTHWEST,none
}
public int getHighestOp()
{
int ans = 0;
for (Operation op:CLOCKWISE)
{
int curr_cost = entry_cost(op);
if(curr_cost > ans)
{
ans = curr_cost;
}
}
return ans;
}
public int getLowestOp()
{
int ans = 10;
for (Operation op:CLOCKWISE)
{
int curr_cost = entry_cost(op);
if(curr_cost< ans && curr_cost != -1)
{
ans = curr_cost;
}
}
return ans;
}
public Node(Node _father, int x,int y,Operation _op)
{
this.father = _father;
// global_index++;
this.pos_x = x;
this.pos_y = y;
this.last_op = _op;
if(this.father != null)
{
this.curr_cost = _father.curr_cost + _father.entry_cost(_op);
}
else
{
this.curr_cost = 0;
}
this.out = false;
this.creation_time = global_index;
}
public Node getParent()
{
return this.father;
}
public int getX(){return this.pos_x;}
public int getY(){return this.pos_y;}
public Node getFather()
{
return this.father;
}
public void print_node()
{
System.out.println(this.getTerrain());
System.out.println(this.getIndex());
System.out.println("-------");
}
public void setOut(boolean isOut)
{
this.out = isOut;
}
public boolean getOut()
{
return this.out;
}
public int getCost()
{
return this.curr_cost;
}
public String getIndex()
{
return this.pos_x+","+(this.pos_y);
}
public char getTerrain()
{
return Ex1.board[this.pos_y][this.pos_x];
}
public String opToString()
{
switch(this.last_op)
{
case NORTH:
return "U";
case NORTHEAST:
return "RU";
case EAST:
return "R";
case SOUTHEAST:
return "RD";
case SOUTH:
return "D";
case SOUTHWEST:
return "LD";
case WEST:
return "L";
case NORTHWEST:
return "LU";
}
return "None";
}
public Operation getLast_op()
{
return last_op;
}
public Node operator(Operation _op)
{
//update new coordinates
int new_x = this.pos_x;
int new_y = this.pos_y;
switch(_op)
{
case NORTH:
new_y--;
break;
case NORTHEAST:
new_y--;
new_x++;
break;
case EAST:
new_x++;
break;
case SOUTHEAST:
new_x++;
new_y++;
break;
case SOUTH:
new_y++;
break;
case SOUTHWEST:
new_y++;
new_x--;
break;
case WEST:
new_x--;
break;
case NORTHWEST:
new_x--;
new_y--;
break;
}
Node child = new Node(this, new_x,new_y,_op);
global_index++;
return child;
}
public boolean hasChild()
{
for (Operation curr_op : Operation.values()) {
if(entry_cost(curr_op)>=0)
{
// System.out.println(curr_op);
return true;
}
}
return false;
}
public Queue<Node> allChilds(boolean clockwise)
{
Queue<Node> children = new LinkedList<>();
Operation[] operations = CLOCKWISE;
int operation_index = 0;
for (; operation_index<operations.length ;operation_index++)
{
int legal_move = this.entry_cost(operations[operation_index]);
if(legal_move>0)
{
Node child = this.operator(operations[operation_index]);
children.add(child);
}
}
return children;
}
public int entry_cost(Operation _op)
{
if(_op== Operation.none)
{
return -1;
}
int x = this.pos_x;
int y = this.pos_y;
switch(_op) {
case NORTH:
y--;
break;
case NORTHEAST:
y--;
x++;
break;
case EAST:
x++;
break;
case SOUTHEAST:
x++;
y++;
break;
case SOUTH:
y++;
break;
case SOUTHWEST:
y++;
x--;
break;
case WEST:
x--;
break;
case NORTHWEST:
x--;
y--;
break;
}
if(x<0 || y<0 || x >= Ex1.board_size || y>= Ex1.board_size)
{
return -1;
}
char terrain = Ex1.board[y][x];
int cost = -10;
switch(terrain)
{
case 'D':
cost = 1;
break;
case 'R':
cost = 3;
break;
case 'H':
//if the move is diagonal
cost = 5;
if(_op == Operation.NORTHEAST || _op == Operation.SOUTHEAST || _op == Operation.SOUTHWEST ||
_op == Operation.NORTHWEST)
{
cost = 10;
}
break;
case 'G':
cost = 5;
break;
case 'X':
cost = -1;
break;
case 'S':
cost = 0;
break;
}
if(this.father!=null)
{
if(reverse_op(this.last_op,_op) || reverse_op(_op,this.last_op))
{
return -1;
}
}
return cost;
}
public static boolean reverse_op(Operation op_a,Operation op_b)
{
if(op_a == Operation.NORTH && op_b == Operation.SOUTH) return true;
if(op_a == Operation.NORTHEAST && op_b == Operation.SOUTHWEST) return true;
if(op_a == Operation.EAST && op_b == Operation.WEST) return true;
if(op_a == Operation.SOUTHEAST && op_b == Operation.NORTHWEST) return true;
return false;
}
}