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

📄 rtcgps.cpp

📁 基于GPS的多目标优化及动态多目标优化源代码 里面 包含MOP, DMOP的程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// GPS.cpp: implementation of the RTCGPS class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RTDMOEA.h"
#include "RTCGPS.h"
#include "Random.h"
#include <assert.h>
#include "float.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int RTCGPS::PopSize ; //=100
int RTCGPS::BetterPolicy; //=1
int RTCGPS::PopTypeInitPolicy;//=1
int RTCGPS::EvaluationNum=0;
char RTCGPS::ProblemName[128];
/************************************************************************/
/* 算法策略                                                             */
/*         1: 使用超变异策略,并使用Delta变异算子                        */
/*         2: 使用超变异策略,使用随机变异算子                           */
/*         3: 使用超变异策略,Baseline和Scale自适应                      */
/*         4: 使用记忆体策略,固定Baseline和Scale                        */
/*         5: 使用记忆体策略,Baseline和Scale自适应                      */
/*         6: 使用向量预测策略来适应Baseline和Scale                     */
/************************************************************************/
int RTCGPS::AlgorithmPolicy;//=1
/************************************************************************/
/* 精英空间的策略                                                       */
/*         1: 利用精英空间中的解                                        */
/*         2: 废弃精英空间中的解                                        */
/************************************************************************/
int RTCGPS::ElitistPolicy ;//=2
BOOL RTCGPS::EliteSpaceFirst ;
/************************************************************************/
/* 生成器策略                                                           */
/*         1: CGPS:采用邻居交叉                                         */
/*         2: CGPS:采用随机邻居交叉                                     */
/*         3: CGPS:采用邻居双交叉                                       */
/*         4: CGPS:采用随机邻居双交叉                                   */
/*         5: CGPS:采用郭涛算法                                         */
/*         6: CGPS:采用SPEA2中的算子                                    */
/*         9: DGPS:                                                     */
/************************************************************************/
int RTCGPS::GeneratorPolicy; // =1

double RTCGPS::HyperMutationRate ;
int RTCGPS::ChangeGenerationNum;//=2000
/************************************************************************/
/* DGPS中的变量                                                         */
/************************************************************************/
double RTCGPS::lamda;
int RTCGPS::CrossoverNum;
/************************************************************************/
/* 郭涛算法中的变量                                                     */
/************************************************************************/
int RTCGPS::ParentsNum;
/************************************************************************/
/* 处理普通多目标优化的变量,用来控制是否停机                           */
/************************************************************************/
int RTCGPS::StopOK = 0;
int RTCGPS::StopGenerationNum =10;

DGPSSortNode::DGPSSortNode()
{
	this->SelctValue =0;
}

DGPSSortNode::~DGPSSortNode()
{
}

RTCGPS::RTCGPS()
{
	BestSolutions = new CSolution[CSolution::OBJectiveNum +1];
	for (int i=0;i< CSolution::OBJectiveNum+1;i++)
	{
		BestSolutions[i].Init();
        BestSolutions[i].type =i;
		BestSolutions[i].Objective[i] =10000;
		//        for (int j=0;j<CSolution::OBJectiveNum+1;j++)
		//		{
		//			BestSolutions[i].Objective[j]=10000;
		//		}
	}
	
	ProblemType =1;
	
	m_Pop = new CSolution[RTCGPS::PopSize];
	EnvironmentChangedTimes=0;
	CurrentGeneration=0;
	m_Selct = new DGPSSortNode[RTCGPS::PopSize];
	GUOPopPtr= new int[ParentsNum];
	GUOCoefficient= new double[ParentsNum];
	
}

RTCGPS::~RTCGPS()
{
	delete[] m_Pop;
	delete[] m_Selct;
	//	delete [] GUOPopPtr;
	//	delete [] GUOCoefficient;
	/*	for (int i=0;i< CSolution::OBJectiveNum+1;i++)
	{
	BestSolutions[i].preDelete();
	}
	delete [] BestSolutions;
	*/	
}

