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

📄 cega.cpp

📁 这是一个用遗传算法来求解目标函数优化问题的源代码
💻 CPP
字号:
#include "StdAfx.h"
#include ".\cega.h"

double A[] = 
{
	-0.1,
	-0.22,
	-0.38,
	-0.4,
	0.1,
	0.22,
	0.15,
	0.38,
	0.4,
	0.85
};

CEGA::CEGA(void)
{
	m_XS = -1;
	m_XE = 2;

	m_M = 10;
	m_L = 1;
	m_K = 3;

	m_PopSize = 100;

	m_Maxgen = 1000;

	m_Pop = new double[m_PopSize];
	m_A = new double[m_M];

	memcpy(m_A, A, sizeof(A));

}

CEGA::~CEGA(void)
{
	delete []m_Pop;
	m_Pop = NULL;

	delete []m_A;
	m_A = NULL;
}


bool CEGA::Better(double x1, double x2)
{
	if (InRegion(x1) && !InRegion(x2))
	{
		return 1;
	}
	else if (!InRegion(x1) && InRegion(x2))
	{
		return 0;
	}
	else if (InRegion(x1) && InRegion(x2) && Function(x1) >= Function(x2))
	{
		return 1;	
	}
	else if (InRegion(x1) && InRegion(x2) && Function(x1) < Function(x2))
	{
		return 0;	
	}
	else
	{
		return 0;
	}

}

bool CEGA::InRegion(double x)
{
	if ( m_XS <= x &&  x <= m_XE)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

double CEGA::Function(double x)
{
	return x * sin(x * 10 * atan(1.0) * 4) + 2.0;
}


void CEGA::InitPop()
{
	double step = (m_XE - m_XS) / (double)(m_PopSize - 1);
	m_Pop[0] = m_XS;

	for (int i=1; i<m_PopSize; i++)
	{
		m_Pop[i] = m_Pop[i-1] + step;
	}

}


void  CEGA::Sort()
{
	for (int i=0; i<m_PopSize-1; i++)
	{
		for (int j=i+1; j<m_PopSize; j++)
		{
			if (Better(m_Pop[j], m_Pop[i]))
			{
				double tem = m_Pop[i];
				m_Pop[i] = m_Pop[j];
				m_Pop[j] = tem;
			}
		}
	}
}


void  CEGA::Select()
{
	double *Tem = new double[m_M];
	bool *Flag = new bool[m_PopSize];
	double *pLSpa = new double[m_M];

	memset(Flag, 0, sizeof(bool)*m_PopSize);
	//选取m_K个最好的个体
	for (int i=0; i<m_K; i++)
	{
		Tem[i] = m_Pop[i];
	}
	
	//从余下的m_PopSize - m_K个个体中,随机选取m_M - m_K个个体
	for (int i=m_K; i<m_M; i++)
	{
		int pos = m_K + rand()%(m_PopSize - m_K);
		while (Flag[pos])
		{
			pos = m_K + rand()%(m_PopSize - m_K);
		}

		Tem[i] = m_Pop[pos];
		Flag[pos] = 1;
	}

	//生成新的个体空间
	for (int i=0; i<m_M; i++)
	{
		Tem[i] = m_A[i]*Tem[i];
	}

	//从新空间中随机选取m_L个个体
	for (int i=0; i<m_L; i++)
	{
		int pos = rand()%m_M;
		pLSpa[i] = Tem[pos];
	}

	//选取m_L个个体中最好的个体_X
	double _X = pLSpa[0];
	for (int i=1; i<m_L; i++)
	{
		if (Better(pLSpa[i], _X))
		{
			_X = pLSpa[i];
		}
	}

	//如Better(_X, Xworst),则用_X代替Xworst
	if (Better(_X, m_Pop[m_PopSize-1]))
	{
		m_Pop[m_PopSize-1] = _X;
	}

	delete []pLSpa;
	pLSpa = NULL;

	delete []Flag;
	Flag = NULL;

	delete []Tem;
	Tem = NULL;

}



void  CEGA::StartRun()
{
	CString str; 

	srand( (unsigned)time( NULL ) );

	InitPop();
	Sort();

	char file[255];
	sprintf(file, "tt.txt");
	m_File = fopen(file, "w+");

	int Run = 0;
	for (Run=0; Run<m_Maxgen; Run++)
	{
		Select();
		Sort();
	}

	fprintf(m_File, "\n\n一共运行了%5d代\n\n____________________________\n\n最终代种群为:\n\n", Run);
	str.Format("\n\n一共运行了 %5d代\n\n____________________________\n\n最终代种群为:\n\n", Run);
	m_Str +=str;

	for (int i=0; i<m_PopSize; i++)
	{
		fprintf(m_File, "个体%5d :  %20f    %20f\n",i+1, m_Pop[i], Function(m_Pop[i]));
		str.Format("个体%5d :  %20f    %20f\n", i+1, m_Pop[i], Function(m_Pop[i]));
		m_Str += str;
	}

	fclose(m_File);
}

⌨️ 快捷键说明

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