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

📄 rtcgps.cpp

📁 基于GPS的多目标优化及动态多目标优化源代码 里面 包含MOP, DMOP的程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   ReplaceSolution(iCount,m_temp);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
		   
		   CrossoverRandom(iCount,&m_temp);
		   SetFitness(m_temp);
		   IsParetoPoint = m_Elist.AddSolution(m_temp);
		   isBetter =Better(m_temp,m_Pop[iCount],m_Pop[iCount].type );
		   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   ReplaceSolution(iCount,m_temp);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
		   
	   }
}

void RTCGPS::RunStep3()
{
	   CSolution m_temp(TRUE);
	   CSolution m_temp2(TRUE);
	   BOOL IsParetoPoint;
	   for (int iCount=0;iCount< RTCGPS::PopSize ;iCount++)
	   {
		   CrossoverDblNeighbor(iCount,&m_temp,&m_temp2);
		   SetFitness(m_temp);
		   SetFitness(m_temp2);
		   IsParetoPoint = m_Elist.AddSolution(m_temp);
		   IsParetoPoint = IsParetoPoint ||m_Elist.AddSolution(m_temp);
		   BOOL isBetter =Better(m_temp,m_Pop[iCount],m_Pop[iCount].type );
		   isBetter =isBetter ||Better(m_temp2,m_Pop[iCount],m_Pop[iCount].type );
           if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   if (Better(m_temp,m_temp2,m_Pop[iCount].type))
			   {
				   ReplaceSolution(iCount,m_temp);
				   continue;
			   }
			   
			   ReplaceSolution(iCount,m_temp2);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   if (Better(m_temp,m_temp2,m_Pop[iCount].type))
			   {
				   ReplaceSolution(iCount,m_temp);
				   continue;
			   }
			   ReplaceSolution(iCount,m_temp2);
			   continue;
		   }
		   Mutation(iCount,&m_temp);
		   SetFitness(m_temp);
		   isBetter =Better(m_temp,m_Pop[iCount],m_Pop[iCount].type );
		   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   ReplaceSolution(iCount,m_temp);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
	   }
}

void RTCGPS::RunStep4()
{
	   CSolution m_temp(TRUE);
	   CSolution m_temp2(TRUE);
	   BOOL IsParetoPoint;
	   for (int iCount=0;iCount< RTCGPS::PopSize ;iCount++)
	   {
		   CrossoverDblRandom(iCount,&m_temp,&m_temp2);
		   SetFitness(m_temp);
		   SetFitness(m_temp2);
		   IsParetoPoint = m_Elist.AddSolution(m_temp);
		   IsParetoPoint = IsParetoPoint ||m_Elist.AddSolution(m_temp);
		   if (Better(m_temp,m_Pop[iCount],m_Pop[iCount].type ) )
		   {
			   ReplaceSolution(iCount, m_temp);
		   }
		   if (Better(m_temp2,m_Pop[iCount],m_Pop[iCount].type ) )
		   {
			   ReplaceSolution(iCount, m_temp2);
			   continue;
		   }
		   if (IsParetoPoint) 
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
		   Mutation(iCount,&m_temp);
		   SetFitness(m_temp);
		   IsParetoPoint = m_Elist.AddSolution(m_temp);
		   BOOL isBetter =Better(m_temp,m_Pop[iCount],m_Pop[iCount].type );
		   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   ReplaceSolution(iCount,m_temp);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
	   }
}

