Theory Practical Artificial Intelligence Programming With Java

Figure 6.1: The test function evaluated over the interval [0.0, 10.0]. The maximum value of 0.56 occurs at x=3.8 tion, and Machine Learning , David Goldberg, 1989. • Provide a numeric fitness function that allows us to rate the fitness of each chromosome in a population. We will use these fitness values to determine which chromosomes in the population are most likely to survive and repro- duce using genetic crossover and mutation operations. The GA toolkit developed in this chapter treats genes as a single bit; while you can consider a gene to be an arbitrary data structure, the approach of using single bit genes and specifying the number of genes or bits in a chromosome is very flexible. A population is a set of chromosomes. A generation is defined as one reproductive cycle of replacing some elements of the chromosome population with new chromosomes produced by using a genetic crossover operation followed by optionally mutating a few chromosomes in the population. We will describe a simple example problem in this section, write a general purpose library in Section 6.2, and finish the chapter in Section 6.3 by solving the problem posed in this section. For a sample problem, suppose that we want to find the maximum value of the function F with one independent variable x in Equation 6.1 and as seen in Figure 6.1: F x = sinx ∗ sin0.4 ∗ x ∗ sin3 ∗ x 6.1 The problem that we want to solve is finding a good value of x to find a near to possible maximum value of F x. To be clear: we encode a floating point number 100 Figure 6.2: Crossover operation as a chromosome made up of a specific number of bits so any chromosome with randomly set bits will represent some random number in the interval [0, 10]. The fitness function is simply the function in Equation 6.1. Figure 6.2 shows an example of a crossover operation. A random chromosome bit index is chosen, and two chromosomes are “cut” at this this index and swap cut parts. The two original chromosomes in generation n are shown on the left of the figure and after the crossover operation they produce two new chromosomes in generation n+1 shown on the right of the figure. In addition to using crossover operations to create new chromosomes from existing chromosomes, we will also use genetic mutation: randomly flipping bits in chromo- somes. A fitness function that rates the fitness value of each chromosome allows us to decide which chromosomes to discard and which to use for the next genera- tion: we will use the most fit chromosomes in the population for producing the next generation using crossover and mutation. We will implement a general purpose Java GA library in the next section and then solve the example problem posed in this section in Section 6.3.

6.2 Java Library for Genetic Algorithms

The full implementation of the GA library is in the Java source file Genetic.java. The following code snippets shows the method signatures defining the public API for the library; note that there are two constructors, the first using default values for the fraction of chromosomes on which to perform crossover and mutation operations and the second constructor allows setting explicit values for these parameters: abstract public class Genetic { public Geneticint num_genes_per_chromosome, 101 int num_chromosomes public Geneticint num_genes_per_chromosome, int num_chromosomes, float crossover_fraction, float mutation_fraction The method sort is used to sort the population of chromosomes in most fit first order. The methods getGene and setGene are used to fetch and change the value of any gene bit in any chromosome. These methods are protected but you will probably not need to override them in derived classes. protected void sort protected boolean getGeneint chromosome, int gene protected void setGeneint chromosome, int gene, int value protected void setGeneint chromosome, int gene, boolean value The methods evolve, doCrossovers, doM utations, and doRemoveDuplicates are utilities for running GA simulations. These methods are protected but you will probably not need to override them in derived classes. protected void evolve protected void doCrossovers protected void doMutations protected void doRemoveDuplicates When you subclass class Genetic you must implement the following abstract method calcF itness that will determine the evolution of chromosomes during the GA sim- ulation. Implement the following method in sub-classes: abstract public void calcFitness; } The class Chromosome represents a bit set with a specified number of bits and a floating point fitness value. 102