@ 初步算法,有待完善,仅供参考,转载请附原文链接。
遗传算法:龟纹图问题
1、Introduction
Jisuwimundo is a hexagonal tortoise problem designed by Chosun scholar Choi Seok-jeong. The tortoise is a shape in which hexagons are interlocked with each other. There is a condition that a ruler is placed so that the sum of the corner numbers of the hexagons is equal. In the case of an exponential return figure with a number of N, numbers from 1 to N are placed. The objective of the problem is that the exponential return problem is an algorithm that cannot be solved within polynomial (NP-hard). Therefore, there are effective genetic algorithms that search for limited space.
The characteristics of hexagonal tortoise problem:
1) Parameters for each corner of the hexagon;
2) The sum of each hexagon is equal.
The Figure 1.1 is the 3 × 3 hexagon tortoise.
2、project process
a) System structure
Since this problem is NP-hard, it is solved using genetic algorithms. The overall system architecture is shown in Figure 2.1. First, create the population, then calculate the fitness, select the parents in the population, cross between the selected parents, and ensure the diversity of the population through mutation and substitution. Finally, the judgment is made by the termination condition. If the condition is met, it is terminated and output. If it is not met, the fitness assessment is repeated until the result appears.
This part of coding is shown in thiss:
int main(void)
{
int seed;
int i, j;
seed = time(NULL);
srand(seed);
MakePopulation(valuesOfParents);
// Show(valuesOfParents);
Fitness();
for(i = 0; i < 10000000; i++)
{
for(j = 0; j < CHILDRENNUM; j++)
{
parentOne = Selection();
parentTwo = Selection();
valuesOfChildren = pmxCrossOver(parentOne, parentTwo);
valuesOfChildren.parent1 = parentOne;
valuesOfChildren.parent2 = parentTwo;
valuesOfChildren = MakeSixAngle(valuesOfChildren);
valuesOfChildren = Mutation(valuesOfChildren);
valuesOfChildren = MakeSixAngle(valuesOfChildren);
children[j] = valuesOfChildren;
}
Replacement();
Fitness();
count++;
Show(valuesOfChildren);
if(valuesOfChildren.dispersion == 0)
{
printf("\n finished! \n");
ResultShow(valuesOfChildren);
break;
}
}
return 0;
}
b) Representation
In the implementation of the genetic algorithm, the first problem is to code the problem reasonably. Translate into digital problems. Here, a locus-based encoding method is adopted. This method is shown in the Figure 2.3.
This part of conding is shown:
//*********制造出不同的数字*****************//
turtle MakeNumber(void)
{
int i, j;
for(i = 0; i < NODENUM; i++)
{
valuesOfParents.nodes[i] = rand()%NODENUM + 1; //产生0~nodenum个随机整数
for(j = 0; j < i; j++)
{
//*************是的所有节点具有不同的值*******************//
if(valuesOfParents.nodes[i]