Introduction to genetic algorithms-serialization 7

Source: Internet
Author: User

Serialization 7

The era of algorithm Evolution Epoch

3.4.2 epoch (times)

The epoch () method is the most popular population in the genetic algorithm class. This is the cycle of the genetic algorithm we mentioned in section 3.3. It is the main workplace of this class. This method is more or less related to all work. Next let's take a closer look at it...
Void cgabob: epoch ()
{
Updatefitnessscores ();
The first thing to do in each epoch cycle is to test the adaptive scores of each member in the chromosome group. Updatefitnessscores () is a function used to decode the binary chromosome codes of each genome. It then decodes a series of results, that is, an integer representing the east, south, west, and north directions is sent to cbobsmap: testroute. Latter checkBobIn the GEO chartBobReturns an adaptive score based on the final distance from the exit. Let me tell you how to calculate with few lines of source codeBobAdaptive score:
Int diffx = ABS (posx-m_iendx );
Int diffy = ABS (posy-m_iendy );
Here, diffx and diffy areBobThe horizontal and vertical deviation between the grid and the exit of the maze. Test the example in Figure 3.6. Gray cell representativesBobThrough the maze, it says that Mr. B's cell is the final place he finally arrived. In this position, diffx = 3, while diffy = 0.

 


Figure 3.6 Bob tries to find the maze exit

Return 1/(double) (diffx + diffy + 1 );

The last row is the calculation.BobAdaptive score. It adds the diffx and diffy numbers and calculates the reciprocal. A 1 value is added to the sum of diffx and diffy to ensure that there is no denominator error in Division. IfBobReach the exit, diffx + diffy = O. Updatefitnessscores also maintains the tracking of the genome that adapts to the highest scores in each generation, and the adaptive scores associated with all base groups. These values must be used when making a bet.
Now that you know all the work done by the updatefitnessscores () function, let's go back to the epoch function discussion. since a new genomic group needs to be created in each epoch, when they are created (two genomics are created each time), we need to find some places to save them.
// Create a new group
Int newbabies = 0; // creates a memory for the baby genome
Vector <sgenome> vecbabygenomes;
We will continue to discuss the various transactions processed in the genetic algorithm loop.
While (newbabies <m_ipopsize)
{// Use the round robin method to select two upper generation (parents)
Sgenome mum = roulettewheelselection ();
Sgenome dad = roulettewheelselection ();
During each iteration, We need to select two genomics as the first generation of the chromosomes of two newborn babies. In the future, I often like to name these two elders dad and mum because they will have children in the future ). You should recall that the higher the adaptability of a genome, the higher the chance of choosing as a parent by means of gambling.
// Hybrid Operation
Sgenome baby1, baby2;
Crossover (Mum. vecbits, Dad. vecbits, baby1.vecbits, baby2.vecbits );
The work of the above two rows is: create two blank genome, which is two infants; they are passed to the crossbreeding function crossover () together with the selected upper generation (). This function performs hybridization (depending on the established hybridization rate m_dcrossoverrate) and stores the binary string of the new chromosome into two newborn babies baby1 and baby2.
// Mutations
Mutate (baby1.vecbits );
Mutate (baby2.vecbits );
The above two steps are sudden changes to the baby! It sounds terrible, but it is good for them. The mutation probability of a baby's location depends on the selected parameter m_dmutationrate (mutation rate ).
// Add two new babies to the new group
Vecbabygenomes. push_back (baby1 );
Vecbabygenomes. push_back (baby2 );
Newbabies + = 2;
}
These two new child generations will eventually join the new group, thus completing a loop iteration process. This process must be repeated until the total number of created offspring is the same as that of the initial group.
// Copy all babies to the initial group
M_vecgenomes = vecbabygenomes; // Add 1 To the count of the Generation
++ M_igeneration;
}
Here, the original group is replaced by a group composed of the new generation, and the generation counter is added to 1 to track the current generation. That's all! Haha, isn't it hard? This epoch function will repeat endlessly until the chromosome converges to a solution, or until the user asks to stop. I will show you the code for the above operations (operators), but let's talk about how to determine the parameter values.

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.