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

📄 nsgapopulation.cpp

📁 遗传算法程序最新版
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      }    }    delete []sortListByObj;  }    if(whichGuys == GUYS)     for(ii = 0; ii < popSize; ii++)       ((NsgaIndividual**)guys)[ii]->setCrowdingDistance(crowdDist[ii]);  else if (whichGuys == COMBINEDGUYS)    for(ii = 0; ii < popSize; ii++)      combinedGuys[ii]->setCrowdingDistance(crowdDist[ii]);  delete [] crowdDist;}/// Quick sort routine used for crowding distance computation. Sorts the individuals in a given pareto front in ascending order of objId th objective value.void NsgaPopulation::quickSort(NsgaIndividual **theGuys, int *output, 			       int left, int right, int objId){  int ii, xx;  double target;    if(right > left) {    target = theGuys[output[right-1]]->getFitness(objId);    ii = left-1;    xx = right-1;    for(;;) {      while(theGuys[output[++ii]]->getFitness(objId) < target)	if(ii >= right-1) break;      if(ii >= xx) break;      while(theGuys[output[--xx]]->getFitness(objId) > target)	if(xx <= 0) break;      if(ii >= xx) break;      swap(output[ii],output[xx]);    }    swap(output[ii],output[right-1]);    quickSort(theGuys, output, left, ii, objId);    quickSort(theGuys, output, ii+1, right, objId);  }}///Swap used by the sort functionvoid NsgaPopulation::swap(int& ii, int& jj){  int temp;  temp = jj; jj = ii; ii = temp;}///Quick sort function to sort individuals in a pareto front according to the crowding distance.void NsgaPopulation::regQSort(double *crowdDist, int *output, int left, int right){  int ii, xx;  double target;  if(right > left) {    target = crowdDist[output[right-1]];    ii = left-1;    xx = right-1;    for(;;) {      while(crowdDist[output[++ii]] < target)	if(ii >= right-1) break;      if(ii >= xx) break;      while(crowdDist[output[--xx]] > target)	if(xx <= 0) break;      if(ii >= xx) break;      swap(output[ii],output[xx]);    }    swap(output[ii],output[right-1]);    regQSort(crowdDist, output, left, ii);    regQSort(crowdDist, output, ii+1, right);  }}/*============================================================** Function Name: NsgaPopulation::computeObjStatistics(int whichGuys)** The flag whichGuys refer to either the current population (for the ** initial generation) or the combined population.** Function Task: this method generates statistics according to ** each objective function values. It finds out the average objective** value for the population and the best and worst objective ** values for each objective function. This depends on whether the ** type of optimization in the problem of each objective function ** is minimization or maximization. ** Output:None** Functions called: Individual::getObjective(int jj)**========================================================*/void NsgaPopulation::computeObjStatistics(int whichGuys) {  int ii, jj, popSize;  double oneOverPopulationSize;  NsgaIndividual **theGuys;    if(whichGuys == GUYS) {    popSize = globalSetup->populationSize;    theGuys = (NsgaIndividual**)guys;  }  else if(whichGuys == NEWGUYS) {    popSize = globalSetup->populationSize;    theGuys = (NsgaIndividual**)newGuys;  }  else if(whichGuys == COMBINEDGUYS) {    popSize = 2*(globalSetup->populationSize);    theGuys = combinedGuys;  }    oneOverPopulationSize = 1.0/(1.0*popSize);    for(jj = 0; jj < globalSetup->finalNoOfObjectives; jj++) {    bestobj[jj] = worstobj[jj] = theGuys[0]->getObjective(jj);    avgobj[jj] = oneOverPopulationSize*(theGuys[0]->getObjective(jj));    for(ii = 1; ii < popSize; ii++) {      avgobj[jj] += oneOverPopulationSize*(theGuys[ii]->getObjective(jj));      if(globalSetup->typeOfOptimizations[jj] == Maximization) {	if(theGuys[ii]->getObjective(jj) > bestobj[jj]) bestobj[jj] = theGuys[ii]->getObjective(jj);	if(theGuys[ii]->getObjective(jj) < worstobj[jj]) worstobj[jj] = theGuys[ii]->getObjective(jj);      }      if(globalSetup->typeOfOptimizations[jj]==Minimization) {	if(theGuys[ii]->getObjective(jj) < bestobj[jj]) bestobj[jj] = theGuys[ii]->getObjective(jj);	if(theGuys[ii]->getObjective(jj) > worstobj[jj]) worstobj[jj] = theGuys[ii]->getObjective(jj);      }          }  }}/*============================================================** Function Name: NsgaPopulation::computeFitnessStatistics(int whichGuys)** The flag whichGuys refer to either the current population ** (for the initial generation) or the combined population.** Function Task: this method generates statistics according to ** every fitness function values. It finds out the average fitness** value for the population and the maximum and minimum fitness ** values as well as the fitness variance for each fitness function.** Output:None** Functions called: Individual::getFitness(int jj)**========================================================*/void NsgaPopulation::computeFitnessStatistics(int whichGuys) {  int ii, jj, popSize;  double oneOverPopulationSize;  NsgaIndividual **theGuys;    if(whichGuys == GUYS) {    popSize = globalSetup->populationSize;    theGuys = (NsgaIndividual**)guys;  }  else if(whichGuys == NEWGUYS) {    popSize = globalSetup->populationSize;    theGuys = (NsgaIndividual**)newGuys;  }  else if(whichGuys == COMBINEDGUYS) {    popSize = 2*(globalSetup->populationSize);    theGuys = combinedGuys;  }    oneOverPopulationSize = 1.0/(1.0*popSize);    for(jj = 0; jj < globalSetup->finalNoOfObjectives; jj++) {    maxfit[jj] = minfit[jj] = theGuys[0]->getFitness(jj);    avgfit[jj] = oneOverPopulationSize*(theGuys[0]->getFitness(jj));    varfit[jj] = oneOverPopulationSize*(theGuys[0]->getFitness(jj))*(theGuys[0]->getFitness(jj));        for(ii = 1; ii < popSize; ii++) {      avgfit[jj] += oneOverPopulationSize*(theGuys[ii]->getFitness(jj));      varfit[jj] += oneOverPopulationSize*(theGuys[ii]->getFitness(jj))*(theGuys[ii]->getFitness(jj));      if(theGuys[ii]->getFitness(jj) > maxfit[jj]) maxfit[jj] = theGuys[ii]->getFitness(jj);      if(theGuys[ii]->getFitness(jj) < minfit[jj]) minfit[jj] = theGuys[ii]->getFitness(jj);    }    varfit[jj] -= avgfit[jj]*avgfit[jj];  }}/***Map each objective to corresponding fitness value depending on minimization or maximization. The flag whichGuys refer to either the current population (for the initial generation) or the combined population. If the jth objective value is a maximization, then the fitness is set equal to the objective value. On the other hand, if the problem is a minimization one, then set the fitness equal to the negative of the objective value. */void NsgaPopulation::mapObjectiveToFitness(int whichGuys) {  int ii,jj, popSize;  NsgaIndividual **theGuys;    if(whichGuys == GUYS) {    popSize = globalSetup->populationSize;    theGuys = (NsgaIndividual **)guys;  }  else if(whichGuys == NEWGUYS) {    popSize = globalSetup->populationSize;    theGuys = (NsgaIndividual **)newGuys;  }  else if(whichGuys == COMBINEDGUYS) {    popSize = 2*(globalSetup->populationSize);    theGuys = combinedGuys;  }  for (jj=0; jj<globalSetup->finalNoOfObjectives; jj++) {    if (globalSetup->typeOfOptimizations[jj] == Maximization)       for(ii = 0; ii < popSize; ii++)        theGuys[ii]->setFitness(jj, theGuys[ii]->getObjective(jj));     else      for (ii=0; ii< popSize; ii++) 	theGuys[ii]->setFitness(jj, -theGuys[ii]->getObjective(jj));  }}

⌨️ 快捷键说明

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