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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? gatsp.cpp

?? 開發游戲人工智能的王道書
?? CPP
?? 第 1 頁 / 共 3 頁
字號:
#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

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
中文子幕无线码一区tr| 亚洲国产成人午夜在线一区| 久久婷婷久久一区二区三区| 亚洲欧美一区二区三区国产精品| 美女性感视频久久| 色综合久久中文综合久久牛| 精品日韩在线一区| 天天综合网 天天综合色| 成人高清免费在线播放| 精品国产伦理网| 亚洲狠狠爱一区二区三区| 99久久精品国产一区二区三区| 91精品国产91热久久久做人人| 日韩毛片在线免费观看| 国产精品一区二区你懂的| 欧美一级电影网站| 亚洲国产欧美一区二区三区丁香婷| 成人爽a毛片一区二区免费| 日韩欧美一区二区三区在线| 亚洲电影第三页| 精品视频在线免费看| 亚洲素人一区二区| 99视频在线精品| 中文字幕亚洲不卡| 色综合久久综合网欧美综合网| 国产精品全国免费观看高清| 国产成人免费在线视频| 久久久久久9999| 丁香亚洲综合激情啪啪综合| 久久一区二区三区国产精品| 看片网站欧美日韩| 欧美一区二区人人喊爽| 日本女人一区二区三区| 日韩精品一区二区三区在线观看| 日韩影院免费视频| 日韩一区二区三区视频在线观看| 午夜激情综合网| 欧美一区二区三区电影| 天天综合日日夜夜精品| 不卡免费追剧大全电视剧网站| 国产三级一区二区三区| 精品亚洲成av人在线观看| 欧美精品久久久久久久久老牛影院| 亚洲欧美日韩精品久久久久| 蜜桃精品视频在线观看| 日韩欧美国产精品| 国产一区二区三区综合| 精品剧情在线观看| 国产精品中文字幕欧美| 2023国产精华国产精品| 国产精品99久久久| 亚洲国产岛国毛片在线| 99在线精品观看| 国产精品天美传媒沈樵| 91视频精品在这里| 亚洲一区二区精品久久av| 成人黄色软件下载| 亚洲免费观看在线观看| 欧美特级限制片免费在线观看| 亚洲一区二区偷拍精品| 欧美日韩国产首页| 久久国产精品一区二区| 久久久www成人免费无遮挡大片| 国产福利一区二区| 亚洲毛片av在线| 国产成人久久精品77777最新版本| 亚洲三级免费电影| 亚洲综合图片区| 91精品国产一区二区| 久色婷婷小香蕉久久| 国产精品网站在线观看| 一本色道综合亚洲| 全国精品久久少妇| 日韩精品中文字幕在线不卡尤物 | 久久国产乱子精品免费女| 欧美电视剧免费全集观看| 国产精品影视天天线| 日韩毛片在线免费观看| 欧美日韩性生活| 精品一区二区免费看| 国产精品第四页| 欧美日本在线观看| 国产 日韩 欧美大片| 亚洲自拍偷拍九九九| 日韩情涩欧美日韩视频| 成人一区二区三区视频在线观看| 亚洲综合一区二区| 久久久久久久久免费| 在线国产电影不卡| 国产精品白丝av| 一区二区三区高清不卡| 日韩女优电影在线观看| 色综合视频一区二区三区高清| 国产精品一级在线| 视频一区欧美日韩| 亚洲欧美aⅴ...| 久久视频一区二区| 日韩三级av在线播放| 欧美性猛片aaaaaaa做受| 国产成人综合亚洲91猫咪| 日韩中文字幕不卡| 最新成人av在线| 久久久久久久久久久黄色| 欧美精品电影在线播放| 99视频一区二区| 加勒比av一区二区| 日韩av午夜在线观看| 午夜av电影一区| 一区二区在线观看视频| 国产日产精品一区| 精品国产一区二区三区av性色| 91蜜桃网址入口| 国产91丝袜在线播放0| 男人的天堂久久精品| 亚洲一区二区免费视频| 亚洲激情自拍偷拍| 最新国产成人在线观看| 欧美精品1区2区| 91精品国产手机| 欧美一区二区美女| 欧美日本一道本在线视频| 色综合视频一区二区三区高清| 国产v综合v亚洲欧| 风流少妇一区二区| 国产suv精品一区二区6| 国产91对白在线观看九色| 秋霞av亚洲一区二区三| 亚洲福利视频一区| 亚洲妇女屁股眼交7| 一区二区三区蜜桃网| 一区二区三区视频在线观看| 亚洲欧美一区二区久久 | 欧美日韩精品系列| 在线精品视频免费观看| 欧美亚洲国产一区二区三区va | 婷婷一区二区三区| 日韩精彩视频在线观看| 亚洲激情在线激情| 国产精品护士白丝一区av| 26uuu亚洲综合色欧美| 久久新电视剧免费观看| 国产欧美日韩三区| 中文字幕一区二区三区在线观看 | 欧美日韩精品一区二区三区四区 | 欧美一区二区女人| 欧美tickling网站挠脚心| 精品国产百合女同互慰| 久久久亚洲精品一区二区三区 | 国产精品456露脸| 久久电影网站中文字幕| 激情五月婷婷综合网| 丰满亚洲少妇av| 91久久久免费一区二区| 欧美丰满高潮xxxx喷水动漫| 欧美不卡视频一区| 国产精品久久久久毛片软件| 一级特黄大欧美久久久| 日本不卡在线视频| 国产黄色91视频| 欧美无乱码久久久免费午夜一区 | 蜜臀av一区二区三区| 午夜不卡av免费| 看电影不卡的网站| 9l国产精品久久久久麻豆| 日本精品一级二级| 91精品国产综合久久久久久| 久久人人爽人人爽| 亚洲综合在线观看视频| 理论片日本一区| 91网站黄www| 日韩欧美成人一区二区| 国产亚洲短视频| 美国精品在线观看| 一本到一区二区三区| 日韩精品一区在线| 尤物在线观看一区| 国产精品1区2区3区在线观看| 91成人看片片| 久久精品男人天堂av| 日韩福利电影在线| 国产99精品视频| 国产午夜精品一区二区三区视频 | 中文字幕一区二区三区四区| 日韩国产欧美一区二区三区| 99久久er热在这里只有精品66| 欧美mv和日韩mv国产网站| 亚洲一区二区欧美日韩| 99久久精品国产网站| 久久综合九色综合久久久精品综合| 亚洲情趣在线观看| 国产成人8x视频一区二区| 正在播放亚洲一区| 亚洲欧美日韩在线| 色婷婷综合久久久中文字幕| 久久久久久久久久久99999| 日韩精品一二三区| 色综合欧美在线| 综合久久国产九一剧情麻豆| 韩国av一区二区三区在线观看| 欧美亚洲丝袜传媒另类|