void RTCGPS::RunStep9()
{
	   CSolution m_temp(TRUE);
	   BOOL IsParetoPoint;
	   ////
	   //  对每一个 个体计算 Select
       //  从小到大排序
	   //  选前n个个体 n= m_1 + m_2 
	   //  整体运动比较少的个体
	   //  前m_1 杂交 后m_2个变异
	   //  
	   
	   /*******BEGIN********************/
	   //****              ************* /
       // For every CSolution *, we must calculate its selection value
	   // and then we sort the sortnodes according their type
	   // we can choose m_1 CSolution *s to crossover and the rest to mutate
	   
	   
	   //***Here calculate the values of selct
	   for (int iCnt=0;iCnt< RTCGPS::PopSize ;iCnt++)
	   {
		   m_Selct[iCnt].SelctValue = lamda* m_Selct[iCnt].Momentum  +(1-lamda)*m_Selct[iCnt].Activity ;
	   }    
   	   //**** for every object 
	   //**** sort the CSolution *s by objectives
	   for (int i=0;i< CSolution::OBJectiveNum+1;i++ )
	   {
		   Sort((int)i*(RTCGPS::PopSize/(CSolution::OBJectiveNum+1)) , (i+1) *(RTCGPS::PopSize/(CSolution::OBJectiveNum+1))-1);
		   
	   }
	   //*********************
	   //for every CSolution *s , run DEA
	   
	   int Division =RTCGPS::PopSize/(CSolution::OBJectiveNum+1);
	   for ( i=0;i< CSolution::OBJectiveNum+1;i++ )
	   {
		   // HERE FOR VALID THE WSM
		   //if (i!=CSolution::OBJectiveNum) continue;
		   
		   for (int m=0;m<CrossoverNum;m++)
		   {
			   iCnt=i*Division+m;
			   Crossover(m_Selct[iCnt].Index ,m_Selct[iCnt+1].Index,&m_temp);
			   SetFitness(m_temp);
			   m_Selct[iCnt].Activity +=1;
			   // Momentum has been added;
			   //			   m_Selct[iCnt].Momentum +=(m_temp.Objective[i] -m_Pop[m_Selct[iCnt].Index].Objective[i]); 
			   IsParetoPoint = m_Elist.AddSolution(m_temp);
			   BOOL isBetter =Better(m_temp,m_Pop[m_Selct[iCnt].Index],m_Pop[m_Selct[iCnt].Index].type);
			   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
			   {
				   ReplaceSolution2(iCnt,m_temp);
				   continue;
			   }
			   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
			   {
				   ReplaceSolution2(iCnt, m_temp);
				   continue;
			   }
		   }
		   
		   for (int k = CrossoverNum; k< RTCGPS::PopSize/(CSolution::OBJectiveNum+1);k++)
		   {
			   int iCnt =i*Division+k;
			   Mutation(m_Selct[iCnt].Index,&m_temp);
			   SetFitness(m_temp);
			   IsParetoPoint = m_Elist.AddSolution(m_temp);
			   
			   BOOL isBetter =Better(m_temp,m_Pop[m_Selct[iCnt].Index],m_Pop[m_Selct[iCnt].Index].type );
			   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
			   {
				   ReplaceSolution2(iCnt,m_temp);
				   continue;
			   }
			   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
			   {
				   ReplaceSolution2(iCnt, m_temp);
				   continue;
			   }
		   }
		   
		   
	   }	   
}

BOOL RTCGPS::ReplaceSolution2(int Which, CSolution &m_temp)
{
	//*******************************
	// 此处的which指的是 在m_Selct中的坐标
	//******************************
	int type =m_Pop[m_Selct[Which].Index].type;
	m_Selct[Which].Momentum += (m_temp.Objective[type] - m_Pop[m_Selct[Which].Index].Objective[type])  ;
	
	m_Pop[m_Selct[Which].Index].Copy (&m_temp);
	return TRUE;
}


void RTCGPS::Sort(int m_Start, int m_End)
{
	// sorting the m_Selct array according the value of Slect asc
	
	BOOL isChanged =FALSE;
	for (int i=m_Start;i<m_End+1;i++)
	{
        isChanged = FALSE;
		for (int j=i; j< m_End;j++)
		{
			if (m_Selct[j].SelctValue <m_Selct[j+1].SelctValue  )
			{
				isChanged =FALSE;
				swap(m_Selct[j],m_Selct[j+1]);
			}
		}
		if (isChanged ==FALSE) return;
	}
	
}


void RTCGPS::CrossoverMultiParent(int PopIndex, CSolution *pOutSolution)
{
	
	int			i,j;
	bool		ValidFlag;
	double		tmpsum,tmpMin,tmpMax;
	
	int Base;
	if(BetterPolicy==2) //当使用多种群策略时
	{
		Base = PopSize / (CSolution::OBJectiveNum +1);
	}
	else
		Base = PopSize;
	
	for(i=0;i<ParentsNum;i++)
	{
		if(BetterPolicy==2) //当使用多种群策略时
		{
			GUOPopPtr[i] = m_Pop[PopIndex].type * Base +CRandom::Random(0,Base -1);
		}
		else
			GUOPopPtr[i] = CRandom::Random(0,Base -1);
	}
	ValidFlag = FALSE;
	while(!ValidFlag)
	{
		// Create ParentsNum coefficients a[i] , range from -0.5 to 1.5 , sum 1
		tmpsum = 0.0;
		for(i=0;i<ParentsNum-1;i++)
		{
			tmpMin = 1 - tmpsum - 1.5 * (ParentsNum - i);
			tmpMax = 1 - tmpsum - (-0.5) * (ParentsNum - i);
			if(tmpMin < -0.5)
				tmpMin = -0.5;
			if(tmpMax > 1.5)
				tmpMax = 1.5;
			GUOCoefficient[i] = CRandom::Random( -0.5,1.5);
			tmpsum += GUOCoefficient[i]; 
		}
		GUOCoefficient[ParentsNum-1] = 1 - tmpsum;
		if((GUOCoefficient[ParentsNum-1] > 1.5) || (GUOCoefficient[ParentsNum-1] < -0.5))
			continue;
		
		ValidFlag = TRUE;	
		
		for(j=0;j<CSolution::GetGeneLength();j++)
		{
			pOutSolution->Gene[j]=0;
			for(i=0;i<ParentsNum;i++)
				pOutSolution->Gene[j] += GUOCoefficient[i]*m_Pop[GUOPopPtr[i]].Gene[j];
		}
	}
	
}

