1 |
|
/* |
2 |
|
* Written by Doug Lea and Bill Scherer with assistance from members |
3 |
|
* of JCP JSR-166 Expert Group and released to the public domain, as |
4 |
< |
* explained at http://creativecommons.org/licenses/publicdomain |
4 |
> |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.*; |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* Perform one run with the given parameters. Each run complete |
204 |
> |
* Performs one run with the given parameters. Each run completes |
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). |
225 |
|
// Thread.sleep(100); |
226 |
|
|
227 |
|
long elapsed = stopTime - startTime; |
228 |
< |
double secs = (double)elapsed / 1000000000.0; |
228 |
> |
double secs = (double) elapsed / 1000000000.0; |
229 |
|
p.printSnapshot(secs); |
230 |
|
} |
231 |
|
|
300 |
|
*/ |
301 |
|
void printSnapshot(double secs) { |
302 |
|
int xs = totalExchanges(); |
303 |
< |
long rate = (xs == 0)? 0L : (long)((secs * 1000000000.0) / xs); |
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) { |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* A Subpop maintains a set of chromosomes.. |
365 |
> |
* A Subpop maintains a set of chromosomes. |
366 |
|
*/ |
367 |
|
static final class Subpop { |
368 |
|
/** The chromosomes, kept in sorted order */ |
406 |
|
* other. It is hardwired because small variations of it |
407 |
|
* don't matter much. |
408 |
|
* |
409 |
< |
* @param g the first generation to run. |
409 |
> |
* @param g the first generation to run |
410 |
|
*/ |
411 |
|
int runUpdates() throws InterruptedException { |
412 |
|
int n = 1 + (rng.next() & ((subpopSize << 1) - 1)); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
< |
* Choose a breeder, exchange strand with another subpop, and |
420 |
< |
* cross them to create new chromosome to replace a chosen |
419 |
> |
* Chooses a breeder, exchanges strand with another subpop, and |
420 |
> |
* crosses them to create new chromosome to replace a chosen |
421 |
|
* dyer. |
422 |
|
*/ |
423 |
|
void update() throws InterruptedException { |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
< |
* Choose a breeder, with exponentially decreasing probability |
435 |
> |
* Chooses a breeder, with exponentially decreasing probability |
436 |
|
* starting at best. |
437 |
|
* @return index of selected breeder |
438 |
|
*/ |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
< |
* Choose a chromosome that will be replaced, with |
450 |
> |
* Chooses a chromosome that will be replaced, with |
451 |
|
* exponentially decreasing probability starting at |
452 |
< |
* worst, ignoring the excluded index |
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 |
|
*/ |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
< |
* Copy current strand to start of c's, and then append all |
487 |
> |
* Copies current strand to start of c's, and then appends all |
488 |
|
* remaining b's that aren't in the strand. |
489 |
|
* @param breeder the breeder |
490 |
|
* @param child the child |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* Fix the sort order of a changed Chromosome c at position k |
525 |
> |
* Fixes the sort order of a changed Chromosome c at position k. |
526 |
|
* @param c the chromosome |
527 |
|
* @param k the index |
528 |
|
*/ |
561 |
|
int fitness; |
562 |
|
|
563 |
|
/** |
564 |
< |
* Initialize to random tour |
564 |
> |
* Initializes to random tour. |
565 |
|
*/ |
566 |
|
Chromosome(int length, RNG random) { |
567 |
|
alleles = new int[length]; |
577 |
|
} |
578 |
|
|
579 |
|
public int compareTo(Object x) { // to enable sorting |
580 |
< |
int xf = ((Chromosome)x).fitness; |
580 |
> |
int xf = ((Chromosome) x).fitness; |
581 |
|
int f = fitness; |
582 |
< |
return ((f == xf)? 0 :((f < xf)? -1 : 1)); |
582 |
> |
return ((f == xf) ? 0 :((f < xf) ? -1 : 1)); |
583 |
|
} |
584 |
|
|
585 |
|
void recalcFitness() { |
592 |
|
f += cities.distanceBetween(p, n); |
593 |
|
p = n; |
594 |
|
} |
595 |
< |
fitness = (int)(f / len); |
595 |
> |
fitness = (int) (f / len); |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* Return tour length for points scaled in [0, 1). |
599 |
> |
* Returns tour length for points scaled in [0, 1). |
600 |
|
*/ |
601 |
|
double unitTourLength() { |
602 |
|
int[] a = alleles; |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
< |
* Check that this tour visits each city |
615 |
> |
* Checks that this tour visits each city. |
616 |
|
*/ |
617 |
|
void validate() { |
618 |
|
int len = alleles.length; |
661 |
|
|
662 |
|
for (int i = 0; i < n; i++) { |
663 |
|
for (int j = 0; j < n; j++) { |
664 |
< |
double dx = (double)xPts[i] - (double)xPts[j]; |
665 |
< |
double dy = (double)yPts[i] - (double)yPts[j]; |
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; |
668 |
> |
distances[i][j] = (ld >= Integer.MAX_VALUE) ? |
669 |
> |
Integer.MAX_VALUE : (int) ld; |
670 |
|
} |
671 |
|
} |
672 |
|
} |
673 |
|
|
674 |
|
/** |
675 |
< |
* Returns the cached distance between a pair of cities |
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; |
682 |
> |
static final double PSCALE = (double) 0x80000000L; |
683 |
|
|
684 |
|
/** |
685 |
< |
* Return distance for points scaled in [0,1). This simplifies |
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; |
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 |
|
|
705 |
|
|
706 |
|
int seed; |
707 |
|
RNG(int seed) { this.seed = seed; } |
708 |
< |
RNG() { this.seed = seedGenerator.nextInt() | 1; } |
708 |
> |
RNG() { this.seed = seedGenerator.nextInt() | 1; } |
709 |
|
|
710 |
|
int next() { |
711 |
|
int x = seed; |