/* * GENESIS Copyright (c) 1986, 1990 by John J. Grefenstette * This program may be freely copied for educational * and research purposes. All other rights reserved. * * file: select.c * * purpose: choose a new population * * modified: 10 sep 90: include ranking option, handle max/min option. * 09 oct 90: emulate steady state when gapsize is 2/Popsize. */ #include "extern.h" Select() { static firstflag = 1; /* indicates first execution */ static int *sample; /* pointers to Selected structures */ double expected; /* expected number of offspring */ double factor; /* normalizer for expected value */ double perf; /* next best perf (for ranking) */ double ptr; /* determines fractional selection */ double rank_max; /* max number of offspring under ranking */ double sum; /* control for selection loop */ int best; /* index of next best structure */ register int i; /* loop control */ register int j; /* loop control */ register int k; /* loop control */ register int temp; /* used for swapping pointers */ Trace("Select entered"); Dtrace("select"); if (firstflag) { sample = (int *) calloc((unsigned) Popsize, sizeof(int)); firstflag = 0; } if (Rankflag) { /* Assign each structure its rank within the population. */ /* rank = Popsize-1 for best, rank = 0 for worst */ /* Use the Needs_evaluation field to store the rank */ /* clear the rank fields */ for (i=0; i Worst) expected = (Old[i].Perf - Worst) * factor; else expected = 0.0; } else { if (Old[i].Perf < Worst) expected = (Worst - Old[i].Perf) * factor; else expected = 0.0; } } for (sum += expected; sum > ptr; ptr++){ sample[k++] = i; } } if (k != Popsize) { printf("select: Help! %d samples selected instead of %d\n", k, Popsize); abort(); } /* randomly shuffle pointers to new structures */ for (i=0; i