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

📄 chromosome.cpp

📁 这是一个利用遗传算法求解函数极值
💻 CPP
字号:
// Chromosome.cpp: implementation of the CChromosome class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GA.h"
#include "Chromosome.h"

#include <stdlib.h>
#include <cmath>
#include <Afxtempl.h>
#include "Random.h"

#define NBIT 4
#define NCHILD 8                 //NCHILD>=2
#define E  2.71828183
#define MINUSVAL  -100000000000000


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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int CChromosome::m_nFuncType;
int CChromosome::m_iLenChrom;
float CChromosome::m_xLeftBound;
float CChromosome::m_xRightBound;
//static CArray <float,float> m_xLeftBound;
//static CArray <float,float> m_xRightBound;
int  CChromosome::m_numX=3;
int  CChromosome::newNum=0;
int  CChromosome::deleteNum=0;
double **CChromosome::m_goodPointSet=NULL;

CChromosome::CChromosome()
{
	m_pVarible=new double[m_numX];
	m_strCode="";	                                       newNum++;
}

CChromosome::~CChromosome()
{
	delete []this->m_pVarible;
                                                     	deleteNum++;
}

void CChromosome::InitChromPop(CPtrArray& ptrArray,int popsize)
{
	int pop,val,j;                                                    
//	RandomNumber();
	CChromosome *pChrom;
	CArray <CString,CString> strArray;
	strArray.SetSize(m_numX);
	for(pop=0;pop<popsize;pop++)
	{
		pChrom=new CChromosome();
		for(j=0;j<m_iLenChrom;j++)
		{
			val=int(2*fRandom());
			pChrom->m_strCode += char(val+48);
		}
		pChrom->ChromCodeToVar();
		pChrom->ObjFunc();
		ptrArray.Add(pChrom);

/*		CString str1,str2;
	str2.Format("%s,",pChrom->GetStrCode());
	AfxMessageBox(str2);*/
	}
	//生成佳点集
	int sDim=m_iLenChrom/NBIT+1;
	m_goodPointSet=new double*[NCHILD];

	//	m_goodPointSet=A;
	for(int i=0;i<NCHILD;i++)
	{	
		m_goodPointSet[i]=new double[sDim];
	}
	
	for(int i=0;i<NCHILD;i++)
	{
//		m_goodPointSet[i]=new double[sDim];
		for(j=0;j<sDim;j++)
		{
			m_goodPointSet[i][j]=pow(E,j+1)*(i+1);
			m_goodPointSet[i][j]-=int(m_goodPointSet[i][j]);
		}
	}

}//初始种群的产生对计算结果有很大影响.

void CChromosome::DestroyChromPop(CPtrArray& ptrArray)
{
	int popsize;
	popsize=ptrArray.GetSize();
	CChromosome *pChrom;
	while(popsize--)
	{
		pChrom=(CChromosome*)ptrArray.GetAt(popsize);
		delete pChrom;
		ptrArray.RemoveAt(popsize);
	}
	for(int i=0;i<NCHILD;i++)
	{
		delete []m_goodPointSet[i];
	}
	delete []m_goodPointSet;
//	m_goodPointSet=NULL;
/*	CString str;
	str.Format("newNum=%d,delete=%d",newNum,deleteNum);
	AfxMessageBox(str);*/
}


void CChromosome::SetChromParam(int precision,int funcType)
{
/*	CString str;
	str.Format("m_iLenChrom=%d,",m_iLenChrom);
	AfxMessageBox(str);*/
	
	m_nFuncType=funcType;
	float xMaxExtent;
	switch(m_nFuncType)
	{
	case 1:
		m_numX=3;
		m_xLeftBound=-5.12f;
		m_xRightBound=5.12f;
		break;
	case 2:
		m_numX=3;
		m_xLeftBound=-2.048f;
		m_xRightBound=2.048f;
		break;
	case 3:
    	m_numX=5;
		m_xLeftBound=-5.12f;
		m_xRightBound=5.12f;
		break;
	case 4:
		m_numX=3;
		m_xLeftBound=-10.0f;
		m_xRightBound=10.0f;
		break;
	case 5:
		m_numX=5;
		m_xLeftBound=-5.0f;
		m_xRightBound=5.0f;
		break;
	case 6:
		m_numX=10;
	}

	xMaxExtent=m_xRightBound-m_xLeftBound;
	m_iLenChrom=int((log10(xMaxExtent)+precision)/log10(2.0)+1);
	m_iLenChrom*=m_numX;
}