void RTCGPS::RunGeneration(int GenerationNum)
{
	   for (int iCount=0;iCount< RTCGPS::PopSize ;iCount++)
	   {
		   SaveBest(&m_Pop[iCount]);
	   }	   
	   for (int iGen=0;iGen< GenerationNum;iGen++,CurrentGeneration++)
	   {
		   if (CurrentGeneration%StopGenerationNum==0)
		   {
			   StopOK = TRUE;
		   }
		   RunStep();
		   bool ChangeFlag= FALSE;
		   //		for (int popi=0; popi<PopSize;popi++ )
		   //		{    
		   //			ChangeFlag = CheckSentinel(m_Pop[popi]);
		   //			if (ChangeFlag) break;
		   //		}
		   if(CurrentGeneration%ChangeGenerationNum ==(ChangeGenerationNum-1))
		   {
			   ChangeFlag= true;
		   }
		   /// if the environment changes
		   if (ChangeFlag)
		   {
			   OnEnvironmentChanged();
		   }
		   if (CurrentGeneration%StopGenerationNum==(StopGenerationNum -1) && StopOK )
		   {
			   break;
		   }
	   }
	   //在停机以后打印最终结果
	   PrintFinalResult();
	   
}

CSolution* RTCGPS::CrossoverDblNeighbor(int Host, CSolution *pOutSolution,CSolution *pOutSolution2)
{
	pOutSolution->Copy(&m_Pop[Host]);
	int GeneSize = CSolution::GetGeneLength();
	double tt= CRandom::Random(CrossoverLowerLimit, CrossoverUpperLimit);
	int other =(Host+1) % PopSize;
	for (int i=0;i< GeneSize;i++)
	{
		pOutSolution->Gene[i]= tt * m_Pop[Host].Gene[i] +(1-tt)*m_Pop[other].Gene[i];
		pOutSolution2->Gene[i] =(1- tt) * m_Pop[Host].Gene[i] +(tt)*m_Pop[other].Gene[i];
	}
	return pOutSolution;
}
CSolution* RTCGPS::Crossover(int Host1, int Host2,CSolution *pOutSolution)
{
	int GeneSize = CSolution::GetGeneLength();
	double tt= CRandom::Random(CrossoverLowerLimit, CrossoverUpperLimit);
	for (int i=0;i< GeneSize;i++)
	{
		pOutSolution->Gene[i]= tt * m_Pop[Host1].Gene[i] +(1-tt)*m_Pop[Host2].Gene[i];
	}
	return pOutSolution;
}

CSolution* RTCGPS::Crossover(CSolution*Ind1, CSolution * Ind2,CSolution *pOutSolution)
{
	int GeneSize = CSolution::GetGeneLength();
	double tt= CRandom::Random(CrossoverLowerLimit, CrossoverUpperLimit);
	for (int i=0;i< GeneSize;i++)
	{
		pOutSolution->Gene[i]= tt * Ind1->Gene[i] +(1-tt)*Ind2->Gene[i];
	}
	return pOutSolution;
}

CSolution* RTCGPS::CrossoverDblRandom(int Host, CSolution *pOutSolution,CSolution *pOutSolution2)
{
	pOutSolution->Copy(&m_Pop[Host]);
	int GeneSize = CSolution::GetGeneLength();
	double tt= CRandom::Random(CrossoverLowerLimit, CrossoverUpperLimit);
    //EDIT HERE
	//当多个种群的时候,似乎需要改动
    //EDIT HERE
	int other;
	   other= CRandom::Random(0,PopSize -1);
	   
	   for (int i=0;i< GeneSize;i++)
	   {
		   pOutSolution->Gene[i]= tt * m_Pop[Host].Gene[i] +(1-tt)*m_Pop[other].Gene[i];
		   pOutSolution2->Gene[i] =(1- tt) * m_Pop[Host].Gene[i] +(tt)*m_Pop[other].Gene[i];
	   }
	   return pOutSolution;
}