void RTCGPS::RunStep5()
{
	   CSolution m_temp(TRUE);
	   BOOL IsParetoPoint;
	   for (int iCount=0;iCount< RTCGPS::PopSize ;iCount++)
	   {
		   CrossoverMultiParent(iCount, &m_temp);
		   SetFitness(m_temp);
		   IsParetoPoint = m_Elist.AddSolution(m_temp);
		   BOOL isBetter =Better(m_temp,m_Pop[iCount],iCount );
		   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   ReplaceSolution(iCount,m_temp);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
		   
		   Mutation(iCount,&m_temp);
		   SetFitness(m_temp);
		   IsParetoPoint = m_Elist.AddSolution(m_temp);
		   isBetter =Better(m_temp,m_Pop[iCount],m_Pop[iCount].type );
		   if ( RTCGPS::EliteSpaceFirst && IsParetoPoint || !RTCGPS::EliteSpaceFirst &&isBetter)
		   {
			   ReplaceSolution(iCount,m_temp);
			   continue;
		   }
		   else if (!RTCGPS::EliteSpaceFirst && IsParetoPoint || RTCGPS::EliteSpaceFirst && isBetter)
		   {
			   ReplaceSolution(iCount, m_temp);
			   continue;
		   }
	   }
}

bool RTCGPS::eDominated(CSolution &m_1, CSolution &m_2)
{
	
	for (int i=0;i<CSolution::OBJectiveNum;i++)
	{
		if(m_1.Objective[i]>(m_2.Objective[i])+ 0.00001 ) return false;
	}
	return true;
}

bool RTCGPS::Dominated(CSolution &m_1, CSolution &m_2)
{
	
	for (int i=0;i<CSolution::OBJectiveNum;i++)
	{
		if( 
			((float)m_1.Objective[i]) >((float)m_2.Objective[i])
			) return false;
	}
	return true;
}

void RTCGPS::PrintPopulation()
{
	//	fprintf(fp,"________________________________________________\n");
	//	fprintf(fp,"________________________________________________\n");
	//	fprintf(fp,"________________________________________________\n");
	FILE *fp;
	sprintf(FileName,"%sPOP_%04d.txt\0",ProblemName,EnvironmentChangedTimes);
	fp = fopen(FileName,"w+t");
	
	
	for (int i=0;i<RTCGPS::PopSize;i++)
	{   
		CSolution *p_temp =&m_Pop[i];
		//		if(p_temp->Distance <1000) continue;
		//打印 目标
		for (int j=0;j<CSolution::OBJectiveNum;j++)
			fprintf(fp,"%0.7f	",p_temp->Objective[j]); 
		fprintf(fp,"\n");
	}
	////打印基因
	fprintf(fp,"*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	fprintf(fp,"*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	fprintf(fp,"*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	for (i=0;i<RTCGPS::PopSize;i++)
	{   
		CSolution *p_temp =&m_Pop[i];
		//		if(p_temp->Distance <1000) continue;
		//打印 基因
		for (int j=0;j<CSolution::GetGeneLength();j++)
			fprintf(fp,"%0.7f	",p_temp->Gene[j]); 
		fprintf(fp,"\n");
	}
	
}

void RTCGPS::PrintFinalResult()
{
	FILE *fp;
	sprintf(FileName,"%s_Final.txt\0",ProblemName);
	fp = fopen(FileName,"w+t");
	for (int i=0;i<m_Elist.ElitistSpace.GetSize();i++)
	{   
		CSolution *p_temp = (CSolution *)m_Elist.ElitistSpace.GetAt(i);
        if(p_temp->Distance <1) continue;
		//打印 目标
		for (int j=0;j<CSolution::OBJectiveNum;j++)
			fprintf(fp,"%0.7f	",p_temp->Objective[j]); 
		fprintf(fp,"\n");
	}
	fprintf(fp,"                                                   ");
	
	//打印种群中的信息
	for ( i=0;i<RTCGPS::PopSize;i++)
	{   
		CSolution *p_temp =&m_Pop[i];
		//		if(p_temp->Distance <1000) continue;
		//打印 目标
		for (int j=0;j<CSolution::OBJectiveNum;j++)
			fprintf(fp,"%0.7f	",p_temp->Objective[j]); 
		fprintf(fp,"\n");
	}
	//	////打印基因
	//	fprintf(fp,"*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	//	fprintf(fp,"*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	//	fprintf(fp,"*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
	//	for (i=0;i<RTCGPS::PopSize;i++)
	//	{   
	//		CSolution *p_temp =&m_Pop[i];
	//		//		if(p_temp->Distance <1000) continue;
	//		//打印 基因
	//		for (int j=0;j<CSolution::GetGeneLength();j++)
	//			fprintf(fp,"%0.7f	",p_temp->Gene[j]); 
	//		fprintf(fp,"\n");
	//	}
	fclose(fp);
}



/* Performs variation. */
int RTCGPS::variate()
{
	int  i ;
    /* do recombination */
	for(i = 0; i < (PopSize - PopSize %2); i+= 2)

⌨️ 快捷键说明

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