亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? gatsp.cpp

?? 《游戲編程中的人工智能技術(shù)》一書中4
?? CPP
?? 第 1 頁(yè) / 共 3 頁(yè)
字號(hào):
#include "gaTSP.h"




//---------------------TestNumber-----------------------------
//
//	checks if a given integer is already contained in a vector
//	of integers.
//------------------------------------------------------------
bool TestNumber(const vector<int> &vec, const int &number)
{
	for (int i=0; i<vec.size(); ++i)
	{
		if (vec[i] == number)
		{
			return true;
		}
	}

	return false;
}




////////////////////////////////////////////////////////////////////////////////

//---------------------GrabPermutation----------------------
//
//	given an int, this function returns a vector containing
//	a random permutation of all the integers up to the supplied
//	parameter.
//------------------------------------------------------------
vector<int> SGenome::GrabPermutation(int &limit)
{
	vector<int> vecPerm;
	
	for (int i=0; i<limit; i++)
	{
		//we use limit-1 because we want ints numbered from zero
		int NextPossibleNumber = RandInt(0, limit-1);

		while(TestNumber(vecPerm, NextPossibleNumber))
		{
			NextPossibleNumber = RandInt(0, limit-1);
		}

		vecPerm.push_back(NextPossibleNumber);
	}

	return vecPerm;
}




/////////////////////////////////////////////////////////////////////////////


//-----------------------CalculatePopulationsFitness--------------------------
//
//	calculates the fitness of each member of the population, updates the
//	fittest, the worst, keeps a sum of the total fittness scores and the
//	average fitness score of the population (most of these stats are required
//	when we apply pre-selection fitness scaling.
//-----------------------------------------------------------------------------
void CgaTSP::CalculatePopulationsFitness()
{

	for (int i=0; i<m_iPopSize; ++i)
	{

		double TourLength = m_pMap->GetTourLength(m_vecPopulation[i].vecCities);

		m_vecPopulation[i].dFitness = TourLength;
		
		//keep a track of the shortest route found each generation
		if (TourLength < m_dShortestRoute)
		{
			m_dShortestRoute = TourLength;
		}
		
		//keep a track of the worst tour each generation
		if (TourLength > m_dLongestRoute)
		{
			m_dLongestRoute = TourLength;
		}

	}//next chromo

	//Now we have calculated all the tour lengths we can assign
	//the fitness scores
	for (i=0; i<m_iPopSize; ++i)
	{
		m_vecPopulation[i].dFitness = m_dLongestRoute - m_vecPopulation[i].dFitness;
	}

	//calculate values used in selection
	CalculateBestWorstAvTot();

}

//-----------------------CalculateBestWorstAvTot-----------------------	
//
//	calculates the fittest and weakest genome and the average/total 
//	fitness scores
//---------------------------------------------------------------------
void CgaTSP::CalculateBestWorstAvTot()
{
	m_dTotalFitness = 0;
	
	double HighestSoFar = -9999999;
	double LowestSoFar  = 9999999;
	
	for (int i=0; i<m_iPopSize; ++i)
	{
		//update fittest if necessary
		if (m_vecPopulation[i].dFitness > HighestSoFar)
		{
			HighestSoFar	 = m_vecPopulation[i].dFitness;
			
			m_iFittestGenome = i;

			m_dBestFitness	 = HighestSoFar;
		}
		
		//update worst if necessary
		if (m_vecPopulation[i].dFitness < LowestSoFar)
		{
			LowestSoFar = m_vecPopulation[i].dFitness;
			
			m_dWorstFitness = LowestSoFar;
		}
		
		m_dTotalFitness	+= m_vecPopulation[i].dFitness;
		
		
	}//next chromo
	
	m_dAverageFitness = m_dTotalFitness / m_iPopSize;

  //if all the fitnesses are zero the population has converged
  //to a grpoup of identical genomes so we should stop the run
  if (m_dAverageFitness == 0)
  {
    m_dSigma = 0;
  }

}

//-----------------------------FitnessScaleRank----------------------
//
//	This type of fitness scaling sorts the population into ascending
//	order of fitness and then simply assigns a fitness score based 
//	on its position in the ladder. (so if a genome ends up last it
//	gets score of zero, if best then it gets a score equal to the size
//	of the population. 
//---------------------------------------------------------------------
void CgaTSP::FitnessScaleRank(vector<SGenome> &pop)
{
	//sort population into ascending order
	if (!m_bSorted)
	{
		sort(pop.begin(), pop.end());

		m_bSorted = true;
	}

	//now assign fitness according to the genome's position on
	//this new fitness 'ladder'
	for (int i=0; i<pop.size(); i++)
	{
		pop[i].dFitness = i;
	}

	//recalculate values used in selection
	CalculateBestWorstAvTot();
}


