Finding the Maximum Value of a Function
x += base; }
base = 2; }
After summing up all on bits times their base
2
value, we need to normalize what is an integer in the range of [0,1023] to a floating point number in the approximate
range of [0, 10]: x = 102.4f;
return x; }
Note that we do not need the reverse method We use the GA library from Section 6.2 to create a population of 10-bit chromosomes; in order to evaluate the fitness
of each chromosome in a population, we only have to convert the 10-bit represen- tation to a floating-point number for evaluation using the following fitness function
Equation 6.1:
private float fitnessfloat x { return floatMath.sinx
Math.sin0.4f x Math.sin3.0f x;
} Table 6.1 shows some sample random chromosomes and the floating point numbers
that they encode. The first column shows the gene indices where the bit is “on,” the second column shows the chromosomes as an integer number represented in binary
notation, and the third column shows the floating point number that the chromosome encodes. The center column in Table 6.1 shows the bits in order where index 0 is the
left-most bit, and index 9 if the right-most bit; this is the reverse of the normal order for encoding integers but the GA does not care: it works with any encoding we use.
Once again, GAs work with encodings.
“On bits” in chromosome As binary
Number encoded 2, 5, 7, 8, 9
0010010111 9.1015625
0, 1, 3, 5, 6 1101011000
1.0449219 0, 3, 5, 6, 7, 8
1001011110 4.7753906
Table 6.1: Random chromosomes and the floating point numbers that they encode Using methods geneT oF loat and f itness we now implement the abstract method
calcF itness from our GA library class Genetic so the derived class T estGenetic
106
is not abstract. This method has the responsibility for calculating and setting the fitness value for every chromosome stored in an instance of class Genetic:
public void calcFitness { for int i=0; inumChromosomes; i++ {
float x = geneToFloati; chromosomes.geti.setFitnessfitnessx;
} }
While it was useful to make this example more clear with a separate geneT oF loat method, it would have also been reasonable to simply place the formula in the
method f itness in the implementation of the abstract in the base class method calcF itness.
In any case we are done with coding this example: you can compile the two example Java files Genetic.java and TestGenetic.java, and run the T estGenetic class to verify
that the example program quickly finds a near maximum value for this function.
You can try setting different numbers of chromosomes in the population and try setting non-default crossover rates of 0.85 and a mutation rates of 0.3. We will look
at a run with a small number of chromosomes in the population created with:
genetic_experiment = new MyGenetic10, 20, 0.85f, 0.3f;
int NUM_CYCLES = 500; for int i=0; iNUM_CYCLES; i++ {
genetic_experiment.evolve; if iNUM_CYCLES5==0 || i==NUM_CYCLES-1 {
System.out.printlnGeneration + i; genetic_experiment.print;
} }
In this experiment 85 of chromosomes will be “sliced and diced” with a crossover operation and 30 will have one of their genes changed. We specified 10 bits per
chromosome and a population size of 20 chromosomes. In this example, I have run 500 evolutionary cycles. After you determine a fitness function to use, you
will probably need to experiment with the size of the population and the crossover and mutation rates. Since the simulation uses random numbers and is thus non-
deterministic, you can get different results by simply rerunning the simulation. Here is example program output with much of the output removed for brevity:
count of slots in roulette wheel=55
107
Generation 0 Fitness for chromosome 0 is 0.505, occurs at x=7.960
Fitness for chromosome 1 is 0.461, occurs at x=3.945 Fitness for chromosome 2 is 0.374, occurs at x=7.211
Fitness for chromosome 3 is 0.304, occurs at x=3.929 Fitness for chromosome 4 is 0.231, occurs at x=5.375
... Fitness for chromosome 18 is -0.282 occurs at x=1.265
Fitness for chromosome 19 is -0.495, occurs at x=5.281 Average fitness=0.090 and best fitness for this
generation:0.505 ...
Generation 499 Fitness for chromosome 0 is 0.561, occurs at x=3.812
Fitness for chromosome 1 is 0.559, occurs at x=3.703 ...
This example is simple but is intended to be show you how to encode parameters for a problem where you want to search for values to maximize a fitness function that
you specify. Using the library developed in this chapter you should be able to set up and run a GA simulation for your own applications.
108
7 Neural Networks
Neural networks can be used to efficiently solve many problems that are intractable or difficult using other AI programming techniques. I spent almost two years on a
DARPA neural network tools advisory panel, wrote the first version of the ANSim neural network product, and have used neural networks for a wide range of applica-
tion problems radar interpretation, bomb detection, and as controllers in computer games. Mastering the use of simulated neural networks will allow you to solve
many types of problems that are very difficult to solve using other methods.
Although most of this book is intended to provide practical advice with some the- oretical background on using AI programming techniques, I cannot imagine being
interested in practical AI programming without also wanting to think about the phi- losophy and mechanics of how the human mind works. I hope that my readers share
this interest.
In this book, we have examined techniques for focused problem solving, concentrat- ing on performing one task at a time. However, the physical structure and dynamics
of the human brain is inherently parallel and distributed [Parallel Distributed Pro- cessing: Explorations in the Microstructure of Cognition
, Rumelhart, McClelland, etc. 1986]. We are experts at doing many things at once. For example, I simul-
taneously can walk, talk with my wife, keep our puppy out of cactus, and enjoy the scenery behind our house in Sedona, Arizona. AI software systems struggle to
perform even narrowly defined tasks well, so how is it that we are able to simulta- neously perform several complex tasks? There is no clear or certain answer to this
question at this time, but certainly the distributed neural architecture of our brains is a requirement for our abilities. Unfortunately, artificial neural network simulations
do not currently address “multi-tasking” other techniques that do address this issue are multi-agent systems with some form or mediation between agents.
Also interesting is the distinction between instinctual behavior and learned behavior. Our knowledge of GAs from Chapter 6 provides a clue to how the brains of espe-
cially lower order animals can be hardwired to provide efficient instinctual behavior under the pressures of evolutionary forces i.e., likely survival of more fit individ-
uals. This works by using genetic algorithms to design specific neural wiring. I have used genetic algorithms to evolve recurrent neural networks for control ap-
plications. This work only had partial success but did convince me that biological genetic pressure is probably adequate to “pre-wire” some forms of behavior in nat-
ural biological neural networks.
109
Figure 7.1: Physical structure of a neuron While we will study supervised learning techniques in this chapter, it is possible to
evolve both structure and attributes of neural networks using other types of neural network models like Adaptive Resonance Theory ART to autonomously learn to
classify learning examples without intervention.
We will start this chapter by discussing human neuron cells and which features of real neurons that we will model. Unfortunately, we do not yet understand all of the
biochemical processes that occur in neurons, but there are fairly accurate models available web search “neuron biochemical”. Neurons are surrounded by thin hair-
like structures called dendrites which serve to accept activation from other neurons. Neurons sum up activation from their dendrites and each neuron has a threshold
value; if the activation summed over all incoming dendrites exceeds this threshold, then the neuron fires, spreading its activation to other neurons. Dendrites are very
localized around a neuron. Output from a neuron is carried by an axon, which is thicker than dendrites and potentially much longer than dendrites in order to affect
remote neurons. Figure 7.1 shows the physical structure of a neuron; in general, the neuron’s axon would be much longer than is seen in Figure 7.1. The axon terminal
buttons transfer activation to the dendrites of neurons that are close to the individual button. An individual neuron is connected to up to ten thousand other neurons in
this way.
The activation absorbed through dendrites is summed together, but the firing of a neuron only occurs when a threshold is passed.