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

📄 population.h

📁 遗传策略的最小路径(tsp)的实际应用
💻 H
字号:

class Population {

private:
     
    Individual ind[POP_SIZE];

    Individual TempInd[Temp_POP_SIZE];

public:


double random(double min,double max)   //产生最小min--最大max的随机数
  {   
   
    int   a;   
    double   r;   

    a=rand();   

    r=((double)a/RAND_MAX)*(max-min)+min;   
    
    return  r;   
  }   


    Population() {

       for(int i = 0; i < POP_SIZE; i++)

		   ind[i].set( random( MIN_1,MAX_1 ),random(MIN_2,MAX_2),random(0.49,0.51),random(0.29,0.31) );
        
    }
    
void shellSort()         //希尔排序
{

       int gap, i, j;

       Individual temp;

       for(gap = Temp_POP_SIZE/2; gap > 0; gap /= 2)

           for(i = gap; i < Temp_POP_SIZE; i++)

              for(j=i-gap; j>=0 && TempInd[j].FitnessEvaluate() < TempInd[j+gap].FitnessEvaluate(); j -= gap) {

                  temp = TempInd[j];

                  TempInd[j] = TempInd[j+gap];

                 TempInd[j+gap] = temp;

              }

    }


void Recombination() {

     int n1,n2;

   for(int i = 0; i < POP_SIZE; i++) {

           TempInd[i] =ind[i];

       }

	for(int i = 0; i < 0.1*POP_SIZE; i += 2) {

           n1 = rand() %(POP_SIZE);

           n2 = rand() %(POP_SIZE);

           Individual temp1, temp2;

           temp1.recombination(TempInd[n1], TempInd[n2]);
              
           temp2.recombination(TempInd[n1], TempInd[n2]);

           TempInd[n1] = temp1; 
		   
		   TempInd[n2] = temp2;

       }

       for(int i = 0; i < POP_SIZE; i++) {

           ind[i] = TempInd[i];

       }

    }


    void Mutation() {

      int n=0;

for(int i=0;i<POP_SIZE;i++)
{
TempInd[n] = ind[i];
n++;
}
     for(int j = 0;j<7; j++) 
{
	for(int i = 0; i <POP_SIZE; i++) 
	{
	TempInd[n] = ind[i].mutation();
    n++;
	}
}
	}

	void Selection()
	{
	Population::shellSort();

	for(int i = 0; i < POP_SIZE; i++) 
{
	ind[i] =TempInd[i];
}  

		}



  Individual getMax() {

       Individual max = ind[0];

       for(int i = 0; i < POP_SIZE-1; i++) {

           if( ind[i+1].FitnessEvaluate() > ind[i].FitnessEvaluate() ) max = ind[i+1];

    }
      return max;

       }

    Individual getMin() {

       Individual min = ind[0];

       for(int i = 0; i < POP_SIZE-1; i++) {

           if( ind[i+1].FitnessEvaluate() < ind[i].FitnessEvaluate() ) min = ind[i+1];

       }

       return min;

    }

  
    void report() {

       for(int i = 0; i < POP_SIZE; i++)

           ind[i].print();

       cout << endl;

    }
};

⌨️ 快捷键说明

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