CString CChromosome::GetStrCode()
{
	return this->m_strCode;
}

const double *CChromosome::GetVarible()
{
	return this->m_pVarible;
}

double CChromosome::GetFitness()
{
	return this->m_dFitness;
}

int CChromosome::GetLenChrom()
{
	return m_iLenChrom;
}

int CChromosome::GetVaribleNum()
{
	return m_numX;
}

void CChromosome::ChromCodeToVar()
{ 
	CArray <CString,CString> strArray;
	strArray.SetSize(m_numX);
	int i,j,len=m_iLenChrom/m_numX;
	for(j=0;j<m_numX;j++)
	{
		for(i=0;i<len;i++)
		{			
			strArray[j]+=(m_strCode.GetAt((i+1)*(j+1)-1));
		}
	}

	unsigned long uVar;
	for(j=0;j<m_numX;j++)
	{
		uVar=0;
		for(int i=0;i<len;i++)
		{
			uVar*=2;
			uVar+=(strArray[j].GetAt(i))-48;
		}
		m_pVarible[j]=m_xLeftBound+uVar*(m_xRightBound-m_xLeftBound)/(pow(2.0,len)-1);
	}
}

void CChromosome::ObjFunc()
{	
	switch(m_nFuncType)
	{
	case 1:
		{
			m_dFitness=0;
			for(int i=0;i<m_numX;i++)
				m_dFitness+=m_pVarible[i]*m_pVarible[i];
			break;
		}
	case 2:
		{
			m_dFitness=100*pow((pow(m_pVarible[0],2)-m_pVarible[1]),2)+pow(1-m_pVarible[0],2);
			break;
		}
	case 3:
		{
			m_dFitness=int(m_pVarible[0])+int(m_pVarible[1])+int(m_pVarible[2])
				+int(m_pVarible[3])+int(m_pVarible[4]);
//			m_dFitness=pow(2,m_dFitness);
			m_dFitness=m_dFitness+100;
			break;
		}
	case 4:
		{
			const double k1=10,k2=1;
			const double PI = 3.14159265;
			//			const int n=10;
			double temp=k1 * pow( sin(PI*(0.25*m_pVarible[0]+1.25)),2);
			for(int i=0;i<m_numX-1;i++)
			{
				temp += pow( 0.25*m_pVarible[i]+1.25-k2 ,2) *
					(1 + k1 * pow( sin(PI * ( 0.25*m_pVarible[i+1] + 1.25)),2) );
			}
			temp *= (PI/m_numX);
			temp += pow( 0.25*m_pVarible[m_numX-1]+1.25-k2,2);
			m_dFitness = temp;
			break;

		} 
	case 5:
		{
			const double k3=0.1,k4=3,k5=1,k6=1,k7=2;
			const double PI = 3.14159265;
			//			const int n=10;
			double temp=pow( sin(PI*k4*m_pVarible[0]),2);
			for(int i=0;i<m_numX-1;i++)
			{
				temp += pow( m_pVarible[i]-k5 ,2) *
					(1 + k6 * pow( sin(PI * k4 * m_pVarible[i+1]),2) );
			}
			temp *= k3;
			temp += pow( m_pVarible[m_numX-1]-k5,2) *
				(1 + k6 * pow( sin(PI * k7 * m_pVarible[m_numX-1]),2) );
			m_dFitness = temp;
			break;

		}
	}
}


void CChromosome::Mutation(int mSite)
{
	if('0'==m_strCode.GetAt(mSite))
	{
		m_strCode.SetAt(mSite,'1');
	}
	else
	{
        m_strCode.SetAt(mSite,'0');
	}
	this->ChromCodeToVar();
	this->ObjFunc();
}


