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

📄 ucom_rw.cpp

📁 遗传算法解决机组组合问题的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
      		   {
      				if(p == 1) //migrate to tempop
                  {
		         			if(tempop[j].chr[0] == 0)
		      		      {
      		        			for(z=1;z<=lchrom;z++)
									{
                  		   	tempop[j].chr[z] = offspring1[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
            		        //cout<<"MIGRATE1";
            				   break;
				            }
                  }
		      		if(p == 2) //migrate to initpop
                  {
		         			if(initpop[j].chr[0] == 0)
		      		      {
      		        			for(z=1;z<=lchrom;z++)
									{
                  		   	initpop[j].chr[z] = offspring1[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
            		        //cout<<"MIGRATE1";
            				   break;
				            }
                  }

      		   }//end for
			   }

   }//endif x == 5

	if(x == 6)
   {
				if(checksum >= 1100 && checksum <= 1200)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
      				if(p == 1) //migrate to tempop
                  {
		         			if(tempop[j].chr[0] == 0)
		      		      {
      		        			for(z=1;z<=lchrom;z++)
									{
                  		   	tempop[j].chr[z] = offspring1[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
            		        //cout<<"MIGRATE1";
            				   break;
				            }
                  }
		      		if(p == 2) //migrate to initpop
                  {
		         			if(initpop[j].chr[0] == 0)
		      		      {
      		        			for(z=1;z<=lchrom;z++)
									{
                  		   	initpop[j].chr[z] = offspring1[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
            		        //cout<<"MIGRATE1";
            				   break;
				            }
                  }

      		   }//end for
			   }

   }//endif x == 6


	return checksum;
}

int migrate2(int p,int x)//p - migrate to temp or init   x - demand load
{

 if((offspring2[0].chr[1] == 0) && (offspring2[0].chr[2] == 0) && (offspring2[0].chr[3] == 0) && (offspring2[0].chr[4] == 0))
   	return -1;

	checksum = 0.0;      
	for(i=1;i<=4;i++)
   {
		p1 = 0.0;p2 = 0.0;p3 = 0.0;p4 = 0.0;
		if(i == 1)
      {
		      	v1 = 0.0;
					v2 = 0.0;
					k = 0;
						for(nomb=5;nomb<15;nomb++)
						{
							k++;
							v1 = v1 + (offspring2[0].chr[nomb] * (pow(2, (10 - k))));
							v2 = v2 + pow(2,(10 - k));
						}
						p1 = (((v1/v2) * 525) + 100);
						if(p1 == 100)
							 p1 = 0.0;
                  //cout<<"\nJOMLAH:"<<p1;

      }

		if(i == 2)
      {
					v1 = 0.0;
					v2 = 0.0;
					k = 0;
						for(nomb=15;nomb<25;nomb++)
						{
							k++;
							v1 = v1 + (offspring2[0].chr[nomb] * (pow(2, (10 - k))));
							v2 = v2 + pow(2,(10 - k));
						}
						p2 = (((v1/v2) * 525) + 100);
						if(p2 == 100)
							 p2 = 0.0;
                  //cout<<"\nJOMLAH2:"<<p2;

      }

		if(i == 3)
      {
					v1 = 0.0;
					v2 = 0.0;
					k = 0;
						for(nomb=25;nomb<35;nomb++)
						{
							k++;
							v1 = v1 + (offspring2[0].chr[nomb] * (pow(2, (10 - k))));
							v2 = v2 + pow(2,(10 - k));
						}
						p3 = (((v1/v2) * 525) + 75);
						if(p3 == 75)
							 p3 = 0.0;
                  //cout<<"\nJOMLAH3:"<<p3;

      }

		if(i == 4)
      {
					v1 = 0.0;
					v2 = 0.0;
					k = 0;
						for(nomb=35;nomb<45;nomb++)
						{
							k++;
							v1 = v1 + (offspring2[0].chr[nomb] * (pow(2, (10 - k))));
							v2 = v2 + pow(2,(10 - k));
						}
						p4 = (((v1/v2) * 425) + 75);
						if(p4 == 75)
							 p4 = 0.0;
                  //cout<<"\nJOMLAH4:"<<p4;

      }

		checksum = checksum + p1 + p2 + p3 + p4;
   }

	if(x == 1)
   {
				if(checksum >= 1100 && checksum <= 1200)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
               	if(p == 1)//migrate to temp
                  {
				         	if(tempop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	tempop[j].chr[z] = offspring2[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }
			         if(p == 2)//migrate to init
                  {
				         	if(initpop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	initpop[j].chr[z] = offspring2[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }

      		   }//end for
			   }

   }//end if x == 1

	if(x == 2)
   {
				if(checksum >= 1400 && checksum <= 1500)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
               	if(p == 1)//migrate to temp
                  {
				         	if(tempop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	tempop[j].chr[z] = offspring2[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }
			         if(p == 2)//migrate to init
                  {
				         	if(initpop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	initpop[j].chr[z] = offspring2[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }

      		   }//end for
			   }

   }//end if x == 2

	if(x == 3)
   {
				if(checksum >= 1600 && checksum <= 1700)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
               	if(p == 1)//migrate to temp
                  {
				         	if(tempop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	tempop[j].chr[z] = offspring2[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }
			         if(p == 2)//migrate to init
                  {
				         	if(initpop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	initpop[j].chr[z] = offspring2[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }

      		   }//end for
			   }

   }//end if x == 3

	if(x == 4)
   {
				if(checksum >= 1800 && checksum <= 1900)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
               	if(p == 1)//migrate to temp
                  {
				         	if(tempop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	tempop[j].chr[z] = offspring2[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }
			         if(p == 2)//migrate to init
                  {
				         	if(initpop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	initpop[j].chr[z] = offspring2[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }

      		   }//end for
			   }

   }//end if x == 4

	if(x == 5)
   {
				if(checksum >= 1400 && checksum <= 1500)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
               	if(p == 1)//migrate to temp
                  {
				         	if(tempop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	tempop[j].chr[z] = offspring2[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }
			         if(p == 2)//migrate to init
                  {
				         	if(initpop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	initpop[j].chr[z] = offspring2[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }

      		   }//end for
			   }

   }//end if x == 5

	if(x == 6)
   {
				if(checksum >= 1100 && checksum <= 1200)
			   {
		      	for(j=1;j<=popsize;j++)
      		   {
               	if(p == 1)//migrate to temp
                  {
				         	if(tempop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	tempop[j].chr[z] = offspring2[0].chr[z];
				                  tempop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }
			         if(p == 2)//migrate to init
                  {
				         	if(initpop[j].chr[0] == 0)
      				      {
            					for(z=1;z<=lchrom;z++)
									{
      		               	initpop[j].chr[z] = offspring2[0].chr[z];
				                  initpop[j].chr[0] = 1;
      				         }
		                   // cout<<"MIGRATE2";
      		      		   break;
		      		      }
                  }

      		   }//end for
			   }

   }//end if x == 6

	return checksum;
}

void migratelit(int x)
{
	gen:
	int randum = random(popsize) + 1;

   if(randum == 0)
   	goto gen;


      	if(x == 1)
         {
         	if(tempop[randum].chr[0] == 0)
            {
				 	for(j=1;j<=lchrom;j++)
               {
						tempop[randum].chr[j] = initpop[fittest].chr[j];
            		tempop[randum].chr[0] = 1;
	            }
               //break;
            }
            else
            	goto gen;
            //cout<<"MIGRATELIT 1";
         }
         if(x == 2)
         {
             if(initpop[randum].chr[0] == 0)
             {
             	for(j=1;j<=lchrom;j++)
               {
						initpop[randum].chr[j] = tempop[fittest].chr[j];
						initpop[randum].chr[0] = 1;
               }
               //break;
	          }
             else
             	goto gen;
	           // cout<<"MIGRATELIT 2";
			}
         //cout<<"randum: "<<randum;

}

int check_vacancy(int x)
{
	bilchrom = 0;

   if(x == 1)
   {
			for(i=1;i<=popsize;i++)
			{
				if(tempop[i].chr[0] == 1)//index 1 - indicate not vacant
					bilchrom++;
			}

    }

   if(x == 2)
   {
			for(i=1;i<=popsize;i++)
			{
				if(initpop[i].chr[0] == 1)//index 1 - indicate not vacant
					bilchrom++;
			}

    }
	return bilchrom;
}

int geterr()
{
	error = maxfit - minfit;
   //cout<<"\nMAXFIT:"<<maxfit<<endl;
   //cout<<"MinFIT:"<<minfit<<endl;
	return error;
}

void display2()
{
	 cout<<"FITTEST:"<<fittest<<" \n";
	for(i=1;i<=popsize;i++)
	{
		cout<<tempop[i].fitness<<" "<<tempop[i].sumpowgen<<"  ";
		for(j=1;j<=lchrom;j++)
		{
			if(j==5||j==15||j==25||j==35)
				cout<<" ";
			cout<<tempop[i].chr[j];
		}
		cout<<endl;
	}
}

void display()
{
  cout<<"FITTEST:"<<fittest<<" \n";
	for(i=1;i<=popsize;i++)
	{
   	cout<<initpop[i].fitness<<" "<<initpop[i].sumpowgen<<"  ";
		for(j=1;j<=lchrom;j++)
		{
			if(j==5||j==15||j==25||j==35)
				cout<<" ";
			cout<<initpop[i].chr[j];
		}
		cout<<endl;
	}

}

int preserve(int x,int y)
{
   gotoxy(39 + x,11);
   cout<<".";
   if(y == 1)//initpop
   {
		for(i=1;i<=lchrom;i++)
			solution[x].chr[i] = initpop[fittest].chr[i];

	   solution[x].fitness = initpop[fittest].fitness;
   }

   if(y == 2)//tempop
   {
		for(i=1;i<=lchrom;i++)
			solution[x].chr[i] = tempop[fittest].chr[i];

	   solution[x].fitness = tempop[fittest].fitness;
	}

   if(marker == 1)
   {
   	solution[x].pw1 = tempop[fittest].pow1;
   	solution[x].pw2 = tempop[fittest].pow2;
   	solution[x].pw3 = tempop[fittest].pow3;
   	solution[x].pw4 = tempop[fittest].pow4;
   }

   if(marker == 2)
   {
   	solution[x].pw1 = initpop[fittest].pow1;
   	solution[x].pw2 = initpop[fittest].pow2;
   	solution[x].pw3 = initpop[fittest].pow3;
   	solution[x].pw4 = initpop[fittest].pow4;
   }

      solution[x].conv = gen;
   	//cout<<endl;

      cout.setf( ios::fixed );
      cout << setprecision(3);

      solution[x].total_pow = solution[x].pw1 + solution[x].pw2 + solution[x].pw3 + solution[x].pw4;

      return solution[x].fitness,solution[x].total_pow,solution[x].conv;
}

void preent(int x)
{

   if(x == 1)
	{
		ofstream out( "load1.txt", ios::app );

			if ( !out )
         {
				cerr << "File could not be open\n";
				getch();
				exit(1);
			}

		srand( time(0) );

⌨️ 快捷键说明

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