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

📄 rtcgpsmop.cpp

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

#include "stdafx.h"
#include "RTDMOEA.h"
#include "RTCGPSMOP.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

RTCGPSMOP::RTCGPSMOP()
{
    theta = new double[CSolution::OBJectiveNum * sizeof(double)];
}

RTCGPSMOP::~RTCGPSMOP()
{
    delete [] theta;
}


void RTCGPSMOP::InitPop()
{
//	PID = GetProblemID(RTCGPS::ProblemName);
	CSolution t_temp;
	t_temp.Init();
	for (int i=0;i<PopSize;i++)
	{
		switch (PID)
		{
		case 91:
			init_COMET(&t_temp);
			break;
		case 92:
		case 93:
			init_KUR(&t_temp);
			break;
		case 14: //ZDT4
			init_ZDT4(&t_temp);
			break;
		case 94:
			init_QV(&t_temp);
			break;
		case 95:
            init_SPHERE(&t_temp);
			break;
		case 11:
		case 12:
		case 13:
		case 16:
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:
		case 27:
			init_Common(&t_temp);
			break;
		default:
			MessageBox(NULL, "No Such Init ", "ERROR", MB_OK);
			exit(0);
			break;
		}
		m_Pop[i].Init();
		m_Pop[i].Copy(&t_temp);
	}
	InitPopType();
	
}

void RTCGPSMOP::ComputeFitness(CSolution &m_1)
{
	EvaluationNum++;
	switch (PID)
	{
	case 91:
		Fix_COMET(&m_1);
        eval_COMET(&m_1) ;
		break;

	case 92:
		Fix_KUR(&m_1);
		eval_KURN(&m_1) ;
		break;
	case 93:
		Fix_KUR(&m_1);
		eval_KURS(&m_1) ;
		break;
	case 94:
		Fix_QV(&m_1);
		eval_QV(&m_1) ;
		break;
	case 11:
        Fix_Common(&m_1);
		eval_ZDT1(&m_1) ;
        break;
	case 12:
        Fix_Common(&m_1);
		eval_ZDT2(&m_1) ;
		break;
	case 13:
        Fix_Common(&m_1);
		eval_ZDT3(&m_1) ;
		break;
	case 14:
       Fix_ZDT4(&m_1);
		eval_ZDT4(&m_1) ;
		break;
	case 16:
//        Fix_Common(&m_1);
        Fix_Common2(&m_1);
		eval_ZDT6(&m_1) ;
		break;
	case 21:
        Fix_Common(&m_1);
		eval_DTLZ1(&m_1);
		break;
	case 22:
        Fix_Common(&m_1);
		eval_DTLZ2(&m_1) ;
		break;
	case 23:
        Fix_Common(&m_1);
		eval_DTLZ3(&m_1) ;
		break;
	case 24:
        Fix_Common(&m_1);
		eval_DTLZ4(&m_1) ;
		break;
	case 25:
        Fix_Common(&m_1);
		eval_DTLZ5(&m_1) ;
		break;
	case 26:
        Fix_Common(&m_1);
		eval_DTLZ6(&m_1) ;
		break;
	case 27:
        Fix_Common(&m_1);
		eval_DTLZ7(&m_1) ;
		break;
	case 95:
//        Fix_Common(&m_1);
		eval_SPHERE(&m_1) ;
		break;
	default:
        MessageBox(NULL, "No Such Functions To Evalute ", "ERROR", MB_OK);
		exit(0);
		break;
	}
	return;
}

void RTCGPSMOP::RandomMutate(int Index)
{
//	MessageBox(NULL,"RandomMutate Should Not Exist","ERROR",MB_OK);
	CSolution * t_temp = &m_Pop[Index];
	for (int i=0;i<PopSize;i++)
	{
		switch (PID)
		{
		case 91:
			init_COMET(t_temp);
			break;
		case 14:
			init_ZDT4(t_temp);
			break;
		case 92:
		case 93:
			init_KUR(t_temp);
			break;
		case 94:
			init_QV(t_temp);
			break;
		case 95:
            init_SPHERE(t_temp);
			break;
		case 11:
		case 12:
		case 13:
		case 16:
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:
		case 27:
			init_Common(t_temp);
			break;
		default:
			MessageBox(NULL, "No Such Init ", "ERROR", MB_OK);
			exit(0);
			break;
		}
	}
}


int RTCGPSMOP::eval_DTLZ1(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    
    double g = 0;
    for (i = n - k + 1; i <= n; i++)
    {
		g += pow(ind->Gene[i-1]-0.5,2) - cos(20 * PI * (ind->Gene[i-1]-0.5));
    }
    g = 100 * (k + g);
    
    for (i = 1; i <= CSolution::OBJectiveNum; i++)
    {
		double f = 0.5 * (1 + g);
		for (j = CSolution::OBJectiveNum - i; j >= 1; j--)
		{
			f *= ind->Gene[j-1];
		}
		if (i > 1)
		{
			f *= 1 - ind->Gene[(CSolution::OBJectiveNum - i + 1) - 1];
		}
		
		ind->Objective[i-1] = f;
    }
	
    return 0;
}

