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

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

?? gatsp.cpp

?? Evenly spaces the cities on the perimeter of a wheel and returns a vector of their coordinates 利用AI
?? 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一区二区三区免费野_久草精品视频
最新成人av在线| 国产精品五月天| 亚洲一区免费在线观看| 在线一区二区视频| 亚洲伦理在线精品| 欧美三级电影一区| 亚洲一区二区四区蜜桃| 欧美在线免费视屏| 亚洲电影你懂得| 精品粉嫩超白一线天av| 岛国精品在线播放| 玉足女爽爽91| 日韩欧美一级片| 国产成都精品91一区二区三| 日韩伦理av电影| 欧美精品一级二级| 韩国精品免费视频| 国产精品毛片久久久久久| 99精品在线观看视频| 亚洲品质自拍视频| 3atv一区二区三区| 精品一二三四区| 欧美激情中文字幕| 欧美日韩另类国产亚洲欧美一级| 麻豆视频一区二区| 亚洲精选视频在线| 精品处破学生在线二十三| www.欧美.com| 全国精品久久少妇| 日本一区二区三区国色天香| 91女厕偷拍女厕偷拍高清| 免费美女久久99| 亚洲色欲色欲www| 精品国产乱码久久久久久浪潮| 99久久免费国产| 久久91精品国产91久久小草| 亚洲欧美日韩国产手机在线| 日韩欧美一二三区| 色哟哟国产精品| 美女在线一区二区| 中文字幕欧美一| 精品剧情v国产在线观看在线| 99久久精品久久久久久清纯| 日一区二区三区| 国产日韩欧美综合一区| 色就色 综合激情| 国产精品综合视频| 午夜亚洲国产au精品一区二区| 国产精品素人一区二区| 日韩免费高清电影| 欧美欧美欧美欧美首页| av亚洲产国偷v产偷v自拍| 极品尤物av久久免费看| 天天色图综合网| 一区二区三区国产豹纹内裤在线| 国产日韩欧美综合在线| 精品电影一区二区三区| 制服.丝袜.亚洲.中文.综合| 91精品办公室少妇高潮对白| 成人免费av在线| 秋霞电影网一区二区| 一区二区三区蜜桃| 久久精品日韩一区二区三区| 国产精品色婷婷| 久久精品综合网| 欧美理论在线播放| 欧洲国产伦久久久久久久| 91首页免费视频| 国产69精品久久久久毛片| 免费成人你懂的| 亚洲一区二区综合| 亚洲伊人色欲综合网| 一区二区三区日韩精品| 亚洲日本在线视频观看| 亚洲三级免费观看| 亚洲人成伊人成综合网小说| 国产精品欧美久久久久无广告| 久久久久久久久久久久久女国产乱| 欧美久久一区二区| 日韩欧美国产成人一区二区| 欧美成人aa大片| 26uuuu精品一区二区| 精品欧美一区二区久久| 精品成人私密视频| 2019国产精品| 国产蜜臀97一区二区三区| 国产精品国产三级国产| 自拍偷拍国产精品| 亚洲一级片在线观看| 天天色天天操综合| 国内久久精品视频| 国产+成+人+亚洲欧洲自线| 99久久精品免费观看| 91国模大尺度私拍在线视频| 91黄视频在线观看| 欧美在线短视频| 欧美一级黄色片| 欧美—级在线免费片| 久久久影视传媒| 国产精品麻豆一区二区| 中文字幕一区二区三区av | 久久精品一区二区三区四区| 久久蜜桃一区二区| 日本一区二区三区四区| 日本一区二区三区在线观看| 国产精品电影一区二区| 亚洲乱码国产乱码精品精可以看| 视频一区在线播放| 加勒比av一区二区| 一本色道久久加勒比精品| 91精品国产日韩91久久久久久| 久久久九九九九| 一区二区三区日韩欧美精品| 精品一区二区免费看| 99re热视频这里只精品| 欧美一级理论片| 亚洲天堂av一区| 免费精品视频在线| 色综合视频在线观看| 欧美成人a视频| 亚洲午夜电影网| 极品销魂美女一区二区三区| 色嗨嗨av一区二区三区| 欧美精品日日鲁夜夜添| 26uuu欧美| 亚洲男女毛片无遮挡| 日韩av在线播放中文字幕| 黄页视频在线91| 欧美精品高清视频| 亚洲男同1069视频| 日韩欧美精品三级| 亚洲视频每日更新| 国内久久婷婷综合| 在线综合亚洲欧美在线视频| 亚洲色图在线看| 国产suv精品一区二区883| 在线播放91灌醉迷j高跟美女 | 91精品国产色综合久久ai换脸| 中文字幕av一区二区三区免费看 | 久久一二三国产| 午夜不卡av在线| 91社区在线播放| 中文字幕乱码日本亚洲一区二区 | 成人性生交大合| 在线综合+亚洲+欧美中文字幕| 1区2区3区精品视频| 久久99精品久久久久婷婷| 97se亚洲国产综合自在线| 精品国产免费视频| 日韩 欧美一区二区三区| 欧美日韩精品一区二区| 亚洲品质自拍视频网站| 99re视频这里只有精品| 日本一区二区动态图| 国产精品一区二区黑丝| 这里只有精品电影| 天天做天天摸天天爽国产一区| 99国产欧美另类久久久精品| 国产欧美日韩综合| 国产成人高清视频| 精品国产一二三| 激情五月播播久久久精品| 91精品国产综合久久久蜜臀粉嫩 | 免费观看日韩av| 91久久香蕉国产日韩欧美9色| 国产亚洲精品中文字幕| 香蕉av福利精品导航 | 在线一区二区三区四区五区| 亚洲视频在线一区观看| 97久久超碰精品国产| 亚洲日本一区二区| 91老师片黄在线观看| 一区二区三区精密机械公司| 日本高清不卡aⅴ免费网站| 国产精品国产精品国产专区不片| 成人性视频免费网站| 久久久久久久久久久久久女国产乱 | 91九色最新地址| 亚洲猫色日本管| 99久久精品免费精品国产| 中文在线免费一区三区高中清不卡| 紧缚奴在线一区二区三区| 久久久久久毛片| 国产一区二区在线影院| 久久免费的精品国产v∧| 另类小说色综合网站| 欧美成人一级视频| 精品一二三四在线| 国产亚洲精品资源在线26u| 不卡一区中文字幕| 最新不卡av在线| 欧美影院一区二区| 一区二区三区精品| 一区二区三区中文在线观看| av亚洲精华国产精华| 亚洲女性喷水在线观看一区| 一本色道亚洲精品aⅴ| 亚洲码国产岛国毛片在线| 欧美日韩另类一区| 国产精品一二一区|