CSolution* RTCGPS::Mutation(int Host, CSolution *pOutSolution)
{
	pOutSolution->Copy(&m_Pop[Host]);
	double tt= CRandom::Random(DeltaLowerLimit, DeltaUpperLimit);
	int who =  CRandom::Random(0, CSolution::GetGeneLength() -1);
	pOutSolution->Gene[who]= tt + pOutSolution->Gene[who] ;
	return pOutSolution;
}

CSolution* RTCGPS::HyperDeltaMutate(int Host)
{
	double tt= CRandom::Random(DeltaLowerLimit, DeltaUpperLimit);
	int who =  CRandom::Random(0, CSolution::GetGeneLength() -1);
	m_Pop[Host].Gene[who]= tt + m_Pop[Host].Gene[who] ;
	return &m_Pop[Host];
}

BOOL RTCGPS::BetterNewWSM(CSolution &m_1, CSolution &m_2,int Type)
{
	
	if (Type != CSolution::OBJectiveNum)
	{
		if (m_1.Objective[Type]< m_2.Objective[Type]) return true;
		else 
			if (eDominated(m_1, m_2)) return true;
			return false;
	}
	
	double Fitness1=0, Fitness2=0;
	for (int i=0; i<CSolution::OBJectiveNum;i++)
	{
		Fitness1  += m_1.Objective[i];
		Fitness2  += m_2.Objective[i];
	}
	if (Fitness1 <Fitness2 )	
		return true;
	else
		return false;
}

BOOL RTCGPS::BetterEcology(CSolution &m_1, CSolution &m_2)
{
	BOOL isDominated=FALSE;
	for (int i=0;i<CSolution::OBJectiveNum ;i++)
	{
		if (m_1.Objective[i]>m_2.Objective[i])
		{
			isDominated = TRUE;
			break;
		}
	}	
	if (!isDominated)return true;
	//******当有3个以上的目标时,***似乎*** 不能使用 CHANGTYPE = TRUE 
	// 和 顺序分布的 Type
	//***************/
	//此处 CSolution::OBJectiveNum  表示中间类型
	
	double Fitness1 =0;
	double Fitness2 =0;
	
	int WhichObj=0;
	if ( (m_1.type <=m_2.type) && ( m_1.type + CSolution::OBJectiveNum /2 >=m_2.type) ||
		(m_1.type >m_2.type) && ( m_2.type + CSolution::OBJectiveNum /2 >=m_1.type)  )
    {
		WhichObj = min(m_1.type ,m_2.type );
	}
	else WhichObj = max(m_1.type ,m_2.type );
	
	
	
	if ((WhichObj==CSolution::OBJectiveNum))
	{
		
		for (int i=0; i<CSolution::OBJectiveNum;i++)
		{
			Fitness1  += (atan(m_1.Objective[i]));
			Fitness2  += (atan(m_2.Objective[i]));
			
		}
		if (Fitness1 <Fitness2 )
			return true;
		else return false;
	}
	else
	{
		if (m_1.Objective[WhichObj] <m_2.Objective[WhichObj] )
			return true;
		else return false;
	}
	
	
	/*	switch (m_1.type)
	{
	case 0:
	switch (m_2.type)
	{
	case 0:// 比较毒性
	case 1:// 比较毒性
			 if (m_1.Objective[0] <m_2.Objective[0] )
			 return true;
			 else return false;
			 break;
			 case 2: //同时比较繁殖力和毒性
			 if ((atan(m_1.Objective[1]) + atan(m_1.Objective[0]))< (atan(m_2.Objective[1]) + atan(m_2.Objective[0]))) return true;
			 else return false;
			 break;
			 }
			 break;
			 case 2:
			 switch (m_2.type)
			 {
			 case 0:// 比较毒性和繁殖力
			 case 2:
			 if ((atan(m_1.Objective[1]) + atan(m_1.Objective[0]))< (atan(m_2.Objective[1]) + atan(m_2.Objective[0]))) return true;
			 else return false;
			 break;
			 case 1: //比较繁殖力
			 if (m_1.Objective[1] <m_2.Objective[1] )
			 return true;
			 else return false;
			 break;
			 }
			 break;
			 case 1:
			 switch (m_2.type )
			 {
			 case 0:// 比较毒性
			 if (m_1.Objective[0] <m_2.Objective[0] )
			 return true;
			 else return false;
			 break;
			 case 2:
			 case 1: //比较繁殖力
			 if (m_1.Objective[1] < m_2.Objective[1] )
			 return true;
			 else return false;
			 break;
			 }
			 break;
			 default:
			 break;
			 }
	*/
	return false;
}

