⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmvsoga.txt

📁 这是我在解决电梯动力学参数写的简单遗传算法(程序带目标函数值、适应度值计算
💻 TXT
📖 第 1 页 / 共 2 页
字号:
 for (i=0;i<popsize;i++)//生成序号
 {
  index.InsertAfter (index.FindIndex(i),i);
 }
 for (i=0;i<popsize;i++)//打乱序号
 {
  point=rand()%(popsize-1);
  temp=index.GetAt(index.FindIndex(i));
  index.SetAt(index.FindIndex(i),
   index.GetAt(index.FindIndex(point)));  
  index.SetAt(index.FindIndex(point),temp);
 }
 for (i=0;i<popsize-1;i+=2)
 {//按顺序序号,按序号选择两个母体进行交叉操作。
  p=double(rand()%1000)/1000.0;
  if (p<crossoverrate)
  {   
   alpha=double(rand()%1000)/1000.0;
   beta=double(rand()%1000)/1000.0;
   current=population.GetAt(population.FindIndex(index.GetAt(index.FindIndex(i))));
   current1=population.GetAt(population.FindIndex(index.GetAt(index.FindIndex(i+1))));//临时使用current1代替
   for(j=0;j<variablenum;j++)
   { 
    //交叉
    current.chromosome[j]=(1-alpha)*current.chromosome[j]+
     beta*current1.chromosome[j];
    if (current.chromosome[j]>variabletop[j])  //判断是否超界.
    {
     current.chromosome[j]=variabletop[j];
    }
    if (current.chromosome[j]<variablebottom [j])
    {
     current.chromosome[j]=variablebottom [j];
    }
    current1.chromosome[j]=alpha*current.chromosome[j]
     +(1- beta)*current1.chromosome[j];
    if (current1.chromosome[j]>variabletop[j])
    {
     current1.chromosome[j]=variabletop[j];
    }
    if (current1.chromosome[j]<variablebottom [j])
    {
     current1.chromosome[j]=variablebottom [j];
    }
   }
   //回代
  }
  newpopulation.InsertAfter  (newpopulation.FindIndex(i),current);
  newpopulation.InsertAfter  (newpopulation.FindIndex(i),current1);
 }
 j=newpopulation.GetCount();
 for (i=0;i<popsize;i++)
 {
  population.SetAt (population.FindIndex(i),
   newpopulation.GetAt (newpopulation.FindIndex(i)));
 }
 newpopulation.RemoveAll();
}
void CMVSOGA:: findbestandworstindividual( )  
{
 int i;
 bestindividual=population.GetAt(population.FindIndex(best_index));
 worstindividual=population.GetAt(population.FindIndex(worst_index));
 for (i=1;i<popsize; i++)
 {
  current=population.GetAt(population.FindIndex(i));
  if (current.fitness>bestindividual.fitness)
  {
   bestindividual=current;
   best_index=i;
  }
  else if (current.fitness<worstindividual.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(bestindividual.fitness>=currentbest.fitness)
  {
   currentbest=bestindividual;
  }
 }

}
void CMVSOGA:: calculatefitnessvalue() //适应度函数值计算,关键是适应度函数的设计
          //current变化,这段程序变化较大,特别是排序。
{
 int  i;
 double temp;//alpha,beta;//适应度函数的尺度变化系数
 double cmax=100;
 for(i=0;i<popsize;i++)
 {
  current=population.GetAt(population.FindIndex(i));
  if(current.value<cmax)
  {
   temp=cmax-current.value;
  }
  else
  {
   temp=0.0;
  }
  /*
  if((population[i].value+cmin)>0.0)
  {temp=cmin+population[i].value;}
 else
 {temp=0.0;
   }
  */
  current.fitness=temp;
  population.SetAt(population.FindIndex(i),current); 
 }
}
void CMVSOGA:: performevolution() //演示评价结果,有冗余代码,current变化,程序应该改变较大
{
 if (bestindividual.fitness>currentbest.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<variablenum;i++)
 {
  Result[i]=currentbest.chromosome[i];
 }
 Result[i]=currentbest.value;
}
CMVSOGA::CMVSOGA()
{
 best_index=0;  
 worst_index=0;
 crossoverrate=0;            //交叉率
 mutationrate=0;            //变异率
 maxgeneration=0;
}
void CMVSOGA::GetPopData(double **PopData)  //不能保证每次计算结果都对,为什么?
{
 int i,j;
 for (i=0;i<popsize;i++)
 {
  current=population.GetAt(population.FindIndex(i));
  for (j=0;j<variablenum;j++)
  {
   PopData[i][j]=current.chromosome[j];
  }
 }
}
void CMVSOGA::SetValueData(double *ValueData)
{
 int j;
 for (j=0;j<popsize;j++)
 {  
  current=population.GetAt(population.FindIndex(j));
  current.value=ValueData[j];
  population.SetAt(population.FindIndex(j),current);
 }
}

DLG中调用代码:

void CMVSOFGADlg::OncalNoGuass() 
{
 // TODO: Add your control notification handler code here
 UpdateData(true);
 UINT i;
 UINT generation;
 double topx[16], bottomx[16];
 double resultx[17];
 ////////////////////////////////////
 double ** PopData;
 double *PopValue;
 PopData = new double*[m_popsize];
 for(i=0; i<m_popsize; i++)/////////////////////////变量数目,程序更改时注意/////////////
 {
  PopData[i] = new double[16];
 }
 PopValue=new double [m_popsize];
 ////////////////////////////////////
 //////////////初始化/////////////
 CMVSOGA  cdata;
 m_result=0;
 topx[0]=1.28;
 topx[1]=1.28;
 topx[2]=1.28;
 topx[3]=1.28;
 topx[4]=1.28;
 topx[5]=1.28;
 topx[6]=1.28;
 topx[7]=1.28;
 topx[8]=1.28;
 topx[9]=1.28;
 topx[10]=1.28;
 topx[11]=1.28;
 topx[12]=1.28;
 topx[13]=1.28;
 topx[14]=1.28;
 topx[15]=1.28;
 bottomx[0]=-1.28;
 bottomx[1]=-1.28;
 bottomx[2]=-1.28;
 bottomx[3]=-1.28;
 bottomx[4]=-1.28;
 bottomx[5]=-1.28;
 bottomx[6]=-1.28;
 bottomx[7]=-1.28;
 bottomx[8]=-1.28;
 bottomx[9]=-1.28;
 bottomx[10]=-1.28;
 bottomx[11]=-1.28;
 bottomx[12]=-1.28;
 bottomx[13]=-1.28;
 bottomx[14]=-1.28;
 bottomx[15]=-1.28;

 cdata.initialpopulation(m_popsize ,m_generation  ,m_csrate ,m_mrate ,topx,bottomx);
 cdata.GetPopData(PopData);  TargetFunctionCal(PopData,PopValue);
 cdata.SetValueData(PopValue);
 cdata.evaluatepopulation();
 for (generation=0;generation<50*m_generation;generation++ )
 {
  cdata.generatenextpopulation();
  cdata.GetPopData(PopData);  
  TargetFunctionCal(PopData,PopValue);
  cdata.SetValueData(PopValue);
  cdata.evaluatepopulation();
  cdata.performevolution();
 }
 cdata.GetResult (resultx);
 m_x1=resultx[0];
 m_x2=resultx[1];
 m_x3=resultx[2];
 m_x4=resultx[3];
 m_x5=resultx[4];
 m_x6=resultx[5];
 m_x7=resultx[6];
 m_x8=resultx[7];
 m_x9=resultx[8];
 m_x10=resultx[9];
 m_x11=resultx[10];
 m_x12=resultx[11];
 m_x13=resultx[12];
 m_x14=resultx[13];
 m_x15=resultx[14];
 m_x16=resultx[15];
 m_result=resultx[16];
 UpdateData(false);
 //不过删除的时候记得要删除PopData[i]里的每个数组:
 for(i=0; i<m_popsize; i++)
 {
  delete [](PopData[i]);
 }
 delete []PopData;
 delete []PopValue;
 //释放数组A;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -