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

📄 gainfo.cpp

📁 遗传算法vc++语言版源程序,台湾大学编写。
💻 CPP
字号:
#include "GAInfo.h"
#include "GABaseSolver.h"
#include "PopSolution.h"
#include <float.h>
#include <iostream>
#include <fstream>
using namespace std;

GAInfo::GAInfo(size_t freq, string filename)
:UOFInfo(freq,filename)
{
	// get start time stamp for this optimization process
	time(&m_StartTime);

	//clear up all info log
	m_popSize.resize(0);
	m_ScoreBest.resize(0);
	m_ScoreWorst.resize(0);
	m_ScoreMean.resize(0);
	m_ScoreDiversity.resize(0);
	m_bestChro.resize(0);
}

bool GAInfo::Better(double a, double b)
{
	PopBaseSolver *pGA = static_cast<PopBaseSolver*>(m_CS);
	if(a<b && pGA->GetOptType() == MINIMIZE)
		return true;
	else if(a>b && !(pGA->GetOptType() == MINIMIZE))
		return true;
	return false;
}

double GAInfo::HistoryBest()
{
	if(m_ScoreBest.empty())
		return DBL_MAX;
	double retval = m_ScoreBest[0];
	for(size_t i=1;i<m_ScoreBest.size();i++)
		if(Better(m_ScoreBest[i], retval))
			retval = m_ScoreBest[i];
	return retval;
}

double GAInfo::HistoryWorst()
{
	if(m_ScoreBest.empty())
		return DBL_MIN;
	double retval = m_ScoreWorst[0];
	for(size_t i=1;i<m_ScoreWorst.size();i++)
		if(!Better(m_ScoreWorst[i], retval))
			retval = m_ScoreWorst[i];
	return retval;
}

double GAInfo::GetLastImprovement(int gen)
{
	if((int)m_ScoreBest.size()<gen)
		return DBL_MAX;
	return m_ScoreBest[m_ScoreBest.size()-1] - m_ScoreBest[m_ScoreBest.size()-1-gen];
}

void GAInfo::Update(UOFSolver *cs)
{
	m_CS = cs;
	PopBaseSolver *pGA = static_cast<PopBaseSolver*>(m_CS);

	m_itr = pGA->GetIteration();
	if( m_itr % m_updateFreq == 0 )
	{
		m_popSize.push_back(pGA->GetPopSize());
		if(pGA->GetOptType() == MINIMIZE)
		{
			double max = pGA->m_Pop[pGA->GetPopSize()-1]->GetScore();
			double min = pGA->m_Pop[0]->GetScore();
			double mean = (max-min)/max;
            m_ScoreWorst.push_back(max);
			m_ScoreBest.push_back(min);
			m_ScoreMean.push_back(mean);
			m_bestChro.push_back(pGA->m_Pop[0]);
		}
		else
		{
			double max = pGA->m_Pop[0]->GetScore();
			double min = pGA->m_Pop[pGA->GetPopSize()-1]->GetScore();
			double mean = (max-min)/max;
            m_ScoreBest.push_back(max);
			m_ScoreWorst.push_back(min);
			m_ScoreMean.push_back(mean);
			m_bestChro.push_back(pGA->m_Pop[pGA->GetPopSize()-1]);
		}
		ofstream file;
		//Output parameter
		if(m_OutputResultFreq)
		{
			char buffer[_MAX_PATH];
			char drive[_MAX_PATH];
			char dir[_MAX_PATH];
			char fname[_MAX_PATH];
			char ext[_MAX_PATH];
			_splitpath(m_pn.c_str(), drive, dir, fname, ext);
			sprintf(buffer, "%s%s%s-%d%s", drive, dir, fname, m_itr, ext);
			file.open(buffer);
			file << (*m_bestChro[m_bestChro.size()-1]);
			file.close();
			if(!m_lastpn.empty())
				remove(m_lastpn.c_str());
			m_lastpn = buffer;
		}
		//Output Log
		if(m_NosLog==0)
		{
			file.open(m_fn.c_str(), (ios::out | ios::trunc));
			pGA->LogHeader(file);
			pGA->LogHeader(cout);
		}
		else
		{
			file.open(m_fn.c_str(),  (ios::out | ios::app));
			file << (int)m_itr << "\t" << HistoryBest() << endl;
			file << "\tParameters:" << endl;
			file << "\t" << (*m_bestChro[m_bestChro.size()-1]);
			if(m_ShowEvoOnline)
				cout << (int)m_itr << "\t" << m_ScoreBest[m_ScoreBest.size()-1] << "\t" << m_ScoreWorst[m_ScoreWorst.size()-1] << endl;
			if(m_ShowParamOnline)
			{
				cout << "\tParameters:" << endl;
				cout << "\t" << (*m_bestChro[m_bestChro.size()-1]);
			}
		}
		file.close();
		//Output Convergence behavior
		if(m_ConvBehavior)
		{
			if(m_NosLog==0)
			{
				file.open(m_cn.c_str(), (ios::out | ios::trunc));
				file << (int)m_itr << "\t" << HistoryBest() << "\t" << m_ScoreBest[m_ScoreBest.size()-1] << endl;
			}
			else
			{
				file.open(m_cn.c_str(),  (ios::out | ios::app));
				file << (int)m_itr << "\t" << HistoryBest() << "\t" << m_ScoreBest[m_ScoreBest.size()-1] << endl;
			}
			file.close();
		}
		m_NosLog++;
	}
	time_t curtime;
	time(&curtime);
	m_TimeElapsed = curtime - m_StartTime;
}

void GAInfo::PrintInfo()
{
	cout << "size:" << (int)m_popSize[m_popSize.size()-1] << ", itr:" << (int)m_itr;
	cout << ", best:" << m_ScoreBest[m_ScoreBest.size()-1];
	cout << ", worst:" << m_ScoreWorst[m_ScoreWorst.size()-1] 
	     << ", mean:" << m_ScoreMean[m_ScoreMean.size()-1] <<endl;
	time_t curtime;
	time(&curtime);
	m_TimeElapsed = curtime - m_StartTime;
	cout << ", total time elapsed:" << (int)m_TimeElapsed <<endl;

	m_bestChro[m_bestChro.size()-1]->PrintSelf(cout);
	cout << endl;
}

⌨️ 快捷键说明

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