遗传算法解决简单优化问题的实例

  • 格式:txt
  • 大小:6.90 KB
  • 文档页数:3
void SwapIndividual(Individual *a, Individual *b);
int FindBestIndividual(void);
int FindWorstIndividual(void);
double CalculateAverageFitness(void);
Select();
Crossover();
Mutate();
#ifdef DEBUG
// PrintPopulation(i);
PrintOutlineInfo(i);
#endif // DEBUG
}
CalculateFitness();
int best = FindBestIndividual();
{
// 求出种群所有个体的适应度之和
double fitnessSum = 0.0;
for (int i=0; i<populationSize; i++)
fitnessSum += population[i].fitness;
பைடு நூலகம்
// 计算累积概率
double *cumulativeProbability = new double[populationSize];
int t = population[i].chromosome[pos];
population[i].chromosome[pos] = population[i+1].chromosome[pos];
population[i+1].chromosome[pos] = t;
{
srand((unsigned)time(NULL));
// 重排种群中个体的顺序,模拟随机配对过程
for (int k=0; k<populationSize/2; k++) // 配对次数等于种群规模除以2
{
int i = rand() % populationSize;
a->fitness = b->fitness;
b->fitness = t;
}
// 找到最好的个体,返回它的下标
int FindBestIndividual(void)
{
int bestIndex = 0;
for (int i=1; i<populationSize; i++)
void Crossover(void);
void Mutate(void);
void Encode(int x, int *chromosome);
int Decode(const int *chromosome);
void CalculateFitness(void);
void CopyIndividual(Individual *dest, const Individual *source);
cumulativeProbability[0] = population[0].fitness / fitnessSum;
for (int i=1; i<populationSize; i++)
cumulativeProbability[i] = cumulativeProbability[i-1] + population[i].fitness / fitnessSum;
int j = rand() % populationSize;
if (i != j)
{
SwapIndividual(&population[i], &population[j]);
}
} // for
// 相邻两个个体以交叉概率pc进行单点交叉
for (int i=0; i<populationSize-1; i+=2)
delete population;
population = newPopulation;
// 删除存放累积概率的数组cumulativeProbability
delete cumulativeProbability;
}
// 交叉操作
void Crossover(void)
{
int chromosome[CHROMOSOME_LENGTH]; // 基因型
int fitness; // 适应度
} Individual;
void GenerateInitialPopulation(void);
void Select(void);
{
for (int p=0; p<CHROMOSOME_LENGTH; p++)
{
dest->chromosome[p] = source->chromosome[p];
}
dest->fitness = source->fitness;
}
// 交换两个个体
void SwapIndividual(Individual *a, Individual *b)
#ifdef DEBUG
CalculateFitness();
// PrintPopulation(0);
PrintOutlineInfo(0);
#endif // DEBUG
for (int i=1; i<=maxGeneration; i++)
{
CalculateFitness();
double pc = 0.6; // 交叉概率
double pm = 0.08; // 变异概率
Individual *population; // 种群
// 主函数
int main(void)
{
GenerateInitialPopulation();
exit(0);
}
// 用轮盘赌算法从当前种群中选择个体并copy到newPopulation中
srand((unsigned)time(NULL));
for (int i=0; i<populationSize; i++)
{
double pointer = rand() % 1001 / (double)1000;
cout << population[best].fitness << endl;
delete population;
}
// 产生初始种群
void GenerateInitialPopulation(void)
{
population = new Individual[populationSize];
{
double p = (double)(rand() % 100) / 100;
if (p <= pc)
{
int pos = rand() % CHROMOSOME_LENGTH;
for (; pos<CHROMOSOME_LENGTH; pos++)
{
int t = 1;
for (int i=CHROMOSOME_LENGTH-1; i>=0; i--)
{
result += chromosome[i] * t;
t <<= 1;
}
return result;
}
// 复制个体
void CopyIndividual(Individual *dest, const Individual *source)
{
for (int i=0; i<populationSize; i++)
{
int t = Decode(population[i].chromosome);
population[i].fitness = t * t;
}
}
// 选择操作
void Select(void)
// 申请新的内存空间以便用来存放选择出来的个体
Individual * newPopulation = new Individual[populationSize];
if (newPopulation == NULL)
{
cout << "Allocate memory failed!" << endl;
//: 本程序用遗传算法求函数 F(x) = x * x 在区间[0, 255]上的最大值
//:遗传算法用到了轮盘赌选择算法、单点交叉算法、单点变异算法
//: 欢迎批评指正!Email:523192573@ 关同学
#include <iostream>
#include <iomanip>
{
int t = rand() % 256;
Encode(t, population[i].chromosome);
// population[i].fitness = t * t;
}
}
// 计算种群中所有个体的适应度
void CalculateFitness(void)
if (population == NULL)
{
cout << "Allocate memory failed!" << endl;

下载文档原格式

  / 3