|
SelectParents
|
|
|
In this routine, the program selects a pair of parent chromosomes from the current population, the probability of selection being an increasing function of fitness. In this case, the more fit the chromosome, the more likely it will be chosen at random if you were to randomly select a fitness value within the range of the full fitness value of a cell. The routine begins by getting a weighted sum. This is merely all of the fitness values of the chromosomes added up. Suppose you had the following chromosomes in a small 2 x 2 cell: 10101001 [ fitness: .22 ] Next, the routine selects two parents which will be used for crossover. A number is generated that is between 0 and the weighted fitness value, so using the example above we would point to some random spot between 0 and 1.13. Think of a sequential dartboard where, instead of each value 1-20 being the same size, somebody makes the 20 wedge cover half dartboard:
If you just threw a dart randomly, odds are you would hit the fatter 20 more than any other number. By analogy, the "fatter" (i.e., higher fitness value of) a chromosome, the more likely it will get selected for being a parent when the selection is random. The order of the numbers wouldn't matter either. So you could switch the 1 and 4, the 2 and 19 in the dartboard above, but so long as the throw of the dart was truly random, the odds would be the same for 1,4,2, and 19 being hit. Likewise, the order of the chromosomes doesn't matter either. The odds of more fit ones being selected are higher. Ideally, when you have chromosomes which are more fit, you want them to be selected with higher likelihood for crossover. These are the ones which are closer to your goal state. In the above example we see a chromosome with a fitness value of .40. It would have a higher chance of being selected for crossover than the others in the cell. So, we establish this weighted sum, and then point to some value within it. Since we are being impolite by pointing, let's label the number pfullSum, meaning that we are pointing at some value within the full sum. If pfullSum were, say, .24, then we would be pointing at the second chromosome, according to the example above--namely, to 10101100. Why so? Any pointing to a value of .22 or below, would fall within the scope of the first chromosomes fitness. But since this is two points over that, and well less than .52--anything over the value of .52 would fall outside the scope of the second chromosome's fitness--that means we are pointing within the fitness range of the second chromosome. As another example, if we had chosen a .74, then we would be pointing within the fourth chromosome's fitness. The GetNextCellSlot( ) routine merely moves through the chromosomes until it finds where the value is that pfullSum happens to be pointing. Each time the routine is called, it simply points to the next slot within the cell--that is, to the location of the next chromosome to be considered. Whatever chromosome that eventually is found to fall within the value of pfullSum is the one that gets to be a parent. This is done for two parents. Finally, you should note that selection is done "with replacement," meaning that the same chromosome can be selected more than once to become a parent. |