A typical application scenario of GOTO

Source: Internet
Author: User

I recently wrote a genetic algorithm that adopts a partially matched crossover strategy. Assume that the two chromosomes are:

X: 2 3 4 5 6 7 8 1

Y: 1 2 3 4 5 6 7 8

Intersection I = 2, j = 6, that is, four genes in the middle are exchanged.

X: 2 3 3 4 5 6 8 1

Y: 1 2 4 5 6 7 7 8

This will generate Duplicate Genes, so we need to map the genes outside the cross segment according to the corresponding relationship within the cross segment.

For example, for the gene 3 of the above chromosome, it is in conflict with 3 in the Cross segment and mapped to 4 in the following cross segment. After the ing, it may conflict with the genes in the Cross segment again. Therefore, you need to re-check each gene in the gene segment.

First, traverse each gene in X to check whether there is any conflicting gene in the Cross segment. If yes, perform the gene ing and execute the previous process cyclically until the conflicting gene cannot be found.

In a word, I used a four-cycle loop to implement it, and the four-loop was originally set in the Two-loop ......

Later I found that if I use Goto, I can directly express my normal thinking. I changed the 4-repeating loop to 2-repeating, which limited my thinking.

// XK> Summary

In the loop structure, you can use continue to jump out of this loop, and use break to jump out of the entire loop structure.

With Goto, you can jump to the beginning of the loop and re-execute the loop structure. Exactly meets the requirements of the above cross-matching strategy: after replacing X [1] with 4 in Y chromosomeRepeatCheck whether it is in conflict with a gene in the Cross segment.

Okay, now I am 2. in the loop, assign the variable I to 0 and then continue can re-execute the loop structure. Writing this blog is to write down how you are 2 ......

However, if you want to jump out of the multi-cycle structure, for example, you need to jump out and re-execute a multi-cycle structure, it is difficult to modify the loop variable at each layer, the break must jump out of multiple inner loops and execute a continue statement in the outermost loop. This is a typical application scenario of Goto: execute multiple loops before jumping out of the multi-cycle structure.

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.