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

📄 test1.cpp

📁 用VC++变得实数编码遗传算法,还挺不错的
💻 CPP
字号:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "fstream.h"
#include "gapop1.h"
#include "gareal.h"
#include "string.h"
#include <windows.h>
#include <shellapi.h>
#include <time.h>

/*
switch(testf)
	{
	case 1:s1=1+(t1+t2+1)*(t1+t2+1)*(19-14*t1+3*t1*t1-14*t2+6*t1*t2+3*t2*t2); 
	//-2<=x(i)<=2
		s2=30+(2*t1-3*t2)*(2*t1-3*t2)*(18-32*t1+12*t1*t1+48*t2-36*t1*t2+27*t2*t2);
		s=s1*s2; 
		ss=s;
		break;
		// min=3 x=0 -1 f1 
    case 2:
		s1=(4-2.1*t1*t1+t1*t1*t1*t1/3)*t1*t1;
		//[-3 x 3 ] [-2 y 2]
		s2=t1*t2+(-4+4*t2*t2)*t2*t2;
		s=s1+s2; ss=s;//s=-s;
		break;
		//min=-1.031628 x= 0.898 -07126 f2 
    case 3:
		for(i=0;i<n1;i++) //-5.12<=x(i)<=5.12 n=3
			s+=x[i]*x[i]; 
		ss=s;
		break;
		//min=0 x=0 0 0 f3
    case 4:
		s1=100*(t1*t1-t2)*(t1*t1-t2);//-2.048<=x(i)<=2.048
		s2=(1-t1)*(1-t1);
		s=s1+s2; 
		ss=s;
		break;
		//min=0 x=1 1 f4
    case 5:
		for(i=1;i<=5;i++) s1+=i*cos((i+1)*t1+i);//-10<=x(i)<=10
		for(i=1;i<=5;i++) s2+=i*cos((i+1)*t2+i);
		s=s1*s2; 
		ss=s;
		break;
		//min=-186.731 x=  f5
    case 6:
		s1=t1*t1+t2*t2; //-100<=x(i)<=100
		s2=50*pow(s1,0.1);
		s=pow(s1,0.25)*(sin(s2)*sin(s2)+1.0);
		ss=s;
		break;
		//min=0 x= 0 0  f6
    case 7:
		s1=t1*t1+t2*t2; //-100<=x(i)<=100
		s2=sqrt(s1);s2=sin(s2)*sin(s2)-0.5;
		s1=1.0+0.001*s1;
		s=0.5+s2/(s1*s1);
		ss=s;s=-s;
		break;
		//min=0 x=0 0  f7
    case 8:
		for(i=1;i<=n1;i++) //-1.28<=x(i)<=1.28 n=30
			s+=i*pow(x[i-1],4);
		ss=s;
		break;
		//min=0 x(i)=0  f8
    case 9:
		for(i=0;i<5;i++) //-65.536<=x(i)<=65.536
		{
			a[0][5*i]=-32;a[0][5*i+1]=-16;
			a[0][5*i+2]=0;a[0][5*i+3]=16;
			a[0][5*i+4]=32;
		}
		for(i=0;i<5;i++) 
		{
			a[1][5*0+i]=-32;a[1][5*1+i]=-16;
			a[1][5*2+i]=0;a[1][5*3+i]=16;
			a[1][5*4+i]=32;
		}
		for(i=0;i<25;i++)
		{
			s1=i+1+pow((t1-a[0][i]),6)+pow((t2-a[1][i]),6);
			s2=s2+1./s1;
		}
		s=0.002+s2;	
		ss=s;s=-s;
		break;
		//max=1.002,x=-32 -32 f9
    case 10:
		s=(20+t1+10*sin(10*t1)+8*cos(3*t1));  // 0<=t1<=10.
		ss=s;
		break;
		// max=46.732  x=8.32966  f10
		// min=3.1874977   x=1.0951176	
	case 11:
		s=sqrt(t1*t1+t2*t2);//0<x<4 n=2
		s1=4.-((t1-2.)*(t1-2.)+(t2-2.)*(t2-2.));
		ga1.nE(1);
		ga1.vecpe(0,s1);
		ss=s;
		//s=s+s1*s1*100;
		//s=s1;
		if(fabs(s1)>0.0000001) { s=s-s1*s1*10;valid=0; }
		s=-s;
		break;
		//min=0.688      x= 0.586 0.586              f11
    case 12:
		s=t1*sin(10*3.1415926*t1)+2.0;  //-1<=t1<=2 f12
		//s=-s;
		ss=-s;
		break;
		//min=0.0497402     x=1.95052
		//max=3.85027              x=1.85055
    case 13:
		s=(3.0/(0.05+t1*t1+t2*t2))*(3.0/(0.05+t1*t1+t2*t2))+(t1*t1+t2*t2)*(t1*t1+t2*t2); 
		//-5.12<=x,y<=5.12
		ss=s;s=-s;
		break;
		//max=3600 x=0 y=0
*/


