ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TSPExchangerTest.java
(Generate patch)

Comparing jsr166/src/test/loops/TSPExchangerTest.java (file contents):
Revision 1.2 by dl, Sat Dec 10 20:08:51 2005 UTC vs.
Revision 1.11 by jsr166, Sat Oct 16 16:22:57 2010 UTC

# Line 11 | Line 11 | import java.util.concurrent.locks.*;
11  
12   /**
13   * A parallel Traveling Salesperson Problem (TSP) program based on a
14 < * genetic algorithm using an Exchanger.  A population of chromosomes
15 < * is distributed among "pools".  The chromosomes represent tours, and
16 < * their fitness is the total tour length. Each chromosome is
17 < * initialized as a random tour.  A Task is associated with each pool.
18 < * Each task repeatedly does, for a fixed number of iterations
19 < * (generations):
14 > * genetic algorithm using an Exchanger.  A population of chromosomes is
15 > * distributed among "subpops".  Each chromosomes represents a tour,
16 > * and its fitness is the total tour length.
17   *
18 + * A set of worker threads perform updates on subpops. The basic
19 + * update step is:
20   * <ol>
21 < *   <li> Select a breeder b from the pool
21 > *   <li> Select a breeder b from the subpop
22   *   <li> Create a strand of its tour with a random starting point and length
23 < *   <li> Offer the strand to the exchanger, receiving a strand from
24 < *        another pool
25 < *   <li> Combine b and the received strand using crossing function to
23 > *   <li> Offer the strand to the exchanger, receiving a strand from
24 > *        another subpop
25 > *   <li> Combine b and the received strand using crossing function to
26   *        create new chromosome c.
27 < *   <li> Replace a chromosome in the pool with c.
27 > *   <li> Replace a chromosome in the subpop with c.
28   * </ol>
29   *
30 + * This continues for a given number of generations per subpop.
31 + * Because there are normally more subpops than threads, each worker
32 + * thread performs small (randomly sized) run of updates for one
33 + * subpop and then selects another. A run continues until there is at
34 + * most one remaining thread performing updates.
35 + *
36   * See below for more details.
32 * <p>
33 *
37   */
38   public class TSPExchangerTest {
39 <    static final int DEFAULT_MAX_THREADS  =
40 <        (Runtime.getRuntime().availableProcessors() + 2);
39 >    static final int NCPUS = Runtime.getRuntime().availableProcessors();
40 >
41 >    /** Runs start with two threads, increasing by two through max */
42 >    static final int DEFAULT_MAX_THREADS  = Math.max(4, NCPUS + NCPUS/2);
43 >
44 >    /** The number of replication runs per thread value */
45 >    static final int DEFAULT_REPLICATIONS = 3;
46 >
47 >    /** If true, print statistics in SNAPSHOT_RATE intervals */
48 >    static boolean verbose = true;
49 >    static final long SNAPSHOT_RATE = 10000; // in milliseconds
50  
51      /**
52       * The problem size. Each city is a random point. The goal is to
# Line 42 | Line 54 | public class TSPExchangerTest {
54       */
55      static final int DEFAULT_CITIES = 144;
56  
57 <    // Tuning parameters.
57 >    // Tuning parameters.
58  
59      /**
60 <     * The number of chromosomes per pool. Must be a power of two.
60 >     * The number of chromosomes per subpop. Must be a power of two.
61       *
62       * Smaller values lead to faster iterations but poorer quality
63       * results
64       */
65 <    static final int DEFAULT_POOL_SIZE = 32;
65 >    static final int DEFAULT_SUBPOP_SIZE = 32;
66  
67      /**
68 <     * The number of iterations per task. Convergence appears
68 >     * The number of iterations per subpop. Convergence appears
69       * to be roughly proportional to #cities-squared
70       */
71      static final int DEFAULT_GENERATIONS = DEFAULT_CITIES * DEFAULT_CITIES;
72  
73      /**
74 <     * The number of pools. The total population is #pools * poolSize,
74 >     * The number of subpops. The total population is #subpops * subpopSize,
75       * which should be roughly on the order of #cities-squared
76       *
77       * Smaller values lead to faster total runs but poorer quality
78       * results
79       */
80 <    static final int DEFAULT_NPOOLS = DEFAULT_GENERATIONS / DEFAULT_POOL_SIZE;
80 >    static final int DEFAULT_NSUBPOPS = DEFAULT_GENERATIONS / DEFAULT_SUBPOP_SIZE;
81  
82      /**
83       * The minimum length for a random chromosome strand.
# Line 74 | Line 86 | public class TSPExchangerTest {
86      static final int MIN_STRAND_LENGTH = 3;
87  
88      /**
89 <     * The probablility mask value for creating random strands,
89 >     * The probability mask value for creating random strands,
90       * that have lengths at least MIN_STRAND_LENGTH, and grow
91 <     * with exposnential decay 2^(-(1/(RANDOM_STRAND_MASK + 1)
91 >     * with exponential decay 2^(-(1/(RANDOM_STRAND_MASK + 1)
92       * Must be 1 less than a power of two.
93       */
94      static final int RANDOM_STRAND_MASK = 7;
95  
96      /**
97 <     * Probablility control for selecting breeders.
97 >     * Probability control for selecting breeders.
98       * Breeders are selected starting at the best-fitness chromosome,
99 <     * with exponentially decaying probablility
100 <     * 1 / (poolSize >>> BREEDER_DECAY).
99 >     * with exponentially decaying probability
100 >     * 1 / (subpopSize >>> BREEDER_DECAY).
101       *
102       * Larger values usually cause faster convergence but poorer
103       * quality results
# Line 93 | Line 105 | public class TSPExchangerTest {
105      static final int BREEDER_DECAY = 1;
106  
107      /**
108 <     * Probablility control for selecting dyers.
108 >     * Probability control for selecting dyers.
109       * Dyers are selected starting at the worst-fitness chromosome,
110 <     * with exponentially decaying probablility
111 <     * 1 / (poolSize >>> DYER_DECAY)
110 >     * with exponentially decaying probability
111 >     * 1 / (subpopSize >>> DYER_DECAY)
112       *
113       * Larger values usually cause faster convergence but poorer
114       * quality results
# Line 104 | Line 116 | public class TSPExchangerTest {
116      static final int DYER_DECAY = 1;
117  
118      /**
107     * The probability mask for a task to give up running and
108     * resubmit itself. On each iteration, a task stops iterating
109     * and resubmits itself with probability 1 / (RESUBMIT_MASK+1).
110     * This avoids some tasks running to completion before others
111     * even start when there are more pools than threads.
112     *
113     * Must be 1 less than a power of two.
114     */
115    static final int RESUBMIT_MASK = 63;
116
117    static final boolean verbose = true;
118    static final long SNAPSHOT_RATE = 10000; // in milliseconds
119
120    /**
119       * The set of cities. Created once per program run, to
120       * make it easier to compare solutions across different runs.
121       */
122 <    static CitySet cities;
122 >    static CitySet cities;
123  
124      public static void main(String[] args) throws Exception {
125          int maxThreads = DEFAULT_MAX_THREADS;
126          int nCities = DEFAULT_CITIES;
127 <        int poolSize = DEFAULT_POOL_SIZE;
127 >        int subpopSize = DEFAULT_SUBPOP_SIZE;
128          int nGen = nCities * nCities;
129 <        int nPools = nCities * nCities / poolSize;
129 >        int nSubpops = nCities * nCities / subpopSize;
130 >        int nReps = DEFAULT_REPLICATIONS;
131  
132          try {
133              int argc = 0;
# Line 137 | Line 136 | public class TSPExchangerTest {
136                  if (option.equals("-c")) {
137                      nCities = Integer.parseInt(args[argc]);
138                      nGen = nCities * nCities;
139 <                    nPools = nCities * nCities / poolSize;
139 >                    nSubpops = nCities * nCities / subpopSize;
140                  }
141                  else if (option.equals("-p"))
142 <                    poolSize = Integer.parseInt(args[argc]);
142 >                    subpopSize = Integer.parseInt(args[argc]);
143                  else if (option.equals("-g"))
144                      nGen = Integer.parseInt(args[argc]);
145                  else if (option.equals("-n"))
146 <                    nPools = Integer.parseInt(args[argc]);
146 >                    nSubpops = Integer.parseInt(args[argc]);
147 >                else if (option.equals("-q")) {
148 >                    verbose = false;
149 >                    argc--;
150 >                }
151 >                else if (option.equals("-r"))
152 >                    nReps = Integer.parseInt(args[argc]);
153                  else
154                      maxThreads = Integer.parseInt(option);
155                  argc++;
# Line 157 | Line 162 | public class TSPExchangerTest {
162          System.out.print("TSPExchangerTest");
163          System.out.print(" -c " + nCities);
164          System.out.print(" -g " + nGen);
165 <        System.out.print(" -p " + poolSize);
166 <        System.out.print(" -n " + nPools);
165 >        System.out.print(" -p " + subpopSize);
166 >        System.out.print(" -n " + nSubpops);
167 >        System.out.print(" -r " + nReps);
168          System.out.print(" max threads " + maxThreads);
169          System.out.println();
170  
171          cities = new CitySet(nCities);
172  
173 <        for (int i = 2; i <= maxThreads; i += 2)
174 <            oneRun(i, nPools, poolSize, nGen);
173 >        if (false && NCPUS > 4) {
174 >            int h = NCPUS/2;
175 >            System.out.printf("Threads: %4d Warmup\n", h);
176 >            oneRun(h, nSubpops, subpopSize, nGen);
177 >            Thread.sleep(500);
178 >        }
179 >
180 >        int maxt = (maxThreads < nSubpops) ? maxThreads : nSubpops;
181 >        for (int j = 0; j < nReps; ++j) {
182 >            for (int i = 2; i <= maxt; i += 2) {
183 >                System.out.printf("Threads: %4d Replication: %2d\n", i, j);
184 >                oneRun(i, nSubpops, subpopSize, nGen);
185 >                Thread.sleep(500);
186 >            }
187 >        }
188      }
189  
190      static void reportUsageErrorAndDie() {
191          System.out.print("usage: TSPExchangerTest");
192          System.out.print(" [-c #cities]");
193 <        System.out.print(" [-p #poolSize]");
193 >        System.out.print(" [-p #subpopSize]");
194          System.out.print(" [-g #generations]");
195 <        System.out.print(" [-n #pools]");
195 >        System.out.print(" [-n #subpops]");
196 >        System.out.print(" [-r #replications]");
197 >        System.out.print(" [-q <quiet>]");
198          System.out.print(" #threads]");
199          System.out.println();
200          System.exit(0);
201      }
202  
203      /**
204 <     * Perform one run with the given parameters.  Each run completes
205 <     * when all but one of the tasks has finished.  The last remaining
206 <     * task may have no one left to exchange with, so the pool is
207 <     * abruptly terminated.
204 >     * Perform one run with the given parameters.  Each run complete
205 >     * when there are fewer than 2 active threads.  When there is
206 >     * only one remaining thread, it will have no one to exchange
207 >     * with, so it is terminated (via interrupt).
208       */
209 <    static void oneRun(int nThreads, int nPools, int poolSize, int nGen)
209 >    static void oneRun(int nThreads, int nSubpops, int subpopSize, int nGen)
210          throws InterruptedException {
211 <        Population p = new Population(nThreads, nPools, poolSize, nGen);
211 >        Population p = new Population(nThreads, nSubpops, subpopSize, nGen);
212          ProgressMonitor mon = null;
213          if (verbose) {
214 +            p.printSnapshot(0);
215              mon = new ProgressMonitor(p);
216              mon.start();
217          }
196        p.printSnapshot(0);
218          long startTime = System.nanoTime();
219          p.start();
220 <        p.awaitTasks();
220 >        p.awaitDone();
221          long stopTime = System.nanoTime();
222          if (mon != null)
223              mon.interrupt();
224          p.shutdown();
225 <        Thread.sleep(100);
225 >        //        Thread.sleep(100);
226  
227          long elapsed = stopTime - startTime;
228 <        long rate = elapsed / (nPools * nGen);
208 <        double secs = (double)elapsed / 1000000000.0;
228 >        double secs = (double) elapsed / 1000000000.0;
229          p.printSnapshot(secs);
210        System.out.printf("%10d ns per transfer\n", rate);
230      }
231  
232  
233      /**
234 <     * A Population creates the pools, tasks, and threads for a run
234 >     * A Population creates the subpops, subpops, and threads for a run
235       * and has control methods to start, stop, and report progress.
236       */
237      static final class Population {
238 <        final Task[] tasks;
238 >        final Worker[] threads;
239 >        final Subpop[] subpops;
240          final Exchanger<Strand> exchanger;
221        final ThreadPoolExecutor exec;
241          final CountDownLatch done;
242          final int nGen;
243 <        final int poolSize;
243 >        final int subpopSize;
244          final int nThreads;
245  
246 <        Population(int nThreads, int nPools, int poolSize, int nGen) {
246 >        Population(int nThreads, int nSubpops, int subpopSize, int nGen) {
247              this.nThreads = nThreads;
248              this.nGen = nGen;
249 <            this.poolSize = poolSize;
249 >            this.subpopSize = subpopSize;
250              this.exchanger = new Exchanger<Strand>();
251 <            this.done = new CountDownLatch(nPools-1);
252 <            this.tasks = new Task[nPools];
253 <            for (int i = 0; i < nPools; i++)
254 <                tasks[i] = new Task(this);
255 <            BlockingQueue<Runnable> tq =
256 <                new LinkedBlockingQueue<Runnable>();
257 <            this.exec = new ThreadPoolExecutor(nThreads, nThreads,
258 <                                               0L, TimeUnit.MILLISECONDS,
259 <                                               tq);
260 <            exec.prestartAllCoreThreads();
251 >            this.done = new CountDownLatch(nThreads - 1);
252 >
253 >            this.subpops = new Subpop[nSubpops];
254 >            for (int i = 0; i < nSubpops; i++)
255 >                subpops[i] = new Subpop(this);
256 >
257 >            this.threads = new Worker[nThreads];
258 >            int maxExchanges = nGen * nSubpops / nThreads;
259 >            for (int i = 0; i < nThreads; ++i) {
260 >                threads[i] = new Worker(this, maxExchanges);
261 >            }
262 >
263          }
264  
244        /** Start the tasks */
265          void start() {
266 <            for (int i = 0; i < tasks.length; i++)
267 <                exec.execute(tasks[i]);
266 >            for (int i = 0; i < nThreads; ++i) {
267 >                threads[i].start();
268 >            }
269          }
270  
271          /** Stop the tasks */
272          void shutdown() {
273 <            exec.shutdownNow();
273 >            for (int i = 0; i < threads.length; ++ i)
274 >                threads[i].interrupt();
275          }
276  
277 <        /** Called by task upon terminations */
256 <        void taskDone() {
277 >        void threadDone() {
278              done.countDown();
279          }
280  
281 <        /** Wait for (all but one) task to complete */
282 <        void awaitTasks() throws InterruptedException {
281 >        /** Wait for tasks to complete */
282 >        void awaitDone() throws InterruptedException {
283              done.await();
284          }
285  
286 <        /**
287 <         * Called by a task to resubmit itself after completing
288 <         * fewer than nGen iterations.
289 <         */
290 <        void resubmit(Task task) {
270 <            exec.execute(task);
286 >        int totalExchanges() {
287 >            int xs = 0;
288 >            for (int i = 0; i < threads.length; ++i)
289 >                xs += threads[i].exchanges;
290 >            return xs;
291          }
292  
293 +        /**
294 +         * Prints statistics, including best and worst tour lengths
295 +         * for points scaled in [0,1), scaled by the square root of
296 +         * number of points. This simplifies checking results.  The
297 +         * expected optimal TSP for random points is believed to be
298 +         * around 0.76 * sqrt(N). For papers discussing this, see
299 +         * http://www.densis.fee.unicamp.br/~moscato/TSPBIB_home.html
300 +         */
301          void printSnapshot(double secs) {
302 <            int gens = 0;
303 <            double best = Double.MAX_VALUE;
304 <            double worst = 0;
305 <            for (int k = 0; k < tasks.length; ++k) {
306 <                gens += tasks[k].gen;
307 <                Chromosome[] cs = tasks[k].chromosomes;
308 <                float b = cs[0].fitness;
309 <                if (b < best)
310 <                    best = b;
311 <                float w = cs[cs.length-1].fitness;
312 <                if (w > worst)
313 <                    worst = w;
314 <            }
315 <            int avegen = (done.getCount() == 0)? nGen : gens / tasks.length;
316 <            System.out.printf("Time:%9.3f Best:%9.3f Worst:%9.3f Gen:%6d Threads:%4d\n",
317 <                              secs, best, worst, avegen, nThreads);
318 <        }
319 <        
320 <        float averageFitness() { // currently unused
321 <            float total = 0;
322 <            int count = 0;
323 <            for (int k = 0; k < tasks.length; ++k) {
324 <                Chromosome[] cs = tasks[k].chromosomes;
325 <                for (int i = 0; i < cs.length; i++)
326 <                    total += cs[i].fitness;
327 <                count += cs.length;
302 >            int xs = totalExchanges();
303 >            long rate = (xs == 0) ? 0L : (long) ((secs * 1000000000.0) / xs);
304 >            Chromosome bestc = subpops[0].chromosomes[0];
305 >            Chromosome worstc = bestc;
306 >            for (int k = 0; k < subpops.length; ++k) {
307 >                Chromosome[] cs = subpops[k].chromosomes;
308 >                if (cs[0].fitness < bestc.fitness)
309 >                    bestc = cs[0];
310 >                int w = cs[cs.length-1].fitness;
311 >                if (cs[cs.length-1].fitness > worstc.fitness)
312 >                    worstc = cs[cs.length-1];
313 >            }
314 >            double sqrtn = Math.sqrt(cities.length);
315 >            double best = bestc.unitTourLength() / sqrtn;
316 >            double worst = worstc.unitTourLength() / sqrtn;
317 >            System.out.printf("N:%4d T:%8.3f B:%6.3f W:%6.3f X:%9d R:%7d\n",
318 >                              nThreads, secs, best, worst, xs, rate);
319 >            //            exchanger.printStats();
320 >            //            System.out.print(" s: " + exchanger.aveSpins());
321 >            //            System.out.print(" p: " + exchanger.aveParks());
322 >        }
323 >    }
324 >
325 >    /**
326 >     * Worker threads perform updates on subpops.
327 >     */
328 >    static final class Worker extends Thread {
329 >        final Population pop;
330 >        final int maxExchanges;
331 >        int exchanges;
332 >        final RNG rng = new RNG();
333 >
334 >        Worker(Population pop, int maxExchanges) {
335 >            this.pop = pop;
336 >            this.maxExchanges = maxExchanges;
337 >        }
338 >
339 >        /**
340 >         * Repeatedly, find a subpop that is not being updated by
341 >         * another thread, and run a random number of updates on it.
342 >         */
343 >        public void run() {
344 >            try {
345 >                int len = pop.subpops.length;
346 >                int pos = (rng.next() & 0x7FFFFFFF) % len;
347 >                while (exchanges < maxExchanges) {
348 >                    Subpop s = pop.subpops[pos];
349 >                    AtomicBoolean busy = s.busy;
350 >                    if (!busy.get() && busy.compareAndSet(false, true)) {
351 >                        exchanges += s.runUpdates();
352 >                        busy.set(false);
353 >                        pos = (rng.next() & 0x7FFFFFFF) % len;
354 >                    }
355 >                    else if (++pos >= len)
356 >                        pos = 0;
357 >                }
358 >                pop.threadDone();
359 >            } catch (InterruptedException fallthrough) {
360              }
301            return total/(float)count;
361          }
362      }
363  
364      /**
365 <     * A Task updates its pool of chromosomes..
365 >     * A Subpop maintains a set of chromosomes..
366       */
367 <    static final class Task implements Runnable {
368 <        /** The pool of chromosomes, kept in sorted order */
367 >    static final class Subpop {
368 >        /** The chromosomes, kept in sorted order */
369          final Chromosome[] chromosomes;
370 +        /** The parent population */
371          final Population pop;
372 <        /** The common exchanger, same for all tasks */
372 >        /** Reservation bit for worker threads */
373 >        final AtomicBoolean busy;
374 >        /** The common exchanger, same for all subpops */
375          final Exchanger<Strand> exchanger;
376          /** The current strand being exchanged */
377          Strand strand;
378          /** Bitset used in cross */
379          final int[] inTour;
380          final RNG rng;
381 <        final int poolSize;
320 <        final int nGen;
321 <        int gen;
381 >        final int subpopSize;
382  
383 <        Task(Population pop) {
383 >        Subpop(Population pop) {
384              this.pop = pop;
385 <            this.nGen = pop.nGen;
326 <            this.gen = 0;
327 <            this.poolSize = pop.poolSize;
385 >            this.subpopSize = pop.subpopSize;
386              this.exchanger = pop.exchanger;
387 +            this.busy = new AtomicBoolean(false);
388              this.rng = new RNG();
389              int length = cities.length;
390              this.strand = new Strand(length);
391              this.inTour = new int[(length >>> 5) + 1];
392 <            this.chromosomes = new Chromosome[poolSize];
393 <            for (int j = 0; j < poolSize; ++j)
392 >            this.chromosomes = new Chromosome[subpopSize];
393 >            for (int j = 0; j < subpopSize; ++j)
394                  chromosomes[j] = new Chromosome(length, rng);
395              Arrays.sort(chromosomes);
396          }
397  
398          /**
399 <         * Run one or more update cycles.
399 >         * Run a random number of updates.  The number of updates is
400 >         * at least 1 and no more than subpopSize.  This
401 >         * controls the granularity of multiplexing subpop updates on
402 >         * to threads. It is small enough to balance out updates
403 >         * across tasks, but large enough to avoid having runs
404 >         * dominated by subpop selection. It is randomized to avoid
405 >         * long runs where pairs of subpops exchange only with each
406 >         * other.  It is hardwired because small variations of it
407 >         * don't matter much.
408 >         *
409 >         * @param g the first generation to run.
410           */
411 <        public void run() {
412 <            try {
413 <                for (;;) {
414 <                    update();
415 <                    if (++gen >= nGen) {
347 <                        pop.taskDone();
348 <                        return;
349 <                    }
350 <                    if ((rng.next() & RESUBMIT_MASK) == 1) {
351 <                        pop.resubmit(this);
352 <                        return;
353 <                    }
354 <                }
355 <            } catch (InterruptedException ie) {
356 <                pop.taskDone();
357 <            }
411 >        int runUpdates() throws InterruptedException {
412 >            int n = 1 + (rng.next() & ((subpopSize << 1) - 1));
413 >            for (int i = 0; i < n; ++i)
414 >                update();
415 >            return n;
416          }
417  
418          /**
419 <         * Choose a breeder, exchange strand with another pool, and
419 >         * Choose a breeder, exchange strand with another subpop, and
420           * cross them to create new chromosome to replace a chosen
421           * dyer.
422           */
# Line 379 | Line 437 | public class TSPExchangerTest {
437           * @return index of selected breeder
438           */
439          int chooseBreeder() {
440 <            int mask = (poolSize >>> BREEDER_DECAY) - 1;
440 >            int mask = (subpopSize >>> BREEDER_DECAY) - 1;
441              int b = 0;
442              while ((rng.next() & mask) != mask) {
443 <                if (++b >= poolSize)
443 >                if (++b >= subpopSize)
444                      b = 0;
445              }
446              return b;
# Line 390 | Line 448 | public class TSPExchangerTest {
448  
449          /**
450           * Choose a chromosome that will be replaced, with
451 <         * exponentially decreasing probablility starting at
451 >         * exponentially decreasing probability starting at
452           * worst, ignoring the excluded index
453           * @param exclude index to ignore; use -1 to not exclude any
454           * @return index of selected dyer
455           */
456          int chooseDyer(int exclude) {
457 <            int mask = (poolSize >>> DYER_DECAY)  - 1;
458 <            int d = poolSize - 1;
457 >            int mask = (subpopSize >>> DYER_DECAY)  - 1;
458 >            int d = subpopSize - 1;
459              while (d == exclude || (rng.next() & mask) != mask) {
460                  if (--d < 0)
461 <                    d = poolSize - 1;
461 >                    d = subpopSize - 1;
462              }
463              return d;
464          }
465  
466 <        /**
466 >        /**
467           * Select a random strand of b's.
468           * @param breeder the breeder
469           */
# Line 450 | Line 508 | public class TSPExchangerTest {
508              int first = cs[0];
509              int j = 0;
510              int[] bs = breeder.alleles;
511 <            while (bs[j] != first)
511 >            while (bs[j] != first)
512                  ++j;
513  
514              // Append remaining b's that aren't already in tour
# Line 459 | Line 517 | public class TSPExchangerTest {
517                  int x = bs[j];
518                  if ((inTour[x >>> 5] & (1 << (x & 31))) == 0)
519                      cs[i++] = x;
520 <            }
520 >            }
521  
522          }
523  
524          /**
525           * Fix the sort order of a changed Chromosome c at position k
526           * @param c the chromosome
527 <         * @param k the index
527 >         * @param k the index
528           */
529          void fixOrder(Chromosome c, int k) {
530              Chromosome[] cs = chromosomes;
531 <            float oldFitness = c.fitness;
531 >            int oldFitness = c.fitness;
532              c.recalcFitness();
533 <            float newFitness = c.fitness;
533 >            int newFitness = c.fitness;
534              if (newFitness < oldFitness) {
535                  int j = k;
536                  int p = j - 1;
# Line 500 | Line 558 | public class TSPExchangerTest {
558          /** Index of cities in tour order */
559          final int[] alleles;
560          /** Total tour length */
561 <        float fitness;
561 >        int fitness;
562  
563 <        /**
563 >        /**
564           * Initialize to random tour
565           */
566          Chromosome(int length, RNG random) {
# Line 519 | Line 577 | public class TSPExchangerTest {
577          }
578  
579          public int compareTo(Object x) { // to enable sorting
580 <            float xf = ((Chromosome)x).fitness;
581 <            float f = fitness;
582 <            return ((f == xf)? 0 :((f < xf)? -1 : 1));
580 >            int xf = ((Chromosome) x).fitness;
581 >            int f = fitness;
582 >            return ((f == xf) ? 0 :((f < xf) ? -1 : 1));
583          }
584  
585          void recalcFitness() {
586              int[] a = alleles;
587              int len = a.length;
588              int p = a[0];
589 <            float f = cities.distanceBetween(a[len-1], p);
589 >            long f = cities.distanceBetween(a[len-1], p);
590              for (int i = 1; i < len; i++) {
591                  int n = a[i];
592                  f += cities.distanceBetween(p, n);
593                  p = n;
594              }
595 <            fitness = f;
595 >            fitness = (int) (f / len);
596 >        }
597 >
598 >        /**
599 >         * Return tour length for points scaled in [0, 1).
600 >         */
601 >        double unitTourLength() {
602 >            int[] a = alleles;
603 >            int len = a.length;
604 >            int p = a[0];
605 >            double f = cities.unitDistanceBetween(a[len-1], p);
606 >            for (int i = 1; i < len; i++) {
607 >                int n = a[i];
608 >                f += cities.unitDistanceBetween(p, n);
609 >                p = n;
610 >            }
611 >            return f;
612          }
613  
614 <        void validate() { // Ensure that this is a valid tour.
614 >        /**
615 >         * Check that this tour visits each city
616 >         */
617 >        void validate() {
618              int len = alleles.length;
619              boolean[] used = new boolean[len];
620 <            for (int i = 0; i < len; ++i)
620 >            for (int i = 0; i < len; ++i)
621                  used[alleles[i]] = true;
622 <            for (int i = 0; i < len; ++i)
622 >            for (int i = 0; i < len; ++i)
623                  if (!used[i])
624                      throw new Error("Bad tour");
625          }
# Line 550 | Line 627 | public class TSPExchangerTest {
627      }
628  
629      /**
630 <     * A Strand is a random sub-sequence of a Chromosome.  Each task
630 >     * A Strand is a random sub-sequence of a Chromosome.  Each subpop
631       * creates only one strand, and then trades it with others,
632       * refilling it on each iteration.
633       */
# Line 561 | Line 638 | public class TSPExchangerTest {
638      }
639  
640      /**
641 <     * A collection of (x,y) points that represent cities.  Distances
565 <     * are scaled in [0,1) to simply checking results.  The expected
566 <     * optimal TSP for random points is believed to be around 0.76 *
567 <     * sqrt(N). For papers discussing this, see
568 <     * http://www.densis.fee.unicamp.br/~moscato/TSPBIB_home.html for
641 >     * A collection of (x,y) points that represent cities.
642       */
643      static final class CitySet {
571        // Scale ints to doubles in [0,1)
572        static final double PSCALE = (double)0x80000000L;
644  
645          final int length;
646          final int[] xPts;
647          final int[] yPts;
648 <        final float[][] distances;
648 >        final int[][] distances;
649  
650          CitySet(int n) {
651              this.length = n;
652              this.xPts = new int[n];
653              this.yPts = new int[n];
654 <            this.distances = new float[n][n];
654 >            this.distances = new int[n][n];
655  
656              RNG random = new RNG();
657              for (int i = 0; i < n; i++) {
# Line 590 | Line 661 | public class TSPExchangerTest {
661  
662              for (int i = 0; i < n; i++) {
663                  for (int j = 0; j < n; j++) {
664 <                    double dX = (xPts[i] - xPts[j]) / PSCALE;
665 <                    double dY = (yPts[i] - yPts[j]) / PSCALE;
666 <                    distances[i][j] = (float)Math.hypot(dX, dY);
664 >                    double dx = (double) xPts[i] - (double) xPts[j];
665 >                    double dy = (double) yPts[i] - (double) yPts[j];
666 >                    double dd = Math.hypot(dx, dy) / 2.0;
667 >                    long ld = Math.round(dd);
668 >                    distances[i][j] = (ld >= Integer.MAX_VALUE) ?
669 >                        Integer.MAX_VALUE : (int) ld;
670                  }
671              }
672          }
673  
674 <        // Retrieve the cached distance between a pair of cities
675 <        float distanceBetween(int i, int j) {
674 >        /**
675 >         * Returns the cached distance between a pair of cities.
676 >         */
677 >        int distanceBetween(int i, int j) {
678              return distances[i][j];
679          }
680 +
681 +        // Scale ints to doubles in [0,1)
682 +        static final double PSCALE = (double) 0x80000000L;
683 +
684 +        /**
685 +         * Returns distance for points scaled in [0,1). This simplifies
686 +         * checking results.  The expected optimal TSP for random
687 +         * points is believed to be around 0.76 * sqrt(N). For papers
688 +         * discussing this, see
689 +         * http://www.densis.fee.unicamp.br/~moscato/TSPBIB_home.html
690 +         */
691 +        double unitDistanceBetween(int i, int j) {
692 +            double dx = ((double) xPts[i] - (double) xPts[j]) / PSCALE;
693 +            double dy = ((double) yPts[i] - (double) yPts[j]) / PSCALE;
694 +            return Math.hypot(dx, dy);
695 +        }
696 +
697      }
698  
699      /**
# Line 612 | Line 705 | public class TSPExchangerTest {
705  
706          int seed;
707          RNG(int seed) { this.seed = seed; }
708 <        RNG()         { this.seed = seedGenerator.nextInt(); }
708 >        RNG()         { this.seed = seedGenerator.nextInt() | 1;  }
709  
710          int next() {
711              int x = seed;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines