资源简介

使用遗传算法(GA)来自动率定经典水文概念性模型———新安江模型的各个参数,供大家学习参考 VS2008 with SP1 平台下编写,打开即可编译运行!

资源截图

代码片段和文件信息

#include 
#include “Gene.h“
#include 
#include 
GeneEvolution::GeneEvolution()
{
   Best_Index=0;
   Worst_Index=0;
   CrossOverRate=0;
   MutationRate=0;
   MaxGeneration=0;
}
GeneEvolution::~GeneEvolution()
{
Best_Index=0;
Worst_Index=0;
CrossOverRate=0;
Generation=0;
MutationRate=0;
MaxGeneration=0;
NewPopulation.RemoveAll();
Population.RemoveAll();
Cfitness.RemoveAll();
}
void GeneEvolution::InitialPopulation(std::ifstream& iflie)//初始化种群
{
iflie>>VariableNum>>PopSize>>MaxGeneration>>CrossOverRate>>MutationRate;
VariableTop.resize(VariableNum);
VariableBottom.resize(VariableNum);
Current.Chromosome.resize(VariableNum);
Current1.Chromosome.resize(VariableNum);
CurrentBest.Chromosome.resize(VariableNum);
for (int i=0;i {
iflie>>VariableBottom[i]>>VariableTop[i];
}
//随机生成一个种群
srand((unsigned int)time(0));
for(int i=0;i {
for(int j=0;j {
Current.Chromosome[j]=double(rand()%10000)/10000*(VariableTop[j]-VariableBottom[j])+VariableBottom[j];
}
Current.Fitness=0;
Current.Value=0;
Population.InsertAfter(Population.FindIndex(i)Current);
}
}
void GeneEvolution::CalculateobjectValue( XAJ m_XAJ)//计算种群的得分,使用外部的目标函数,(本程序用于新安江自动率参试验)
              //或者说需要寻优的实际问题的参数好坏的评价函数对该参数序列做好坏评价
{
for (int i=0;i {
Current=Population.GetAt(Population.FindIndex(i));
m_XAJ.InitialPA(Current.Chromosome);
m_XAJ.MainCompute();
Current.Value=m_XAJ.ComputeValue();
Population.SetAt(Population.FindIndex(i)Current);
}
}
void GeneEvolution::CalculateFitnessValue()//计算适应分,种群进化的评价标准该函数的可变范围较大,要寻找一个合适的评分标准
{
for (int i=0;i {
Current=Population.GetAt(Population.FindIndex(i));
if (Generation>=MaxGeneration*7/8)
{
Current.Fitness=pow(Current.Value1.5);
MutationRate=0.08;
CrossOverRate=0.9;
}
else if(Generation<=MaxGeneration*7/8&&Generation>=MaxGeneration*5/8)
{
MutationRate=0.06;
CrossOverRate=0.85;
Current.Fitness=pow(Current.Value2.5);
}
else
{
Current.Fitness=pow(Current.Value3.5);
}
Population.SetAt(Population.FindIndex(i)Current);
}
}
void GeneEvolution::FindBestAndWorstIndividual()//寻找最好和最坏的种群
{
BestIndividual=Population.GetAt(Population.FindIndex(Best_Index));
WorstIndividual=Population.GetAt(Population.FindIndex(Worst_Index));
for (int i=0;i {
Current=Population.GetAt(Population.FindIndex(i));
if (Current.Fitness>=BestIndividual.Fitness)
{
BestIndividual=Current;
Best_Index=i;
}
else if (Current.Fitness {
WorstIndividual=Current;
Worst_Index=i;
}
}
Population.SetAt(Population.FindIndex(Worst_Index)Population.GetAt(Population.FindIndex(Best_Index)));
//用最好的代替最差的
if (MaxGeneration==0)
{
CurrentBest=BestIndividual;
}
else
{
if (CurrentBest.Fitness

评论

共有 条评论