int RTCGPSMOP::eval_DTLZ2(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    
    double g = 0;
    for (i = n - k + 1; i <= n; i++)
    {
		g += pow(ind->Gene[i-1]-0.5,2);
    }
    
    for (i = 1; i <= CSolution::OBJectiveNum; i++)
    {
		double f = (1 + g);
		for (j = CSolution::OBJectiveNum - i; j >= 1; j--)
		{
			f *= cos(ind->Gene[j-1] * PI / 2);
		}
		if (i > 1)
		{
			f *= sin(ind->Gene[(CSolution::OBJectiveNum - i + 1) - 1] * PI / 2);
		}
		
		ind->Objective[i-1] = f;
    }
	
    return 0;
}

int RTCGPSMOP::eval_DTLZ3(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    
    double g = 0;
    for (i = n - k + 1; i <= n; i++)
    {
		g += pow(ind->Gene[i-1]-0.5,2) - cos(20 * PI * (ind->Gene[i-1]-0.5));
    }
    g = 100 * (k + g);
    
    for (i = 1; i <= CSolution::OBJectiveNum; i++)
    {
		double f = (1 + g);
		for (j = CSolution::OBJectiveNum - i; j >= 1; j--)
		{
			f *= cos(ind->Gene[j-1] * PI / 2);
		}
		if (i > 1)
		{
			f *= sin(ind->Gene[(CSolution::OBJectiveNum - i + 1) - 1] * PI / 2);
		}
		
		ind->Objective[i-1] = f;
    }
	
    return 0;
}

int RTCGPSMOP::eval_DTLZ4(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    double alpha = 100;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    
    double g = 0;
    for (i = n - k + 1; i <= n; i++)
    {
		g += pow(ind->Gene[i-1]-0.5,2);
    }
    
    for (i = 1; i <= CSolution::OBJectiveNum; i++)
    {
		double f = (1 + g);
		for (j = CSolution::OBJectiveNum - i; j >= 1; j--)
		{
			f *= cos(pow(ind->Gene[j-1],alpha) * PI / 2);
		}
		if (i > 1)
		{
			f *= sin(pow(ind->Gene[(CSolution::OBJectiveNum - i + 1) - 1],alpha) * PI / 2);
		}
		
		ind->Objective[i-1] = f;
    }
	
    return 0;
}

int RTCGPSMOP::eval_DTLZ5(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    double t = 0;
    double g = 0;
    
    for (i = n - k + 1; i <= n; i++)
    {
		g += pow(ind->Gene[i-1] - 0.5, 2);
    }
	
    t = PI / (4 * (1 + g));
    theta[0] = ind->Gene[0] * PI / 2;
    for (i = 2; i <= CSolution::OBJectiveNum - 1; i++)
    {
		theta[i-1] = t * (1 + 2 * g * ind->Gene[i-1]);
    }
    
    for (i = 1; i <= CSolution::OBJectiveNum; i++)
    {
		double f = (1 + g);
		for (j = CSolution::OBJectiveNum - i; j >= 1; j--)
		{
			f *= cos(theta[j-1]);
		}
		if (i > 1)
		{
			f *= sin(theta[(CSolution::OBJectiveNum - i + 1) - 1]);
		}
		
		ind->Objective[i-1] = f;
    }
	
    return 0;
}

int RTCGPSMOP::eval_DTLZ6(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    double t = 0;
    double g = 0;
    
    for (i = n - k + 1; i <= n; i++)
    {
		g += pow(ind->Gene[i-1], 0.1);
    }
	
    t = PI / (4 * (1 + g));
    theta[0] = ind->Gene[0] * PI / 2;
    for (i = 2; i <= CSolution::OBJectiveNum - 1; i++)
    {
		theta[i-1] = t * (1 + 2 * g * ind->Gene[i-1]);
    }
    
    for (i = 1; i <= CSolution::OBJectiveNum; i++)
    {
		double f = (1 + g);
		for (j = CSolution::OBJectiveNum - i; j >= 1; j--)
		{
			f *= cos(theta[j-1]);
		}
		if (i > 1)
		{
			f *= sin(theta[(CSolution::OBJectiveNum - i + 1) - 1]);
		}
		
		ind->Objective[i-1] = f;
    }
	
    return 0;
}

int RTCGPSMOP::eval_DTLZ7(CSolution *ind)
{    
    int i = 0;
    int j = 0;
    int n = CSolution::GetGeneLength();
    int k = n - CSolution::OBJectiveNum + 1;
    double g = 0;
    double h = 0;
    
    for (i = n - k + 1; i <= n; i++)
    {
		g += ind->Gene[i-1];
    }
    g = 1 + 9 * g / k;
	
    for (i = 1; i <= CSolution::OBJectiveNum - 1; i++)
    {
		ind->Objective[i-1] = ind->Gene[i-1];
    }
	
    for (j = 1; j <= CSolution::OBJectiveNum - 1; j++)
    {
		h += ind->Gene[j-1] / (1 + g) * (1 + sin(3 * PI * ind->Gene[j-1]));
    }
    h = CSolution::OBJectiveNum - h;
    ind->Objective[CSolution::OBJectiveNum - 1] = (1 + g) * h;
	
    return 0;
}

int RTCGPSMOP::eval_COMET(CSolution *ind)
{

⌨️ 快捷键说明

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