//----------------------------- FitnessScaleSigma ------------------------
//
//  Scales the fitness using sigma scaling based on the equations given
//  in Chapter 5 of the book.
//------------------------------------------------------------------------
void CgaTSP::FitnessScaleSigma(vector<SGenome> &pop)
{
  double RunningTotal = 0;

  //first iterate through the population to calculate the standard
  //deviation
  for (int gen=0; gen<pop.size(); ++gen)
  {
    RunningTotal += (pop[gen].dFitness - m_dAverageFitness) *
                    (pop[gen].dFitness - m_dAverageFitness);
  }

  double variance = RunningTotal/(double)m_iPopSize;

  //standard deviation is the square root of the variance
  m_dSigma = sqrt(variance);

  //now iterate through the population to reassign the fitness scores
  for (gen=0; gen<pop.size(); ++gen)
  {
    double OldFitness = pop[gen].dFitness;

    pop[gen].dFitness = (OldFitness - m_dAverageFitness) /
                                    (2 * m_dSigma);
  }

  //recalculate values used in selection
	CalculateBestWorstAvTot();

}   

//------------------------- FitnessScaleBoltzmann ------------------------
//
//  This function applies Boltzmann scaling to a populations fitness
//  scores as described in Chapter 5.
//  The static value Temp is the boltzmann temperature which is reduced
//  each generation by a small amount. As Temp decreases the difference 
//  spread between the high and low fitnesses increases.
//------------------------------------------------------------------------
void CgaTSP::FitnessScaleBoltzmann(vector<SGenome> &pop)
{

  //reduce the temp a little each generation
  m_dBoltzmannTemp -= BOLTZMANN_DT;

  //make sure it doesn't fall below minimum value
  if (m_dBoltzmannTemp< BOLTZMANN_MIN_TEMP)
  {
    m_dBoltzmannTemp = BOLTZMANN_MIN_TEMP;
  }

  //first calculate the average fitness/Temp
  double divider = m_dAverageFitness/m_dBoltzmannTemp;

  //now iterate through the population and calculate the new expected
  //values
  for (int gen=0; gen<pop.size(); ++gen)
  {
    double OldFitness = pop[gen].dFitness;

    pop[gen].dFitness = (OldFitness/m_dBoltzmannTemp)/divider;
  }

  //recalculate values used in selection
	CalculateBestWorstAvTot();
}

//--------------------------FitnessScale----------------------------------
//
//  This is simply a switch statement to choose a selection method
//  based on the user preference
//------------------------------------------------------------------------
void CgaTSP::FitnessScaleSwitch()
{
  switch(m_ScaleType)
  {
  case NONE:

    break;

  case SIGMA:
    
    FitnessScaleSigma(m_vecPopulation);

    break;

  case BOLTZMANN:
    
    FitnessScaleBoltzmann(m_vecPopulation);

    break;

  case RANK:
    
    FitnessScaleRank(m_vecPopulation);

    break;
  }
}
//-------------------------GrabNBest----------------------------------
//
//	This works like an advanced form of elitism by inserting NumCopies
//  copies of the NBest most fittest genomes into a population vector
//--------------------------------------------------------------------
void CgaTSP::GrabNBest(int				      NBest,
					             const int        NumCopies,
					             vector<SGenome>	&vecNewPop)
{
	//first we need to sort our genomes
	if (!m_bSorted)
	{
		sort(m_vecPopulation.begin(), m_vecPopulation.end());

		m_bSorted = true;
	}

	//now add the required amount of copies of the n most fittest 
	//to the supplied vector
	while(NBest--)
	{
		for (int i=0; i<NumCopies; ++i)
		{
			vecNewPop.push_back(m_vecPopulation[(m_iPopSize - 1) - NBest]);
		}
	}
}

//--------------------------RouletteWheelSelection----------------------
//
//	selects a member of the population by using roulette wheel selection
//	as described in the text.
//-----------------------------------------------------------------------
SGenome& CgaTSP::RouletteWheelSelection()
{
	double fSlice	= RandFloat() * m_dTotalFitness;
	
	double cfTotal	= 0.0;
	
	int	SelectedGenome = 0;
	
	for (int i=0; i<m_iPopSize; ++i)
	{
		
		cfTotal += m_vecPopulation[i].dFitness;
		
		if (cfTotal > fSlice) 
		{
			SelectedGenome = i;
			
			break;
		}
	}
	
	return m_vecPopulation[SelectedGenome];
}

//----------------------- SUSSelection -----------------------------------
//
//  This function performs Stochasitic Universal Sampling.
//
//  SUS uses N evenly spaced hands which are spun once to choose the 
//  new population. As described in chapter 5.
//------------------------------------------------------------------------
void CgaTSP::SUSSelection(vector<SGenome> &NewPop)
{
  //this algorithm relies on all the fitnesses to be positive so
  //these few lines check and adjust accordingly (in this example
  //Sigma scaling can give negative fitnesses
  if (m_dWorstFitness < 0)
  {
    //recalculate
    for (int gen=0; gen<m_vecPopulation.size(); ++gen)
    {
      m_vecPopulation[gen].dFitness += fabs(m_dWorstFitness);
    }

    CalculateBestWorstAvTot();
  }

  int curGen = 0;
  double sum = 0;

  //NumToAdd is the amount of individuals we need to select using SUS.
  //Remember, some may have already been selected through elitism
  int NumToAdd = m_iPopSize - NewPop.size();

  //calculate the hand spacing
  double PointerGap = m_dTotalFitness/(double)NumToAdd;

  //choose a random start point for the wheel
  float ptr = RandFloat() * PointerGap;

	while (NewPop.size() < NumToAdd)
  {
	  for(sum+=m_vecPopulation[curGen].dFitness; sum > ptr; ptr+=PointerGap)
    {
	     NewPop.push_back(m_vecPopulation[curGen]);

       if( NewPop.size() == NumToAdd)
       {
         return;
       }
    }

    ++curGen;
  }
}


