Search This Blog

Monday, February 23, 2009

Introduction to Genetic Algorithms with JGAP

Out of interest I am familiarizing myself in genetic algorithms, in short GA. My interest in GA came when I first heard about the JGAP project. As mentioned on the project's site "JGAP (pronounced "jay-gap") is a Genetic Algorithms and Genetic Programming component provided as a Java framework.". For a newcomer I found it difficult to get a good overview about all the concepts introduced in genetic algorithms. Before diving into JGAP, I think it is essential that these concepts are well understood. This post is an introduction to genetic algorithms (GA) with JGAP and is explained with a concrete example. In one of my next posts I will demonstrate solving a problem with genetic programming (GP).

So what is a genetic algorithm? Given is the following definition from John R. Koza:

The genetic algorithm is a probabilistic search algorithm that iteratively transforms a set (called a population) of mathematical objects (typically fixed-length binary character strings), each with an associated fitness value, into a new population of offspring objects using the Darwinian principle of natural selection and using operations that are patterned after naturally occurring genetic operations, such as crossover (sexual recombination) and mutation.

In genetic algorithms, a potential solution is called a chromosome. A chromosome consists of a fixed length of genes. A gene is a distinct component of a potential solution. During the evolution of the genetic algorithm, multiple solutions (chromosomes) are combined (crossover and mutation) to form, potentially, better solutions. The evolution is done over a population of solutions. The population of solutions is called a genotype and consists of a fixed-length of chromosomes. During each evolution, natural selection is applied to determine which solutions (chromosomes) make it to the next evolution. The input criteria for the selection process is the so-called fitness of a potential solution. Solutions with a better fitness value are more likely to appear in the next evolution than solutions with a worse fitness value. The fitness value of a potential solution is determined by a user-supplied fitness function.

Although it is possible to implement the above concepts yourself, JGAP already took care of this. Because the best way to learn is by example, let me first introduce the problem domain which I am going to solve with genetic algorithms. During the example, the concepts mentioned above are further clarified.

Consider a moving company which is specialized in moving boxes (with things in it) from one location to another. These boxes have varying volumes. The boxes are put in vans in which the boxes are moved from location to location. To reduce transport costs, it is crucial for the moving company to use as minimal vans as possible. Problem statement: given a number of boxes of varying volumes, what is the optimal arrangement of the boxes so that a minimal number of vans is needed? The following example shows how to solve this problem with genetic algorithms and JGAP.

First: with the arrangement of the boxes I mean the following: consider 5 boxes with the following volumes (in cubic meters): 1,4,2,2 and 2 and vans with a capacity of 4 cubic meters. When the boxes are put in the vans based on the initial arrangement, the distribution of the boxes in the vans is like this:

Van

Boxes

Space wasted

Van 1

Box 1

3

Van 2

Box 4

0

Van 3

Box 2, Box 2

0

Van 4

Box 2

2

Fitness value = 3+2 * 4 = 20. See section fitness function for an explanation of the fitness function for this particular problem.

A total of 4 vans is needed. But when the number of vans needed is calculated, which is the total volume of the boxes divided by the volume of the vans, the optimal number of vans is: 11 / 4 = 2.75. Because no partial vans can be used the optimal number of vans needed is 3. The optimal arrangement of the boxes is the following: 1,2,2,2,4. Based on this arrangement the distribution looks like this:

Van

Boxes

Space wasted

Van 1

Box 1, Box 2

1

Van 2

Box 2, Box 2

0

Van 3

Box 4

0

Fitness value of 1 * 3 = 3.

Before implementing the actual solution, the following preparatory steps must be taken. These preparatory steps are always needed if genetic algorithms is used to solve a particular problem.

Define the genetical representation of the problem domain. The boxes which must be put in the vans are represented by an array of Box instances. The genetic algorithm must find the optimal arrangement in the array as how to put the boxes in the vans. A chromosome is a potential solution and consists of a fixed-length of genes. A potential solution in this example consists of a list of indexes where each index represents a Box in the box array. To represent such index, I use an IntegerGene. As mention earlier, a gene is a distinct part of the solution. In this example, a solution (chromosome) consists of as many genes as there are boxes. The genes must be ordered by the genetic program in such a way that it represents a (near) optimal arrangement. For example: if there are 50 boxes, a chromosome with 50 IntegerGene's is constructed, where each gene's value is initialized to an index in the box array, in this case from 0 to 49.

