資源簡介
使用遺傳算法(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
- 上一篇:使用數字電流傳感器進行功耗和能耗監控應用筆記
- 下一篇:HFSS15破解文件
評論
共有 條評論