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

📄 ca.cpp

📁 文化算法的实际应用
💻 CPP
字号:
#include "ca.h"
using namespace CAEP;

CAEP::CA::CA(bool isLog)
{
	mIsLog = isLog;
}

CAEP::CA::~CA()
{
	if(bs != NULL) { delete bs; }
	if(ps != NULL) { delete ps; }
}

void CAEP::CA::Run(int Gmax, float seed, string fileName)
{
	//Log start time
	char stime[20];
	time_t now;
	time(&now);
	strftime(stime,20,"%Y-%m-%d %X",localtime(&now));

	mLog.Open(fileName, false);
	if(mIsLog) 
	{ 
		mLog<<stime<<endl;
		mLog<<"Seed:\t"<<seed<<"\tMax Generation\t"<<Gmax<<endl; 
		mConfiger.ToString(mLog.out);
	}

	clock_t start, end;
	double totalTime;
	start = clock();

	Individual::SetConfiger(&mConfiger);

	mResult = new Result(mConfiger);
	CARandom mRandom(seed);
	bs = new BeliefSpace(mRandom, mConfiger);
	ps = new PopulationSpace(bs, mRandom, mConfiger, mResult);
	ps->InitViolation();
	ps->Evalute();

	for(int i = 0; i < Gmax; i++)
	{
		bs->Update(ps, i);
		ps->GenerateChildren(bs);
		ps->Evalute();
		ps->Select();

		if(mIsLog)
		{
			LogInfo(Gmax, i);
		}
	}

	end = clock();
	time(&now);
	strftime(stime,20,"%Y-%m-%d %X",localtime(&now));
	totalTime=((double)(end-start))/CLOCKS_PER_SEC;

	if(mIsLog)
	{
		ps->ShowInfo(mLog.out);
		mLog<<"Total Time:"<<totalTime<<endl;
		mLog<<stime<<endl;

		ps->ShowInfo();	
	}
}

void CAEP::CA::LogInfo(int Gmax, int gen)
{
	int j;
	if (gen >= Gmax - 11 || (gen > 100 && (gen + 1)%((Gmax + 1)/100) == 0)) 
	{
		ps->FetchValue();

		mLog<<"Generation:"<<gen<<endl;
		mLog<<"Value average:"<<mResult->mAverage<<"\t Max Value:"<<ps->mPopulation[mResult->mIndexMax]->mValue<<"\t Min Value:"<<ps->mPopulation[mResult->mIndexMin]->mValue<<endl;
		mLog<<"Better individual:"<<endl;

		mLog<<"Value:\t"<<ps->mPopulation[mResult->mIndexMin]->mValue<<endl;

		mLog<<"Variables:";
		for (j = 0; j < mConfiger.VariableCount; j++) 
		{
			mLog<<ps->mPopulation[mResult->mIndexMin]->mVariables[j]<<"\t";
		}
		mLog<<""<<endl;

		mLog<<"Constraints:";
		for (j = 0; j < mConfiger.ConstraintCount; j++) 
		{
			mLog<<ps->mPopulation[mResult->mIndexMin]->mConstraints[j]<<"\t";
		}
		mLog<<""<<endl;

		mLog<<"Violation:\t"<<ps->mPopulation[mResult->mIndexMin]->mViolation<<endl;

		mLog<<"The best individual is ";
		if (!ps->mPopulation[mResult->mIndexMin]->mIsFeasible) 
		{
			mLog<<"NOT ";
		}
		mLog<<"feasible!"<<endl<<endl;
	}		
}

⌨️ 快捷键说明

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