Determine the fitness function. The fitness function determines how good a potential solution is compared to other solutions. In this problem domain, a solution is fitter when fewer vans are needed so less space is wasted.

Determine the parameters used for the run. For the run I use a population size of 50 and a total number of 5000 evolutions. So the genotype (the population) initially consists of 50 chromosomes (potential solutions). These values are chosen based on some experimentation and can vary based on the specific problem.

Determine the termination criteria. The program ends when 5000 evolutions are reached or when the optimal number of vans needed is reached. The optimal number of vans can be calculated by dividing the total volume of the boxes by the capacity of the vans and rounding the result up (because no partial vans can be used).

InitializationThe Box class has a volume. In this example 125 boxes are created with varying volumes between 0.25 and 3.00 cubic meters. The boxes are stored in an array. The following code creates the boxes:

Before we configure JGAP we must first implement a fitness function. The fitness function is the most important part in genetic algorithms as it determines which populations potentially make in to the next evolution. The fitness function for this problem looks like this:

/** * Fitness function. A lower value value means the difference between the * total volume of boxes in a van is small, which is better. This means a * more optimal distribution of boxes in the vans. The number of vans needed * is multiplied by the size difference as more vans are more expensive. */ @Override protected double evaluate(IChromosome a_subject) { double wastedVolume = 0.0D;

The above fitness function loops through all the genes in the supplied potential solution (where each gene in the chromosome represents an index in the box array) and calculates how many vans are needed for this arrangement of boxes to fit in the vans. The fitness value is based on the space wasted in every van when a new van is needed, called the wasted volume. The total volume wasted is multiplied by the number of vans needed. This is done to create a much worse fitness value when more vans are needed. In the above, simplified, example the fitness value of the first solution is 20 and the fitness value of the second, optimal, solution is 3. One term deserves more explanation and that is allele. In the above code the getAllele method on the gene is called. Allele is just another word for the value of the gene. Because all genes all IntegerGene's, the value of each gene is of type Integer.

// Only use the swapping operator. Other operations makes no sense here// and the size of the chromosome must remain constantgaConf.getGeneticOperators().clear();SwappingMutationOperator swapper = new SwappingMutationOperator(gaConf);gaConf.addGeneticOperator(swapper);

// We are only interested in the most fittest individualgaConf.setPreservFittestIndividual(true);gaConf.setKeepPopulationSizeConstant(false);

gaConf.setPopulationSize(50);// The number of chromosomes is the number of boxes we have. Every chromosome represents one box.int chromeSize = this.boxes.length;Genotype genotype;

// Setup the structure with which to evolve the solution of the problem.// An IntegerGene is used. This gene represents the index of a box in the boxes array.IChromosome sampleChromosome = new Chromosome(gaConf, new IntegerGene(gaConf), chromeSize);gaConf.setSampleChromosome(sampleChromosome);// Setup the fitness functionMoverFitnessFunction fitnessFunction = new MoverFitnessFunction();fitnessFunction.setBoxes(this.boxes);fitnessFunction.setVanCapacity(VOLUME_OF_VANS);gaConf.setFitnessFunction(fitnessFunction);

In the above code we setup the JGAP library. The provided Javadoc should be self-explanatory. A population (genotype) of 50 potential solutions (chromosomes) is created where every chromosome consists of the same number of genes as there are boxes. Because in this example a lower fitness value is better, theDeltaFitnessEvaluator is used.

Next, it is time to evolve the population. The population is evolved 5000 times. When the optimal amount of vans is reached earlier, the run ends. The following code demonstrates the evolution of the problem solution:

Because we set the preserveFittest property on the JGAP configuration object to true, we have access to the most fittest chromosome with the getFittestChromosome() method. The fittest chromosome consists of 125 genes, the indexes of the boxes in the array, in the arrangement of how to put the boxes in the vans. The actual evolution is performed by JGAP. The fitness value determines which populations have the highest chance to make it to the next evolution. Eventually a (near) optimal solution is formed. This indicates the importance of a well chosen fitness function as it is used in the selection process of the chromosomes. Below is the output of a sample run:

The total volume of the [125] boxes is [210.25989987666645] cubic metres.The optimal number of vans needed is [49]Number of evolutions [0]Fitness value [4123.992085987977]The total number of vans needed is [63]Fitness value [3458.197333300851]The total number of vans needed is [61]Fitness value [3138.2899569572887]The total number of vans needed is [60]Fitness value [2865.5105375433063]The total number of vans needed is [59]Fitness value [2562.282028584251]The total number of vans needed is [58]Fitness value [2267.7135196251966]The total number of vans needed is [57]Fitness value [1981.8050106661412]The total number of vans needed is [56]Fitness value [1704.5565017070858]The total number of vans needed is [55]Fitness value [1479.769464870246]The total number of vans needed is [54]Number of evolutions [250]Fitness value [1215.9278601031112]The total number of vans needed is [53]Fitness value [1002.6487336510297]The total number of vans needed is [52]Number of evolutions [500]Fitness value [774.5352329142294]The total number of vans needed is [51]Number of evolutions [750]Number of evolutions [1000]Fitness value [535.1696373214758]The total number of vans needed is [50]Number of evolutions [1250]Number of evolutions [1500]Number of evolutions [1750]Number of evolutions [2000]Number of evolutions [2250]Fitness value [307.8713731063958]The total number of vans needed is [49]Van [1] has contents with a total volume of [4.204196540671411] and contains the following boxes:Box:0, volume [2.2510465109421443] cubic metres.Box:117, volume [1.9531500297292665] cubic metres.Van [2] has contents with a total volume of [4.185233471369987] and contains the following boxes:Box:17, volume [1.0595047801111055] cubic metres.Box:110, volume [0.5031165156303853] cubic metres.Box:26, volume [2.6226121756284955] cubic metres.Van [3] has contents with a total volume of [4.312990612147265] and contains the following boxes:Box:91, volume [1.8897555340430203] cubic metres.Box:6, volume [2.423235078104245] cubic metres....Further output omitted

As seen in the above output, the optimal number of vans is reached between 2250 and 2500 evolutions. The program also outputs the distribution of the boxes in the vans. The complete source code of the above example can be downloaded from http://code.google.com/p/jc-examples/. The down-loadable artifact is called ga-moving-example-1.0.jar.

ConclusionGenetic algorithms and programming is an exciting technology but with a lot of concepts which are hard to grasp if you are new to the field. This post is an introduction of the concepts for genetic algorithms and showed how to implement a GA solution with JGAP. In one of my next posts, genetic programming is explained with a concrete example.

The silver bullet rule applies to genetic algorithms as well. To give you an impression, the following problems are good candidates to solve with GA:

Problems where it is hard to find a solution but once a solution is found, measure how good this particular solution is.

Problems where the search space is very large, complex or poorly understood.

Problems where a near optimal solution is acceptable.

Problems where no mathematical analysis is available.

Although I am not an expert on the subject, feel free to ask your questions and I will try to answer them as best as possible.

Good article Jamie,I am looking for solution to specific problem, and i read JGap introduction few months ago. And i think i'll be able to solve the problem ,and i got link to your blog article from JGap website...

I need help in understanding whether it is possible to use GA or not?? (i think it is not)...

Here is my problem..

Same set of events (crane movement-start & movement-stopped) is recorded by 2 different type of devices, My list of events will be like..

I'll have list for both devices which are running on difference computers..Now it is possible that one device is malfunctioning, and i want to align list of events based on data.. So i can know that event 33 of Device_A is aligned with event 66 of Device_B and then i can take time difference from aligned event and find out actual event.. (user wants to find that what is the position on crane at given time)

As I understand it correctly you have two different lists with the same type of events generated by two different devices. The event indicates the start- and stoptime of a specific action. I presume the moving speed is constant. Is this correct?

Just for my understanding: do the two computers operate different devices?

Can you show me a small example of the list of events. Based on this data can you give me an example of how to manually find the aligned event in some small amount of data? Maybe we can then figure out if we can represent this solution genetically and how to define a fitness function which determines how good this solution is. If it is not possible to define a genetical representation than GA might not be applicable for this particular problem.

Speed is not constant, but one of the device (Laser) which provides data in Millimeter precision of Crane's current position, data output speed is 1 sec. So you assume that i have data for each second about position of crane.

One device is connected to Server, and other device is connected to Crane computer. These device produce data (like Mouse,Computer) ..

After some further thoughts I don't think this is a suitable problem for solving by genetic algorithms. With this problem you have two (fixed) lists which you want to align. Even if we could form this problem to use GA, GA not always find the optimal solution. I think in this case you would want the optimal solution. Correct me if I am wrong.

Maybe you can try a graphical approach by drawing both lists on a timescale and see where they overlap.

Please add your site at http://www.sweebs.com. Sweebs.com is a place where other people can find you among the best sites on the internet!Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.

Thanks for the article. I need your advice.Basically, I have a spectrum of values, say from 2.2 - 5.8. Now, I would like to find out how many times should I divide the spectrum of values so that I could maximize the accuracy. Can I use GA to find out the optimal boundary values, say if there for example 3 intermediate values (e.g. 3.6, 4.2, 5.2)?

Thanks for your reply. Here is an example of the problem:I want to implement a gender classification. Given is a list of speakers say speaker 1 to speaker 100. Each speaker has a certain value, for example their pitch. However in a gender say for males, we have a range of pitch, lowest to highest; and same for females. With that observation, I'd would like to create groups-low, med, high-for each gender. Now, I'd like GA to give me the boundary values of pitch for each group with the maximum classification accuracy. A specific example maybe this:One of the possible range values would be:Low: 0.2 - 1.5Med: 1.51 - 3.7High: 3.71 - 5.6

Can you suggest a way how to use GA to optimize these boundary values?

As I understand it correctly the bounds must be chosen so that there are an equal number of speakers in each bound? Is this correct? Because I find this an interesting problem I created a little sample application with JGAP that finds those optimum bounds for a given number of speakers with a variety of pitches. This application should get you started on your problem. Please note that this is not production quality code, just an indication of how you can solve this specific problem with JGAP. You may need to adapt this example based on your specific needs.

The source code of the example can be found here: http://code.google.com/p/jc-examples/source/browse/#svn/trunk/pitch-classification-boundary/src

In JGAP you can specify a chromosome size which specifies the number of genes in a chromosome.

Thank you very much for taking time to even code a sample app:)Well, regarding the bounds, it is not necessary that the number of speakers be equal, rather that the accuracy of gender classification be maximum.Regarding the second one,what I'd like also to know about JGAP genes is how to control the range of the number of genes being randomized. I had an experiment before where my chromosome size was 54; I only got between 25 - 35 number of genes in a chromosome. Is there a way to increase the range, for instance, 1-54 number of genes?

