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

📄 ucom_rw.cpp

📁 遗传算法解决机组组合问题的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {	 	if(j==5||j==15||j==25||j==35)
							out<<" ";
						out << initpop[b].chr[j];
            }
	  			out <<"    "<<initpop[b].pow1<<"  "<<initpop[b].pow2<<"  "<<initpop[b].pow3<<"  "<<initpop[b].pow4<<"  "<<initpop[b].sumpowgen<<"  "<<initpop[b].fitness;
				out << "\n";

		}
		out<<" avg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
	}
   if(x == 2)
   {
		ofstream out( "load2.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
				{
            		if(j==5||j==15||j==25||j==35)
							out<<" ";
            		out << initpop[b].chr[j];
            }
				out <<"    "<<initpop[b].pow1<<"  "<<initpop[b].pow2<<"  "<<initpop[b].pow3<<"  "<<initpop[b].pow4<<"  "<<initpop[b].sumpowgen<<"  "<<initpop[b].fitness;
		  		out << "\n";

		}
		out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
   if(x == 3)
	{
		ofstream out( "load3.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
				{
            		if(j==5||j==15||j==25||j==35)
							out<<" ";
            		out << initpop[b].chr[j];
            }
				out <<"    "<<initpop[b].pow1<<"  "<<initpop[b].pow2<<"  "<<initpop[b].pow3<<"  "<<initpop[b].pow4<<"  "<<initpop[b].sumpowgen<<"  "<<initpop[b].fitness;
		  		out << "\n";

		}
		out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
   if(x == 4)
	{
		ofstream out( "load4.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
				{
            		if(j==5||j==15||j==25||j==35)
							out<<" ";
            		out << initpop[b].chr[j];
            }
				out <<"    "<<initpop[b].pow1<<"  "<<initpop[b].pow2<<"  "<<initpop[b].pow3<<"  "<<initpop[b].pow4<<"  "<<initpop[b].sumpowgen<<"  "<<initpop[b].fitness;
		  		out << "\n";

		}
		out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
   if(x == 5)
	{
		ofstream out( "load5.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
				{
            		if(j==5||j==15||j==25||j==35)
							out<<" ";
            		out << initpop[b].chr[j];
            }
				out <<"    "<<initpop[b].pow1<<"  "<<initpop[b].pow2<<"  "<<initpop[b].pow3<<"  "<<initpop[b].pow4<<"  "<<initpop[b].sumpowgen<<"  "<<initpop[b].fitness;
		  		out << "\n";

		}
		out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }

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

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
				{
            		if(j==5||j==15||j==25||j==35)
							out<<" ";
            		out << initpop[b].chr[j];
            }
		   	out <<"    "<<initpop[b].pow1<<"  "<<initpop[b].pow2<<"  "<<initpop[b].pow3<<"  "<<initpop[b].pow4<<"  "<<initpop[b].sumpowgen<<"  "<<initpop[b].fitness;
		  		out << "\n";

		}
		out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }


}

