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

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

?? gatsp.cpp

?? 開發(fā)游戲人工智能的王道書
?? 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

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲视频在线观看三级| 在线成人av影院| 亚洲电影欧美电影有声小说| 精品福利视频一区二区三区| 欧美日韩国产高清一区二区三区| 成人sese在线| 国产一区二区在线观看免费| 亚洲中国最大av网站| 亚洲精品成人天堂一二三| 精品对白一区国产伦| 欧美精品777| 欧美日韩电影一区| 成人免费黄色在线| 99在线精品一区二区三区| 免费在线欧美视频| 欧美aaaaaa午夜精品| 亚洲精品国产精品乱码不99| 国产精品免费视频一区| 亚洲精品国产无天堂网2021 | 首页国产欧美久久| 亚洲一区二区三区四区的| 视频在线观看一区| 天天做天天摸天天爽国产一区| 一区二区在线观看视频| 91小宝寻花一区二区三区| 精品久久免费看| 色悠久久久久综合欧美99| 91精品国产综合久久国产大片| 久久精品欧美日韩| 亚洲国产一区在线观看| 国产大陆亚洲精品国产| 91麻豆精品国产自产在线| 自拍av一区二区三区| 国精品**一区二区三区在线蜜桃| 91国偷自产一区二区开放时间| 久久综合色天天久久综合图片| 一区二区三区四区高清精品免费观看 | 国产精品国产三级国产普通话蜜臀| 亚洲国产一区二区视频| 大桥未久av一区二区三区中文| 在线播放日韩导航| 一区二区三区欧美日韩| 国产suv精品一区二区6| 欧美一级高清片在线观看| 亚洲蜜臀av乱码久久精品| 国产精品99久久久久久宅男| 欧美视频在线一区二区三区 | 国产麻豆成人传媒免费观看| 欧美日韩国产a| 亚洲免费在线看| www.日韩在线| 国产农村妇女精品| 激情综合色播五月| 欧美一区二区三区啪啪| 亚洲一区视频在线观看视频| 99在线精品视频| 国产精品国产三级国产专播品爱网| 激情欧美日韩一区二区| 欧美一区二区精品久久911| 亚洲一区二区三区四区不卡| 色婷婷激情久久| 亚洲欧美日韩一区| 波多野结衣中文一区| 国产欧美一区二区精品忘忧草 | 国产亚洲精品中文字幕| 极品少妇xxxx偷拍精品少妇| 精品欧美乱码久久久久久| 另类小说色综合网站| 日韩一区和二区| 免费三级欧美电影| 日韩一级大片在线观看| 美女视频黄 久久| 欧美日本韩国一区| 日日欢夜夜爽一区| 日韩一区二区三区av| 蜜臀av一级做a爰片久久| 日韩亚洲欧美高清| 久久99在线观看| 欧美精品一区二区精品网| 国产又黄又大久久| 国产精品视频一二| 99久久国产综合精品色伊| 日韩伦理免费电影| 91浏览器打开| 亚洲国产wwwccc36天堂| 欧美精三区欧美精三区| 日本美女一区二区| 久久综合给合久久狠狠狠97色69| 国内精品视频666| 欧美国产精品一区二区三区| 成人av影院在线| 亚洲精品美腿丝袜| 欧美日韩国产电影| 久久se这里有精品| 国产精品色哟哟网站| 一本到三区不卡视频| 亚洲国产综合视频在线观看| 91麻豆精品国产91久久久久久久久| 美日韩一区二区三区| 久久久精品国产免大香伊| 99精品视频免费在线观看| 亚洲福利一区二区三区| 精品日韩欧美在线| 成人美女在线观看| 一区二区不卡在线播放| 欧美一区二区福利在线| 国产激情偷乱视频一区二区三区| 中文字幕亚洲一区二区va在线| 在线观看欧美日本| 久久精品国产亚洲aⅴ | 91免费版在线| 午夜欧美2019年伦理| 欧美精品一区在线观看| 99精品视频在线免费观看| 日本视频在线一区| 欧美韩国日本不卡| 欧美人xxxx| 国产91综合一区在线观看| 亚洲与欧洲av电影| 久久久www免费人成精品| 色婷婷精品大视频在线蜜桃视频| 看电影不卡的网站| 亚洲少妇30p| 日韩欧美国产电影| 972aa.com艺术欧美| 日韩不卡一二三区| 国产精品福利一区二区三区| 欧美一区三区四区| 色综合视频在线观看| 精品亚洲成a人| 亚洲国产精品一区二区尤物区| 久久蜜桃av一区精品变态类天堂| 在线观看网站黄不卡| 国产剧情一区二区三区| 一区二区三区中文免费| 久久久亚洲精品石原莉奈| 欧美在线观看一区| 成人午夜视频在线观看| 蜜桃视频一区二区三区| 亚洲美女精品一区| 久久久www成人免费毛片麻豆| 欧美日韩国产精品自在自线| 不卡一区二区中文字幕| 免费日韩伦理电影| 亚洲在线视频免费观看| 国产精品乱码久久久久久| 日韩视频免费观看高清完整版 | 亚洲国产精品久久久久秋霞影院| 国产欧美va欧美不卡在线| 在线播放91灌醉迷j高跟美女| av亚洲精华国产精华| 精品午夜久久福利影院| 亚洲va国产天堂va久久en| 中文字幕一区二区三区视频| 精品美女一区二区三区| 欧美日韩精品系列| 色久综合一二码| 成人精品国产一区二区4080| 捆绑紧缚一区二区三区视频| 亚洲成人第一页| 亚洲猫色日本管| 中文字幕在线一区免费| 国产日韩欧美精品在线| 精品欧美乱码久久久久久1区2区| 欧美精品自拍偷拍动漫精品| 欧美午夜宅男影院| 在线观看国产91| 91啪在线观看| 不卡的看片网站| 国产福利一区二区| 国产一区欧美二区| 狠狠色丁香久久婷婷综合_中| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲午夜电影网| 夜夜精品视频一区二区| 亚洲免费在线看| 亚洲精品视频免费观看| 亚洲欧美一区二区三区孕妇| 亚洲国产精品激情在线观看| 久久色视频免费观看| 精品国产91九色蝌蚪| 精品国产成人系列| 欧美变态口味重另类| 欧美高清视频不卡网| 欧美人xxxx| 欧美久久久久久蜜桃| 欧美片网站yy| 欧美丰满嫩嫩电影| 在线播放国产精品二区一二区四区| 欧美日韩激情在线| 制服丝袜亚洲色图| 69av一区二区三区| 日韩精品一区二区三区蜜臀| 精品国产精品网麻豆系列 | 国产成人精品免费一区二区| 国产成人精品午夜视频免费 | 亚洲国产欧美在线人成| 亚洲国产另类精品专区| 丝袜诱惑亚洲看片| 经典一区二区三区|