void main1()
{

	time_t t1,t2;

	double lb[2]={-3.,-2.};
	double hb[2]={3.,2.};
	char ss[32]={""};

	gapop1 p1(20,500,0.6,0.4,10,20646.,20.646);

	srand(time(NULL));
	t1=time(NULL);
	cout<<"now the time is "<<ctime(&t1)<<endl;
//	p1.objfunc(ga1);
//	p1.init();
//	p1.rlast();
//	p1.wlast();
//	p1.poprun();
//	p1.repoprun();
/*	gapop1 p2(20,1000,0.6,0.4,10,20646.,20.646);
	p2.poprun();
	cout<<p2.bestgareal;
	/*ShellExecute(NULL,"open","fo1.txt","","",SW_SHOW );
    ShellExecute(NULL,"open","fo2.txt","","",SW_SHOW );
	ShellExecute(NULL,"open","fo3.txt","","",SW_SHOW );
	ShellExecute(NULL,"open","ga313.txt","","",SW_SHOW );*/
//	WinExec("c:\\winnt\\system32\\notepad.exe myfo1.txt",SW_SHOW);
//    WinExec("c:\\winnt\\system32\\notepad.exe myfo2.txt",SW_SHOW);
//	WinExec("c:\\winnt\\system32\\notepad.exe myfo3.txt",SW_SHOW);
//	WinExec("c:\\winnt\\system32\\notepad.exe ga313.txt",SW_SHOW);
//	system("c:\\winnt\\system32\\notepad.exe ga313.txt");
//	ff();
	t2=time(NULL);
	//long t=t2-t1;
	cout<<"now the time is "<<ctime(&t2)<<endl;
	cout<<"the programm have run time is "<<t2-t1<<"s"<<endl;
}

void ff()
{
	fstream fo;
	int n;
	double lb[2]={-3.,-2.};
	double hb[2]={3.,2.};
	n=20;
	fo.open("ga11.txt",ios::out);
//	gapop1 p1(40,500,0.6,0.4,2,4,0);
//    gapop1 p1(20,60,0.8,0.2,2,hb,lb);
//	gapop1 p1(40,500,0.8,0.2,2,.8,0.);
 //	gapop1 p1(30,80,0.8,0.1,2,2.0,-2.);
	gapop1 p1(30,100,0.8,0.4,10,20646.0,20.6);
//	gapop1 p1(20,50,0.8,0.2,2,2.0,-2.);
	srand(time(NULL));
	for(int i=0;i<n;i++)
	{
		cout<<i<<endl;
		p1.poprun();
		fo<<i<<"\t"<<p1.Bestgen()<<"\t"<<p1.bestgareal;
//		system("ren ga313.txt g01.txt");
	}
	fo.close();
	gapop1 p2(30,2000,0.8,0.5,10,20646.0,20.6);
	p2.poprun();
	//system("c:\\winnt\\system32\\notepad.exe ga11.txt");
	WinExec("c:\\winnt\\system32\\notepad.exe ga11.txt",SW_SHOW);
}

void main()
{
	time_t t1,t2;
    gapop1 p1(20,500,0.6,0.4,2,2.,-2.);
	srand(time(NULL));
	t1=time(NULL);
	cout<<"now the time is "<<ctime(&t1)<<endl;
	//long t=t2-t1;
	p1.objopt1=1;
    p1.settestf(1);
	p1.setmutopt(0,0);
	p1.poprun();
	t2=time(NULL);
	cout<<"now the time is "<<ctime(&t2)<<endl;
	cout<<"the programm have run time is "<<t2-t1<<"s"<<endl;
}
	/* mutation opt1=0-5 
	all-garealn r-rand rp-randpart
	0-all,1-one,2-r>0,3-rp>0,4-a+r0.5,5-a+rp0.5
	opt2=0-10 
	nu-nonuniform u-uniform g-gaussian r-rand
    0-nu,1-u,2-g,3-u+nu0.5,4-g+nu0.5,5-u+g+nu0.33,6-u+nu-r,7-g+nu-r,8-g+u-r,9-nu+u+g-r
	*/
    /* cross opt1=0-4 
	all-garealn r-rand rp-randpart
	0-all,1-r>0,2-rp>0,3-a+r0.5,4-a+rp0.5
	opt2=0-10 
	ac-arithmetic crossover bc-blend crossover fc-flat crossover r-rand
    0-ac,1-bc,2-fc,3-ac+bc0.5,4-ac+fc0.5,5-fc+bc0.5,6-ac+bc-r,7-ac+fc-r,8-bc+fc-r,9-ac+bc+fc-r
	*/

⌨️ 快捷键说明

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