Debian Codegraph @

org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0536/A.java

raw
1 /*
2 * Created on Jan 20, 2004
3 *
4 * To change the template for this generated file go to
5 * Window>Preferences>Java>Code Generation>Code and Comments
6 */
7 package test0536;
8
9 import java.util.HashMap;
10 import java.util.LinkedList;
11
12 import SerSolver.SerBranchNode;
13 import SerSolver.SerChipExitNode;
14 import SerSolver.SerChipGoalNode;
15 import SerSolver.SerColor;
16 import SerSolver.SerGoalNode;
17 import SerSolver.SerNode;
18
19 import utilities.*;
20 import utilities.Move;
21 import utilities.NodeStatistics;
22
23 abstract public class A
24 {
25
26 protected static final int STAGESIZE = 15;
27 protected static int globalTargetDistance;
28 protected static Move[] moves;
29 static HashMap map;
30 public static SerNode exitNode;
31 static
32 {
33 globalTargetDistance = 10000;
34 exitNode = null;
35 moves = new Move[100];
36 for (int i = 0; i < 100; i++)
37 {
38 moves[i] = new Move();
39 }
40 map = new HashMap(100000);
41 }
42
43 protected int chips;
44 protected SerColor currentKey;
45 protected int startIndex;
46 protected int moveIndex;
47 protected int targetDistance;
48 protected LinkedList[] dependencies;
49 protected A nextSolver;
50 A(int _startIndex)
51 {
52 dependencies = new LinkedList[5];
53 for (int i = 0; i < 5; i++)
54 dependencies[i] = new LinkedList();
55 startIndex = _startIndex;
56 }
57 /**
58 * @param moveIndex
59 * @param moves
60 * @param chips
61 * @param dependencies
62 * @return the best solution for the remainder of the board
63 */
64 v
65 public Solution subSolve(int _moveIndex, int _chips, LinkedList[] _dependencies)
66 {
67 assert (_moveIndex==startIndex);
68 key.set(_dependencies);
69 Solution solution = (Solution) map.get(key);
70 if (solution == null)
71 {
72 chips = _chips;
73 targetDistance = 10000;
74 moveIndex = _moveIndex;
75 currentKey = moves[startIndex - 1].node.getKey();
76 for (int i = 1; i < 5; i++)
77 {
78 dependencies[i].clear();
79 dependencies[i].addAll(_dependencies[i]);
80 }
81
82 solution = findSolution();
83 Key saveKey = new Key(key);
84 map.put(saveKey, solution);
85
86 }
87 else
88 {
89 solution.hits++;
90 }
91 return solution;
92 }
93
94 /**
95 * like the method in the main solution finder but it is
96 * only looking for the best end game
97 * @param chips
98 */
99 abstract protected Solution findSolution();
100
101 /**
102 * @return
103 */
104 protected boolean pickNode(int index)
105 {
106 LinkedList list = dependencies[currentKey.getValue()];
107 if (list.isEmpty())
108 return false;
109 else
110 {
111 SerNode node = (SerNode) list.get(index);
112 Move lastMove = moves[moveIndex - 1];
113 int distance = NodeStatistics.nodeDistance(lastMove.node, node);
114 int cost = distance + lastMove.distance;
115 if (cost > targetDistance)
116 return false;
117 list.remove(index);
118 chooseNode(node, index, cost);
119 return true;
120 }
121
122 }
123
124 /**
125 *
126 */
127 protected int backoff()
128 {
129 Move move = moves[--moveIndex];
130 if (move.node instanceof SerChipGoalNode)
131 {
132 chips--;
133 }
134 if (move.node instanceof SerBranchNode)
135 {
136 SerNode[] nodes = ((SerBranchNode) move.node).getDependentNodes();
137 removeFromStack(nodes[0]);
138 removeFromStack(nodes[1]);
139 }
140 else
141 removeFromStack(((SerGoalNode) move.node).getDependentNode());
142 currentKey = move.node.getDoor();
143 boolean more = reputStack(move.node, move.indexOnStack);
144 if (!more)
145 return backoff();
146 else
147 return move.indexOnStack + 1;
148 }
149
150 /**
151 * @param node
152 */
153 protected void chooseNode(SerNode node, int cameFrom, int distance)
154 {
155 //NOTE assume already own green key
156 // if (node instanceof SerGreenKeyGoalNode)
157 // {
158 // sawGreen = true;
159 // drainGreenQueue();
160 // }
161 if (node instanceof SerBranchNode)
162 {
163 SerBranchNode branch = (SerBranchNode) node;
164 putStack(branch.getDependentNodes()[0]);
165 putStack(branch.getDependentNodes()[1]);
166 }
167 else
168 {
169 putStack(((SerGoalNode) node).getDependentNode());
170 if (node instanceof SerChipGoalNode)
171 {
172 chips++;
173 }
174 }
175
176 currentKey = node.getKey();
177
178 moves[moveIndex].indexOnStack = cameFrom;
179 moves[moveIndex].node = node;
180 moves[moveIndex].distance = distance;
181 moveIndex++;
182 }
183 /**
184 * @param board
185 * @param node
186 */
187 protected void putStack(SerNode node)
188 {
189 /* if the last node on a branch is a goal node it doesn't expose any other nodes
190 */
191 if (node == null)
192 return;
193 else if (node instanceof SerChipExitNode)
194 exitNode = node;
195 //NOTE assume already own green key
196 // else if (!sawGreen && node instanceof SerChipGoalNode)
197 // getDependencies(SerColor.GREEN_LITERAL).add(node);
198 else
199 dependencies[node.getDoor().getValue()].add(node);
200
201 }
202 /**
203 * @param node
204 */
205 protected void removeFromStack(SerNode node)
206 {
207 if (node == null)
208 return;
209 if (node instanceof SerChipExitNode)
210 return;
211
212 LinkedList list = dependencies[node.getDoor().getValue()];
213 if (!list.remove(node))
214 throw new RuntimeException("door not available");
215 }
216 /**
217 * @param node
218 * @param i
219 * @return
220 */
221 protected boolean reputStack(SerNode node, int i)
222 {
223 LinkedList list = dependencies[node.getDoor().getValue()];
224 list.add(i, node);
225 return list.size() > (i + 1);
226 }
227 protected void dumpMoves()
228 {
229 StringBuffer output = new StringBuffer(3000);
230 SerNode oldNode = moves[0].node;
231 int testDistance = 2;
232 for (int i = 1; i < moveIndex; i++)
233 {
234 SerNode node = moves[i].node;
235 int distance = moves[i].distance;
236 testDistance += NodeStatistics.nodeDistance(oldNode,node);
237 assert(testDistance==distance);
238 output.append(
239 i
240 + "("
241 + node.getColumn()
242 + ","
243 + node.getRow()
244 + ") "
245 + (2 * distance - 1)
246 + "\n");
247 oldNode = node;
248 }
249 System.out.println(output);
250
251 }
252 }Content-Type: text/plain
253
254 /*
255 * Created on Jan 20, 2004
256 *
257 * To change the template for this generated file go to
258 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
259 */
260 package test0536;
261
262 import java.util.HashMap;
263 import java.util.LinkedList;
264
265 import SerSolver.SerBranchNode;
266 import SerSolver.SerChipExitNode;
267 import SerSolver.SerChipGoalNode;
268 import SerSolver.SerColor;
269 import SerSolver.SerGoalNode;
270 import SerSolver.SerNode;
271
272 import utilities.*;
273 import utilities.Move;
274 import utilities.NodeStatistics;
275
276 abstract public class A
277 {
278
279 protected static final int STAGESIZE = 15;
280 protected static int globalTargetDistance;
281 protected static Move[] moves;
282 static HashMap map;
283 public static SerNode exitNode;
284 static
285 {
286 globalTargetDistance = 10000;
287 exitNode = null;
288 moves = new Move[100];
289 for (int i = 0; i < 100; i++)
290 {
291 moves[i] = new Move();
292 }
293 map = new HashMap(100000);
294 }
295
296 protected int chips;
297 protected SerColor currentKey;
298 protected int startIndex;
299 protected int moveIndex;
300 protected int targetDistance;
301 protected LinkedList[] dependencies;
302 protected A nextSolver;
303 A(int _startIndex)
304 {
305 dependencies = new LinkedList[5];
306 for (int i = 0; i < 5; i++)
307 dependencies[i] = new LinkedList();
308 startIndex = _startIndex;
309 }
310 /**
311 * @param moveIndex
312 * @param moves
313 * @param chips
314 * @param dependencies
315 * @return the best solution for the remainder of the board
316 */
317 v
318 public Solution subSolve(int _moveIndex, int _chips, LinkedList[] _dependencies)
319 {
320 assert (_moveIndex==startIndex);
321 key.set(_dependencies);
322 Solution solution = (Solution) map.get(key);
323 if (solution == null)
324 {
325 chips = _chips;
326 targetDistance = 10000;
327 moveIndex = _moveIndex;
328 currentKey = moves[startIndex - 1].node.getKey();
329 for (int i = 1; i < 5; i++)
330 {
331 dependencies[i].clear();
332 dependencies[i].addAll(_dependencies[i]);
333 }
334
335 solution = findSolution();
336 Key saveKey = new Key(key);
337 map.put(saveKey, solution);
338
339 }
340 else
341 {
342 solution.hits++;
343 }
344 return solution;
345 }
346
347 /**
348 * like the method in the main solution finder but it is
349 * only looking for the best end game
350 * @param chips
351 */
352 abstract protected Solution findSolution();
353
354 /**
355 * @return
356 */
357 protected boolean pickNode(int index)
358 {
359 LinkedList list = dependencies[currentKey.getValue()];
360 if (list.isEmpty())
361 return false;
362 else
363 {
364 SerNode node = (SerNode) list.get(index);
365 Move lastMove = moves[moveIndex - 1];
366 int distance = NodeStatistics.nodeDistance(lastMove.node, node);
367 int cost = distance + lastMove.distance;
368 if (cost > targetDistance)
369 return false;
370 list.remove(index);
371 chooseNode(node, index, cost);
372 return true;
373 }
374
375 }
376
377 /**
378 *
379 */
380 protected int backoff()
381 {
382 Move move = moves[--moveIndex];
383 if (move.node instanceof SerChipGoalNode)
384 {
385 chips--;
386 }
387 if (move.node instanceof SerBranchNode)
388 {
389 SerNode[] nodes = ((SerBranchNode) move.node).getDependentNodes();
390 removeFromStack(nodes[0]);
391 removeFromStack(nodes[1]);
392 }
393 else
394 removeFromStack(((SerGoalNode) move.node).getDependentNode());
395 currentKey = move.node.getDoor();
396 boolean more = reputStack(move.node, move.indexOnStack);
397 if (!more)
398 return backoff();
399 else
400 return move.indexOnStack + 1;
401 }
402
403 /**
404 * @param node
405 */
406 protected void chooseNode(SerNode node, int cameFrom, int distance)
407 {
408 //NOTE assume already own green key
409 // if (node instanceof SerGreenKeyGoalNode)
410 // {
411 // sawGreen = true;
412 // drainGreenQueue();
413 // }
414 if (node instanceof SerBranchNode)
415 {
416 SerBranchNode branch = (SerBranchNode) node;
417 putStack(branch.getDependentNodes()[0]);
418 putStack(branch.getDependentNodes()[1]);
419 }
420 else
421 {
422 putStack(((SerGoalNode) node).getDependentNode());
423 if (node instanceof SerChipGoalNode)
424 {
425 chips++;
426 }
427 }
428
429 currentKey = node.getKey();
430
431 moves[moveIndex].indexOnStack = cameFrom;
432 moves[moveIndex].node = node;
433 moves[moveIndex].distance = distance;
434 moveIndex++;
435 }
436 /**
437 * @param board
438 * @param node
439 */
440 protected void putStack(SerNode node)
441 {
442 /* if the last node on a branch is a goal node it doesn't expose any other nodes
443 */
444 if (node == null)
445 return;
446 else if (node instanceof SerChipExitNode)
447 exitNode = node;
448 //NOTE assume already own green key
449 // else if (!sawGreen && node instanceof SerChipGoalNode)
450 // getDependencies(SerColor.GREEN_LITERAL).add(node);
451 else
452 dependencies[node.getDoor().getValue()].add(node);
453
454 }
455 /**
456 * @param node
457 */
458 protected void removeFromStack(SerNode node)
459 {
460 if (node == null)
461 return;
462 if (node instanceof SerChipExitNode)
463 return;
464
465 LinkedList list = dependencies[node.getDoor().getValue()];
466 if (!list.remove(node))
467 throw new RuntimeException("door not available");
468 }
469 /**
470 * @param node
471 * @param i
472 * @return
473 */
474 protected boolean reputStack(SerNode node, int i)
475 {
476 LinkedList list = dependencies[node.getDoor().getValue()];
477 list.add(i, node);
478 return list.size() > (i + 1);
479 }
480 protected void dumpMoves()
481 {
482 StringBuffer output = new StringBuffer(3000);
483 SerNode oldNode = moves[0].node;
484 int testDistance = 2;
485 for (int i = 1; i < moveIndex; i++)
486 {
487 SerNode node = moves[i].node;
488 int distance = moves[i].distance;
489 testDistance += NodeStatistics.nodeDistance(oldNode,node);
490 assert(testDistance==distance);
491 output.append(
492 i
493 + "("
494 + node.getColumn()
495 + ","
496 + node.getRow()
497 + ") "
498 + (2 * distance - 1)
499 + "\n");
500 oldNode = node;
501 }
502 System.out.println(output);
503
504 }
505 }