AI Programming in Games (i) Introduction of the Genetic algorithm __ algorithm

Source: Internet
Author: User
As the first of the AI programming in the game, this paper mainly introduces the genetic algorithm and uses the genetic algorithm to calculate the path.

Introduction of genetic algorithm
Before continuing to read the following, please make sure you know what is genetic, mutation, chromosome and other biological knowledge. If you do not understand this knowledge, I suggest you look at the high school biology textbook. Of course, you don't have to be proficient in this area of knowledge. Well, if you're ready, let's get started.
The process of genetic algorithm is actually the imitation of the evolution of nature. First, you need to define an encoding for the solution that represents the problem, just like a chromosome (remember that there are four species of chromosomes, 22 pairs, combined into a double helix structure, which is the coded form of chromosomes). Then you need to initialize some chromosomes, each of which represents a candidate for the solution. These initialized chromosomes can be random. Then let the chromosomes begin to multiply and evolve (with some of these mutations in the middle). After evolution, it is possible to find the optimal solution (or more appropriate solution).
Genetic algorithm can not guarantee the optimal solution, it seems that this is not the advantage of genetic algorithm. But the computational process of genetic algorithm may be highly efficient. However, the most important thing is that the solution process does not need to know the process of the solution, just need to know how to encode the solution, decode the solution, and verify the rationality of the solution.

1. Chromosome chromosomes generally use binary encoding, as follows: 01010010100101001111 Importantly, this chromosome can be decoded to represent a solution. This can be a very bad solution (usually some of the first chromosomes). But each chromosome represents a possible solution.

2. Solution steps
Loop through the following steps until you find the solution:
1. Test each chromosome to mark the fraction of the solution of each chromosome. (i.e. the degree of suitability of the solution)
2. Find out two of these solutions (using the algorithm to ensure that the highest score of the chromosome optimal may be selected), you can use roulette wheel selection.
3. Cross two chromosomes
4. Appropriate mutation processing
5. Repeat 2 3 4 until a new mass of chromosomes is generated (the number can be set by itself).

3. What is roulette wheel selection when choosing a chromosome, the higher the chromosome score, the easier it is to be selected. Imagine a pie chart (pie chart), where the higher the score the larger the area of the chromosome, and then random selection. Specific algorithm after the article will be mentioned.

3. Crossover This is a way to produce a new chromosome from two chromosomes, such as I use the simplest algorithm to reverse a chromosome at 10, so that two new chromosomes can be produced.
Old chromosome:
a:10001001110010010
b:01010001001000011
New chromosome: (Note that I added a space)
c:100010011 01000011
d:010100010 10010010

4. Summing up this, the basic genetic algorithm is finished. The next step will be to address specific issues.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.