root/trunk/Test.java

Revision 32, 3.0 kB (checked in by atack2, 4 years ago)

I have done some encapsulation work here on the Path class. The data member 'totalCost' is now private and must be changed and accessed by setTotalCost() and getTotalCost(), two public functions. This commit includes changes to AStarSearch, Path, and Test, so there should be no conflict with this new usage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 // Test - copyright Tom Felker, <tfelker2@uiuc.edu>, December 2004
2 //
3 // Runnable class that does a simple test of the AStarSearch
4 // which is a part of UIUC Pathways
5
6 import java.io.*;
7 import java.util.*;
8
9 class Test {
10         public static void main(String argv[]) throws IOException, ClassNotFoundException {
11
12                 int xSize, ySize, n;
13                 double wiggle, criscross;
14                 String filename = null;
15
16                 try {
17                         xSize = Integer.parseInt(argv[0]);
18                         ySize = Integer.parseInt(argv[1]);
19                         wiggle = Double.parseDouble(argv[2]);
20                         criscross = Double.parseDouble(argv[3]);
21                         n = Integer.parseInt(argv[4]);
22                         if(argv.length > 5) filename = argv[5];
23                 } catch(Exception e) {
24                         System.out.println("Usage - java Test xSize ySize wiggle criscross searches [filename]");
25                         System.exit(1);
26                         return;
27                 }
28
29                 System.out.println("Creating regular " + xSize + "x" + ySize +
30                                    " grid with " + wiggle + " wiggle and " +
31                                    criscross + " chance for criscross...");
32
33                 long time = System.currentTimeMillis();
34
35                 Graph graph = new Graph();
36                 graph.createRegularGrid(xSize, ySize, wiggle, criscross);
37
38                 time = System.currentTimeMillis() - time;
39                 System.out.println("Done in " + time + " milliseconds.");
40
41                 if(filename != null) {
42                         System.out.println("Saving graph to " + filename);
43                         graph.saveToFile(filename);
44                         System.out.println("Filesize is " + (new File(filename)).length() + " bytes.");
45                         System.out.println("Reading in from file...");
46                         graph = null;
47                         System.gc();
48                         time = System.currentTimeMillis();
49                         graph = Graph.loadFromFile(filename);
50                         time = System.currentTimeMillis() - time;
51                         System.out.println("Done in " + time + " milliseconds.");
52                 }
53
54                 // this is how you use a search object...
55                 AStarSearch search = new AStarSearch();
56                 search.setWalkingSpeed(1);
57
58
59                 double averageCostComparisons = 0, averagePathLength = 0, averagePathCost = 0;
60
61                 System.out.println("Performing " + n + " random searches.");
62
63                 time = System.currentTimeMillis();
64
65                 for(int i = 0; i < n; ++i) {
66                         search.setOrigin((PhysicalNode)graph.getRandomNode());
67                         search.setDestination((PhysicalNode)graph.getRandomNode());
68                         // perform the actual search
69                         Cost.comparisonCounter = 0;
70                         Path path = search.computeBestPath();
71                         if(path == null) path = new Path();
72                         averageCostComparisons += Cost.comparisonCounter;
73                         averagePathLength += path.edges.size();
74                         averagePathCost += path.getTotalCost().getTime();
75                 }
76                 time = System.currentTimeMillis() - time;
77                 double averageTime = (double)time / n;
78                 averageCostComparisons /= n;
79                 averagePathLength /= n;
80                 averagePathCost /= n;
81                 System.out.println("Done");
82                 System.out.println("Average search time in milliseconds: " + averageTime);
83                 System.out.println("Average Cost comparisons: " + averageCostComparisons);
84                 System.out.println("Average path length: " + averagePathLength);
85                 System.out.println("Average path cost: " + averagePathCost);
86                 System.out.println("In CSV format,");
87                 System.out.println(averageTime + "," + averageCostComparisons +
88                                    "," + averagePathLength + "," + averagePathCost);
89         }
90 }
91
Note: See TracBrowser for help on using the browser.