The size of the population can be set with the following statement: gaConf.setPopulationSize(35); which creates 35 chromosomes. The statement:gaConf.setKeepPopulationSizeConstant(true); specifies that the number of chromosomes stays fixed on 35.

To specify the number of genes in each chromosome you create your chromosomes as follows: IChromosome sampleChromosome = new Chromosome(gaConf, new IntegerGene(gaConf), 50); which creates chromosomes with 50 genes.

Hi Jamie, I got another opportunity to use JGAP :), I have completed most of implementation of my application.

But, I wonder, if you have solution for following scenario... In our application, There are some constraints which should not be broken in any case... and others are desired (i.e. less space wasting in each truck). Is there anyway to achieve this??

Right now i am giving too much higher value to constraints which should not be broken.. and lower values to constraints which are desired...

We are developing system for stacking Circle Cylinder shape items on containers. (Like coin stack..)

And this problem have many constraints based on container type. (i.e Weight, Diameter, Type, Color)

We have defined these constraints into two categories . Hard and Soft (From analogy of JBoss Drools Solver, I tried it before JGAP)

Hard constraints are compulsory. If it does not fulfills then solution cannot be determined. Soft constraints are constraints which should be optimized (I have set to Lower score means better score)

And Input will be “List of Containers” and “List of Cylinders”. Usual input will be 10 to 30 Cylinders and 4 to 8 containersAnd Output should be filling up all the containers + [remaining unassigned cylinders] OR using all the cylinders [AND Hard constraints must be adhered and soft constrains should be near to optimum]