//---------------------------- TournamentSelection -----------------
//
//  performs standard tournament selection given a number of genomes to
//  sample from each try.
//------------------------------------------------------------------------
SGenome& CgaTSP::TournamentSelection(int N)
{
  double BestFitnessSoFar = 0;
  
  int ChosenOne = 0;

  //Select N members from the population at random testing against 
  //the best found so far

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲欧洲精品天堂一级| 久久九九国产精品| 国产999精品久久久久久| 国产成人精品三级| 一区二区在线观看视频| 日韩亚洲国产中文字幕欧美| 成人综合婷婷国产精品久久| 污片在线观看一区二区| 亚洲国产精品t66y| 日韩欧美一区二区久久婷婷| 色综合天天综合在线视频| 九九九久久久精品| 午夜一区二区三区在线观看| 国产精品久久久久久久浪潮网站| 日韩欧美一级二级三级久久久| 欧美系列日韩一区| 97精品久久久久中文字幕| 国产自产高清不卡| 免费一区二区视频| 三级不卡在线观看| 亚洲午夜日本在线观看| 国产精品久久网站| 国产女人水真多18毛片18精品视频| 欧美猛男男办公室激情| 在线亚洲免费视频| 一本在线高清不卡dvd| 成人国产精品免费网站| 国产精品 日产精品 欧美精品| 九色综合国产一区二区三区| 日韩在线a电影| 午夜激情一区二区| 亚洲高清中文字幕| 性做久久久久久免费观看| 亚洲综合在线电影| 一区二区三区视频在线看| 亚洲品质自拍视频网站| 中文字幕在线观看一区二区| 国产亚洲欧洲997久久综合| 久久在线观看免费| 精品国产乱码久久久久久蜜臀 | 一区二区三区四区av| 成人欧美一区二区三区| 亚洲欧美一区二区视频| 亚洲欧洲韩国日本视频| 一区二区三区在线视频观看58 | 亚洲一区二区三区中文字幕在线| 专区另类欧美日韩| 亚洲靠逼com| 一区二区日韩电影| 亚洲成人免费电影| 日本中文字幕一区二区有限公司| 日韩高清不卡在线| 黑人巨大精品欧美一区| 国产一区二区三区日韩 | 91在线看国产| 色菇凉天天综合网| 欧美午夜精品久久久| 欧美日韩中文国产| 日韩一区二区三区四区五区六区| 日韩欧美一区在线观看| 久久亚洲综合色一区二区三区| 久久综合久久鬼色中文字| 中文字幕的久久| 亚洲人成网站色在线观看| 亚洲国产日韩a在线播放性色| 欧美bbbbb| 国产大陆a不卡| 99视频精品全部免费在线| 欧美日韩国产免费| 久久久久久97三级| 亚洲乱码精品一二三四区日韩在线| 亚洲一二三四区不卡| 男女激情视频一区| 成人精品免费视频| 欧美三级三级三级爽爽爽| 日韩精品中午字幕| 中文字幕一区二区三区不卡在线 | 国产资源在线一区| 99久久综合国产精品| 5月丁香婷婷综合| 国产欧美一区二区三区在线看蜜臀| 亚洲精品免费看| 精品一区中文字幕| 色噜噜狠狠色综合欧洲selulu| 日韩一区二区在线观看视频播放| 中文字幕乱码亚洲精品一区| 亚洲第四色夜色| 国产激情偷乱视频一区二区三区 | 欧美精选在线播放| 中文字幕第一页久久| 五月天激情小说综合| 成人小视频免费观看| 欧美精品777| 中文字幕一区二区三区乱码在线| 麻豆中文一区二区| 色哟哟欧美精品| 精品久久久网站| 亚洲一区二区成人在线观看| 国产精品一区二区三区四区| 欧美色欧美亚洲另类二区| 中文字幕av一区二区三区高| 日韩一区欧美二区| 色视频欧美一区二区三区| 精品99久久久久久| 日韩综合在线视频| 色94色欧美sute亚洲线路二| 久久久久国产精品麻豆| 日韩成人一级大片| 欧美无砖砖区免费| 中文字幕亚洲在| 国产精品66部| 欧美mv和日韩mv的网站| 亚洲一区在线观看网站| 成人av高清在线| 日韩免费高清av| 日韩福利视频导航| 欧美专区日韩专区| 亚洲桃色在线一区| www.欧美日韩| 国产欧美日韩卡一| 国产福利精品导航| 久久精品一区二区三区不卡| 久久国产精品一区二区| 91精品欧美久久久久久动漫 | 久久精品国产秦先生| 欧美日韩久久一区| 亚洲国产欧美另类丝袜| 色综合久久久久| 亚洲免费伊人电影| 99国产一区二区三精品乱码| 国产精品网曝门| 国产ts人妖一区二区| 国产色91在线| 国产精品综合二区| 国产亚洲va综合人人澡精品| 国产精品99久久久久久宅男| 久久伊99综合婷婷久久伊| 国产乱码精品一品二品| 国产日韩欧美不卡在线| 国产精品99久久久久久有的能看| 久久精品视频一区二区三区| 国产成人精品网址| 国产精品电影院| 91久久国产最好的精华液| 亚洲一区在线观看免费| 欧美日本一区二区| 久久99国产精品尤物| 久久亚洲综合av| 99re热这里只有精品免费视频| 自拍偷在线精品自拍偷无码专区 | 天天影视涩香欲综合网| 91精品久久久久久久99蜜桃| 日韩成人午夜精品| 欧美不卡在线视频| 丰满白嫩尤物一区二区| 亚洲女女做受ⅹxx高潮| 欧美日本在线播放| 国内成人精品2018免费看| 国产午夜亚洲精品不卡| 97国产精品videossex| 一片黄亚洲嫩模| 日韩一区二区免费在线电影| 国产一区二区三区电影在线观看| 国产精品色婷婷| 欧美手机在线视频| 九九国产精品视频| 国产精品丝袜久久久久久app| 色综合久久88色综合天天6| 视频在线观看国产精品| 国产三区在线成人av| 欧美专区在线观看一区| 狠狠色丁香婷婷综合久久片| 日本一区二区三区电影| 91国偷自产一区二区三区成为亚洲经典 | 综合激情网...| 欧美一卡2卡3卡4卡| 成人午夜视频免费看| 亚洲va欧美va人人爽午夜| 久久亚洲综合色| 欧美日韩一二区| 国产成人精品免费在线| 亚洲国产欧美另类丝袜| 国产欧美日韩在线| 欧美高清www午色夜在线视频| 国产成人免费网站| 午夜欧美在线一二页| 日本一区二区三区在线观看| 欧美无乱码久久久免费午夜一区| 国产在线一区观看| 亚洲成年人网站在线观看| 国产日韩欧美电影| 91精品国产综合久久福利| 国产suv精品一区二区883| 视频一区欧美精品| 亚洲老妇xxxxxx| 国产精品情趣视频| 精品国内片67194| 欧美日韩国产电影| 91在线一区二区| 国产成人在线视频网站|