本文实例讲述了C++实现简单遗传算法。分享给大家供大家参考。具体实现方法如下:
// CMVSOGA.h : main header file for the CMVSOGA.cpp
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
#if !defined(AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_)
#define AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Afxtempl.h"
#define variablenum 14
class CMVSOGA
{
public:
CMVSOGA();
~CMVSOGA();
void selectionoperator();
void crossoveroperator();
void mutationoperator();
void initialpopulation(int, int ,double ,double,double *,double *); //种群初始化
void generatenextpopulation(); //生成下一代种群
void evaluatepopulation(); //评价个体,求最佳个体
void calculateobjectvalue(); //计算目标函数值
void calculatefitnessvalue(); //计算适应度函数值
void findbestandworstindividual(); //寻找最佳个体和最差个体
void performevolution();
void GetResult(double *);
void GetPopData(CList &);
void SetFitnessData(CList &,CList &,CList &);
private:
struct individual
{
double chromosome[variablenum]; //染色体编码长度应该为变量的个数
double value;
double fitness;//适应度
};
double variabletop[variablenum]; //变量值
double variablebottom[variablenum]; //变量值
int popsize;//种群大小
// int generation;//世代数
int best_index;
int worst_index;
double crossoverrate; //交叉率
double mutationrate; //变异率
int maxgeneration;//最大世代数
struct individual bestindividual; //最佳个体
struct individual worstindividual; //最差个体
struct individual current;//当前个体
struct individual current1;//当前个体
struct individual currentbest; //当前最佳个体
CList population; //种群
CList newpopulation; //新种群
CList cfitness; //存储适应度值
//怎样使链表的数据是一个结构体????主要是想把种群作成链表。节省空间。
};
#endif
执行文件:
// CMVSOGA.cpp : implementation file
//
#include "stdafx.h"
//#include "vld.h"
#include "CMVSOGA.h"
#include "math.h"
#include "stdlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMVSOGA.cpp
CMVSOGA::CMVSOGA()
{
best_index=0;
worst_index=0;
crossoverrate=0; //交叉率
mutationrate=0; //变异率
maxgeneration=0;
}
CMVSOGA::~CMVSOGA()
{
best_index=0;
worst_index=0;
crossoverrate=0; //交叉率
mutationrate=0; //变异率
maxgeneration=0;
population.RemoveAll(); //种群
newpopulation.RemoveAll(); //新种群
cfitness.RemoveAll();
}
void CMVSOGA::initialpopulation(int ps, int gen ,double cr ,double mr,double *xtop,double *xbottom) //第一步,初始化。
{
//应该采用一定的策略来保证遗传算法的初始化合理,采用产生正态分布随机数初始化?选定中心点为多少?
int i,j;
popsize=ps;
maxgeneration=gen;
crossoverrate=cr;
mutationrate =mr;
for (i=0;ivariabletop[j])
{
current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
}
if (current.chromosome[j]=pc&&pindex index;
int point,temp;
double p;
// srand( (unsigned)time( NULL ) );
for (i=0;ivariabletop[j]) //判断是否超界.
{
current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
}
if (current.chromosome[j]variabletop[j])
{
current1.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
}
if (current1.chromosome[j]bestindividual.fitness)
{
bestindividual=current;
best_index=i;
}
else if (current.fitness=currentbest.fitness)
{
currentbest=bestindividual;
}
}
}
void CMVSOGA:: calculatefitnessvalue() //适应度函数值计算,关键是适应度函数的设计
//current变化,这段程序变化较大,特别是排序。
{
int i;
double temp;//alpha,beta;//适应度函数的尺度变化系数
double cmax=100;
for(i=0;icurrentbest.fitness)
{
currentbest=population.GetAt(population.FindIndex(best_index));
}
else
{
population.SetAt(population.FindIndex(worst_index),currentbest);
}
}
void CMVSOGA::GetResult(double *Result)
{
int i;
for (i=0;i&PopData)
{
PopData.RemoveAll();
int i,j;
for (i=0;i&PopData,CList &FitnessData,CList &ValueData)
{
int i,j;
for (i=0;i
#include
#include
#include
#define kMaxFlowers 10
using std::cout;
using std::endl;
class ai_World
{
public:
ai_World()
{
srand(time(0));
}
~ai_World() {}
int temperature[kMaxFlowers]; //温度
int water[kMaxFlowers]; //水质
int sunlight[kMaxFlowers]; //阳光
int nutrient[kMaxFlowers]; //养分
int beneficialInsect[kMaxFlowers]; //益虫
int harmfulInsect[kMaxFlowers]; //害虫
int currentTemperature;
int currentWater;
int currentSunlight;
int currentNutrient;
int currentBeneficialInsect;
int currentHarmfulInsect;
void Encode();
int Fitness(int flower);
void Evolve();
inline int tb_Rnd(int start, int end)
{
if (start > end)
return 0;
else
{
//srand(time(0));
return (rand() % (end + 1) + start);
}
}
void show();
};
// ----------------------------------------------------------------- //
void ai_World::Encode()
// ----------------------------------------------------------------- //
{
int i;
for (i=0;ileastFit)
{
leastFit=Fitness(i);
leastFitIndex=i;
}
temperature[leastFitIndex]=temperature[tb_Rnd(0,kMaxFlowers - 1)];
water[leastFitIndex]=water[tb_Rnd(0,kMaxFlowers - 1)];
sunlight[leastFitIndex]=sunlight[tb_Rnd(0,kMaxFlowers - 1)];
nutrient[leastFitIndex]=nutrient[tb_Rnd(0,kMaxFlowers - 1)];
beneficialInsect[leastFitIndex]=beneficialInsect[tb_Rnd(0,kMaxFlowers - 1)];
harmfulInsect[leastFitIndex]=harmfulInsect[tb_Rnd(0,kMaxFlowers - 1)];
for (i=0;i
#include "ai_World.h"
using namespace std;
int main()
{
ai_World a;
a.Encode();
// a.show();
for (int i = 0; i < 10; i++)
{
cout << "Generation " << i << endl;
a.Evolve();
a.show();
}
system("PAUSE");
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。