Right now, I am giving Hard constraint much higher fitness then soft constraint. So if ONE hard constraint is broken then fitness will be say 30000, and if ONE soft constraint is broken then fitness will be 2500

Soft Constraints1. Minimum capacity wastage of containers2. Lower diameter circle cannot be placed on higher diameter circle (i.e C3 on layer-1 and C2 on layer-2 should be avoided)3. Circles with group [A,B] or [C,D] should be kept in one base. (i.e. rather than putting C1,C3 together prefer, C1,C5 together)

So in above detail, Input is CIRCLES + CONTAINERS and output should be (it’s just a representation, not a proper solution);CON1 [0:C1, 1:C5,2:C6], (0,1,2 denotes layers)CON2 [0:C2, 1:C4,2:C7], CON3 [0:C3, 1:C5]CON4 []

In this result, CON4 will be empty.. as there are no Circles available. Sometimes in input number of circles are more around 30 and number of Containers are lesser (like 3 or 4) so, in that case.. elements list will remain unassigned.

Please provide your view and suggest other things also, I am giving 1500 NUMBER_OF_EVOLUTION and 250 SIZE_OF_POPULATION.. But after 120 to 150 number of evolutions.. I do not get better fitness chromosome.

This was a great introduction to GA's. Futhermore I appreciate theoptimization example utilizingboxes.