BOOL RTCGPS::BetterRandom(CSolution &m_1, CSolution &m_2)
{
	BOOL isDominated=FALSE;
	for (int i=0;i<CSolution::OBJectiveNum ;i++)
	{
		if (m_1.Objective[i]>m_2.Objective[i])
		{
			isDominated = TRUE;
			break;
		}
	}	
	if (!isDominated)return true;
	double t1= CRandom::Random(0.0,0.99999999);
	int obj=0;
    int tObjNum=(int)floor(t1 * (CSolution::OBJectiveNum +1));
    if (tObjNum==CSolution::OBJectiveNum)
	{
		double Fitness1=0, Fitness2=0;
		for (int i=0; i<CSolution::OBJectiveNum;i++)
		{
			Fitness1  += (atan(m_1.Objective[i]));
			Fitness2  += (atan(m_2.Objective[i]));
			
		}
		if (Fitness1 <Fitness2 )
			return true;
		else return false;
	}
	if (m_1.Objective[tObjNum] < m_2.Objective[tObjNum]) return true;
	else return false;
}

BOOL RTCGPS::BetterIndependent(CSolution &m_1, CSolution &m_2,int Type)
{
	//	if (	Dominated(m_1,m_2) ) return TRUE;
	///单独演化策略//
	//	if  ( 
	//		(Type != CSolution::OBJectiveNum) 
	//		&& 	fabs(m_1.Objective[Type ] - m_2.Objective[Type]) < 0.001 
	//		|| 
	//        (Type == CSolution::OBJectiveNum) 
	//		)
	//	{
	//			if (m_1.Objective[] <Fitness2 )
	//			return true;
	//		else return false;
	//	}
	
	if (m_1.Objective[Type ] < m_2.Objective[Type]) return true;
	else 
	{
		if (m_1.Objective[Type ] == m_2.Objective[Type])
		{
			if (Type == CSolution::OBJectiveNum)  
				return true; 
			return (m_1.Objective[CSolution::OBJectiveNum] < m_2.Objective[CSolution::OBJectiveNum]);
		}
	}return false;
}

BOOL RTCGPS::Better(CSolution &m_1, CSolution &m_2,int Type)
{
	//	ASSERT(Type >=0 && Type <= CSolution::OBJectiveNum +1);
    if (Type <0 || Type > CSolution::OBJectiveNum +1)
	{
		char a[128];
		sprintf(a,"ErrorType:%d\n\0",Type);
	}
	switch (BetterPolicy)
	{
	case 0: 
		return BetterRandom(m_1,m_2);
	case 1: 
		return BetterEcology(m_1,m_2);
	case 2:
		return BetterIndependent(m_1,m_2,Type);
	case 4:
		return BetterNewWSM(m_1,m_2,Type);
	default:
		MessageBox(NULL,"Better Policy","Error ",MB_OK);
		exit(0);
	}
}
void RTCGPS::RunStep()
{
    switch (GeneratorPolicy)
	{

⌨️ 快捷键说明

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