| ||||||||||||||||
IntroductionThis article describes how to solve a logic problem using a Genetic Algorithm. It assumes no prior knowledge of GAs. In fact, half of this article is dedicated to explaining the internal structure of a Genetic Algorithm. So what is the problem domain we are trying to solve?Well, GAs can be used to solve many problems. In fact, GAs have been used to grow new mathematical syntax trees, train multi-layer neural networks, to name but a few instances. However, for this example, I have used a simple card splitting excercise, which is as detailed here:
Now, I am not saying that this could not be done by hand, using old fashioned brain juice, it's just better suited to a GA, as it could take 100s or even 1000s of different combinations to get the correct result. Well, probably not that many for this simple problem, but it certainly could take a lot of combinations for a more difficult problem. Suffice to say, it is just good fun to do it with a GA. So, let's carry on. So what is a Genetic Algorithm?Well, Wikipedia says this: A genetic algorithm is a search technique used in computing, to find true or approximate solutions to optimization and search problems, and is often abbreviated as GA. Genetic algorithms are categorized as global search heuristics. Genetic algorithms are a particular class of evolutionary algorithms that use techniques inspired by evolutionary biology such as inheritance, mutation, selection, and crossover (also called recombination). Genetic algorithms are implemented as a computer simulation in which a population of abstract representations (called chromosomes or the genotype or the genome) of candidate solutions (called individuals, creatures, or phenotypes) to an optimization problem evolves towards better solutions. Traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible. The evolution usually starts from a population of randomly generated individuals, and happens in generations. In each generation, the fitness of every individual in the population is evaluated, multiple individuals are stochastically selected from the current population (based on their fitness), and modified (recombined and possibly mutated) to form a new population. The new population is then used in the next iteration of the algorithm. Follow that?? If not, let's try a diagram. (Note that this is a Microbial GA, there are lots of GA types, but I just happen to like this one, and it's the one this article uses.)
I prefer to think of a GA as a way of really quickly (well, may be quite slow, depending on the problem) trying out some evolutionary programming techniques, that mother nature has always had. So how does this translate into an algorithm (this article uses a Microbial GA, but there are many other varieties)?The basic operation of the Microbial GA training is as follows:
That is:
That's it. That is the complete algorithm. But there are some essential issues to be aware of, when playing with GAs:
These two items must be developed again, whenever a new problem is specified. For example, if we wanted to find a person's favourite pizza toppings, the genotype and fitness would be different from that which is used for this article's problem domain. These two essential elements of a GA (for this article's problem domain) are specified below. 1. The Geneotype
Well, for this article, the problem domain states that we have 10 cards. So, I created a two dimensional genes array, which is a 30*10 array. The 30 represents a population size of 30. I picked this. It could be any size, but should be big enough to allow some dominant genes to form. 2. The Fitness FunctionRemembering that the problem domain description stated the following:
Well, all that is being done is the following :
Using the codeThe demo project attached actually contains a Visual Studio 2005 solution, with the following two classes. Program classIs the main entry point into the Simple_GeneticAlgorithm application. All this class does is create a new
Simple_GeneticAlgorithm classRuns the GA to solve the problem domain.
The results | ||||||||||||||||
![]() | I am currently doing an MSc at Sussex University in Information Technology for E-Commerce (ITEC). I currently hold a 1st class (or summa cum laude, if you like that sort of thing) BSc Hons degree from Sussex University, in Computer Science & Artificial Intelligence. Award(s) I was awarded the "Best IT project for 2006" award, at Sussex University, for my final year degree project, by the British Computer Society. 1st Place for C# monthly code project competition for this article, in March 2007. Almost But Not Quite Award(s) 2nd Place for ASP .NET monthly code project competition for this article, in January 2007. The winner was Omar Al Zabir, for this article. 2nd Place for C# monthly code project competition for this article, in December 2006. The winner was Omar Al Zabir, for this article. 2nd Place for C# monthly code project competition for this article, in November 2006. The winner was Andrew Kirillov, for this article. Interests I am quite interested in AI / computer vision / C# / web development / imaging / compilers and reflection. I also enjoy looking at new technologies such as LINQ / WPF. I just enjoy the learning process Final Words I would encourage people to write articles for codeproject, as having a real project is a really good way to pick up new skills. It has certainly helped me pick up new ideas, and new coding concepts. Go for it My Blog sachabarber.net Click here to view Sacha Barber's online profile. |
本文介绍了一种使用遗传算法解决特定逻辑问题的方法,通过模拟自然选择过程来寻找最优解。以10张编号从1到10的卡片为例,目标是将其分为两堆,使一堆的总和接近36,另一堆的乘积接近360。








2677

被折叠的 条评论
为什么被折叠?