void CChromosome::CrossOver(CChromosome *pChrom)
{   
	CString str;
	CArray <int , int> jSet;
	CArray <CString,CString> childStrCode;
	childStrCode.SetSize(NCHILD);

	for(int i=0;i<m_iLenChrom;i++)
	{
		if((this->m_strCode).GetAt(i)!=(pChrom->m_strCode).GetAt(i))
		{
			jSet.Add(i);
		}
	}

	int tDim=jSet.GetSize();
	if(tDim==0)
		return ;
	int sDim=tDim/NBIT+1,j;
	double maxFitness,subMaxFitness,tempFitness;  
	CString maxStrCode,subMaxStrCode;
//	CArray <double,double> r;
//	r.SetSize(sDim);
	double twoPow=pow(2.0,NBIT);
	int  len,iTemp,nbit;
	char ch[NBIT];

//	for(i=0;i<sDim;i++)
//	{
//		r[i]=pow(E,i+1);
//	}

	for(int i=0;i<NCHILD;i++)
	{
		str="";
		len=tDim;
		nbit=NBIT;
		twoPow=pow(2.0,NBIT);
		while(len>0)
		{	
			if(len<NBIT)
			{
				twoPow=pow(2.0,len);
				nbit=len;
			}
//			iTemp=int((r[sDim-len/NBIT-1]*(j+1)-int(r[sDim-len/NBIT-1]*(j+1)))*twoPow);
			iTemp=int(m_goodPointSet[i][sDim-len/NBIT-1]*twoPow);
			for(j=0;j<nbit;j++)
			{
				ch[nbit-1-j]=iTemp%2+48;
				iTemp=iTemp/2;
			}
			for(j=0;j<nbit;j++)
				str+=ch[j];
			len-=NBIT;
		}

		childStrCode[i]=this->m_strCode;
		for(j=0;j<tDim;j++)
		{
			childStrCode[i].SetAt(jSet.GetAt(j),str[j]);
		}

//		AfxMessageBox(str);
	}

/*	this->m_strCode=childStrCode[0];
	pChrom->m_strCode=childStrCode[1];*/
	maxFitness=subMaxFitness=MINUSVAL;
	for(int i=0;i<NCHILD;i++)
	{	
		pChrom->m_strCode=childStrCode[i];
		pChrom->ChromCodeToVar();
		pChrom->ObjFunc();
		tempFitness=pChrom->GetFitness();
		if(tempFitness>maxFitness)
		{
			subMaxStrCode=maxStrCode;
			subMaxFitness=maxFitness;
			maxStrCode=childStrCode[i];
			maxFitness=tempFitness;
		}
		else
		{
			if(tempFitness>subMaxFitness)
			{
				subMaxStrCode=childStrCode[i];
				subMaxFitness=tempFitness;
			}
		}
	
	}

	pChrom->m_strCode=maxStrCode;
	pChrom->ChromCodeToVar();
	pChrom->ObjFunc();
	this->m_strCode=subMaxStrCode;
	this->ChromCodeToVar();
	this->ObjFunc();

/*	this->ChromCodeToVar();
	this->ObjFunc();
	pChrom->ChromCodeToVar();
	pChrom->ObjFunc();*/




		
//	CString strCode1,strCode2;
/*	strCode1=m_strCode;
	strCode2=pChrom->m_strCode;

	m_strCode=strCode1.Left(xsite);
	m_strCode+=strCode2.Right(m_iLenChrom-xsite);
	pChrom->m_strCode=strCode2.Left(xsite);
	pChrom->m_strCode+=strCode1.Right(m_iLenChrom-xsite);

	ChromCodeToVar();
	ObjFunc();
	pChrom->ChromCodeToVar();
	pChrom->ObjFunc();

//	m_count=1;
//	pChrom->m_count=1;
/////////////////////
/*	CString str;
	str.Format("crossover=%d",m_strCode.GetLength());
	AfxMessageBox(str);*/
}

CChromosome* CChromosome::CopyChrom()
{
	CChromosome *pChild;
	pChild=new CChromosome();
	pChild->m_strCode=m_strCode;
	for(int i=0;i<m_numX;i++)
	{
		pChild->m_pVarible[i]=m_pVarible[i];
	}
	pChild->m_dFitness=m_dFitness;
	return pChild;
}

⌨️ 快捷键说明

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