root/trunk/SearchNode.java

Revision 3, 2.2 kB (checked in by dpaola2, 1 month ago)

old pathways project added

Line 
1 //
2 //  SearchNode.java
3 //  Pathways
4 //
5 //  Created by Andres J. Tack on 1/9/05.
6 //  Copyright 2005 __MyCompanyName__. All rights reserved.
7 //
8
9 // Searchnode holds everything we "mark" on a node - it's costs, it's parent,
10 // and which list it's part of.  There is no explicit closed list, instead,
11 // if the searchnode exists, look at its isOpen property to see whether it's
12 // open or closed.  This avoids costly TreeSet membership tests, in favor of
13 // cheaper membership tests on a HashMap.
14
15 class SearchNode implements Comparable {
16
17         public SearchNode(Node n) {
18                 node = n;
19                 f = h = g = Cost.ZERO;
20         }
21
22         // TreeSet, a SortedSet, will use this to maintain its ordering
23         public int compareTo(Object o) {
24                 // XXX NOTE UGLY HACK!!!
25                 // We are using a TreeMap that indexes on Cost as the basis for
26                 // our open list.  Maps can't have multiple values for one key,
27                 // so if two nodes have the same f-cost, they will overwrite
28                 // each other in the open list, which is obviously bad.
29                 //
30                 // The correct way to fix this would be to implement a special
31                 // OpenList class, which would maintain a treemap that maps
32                 // costs to linked lists of possibly multiple nodes.  This
33                 // would probably be bad for performance.
34                 //
35                 // Instead, we will cause SearchNode to violate the general
36                 // contract of compareTo, given in the Java docs, that
37                 // (x.compareTo(y) > 0 == y.compareTo(x) < 0).  By ensuring that
38                 // SearchNodes are never equal, they will never overwrite each
39                 // other in the open list, and the performance should still
40                 // be good.  The main loop also includes tests to ensure
41                 // that this has worked.
42
43                 // Sane version:
44                 //return f.compareTo(((SearchNode)o).f);
45
46                 // Evil version:
47                 if(f.compareTo(((SearchNode)o).f) <= 0) return -1;
48                 else return 1; // never return 0
49         }
50
51         // only for debugging purposes
52         public String toString() {
53                 String ret = new String();
54                 ret += "Node " + node.id + " f:" + f + " g:" + g + " h:" + h;
55                 return ret;
56         }
57
58         // which node we refer to
59         public final Node node;
60
61         // where we came from, useful for backtracking to determine the path
62         public Edge parent;
63         // the f, h, and g costs used in the algorithm
64         public Cost f, h, g;
65         // is the node on the open list?  otherwise, it's closed
66         public boolean isOpen;
67 }
Note: See TracBrowser for help on using the browser.