--- jsr166/src/test/loops/TSPExchangerTest.java 2009/10/29 23:09:08 1.6 +++ jsr166/src/test/loops/TSPExchangerTest.java 2012/02/21 01:54:03 1.14 @@ -1,7 +1,7 @@ /* * Written by Doug Lea and Bill Scherer with assistance from members * of JCP JSR-166 Expert Group and released to the public domain, as - * explained at http://creativecommons.org/licenses/publicdomain + * explained at http://creativecommons.org/publicdomain/zero/1.0/ */ import java.util.*; @@ -88,7 +88,7 @@ public class TSPExchangerTest { /** * The probability mask value for creating random strands, * that have lengths at least MIN_STRAND_LENGTH, and grow - * with exposnential decay 2^(-(1/(RANDOM_STRAND_MASK + 1) + * with exponential decay 2^(-(1/(RANDOM_STRAND_MASK + 1) * Must be 1 less than a power of two. */ static final int RANDOM_STRAND_MASK = 7; @@ -201,7 +201,7 @@ public class TSPExchangerTest { } /** - * Perform one run with the given parameters. Each run complete + * Performs one run with the given parameters. Each run completes * when there are fewer than 2 active threads. When there is * only one remaining thread, it will have no one to exchange * with, so it is terminated (via interrupt). @@ -225,7 +225,7 @@ public class TSPExchangerTest { // Thread.sleep(100); long elapsed = stopTime - startTime; - double secs = (double)elapsed / 1000000000.0; + double secs = (double) elapsed / 1000000000.0; p.printSnapshot(secs); } @@ -300,7 +300,7 @@ public class TSPExchangerTest { */ void printSnapshot(double secs) { int xs = totalExchanges(); - long rate = (xs == 0)? 0L : (long)((secs * 1000000000.0) / xs); + long rate = (xs == 0) ? 0L : (long) ((secs * 1000000000.0) / xs); Chromosome bestc = subpops[0].chromosomes[0]; Chromosome worstc = bestc; for (int k = 0; k < subpops.length; ++k) { @@ -416,8 +416,8 @@ public class TSPExchangerTest { } /** - * Choose a breeder, exchange strand with another subpop, and - * cross them to create new chromosome to replace a chosen + * Chooses a breeder, exchanges strand with another subpop, and + * crosses them to create new chromosome to replace a chosen * dyer. */ void update() throws InterruptedException { @@ -432,7 +432,7 @@ public class TSPExchangerTest { } /** - * Choose a breeder, with exponentially decreasing probability + * Chooses a breeder, with exponentially decreasing probability * starting at best. * @return index of selected breeder */ @@ -447,9 +447,9 @@ public class TSPExchangerTest { } /** - * Choose a chromosome that will be replaced, with + * Chooses a chromosome that will be replaced, with * exponentially decreasing probability starting at - * worst, ignoring the excluded index + * worst, ignoring the excluded index. * @param exclude index to ignore; use -1 to not exclude any * @return index of selected dyer */ @@ -484,7 +484,7 @@ public class TSPExchangerTest { } /** - * Copy current strand to start of c's, and then append all + * Copies current strand to start of c's, and then appends all * remaining b's that aren't in the strand. * @param breeder the breeder * @param child the child @@ -522,7 +522,7 @@ public class TSPExchangerTest { } /** - * Fix the sort order of a changed Chromosome c at position k + * Fixes the sort order of a changed Chromosome c at position k. * @param c the chromosome * @param k the index */ @@ -561,7 +561,7 @@ public class TSPExchangerTest { int fitness; /** - * Initialize to random tour + * Initializes to random tour. */ Chromosome(int length, RNG random) { alleles = new int[length]; @@ -577,9 +577,9 @@ public class TSPExchangerTest { } public int compareTo(Object x) { // to enable sorting - int xf = ((Chromosome)x).fitness; + int xf = ((Chromosome) x).fitness; int f = fitness; - return ((f == xf)? 0 :((f < xf)? -1 : 1)); + return ((f == xf) ? 0 :((f < xf) ? -1 : 1)); } void recalcFitness() { @@ -592,11 +592,11 @@ public class TSPExchangerTest { f += cities.distanceBetween(p, n); p = n; } - fitness = (int)(f / len); + fitness = (int) (f / len); } /** - * Return tour length for points scaled in [0, 1). + * Returns tour length for points scaled in [0, 1). */ double unitTourLength() { int[] a = alleles; @@ -612,7 +612,7 @@ public class TSPExchangerTest { } /** - * Check that this tour visits each city + * Checks that this tour visits each city. */ void validate() { int len = alleles.length; @@ -661,36 +661,36 @@ public class TSPExchangerTest { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { - double dx = (double)xPts[i] - (double)xPts[j]; - double dy = (double)yPts[i] - (double)yPts[j]; + double dx = (double) xPts[i] - (double) xPts[j]; + double dy = (double) yPts[i] - (double) yPts[j]; double dd = Math.hypot(dx, dy) / 2.0; long ld = Math.round(dd); - distances[i][j] = (ld >= Integer.MAX_VALUE)? - Integer.MAX_VALUE : (int)ld; + distances[i][j] = (ld >= Integer.MAX_VALUE) ? + Integer.MAX_VALUE : (int) ld; } } } /** - * Returns the cached distance between a pair of cities + * Returns the cached distance between a pair of cities. */ int distanceBetween(int i, int j) { return distances[i][j]; } // Scale ints to doubles in [0,1) - static final double PSCALE = (double)0x80000000L; + static final double PSCALE = (double) 0x80000000L; /** - * Return distance for points scaled in [0,1). This simplifies + * Returns distance for points scaled in [0,1). This simplifies * checking results. The expected optimal TSP for random * points is believed to be around 0.76 * sqrt(N). For papers * discussing this, see * http://www.densis.fee.unicamp.br/~moscato/TSPBIB_home.html */ double unitDistanceBetween(int i, int j) { - double dx = ((double)xPts[i] - (double)xPts[j]) / PSCALE; - double dy = ((double)yPts[i] - (double)yPts[j]) / PSCALE; + double dx = ((double) xPts[i] - (double) xPts[j]) / PSCALE; + double dy = ((double) yPts[i] - (double) yPts[j]) / PSCALE; return Math.hypot(dx, dy); } @@ -705,7 +705,7 @@ public class TSPExchangerTest { int seed; RNG(int seed) { this.seed = seed; } - RNG() { this.seed = seedGenerator.nextInt() | 1; } + RNG() { this.seed = seedGenerator.nextInt() | 1; } int next() { int x = seed;