While reviewing the code, I noticed that there is a 'van' object that consistently is left out of the list when invokingnumberOfVansNeeded()...When # of boxes = 7, I have noticed this situation. The following method appears to resolve the problem:

December 5, 2009MargaritisHello JamieI am trying to compile and run GaHelloWold in Eclipce. I have created a project with name: helloworld, and under directory helloworld\ have created bin\ and src\ folders. Where and how must I put source files (two java files)from nl\jamiecraane\gahelloworld folder and the jgap.jar, log4j.jar files from lib\ folder?Thanks

I've been using JGAP for a time already. My question is about the number of genes generated for every chromosome. How does JGAP generates these numbers? For example, I have 54 genes but during the run I only see between 20-35 genes in every chromosome; nothing 1-20 or 35-50. Is it possible to modify the range of distribution?Hope anyone could enlighten me on this.

Using JGAP I wish to have a fixed e.g 70% x-over of population. Please let me know if a double value crossoverRatePercent = 1.2 is corresponding to 1 / 1.2 = 0.833 that is 83,3% of population is crossovered ?Also an integer value crossoverRate = 3 is corresponding to 1 / 3 = 0.33, that is 33,3% of population is crossovered ? Thanks in advance

Using JGAP I wish to have a fixed e.g 70% x-over of population. Please let me know if a double value crossoverRatePercent = 1.2 is corresponding to 1 / 1.2 = 0.833 that is 83,3% of population is crossovered ?Also an integer value crossoverRate = 3 is corresponding to 1 / 3 = 0.33, that is 33,3% of population is crossovered ? Thanks in advance

The fitness function defines how good a particular solution is compared to other solutions. In my example, the less space wasted, the better the solution.

In my example, to produce legitimate solutions, I only use the swapping operator. The genes in a solution represent an index in the array. Since the values of the genes are randomly initialized, I set the values of the genes to the array indices when I set up my initial genotype.

So to conclude: I have some constraints during the initial setup of the genotype. The fitness function evaluates the fitness of the solution. No additional constraints are implemented by the fitness function.

Thanks for the referral. I saw that the corresponding post was about generating music with GA. Have you seen my other post: http://jcraane.blogspot.nl/2009/06/melody-composition-using-genetic.html where I use simple music theory to generate a melody with GA?

Regarding the fitness function. If you want some unit input I was thinking of let the user decide which generated melodies sound good and base new populations on them.

Or maybe use a neural network to train the fitness function based on existing music.