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

📄 objective.cpp

📁 多目标优化进化算法目前公认效果收敛性最好的算法NSGA2c++源码,具有一般性
💻 CPP
字号:
///////////////////////////////////////////////////////////////////
/*****************************************************************
Copyright: Artificial Life and Adaptive Robotics Laboratory - ALAR
School of ITEE, UNSW@ADFA, Australia, 2005
*******************************************************************/
/////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "objective.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////objective::objective(){	objective_value=&objective::problem1; //default;	}objective::objective(int ID){	setObjID(ID);	}objective::~objective(){	}/* ------------------Usage---------------------------------objective obj; //general objective;cout << obj.objective_value1(0.7,0.2,&objective::f1); double objective::objective_value1(double x1, double x2,double (objective::*fp)(double,double))	{	return (this->*fp)(x1,x2);	}---------------------end---------------------------------------*/void objective::setObjID(int ID){	rnd.setSeed(1000);			probID=ID;		switch(ID)	{	case 1:		objective_value=&objective::problem1;		break;	case 2:		objective_value=&objective::problem2;		break;	case 3:		objective_value=&objective::problem3;		break;	case 4:		objective_value=&objective::problem4;		break;	case 6:		objective_value=&objective::problem6;		break;	}} double objective::problem1(double *x, int size,int funcID) { 	if (funcID==0)  		return x[0]; 	else 	{ 		int i; 		double g=0.0; 		double F1=x[0]; 		for (i=1;i<size;i++) 			g+=x[i]; 		g=1.0+9.0*g/(size-1); 		return g*(1-sqrt(F1/g)); 	} } double objective::problem2(double *x, int size,int funcID) { 	if (funcID==0)  		return x[0]; 	else 	{ 		int i; 		double g=0.0; 		double F1=x[0]; 		for (i=1;i<size;i++) 			g+=x[i]; 		g=1.0+9.0*g/(size-1); 		return g*(1-pow(F1/g,2)); 	} } double objective::problem3(double *x, int size,int funcID) { 	if (funcID==0)  		return x[0]; 	else 	{ 		int i; 		double g=0.0; 		double F1=x[0]; 		for (i=1;i<size;i++) 			g+=x[i]; 		g=1.0+9.0*g/(size-1); 		return g*(1-pow(F1/g,2)-(F1/g)*sin(10*PI*F1)); 	} } double objective::problem4(double *x, int size,int funcID) { 	double g=0,F1=x[0]; 	int i; 	if (funcID==0) 		return F1; 	else 	{ 		for (i=1;i<size;i++) 			g+=x[i]*x[i]-10*cos(4*PI*x[i]); 		g=1+10*(size-1)+g; 		return g*(1-sqrt(F1/g)); 	} } double objective::problem5(unsigned int *x, int size,int funcID) { 	 	double g=0,F1=0; 	int i,j,temp=0; 	for (i=0;i<30;i++) 	{ 		 		if (x[i]==1) F1+=1; 	} 	     F1=1+F1; 	if (funcID==0) 		return F1; 	else 	{ 		 		for (i=1;i<11;i++) 		{ 			temp=0; 			for (j=0;j<5;j++) 				if (x[30+5*(i-1)+j]==1)  temp++; 				if (temp<5)  					g+=2+temp; 				else 					g+=1; 				 		} 		return g*(1/F1); 	} } double objective::problem6(double *x, int size,int funcID) { 	 	double g=0,F1=1-exp(-4*x[0])*pow(sin(6*PI*x[0]),6); 	int i; 	if (funcID==0) 		return F1; 	else 	{ 		for (i=1;i<size;i++) 			g+=x[i]; 		g=1+9*pow((g/(size-1)),0.25); 		return g*(1-pow(F1/g,2)); 	}	 }double objective::problem7(double *x, int size, int funcID, int evaltime, int gen){		double g=0,F1=0;	//int t;	if (funcID==0)	{		if (gen==0)			return evaltime;		else		{						return 100+gen;			}	}else	{		//t= evaltime % 5000;		//if (t==0)			//{		//	change_peaks();		//}		g = maxheight - eval_movpeaks(x);				logging();		return g;	}}double objective::problem8(double *x, int size, int funcID, int evaltime, int gen){		double g=0,F1=0;	//int t;	if (funcID==0)	{		return rnd.nextDouble();	}else	{						g = maxheight - eval_movpeaks(x);		logging();				return g;	}}double objective::problem9(double *x, int size, int funcID, int evaltime, int gen){		double g=0,F1=0;	//int t;	if (funcID==0)	{		return 0;		}else	{						g = maxheight - eval_movpeaks(x);		logging();		return g;	}}void objective::peaksetting(int size){	geno_size=size;	peak_function=peak_function_cone;	use_basis_function = 0;	lambda=0;	init_peaks();


		char *st_prob=new char[2];
	char *filename = new char[40];
	sprintf(st_prob,"%d_",probID);
	
	char *st_pc=new char[5];
	sprintf(st_pc,"%1.2f_",al_pc);
	
	char *st_pm=new char[5];
	sprintf(st_pm,"%1.2f_",al_pm);
	
	
	char *st_seed=new char[5];
	sprintf(st_seed,"%d",cur_SEED);
	
	strcpy(filename,st_prob);
	
	strcat(filename,st_pc);
	strcat(filename,st_pm);
	
	strcat(filename,st_seed);
	strcat(filename,".txt");
	f1.open(filename,ios::app);}void objective::logging(){	//int eval=get_number_of_evals();	double avg_error=get_avg_error();	//double curr_error=get_current_error();	//double offline_per=get_offline_performance();	double offile_error=get_offline_error();		//f1  << eval << "\t" 	//f1	<< setiosflags(ios::fixed|ios::showpoint) << setprecision(3) << global_max   << "\t"	f1		<< setiosflags(ios::fixed|ios::showpoint) << setprecision(3) << avg_error    << "\t"	//f1	<< setiosflags(ios::fixed|ios::showpoint) << setprecision(3) << curr_error   << "\t"	//	<< setiosflags(ios::fixed|ios::showpoint) << setprecision(3) << offline_per  << "\t"		<< setiosflags(ios::fixed|ios::showpoint) << setprecision(3) << offile_error << "\t" << endl;	//f1.close(); }double objective::problem10(double *x, int size, int funcID, int evaltime, int gen)
{
	double g=0,F1=0;
	//int t;
	if (funcID==0)
	{
		return 0;	
	}else
	{
		
		
		g = maxheight - eval_movpeaks(x);
		logging();
		return g;
	}
}

double objective::problem11(double *x, int size, int funcID, int evaltime, int gen)
{
	double g=0,F1=0;
	//int t;
	if (funcID==0)
	{
		return 0;	
	}else
	{
		
		
		g = maxheight - eval_movpeaks(x);
		logging();
		return g;
	}
}
double objective::problem12(double *x, int size, int funcID, int evaltime, int gen)
{
	double g=0,F1=0;
	//int t;
	if (funcID==0)
	{
		return 0;	
	}else
	{
		
		g = maxheight - eval_movpeaks(x);
		logging();
		return g;
	}
}

⌨️ 快捷键说明

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