void preent2(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) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {
            		if(j==5||j==15||j==25||j==35)
			  				out<<" ";
			 			out << tempop[b].chr[j];
            }
	     	   out <<"    "<<tempop[b].pow1<<"  "<<tempop[b].pow2<<"  "<<tempop[b].pow3<<"  "<<tempop[b].pow4<<"  "<<tempop[b].sumpowgen<<"  "<<tempop[b].fitness;
		  		out << "\n";

		}
      out<<" avg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }

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

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {
            		if(j==5||j==15||j==25||j==35)
			 				out<<" ";
			  			out << tempop[b].chr[j];
            }
				out <<"    "<<tempop[b].pow1<<"  "<<tempop[b].pow2<<"  "<<tempop[b].pow3<<"  "<<tempop[b].pow4<<"  "<<tempop[b].sumpowgen<<"  "<<tempop[b].fitness;
			  	out << "\n";

		}
      out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
	if(x == 3)
	{
		ofstream out( "load3.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {
            		if(j==5||j==15||j==25||j==35)
			 				out<<" ";
			  			out << tempop[b].chr[j];
            }
			  	out <<"    "<<tempop[b].pow1<<"  "<<tempop[b].pow2<<"  "<<tempop[b].pow3<<"  "<<tempop[b].pow4<<"  "<<tempop[b].sumpowgen<<"  "<<tempop[b].fitness;
			  	out << "\n";

		}
      out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
	if(x == 4)
   {
		ofstream out( "load4.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {
            		if(j==5||j==15||j==25||j==35)
			  				out<<" ";
			 			out << tempop[b].chr[j];
            }
		  		out <<"    "<<tempop[b].pow1<<"  "<<tempop[b].pow2<<"  "<<tempop[b].pow3<<"  "<<tempop[b].pow4<<"  "<<tempop[b].sumpowgen<<"  "<<tempop[b].fitness;
		  		out << "\n";

		}
      out<<" avg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }

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

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {
            		if(j==5||j==15||j==25||j==35)
			 				out<<" ";
			  			out << tempop[b].chr[j];
            }
			  	out <<"    "<<tempop[b].pow1<<"  "<<tempop[b].pow2<<"  "<<tempop[b].pow3<<"  "<<tempop[b].pow4<<"  "<<tempop[b].sumpowgen<<"  "<<tempop[b].fitness;
			  	out << "\n";

		}
      out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
	if(x == 6)
	{
		ofstream out( "load6.txt", ios::app );

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

		srand( time(0) );

 	 	out <<"\n\nGeneration "<<gen<<endl;
      out.setf( ios::fixed );
      out << setprecision(3);
		for(int b=1; b<=popsize; b++)
		{
				for(j=1;j<=lchrom;j++)
            {
            		if(j==5||j==15||j==25||j==35)
			 				out<<" ";
			  			out << tempop[b].chr[j];
            }
			  	out <<"    "<<tempop[b].pow1<<"  "<<tempop[b].pow2<<"  "<<tempop[b].pow3<<"  "<<tempop[b].pow4<<"  "<<tempop[b].sumpowgen<<"  "<<tempop[b].fitness;
			  	out << "\n";

		}
      out<<" \navg:"<<avgfit<<"  maxfit:"<<maxfit<<"  minfit:"<<minfit<<"  error:"<<error;
      //out<<error<<endl;
    }
}

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

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

		srand( time(0) );

 	 	out <<endl;


    }

	if(x == 2)
   {
		ofstream out( "load2.txt", ios::out );

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

		srand( time(0) );

	 	out <<endl;

    }
	if(x == 3)
 	{
		ofstream out( "load3.txt", ios::out );

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

		srand( time(0) );

	 	out <<endl;

    }
	if(x == 4)
   {
		ofstream out( "load4.txt", ios::out );

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

		srand( time(0) );

 	 	out <<endl;


    }

	if(x == 5)
   {
		ofstream out( "load5.txt", ios::out );

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

		srand( time(0) );

	 	out <<endl;

    }
	if(x == 6)
 	{
		ofstream out( "load6.txt", ios::out );

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

		srand( time(0) );

	 	out <<endl;

   }
}

void jeremy()
{
	clrscr();
   gotoxy(30,11);
   cout<<"Solution found.";
   gotoxy(20,13);
   cout<<"Press any key to see the statistics...";
   getch();
   clrscr();
   gotoxy(1,7);
   cout<<"--------------------------------------------------------------------------------";
   gotoxy(1,11);
   cout<<"--------------------------------------------------------------------------------";
   gotoxy(1,20);
   cout<<"--------------------------------------------------------------------------------";

   gotoxy(2,9);
   cout<<"Stage\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(4,12 + rini);
		cout<<rini;
   }

   gotoxy(10,9);
   cout<<"PG1\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(8,12 + rini);
		cout<<solution[rini].pw1;
   }

   gotoxy(20,9);
   cout<<"PG2\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(18,12 + rini);
		cout<<solution[rini].pw2;
   }

   gotoxy(30,9);
   cout<<"PG3\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(28,12 + rini);
		cout<<solution[rini].pw3;
   }

   gotoxy(40,9);
   cout<<"PG4\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(38,12 + rini);
		cout<<solution[rini].pw4;
   }

   gotoxy(48,9);
   cout<<"Total(MW)\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(48,12 + rini);
		cout<<solution[rini].total_pow;
   }

   gotoxy(59,9);
   cout<<"Total(RM)\n";
	for(rini=1;rini<=6;rini++)
   {
      gotoxy(59,12 + rini);
		cout<<solution[rini].fitness;
   }

   gotoxy(71,9);
   cout<<"TotalGen.\n";

	for(rini=1;rini<=6;rini++)
   {
      gotoxy(73,12 + rini);
		cout<<solution[rini].conv;
   }

}



⌨️ 快捷键说明

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