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

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

?? gatsp.cpp

?? 《游戲編程中的人工智能技術》一書中4
?? CPP
字號:
#include "gaTSP.h"





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

//---------------------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;
}

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

	return false;
}






/////////////////////////////////////////////////////////////////////////////
//
//	Methods for CgaTSP
//
////////////////////////////////////////////////////////////////////////////

//-----------------------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 each chromo
	for (int i=0; i<m_iPopSize; ++i)
	{

		//calculate the tourlength for each chromosome
		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;

			m_iFittestGenome = i;
		}
		
		//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;
	}

}




//--------------------------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];
}

//-----------------------------ChooseSection----------------------------
//
//	given a <vector> size and a min span, this will calculate a random
//	beginning and end point within the <vector>. Used mainly in 
//	mutation and crossover operators
//----------------------------------------------------------------------
void ChooseSection(int &beg,
				   int &end,
				   const int vec_size,
				   const int min_span)
{
	
	beg = RandInt(0, vec_size-1-min_span);
	
	end = beg;
	
	//find an end
	while (end <= beg)
	{
		end = RandInt(0, vec_size-1);
	}
}
//---------------------------MutateEM-----------------------------
//
//	Mutates the chromosome by choosing two random genes and swapping
//	their position.
//-----------------------------------------------------------------
void CgaTSP::MutateEM(vector<int> &chromo)
{
	//return dependent upon mutation rate
	if (RandFloat() > m_dMutationRate) return;

	//choose first gene
	int pos1 = RandInt(0, chromo.size()-1);

	//choose second
	int pos2 = pos1;

	while (pos1 == pos2)
	{
		pos2 = RandInt(0, chromo.size()-1);
	}

	//swap their positions
	swap(chromo[pos1], chromo[pos2]);
}



//-------------------------CrossoverPMX---------------------------------
//
// crossover operator based on 'partially matched crossover' as 
// defined in the text
//-------------------------------------------------------------------
void CgaTSP::CrossoverPMX(const vector<int>	&mum, 
							            const vector<int>	&dad, 
							            vector<int>	      &baby1, 
						            	vector<int>       &baby2)
{
	baby1 = mum;
	baby2 = dad;
	
	//just return dependent on the crossover rate or if the
	//chromosomes are the same.
	if ( (RandFloat() > m_dCrossoverRate) || (mum == dad)) 
	{
		return;
	}

	//first we choose a section of the chromosome
	int beg = RandInt(0, mum.size()-2);
	
	int end = beg;
	
	//find an end
	while (end <= beg)
	{
		end = RandInt(0, mum.size()-1);
	}

	//now we iterate through the matched pairs of genes from beg
	//to end swapping the places in each child
	vector<int>::iterator posGene1, posGene2;

	for (int pos = beg; pos < end+1; ++pos)
	{
		//these are the genes we want to swap
		int gene1 = mum[pos];
		int gene2 = dad[pos];

		if (gene1 != gene2)
		{
			//find and swap them in baby1
			posGene1 = find(baby1.begin(), baby1.end(), gene1);
			posGene2 = find(baby1.begin(), baby1.end(), gene2);

			swap(*posGene1, *posGene2);

			//and in baby2
			posGene1 = find(baby2.begin(), baby2.end(), gene1);
			posGene2 = find(baby2.begin(), baby2.end(), gene2);
			
			swap(*posGene1, *posGene2);
		}
		
	}//next pair
}	


//-----------------------CreateStartingPopulation()------------------------
//
//	clears any existing population, fills a vector with a random population
//	of genomes and resets appropriate member variables
//-------------------------------------------------------------------------
void CgaTSP::CreateStartingPopulation()
{
	//make sure the vector of genomes is empty before we
	//start
	m_vecPopulation.clear();
	
	//create a new population of random genomes
	for (int i=0; i<m_iPopSize; ++i)
	{
		m_vecPopulation.push_back(SGenome(m_iChromoLength));
	}

	//make sure variables are initialised correctly
	m_iGeneration	 = 0;
	m_dShortestRoute = 9999999;
	m_iFittestGenome = 0;
	m_bStarted			 = false;

}
//-----------------------------------Run-----------------------------------
//
//	This is the function that initializes a new population and sets the
//  ga running 
//------------------------------------------------------------------------
void CgaTSP::Run(HWND hwnd)
{
	//The first thing we have to do is create a random population
	//of genomes
	CreateStartingPopulation();

	m_bStarted = true;

}
//-------------------------Reset()------------------------------
//
//	resets all the relevant variables ready for a new generation
//--------------------------------------------------------------
void CgaTSP::Reset()
{
	//just make the shortest route some excessively large distance
	m_dShortestRoute	= 999999999;
	m_dLongestRoute		= 0;
	m_dTotalFitness		= 0;
}

//------------------------Epoch-------------------------------
//
//	creates a new population of genomes using the selection,
//	mutation and crossover operators
//------------------------------------------------------------
void CgaTSP::Epoch()
{

	//first reset variables and calculate the fitness of each genome
	Reset();
	
	CalculatePopulationsFitness();

	//if a solution is found exit
	if ((m_dShortestRoute <= m_pMap->BestPossibleRoute()))
	{
		m_bStarted = false;
		
		return;
	}
	
	//create a temporary vector for the new population
	vector<SGenome> vecNewPop;
	
	//First add NUM_BEST_TO_ADD number of the last generation's
	//fittest genome(elitism)
	for (int i=0; i<NUM_BEST_TO_ADD; ++i)
	{
		vecNewPop.push_back(m_vecPopulation[m_iFittestGenome]);
	}
	

	//now create the remainder of the population
	while (vecNewPop.size() != m_iPopSize)
	{
	
		//grab two parents
		SGenome mum = RouletteWheelSelection();
		SGenome dad = RouletteWheelSelection();

		//create 2 children
		SGenome baby1, baby2;
		
		//Breed them
		CrossoverPMX(mum.vecCities,
					       dad.vecCities,
					       baby1.vecCities,
					       baby2.vecCities);

		//and mutate them
		MutateEM(baby1.vecCities);
		MutateEM(baby2.vecCities);

		//add them to new population
		vecNewPop.push_back(baby1);
		vecNewPop.push_back(baby2);
	}

	//copy into next generation
	m_vecPopulation = vecNewPop;

	//increment generation counter
	++m_iGeneration;
}

//-------------------------------Render-------------------------------
//
//	This function does all our drawing and textual output. Called from
// the winproc when it receives a WM_PAINT msg
//--------------------------------------------------------------------
void CgaTSP::Render(HDC surface, int cx, int cy)
{

		//draw all the cities
	for (int city_num = 0; city_num < m_pMap->m_vecCityCoOrds.size(); ++city_num)
	{
		int x = (int)m_pMap->m_vecCityCoOrds[city_num].x;
		int y = (int)m_pMap->m_vecCityCoOrds[city_num].y;
		
		Ellipse(surface, x-CITY_SIZE, y+CITY_SIZE, x+CITY_SIZE, y-CITY_SIZE);
	}
	
	//draw the fittest route so far
	vector<int> route = m_vecPopulation[m_iFittestGenome].vecCities;
	
	//only display the routes if we are in a run
	if (m_iGeneration != 0)
	{
		
		MoveToEx(surface, (int)m_pMap->m_vecCityCoOrds[route[0]].x, (int)m_pMap->m_vecCityCoOrds[route[0]].y, NULL);
		
		for (int i=1; i<route.size(); ++i)
		{
			int CityX = (int)m_pMap->m_vecCityCoOrds[route[i]].x;
			int CityY = (int)m_pMap->m_vecCityCoOrds[route[i]].y;
			
			LineTo(surface, CityX, CityY);
			
			//draw the numbers representing the order the cities are visited.
			//No point drawing them after about a 100 cities as the display
			//just becomes confused
			if (NUM_CITIES < 100)
			{
				string CityNumber = itos(i);
			
				TextOut(surface, CityX, CityY, CityNumber.c_str(), CityNumber.size());
			}
		}

		//now draw the line from the last city visited back to the starting point
		LineTo(surface, (int)m_pMap->m_vecCityCoOrds[route[0]].x, (int)m_pMap->m_vecCityCoOrds[route[0]].y);
	}	
	
	//print stats
	string sGen = itos(m_iGeneration);
	sGen = "Generation: " + sGen;
	TextOut(surface, 5, 5, sGen.c_str(), sGen.size());


	if (!m_bStarted)
	{
		string Start = "Press Return to start a new run";

		TextOut(surface, cx/2 - (Start.size() * 3), cy - 20, Start.c_str(), Start.size());
	}

	else

	{
		string Start = "Space to stop";
		
		TextOut(surface, cx/2 - (Start.size() * 3), cy - 20, Start.c_str(), Start.size());
	}	
}


?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
中文字幕av在线一区二区三区| 中文字幕一区二| 午夜欧美大尺度福利影院在线看 | 久久国产精品无码网站| 成人午夜激情片| 久久久久久夜精品精品免费| 国产一区二区三区四区五区美女| 精品1区2区在线观看| 欧洲国内综合视频| 亚洲宅男天堂在线观看无病毒| 欧美日韩国产大片| 美国欧美日韩国产在线播放| 久久综合视频网| 菠萝蜜视频在线观看一区| 亚洲四区在线观看| 欧美日韩国产精选| 99麻豆久久久国产精品免费| 午夜精品一区二区三区免费视频 | 亚洲综合偷拍欧美一区色| 久久久久一区二区三区四区| 制服.丝袜.亚洲.另类.中文| 麻豆高清免费国产一区| 亚洲午夜久久久久久久久久久| 国产成人啪午夜精品网站男同| 亚洲高清免费一级二级三级| 日韩一级免费观看| 国产精品一区三区| 亚洲男女一区二区三区| 欧美一区二区视频在线观看2022 | 亚洲一区二区在线视频| a在线欧美一区| 国产欧美一区二区三区在线看蜜臀| 日韩伦理av电影| 国产成人午夜视频| 国产精品免费丝袜| 亚洲va国产va欧美va观看| 国产精品一区二区在线观看网站| 日韩免费电影一区| 亚洲第四色夜色| 欧美综合久久久| 亚洲一区二区三区三| 丁香婷婷综合色啪| 欧美又粗又大又爽| 欧美视频中文一区二区三区在线观看| 亚洲天堂成人在线观看| 色噜噜偷拍精品综合在线| 久久综合九色综合97婷婷 | 在线观看国产日韩| 亚洲一区二区免费视频| 欧美日韩精品一区二区在线播放| 午夜精品一区二区三区免费视频| 欧美一区二区视频网站| 国产一区二区三区在线观看精品| 中文字幕精品一区二区精品绿巨人| 成人精品国产一区二区4080| 一区二区三区高清不卡| 国产成人在线视频播放| 国产精品色婷婷| 日本道免费精品一区二区三区| 亚洲bt欧美bt精品| 亚洲精品一区二区三区福利| 亚洲精品国产第一综合99久久| 亚洲国产激情av| av一区二区不卡| 亚洲一区二区欧美日韩| 欧美一个色资源| 国产大片一区二区| 一区二区三区在线看| 欧美一区二区三区四区视频| 成人app在线| 日韩av网站在线观看| 欧美久久一区二区| 亚洲午夜在线电影| 2欧美一区二区三区在线观看视频| 北条麻妃一区二区三区| 日韩国产一二三区| 日韩一区在线看| 欧美变态tickling挠脚心| 美女一区二区久久| 亚洲视频在线一区二区| 日韩精品中文字幕一区二区三区 | 国产99久久久国产精品潘金| 亚洲人快播电影网| 91精品国产入口在线| 成a人片亚洲日本久久| 日韩国产欧美在线视频| 综合av第一页| www国产精品av| 欧美色综合天天久久综合精品| 精品一区二区免费看| 欧美老女人第四色| 豆国产96在线|亚洲| 日韩高清电影一区| 亚洲午夜久久久久久久久电影院| 国产欧美日韩精品a在线观看| 制服视频三区第一页精品| 色中色一区二区| 日韩在线观看一区二区| 精品欧美乱码久久久久久1区2区| 在线一区二区三区四区五区| 成人高清av在线| 国产精品一二三四| 97精品国产97久久久久久久久久久久| 在线日韩av片| 国产精品一级二级三级| 免费成人在线影院| 欧美激情一区三区| 精品sm在线观看| 日韩欧美成人一区二区| 7777精品伊人久久久大香线蕉最新版| av色综合久久天堂av综合| 国产成人在线色| 捆绑紧缚一区二区三区视频| 天堂蜜桃一区二区三区| 亚洲一区在线视频| 亚洲成人免费在线| 午夜激情一区二区| 日日夜夜一区二区| 日韩精品一二区| 视频一区欧美精品| 麻豆国产91在线播放| 久久爱另类一区二区小说| 久久99精品国产麻豆不卡| 久久av老司机精品网站导航| 韩国女主播成人在线| 亚洲柠檬福利资源导航| 一区二区在线观看不卡| 亚洲国产日日夜夜| 日韩国产精品久久久久久亚洲| 日韩国产欧美在线播放| 久久精品噜噜噜成人av农村| 狠狠色综合播放一区二区| 国产v综合v亚洲欧| 96av麻豆蜜桃一区二区| 欧美亚洲日本国产| 91麻豆精品国产自产在线观看一区| 日韩免费性生活视频播放| 久久久久久久久蜜桃| 国产精品久久久久久久久搜平片| 91麻豆精品国产91久久久资源速度 | 91国产丝袜在线播放| 欧美色图片你懂的| 国产精品亚洲成人| av在线不卡观看免费观看| 欧美在线不卡视频| 欧美一区二区三区视频在线| 久久久青草青青国产亚洲免观| 中文字幕欧美一| 亚洲激情综合网| 裸体一区二区三区| 成人av电影在线| 色综合天天综合网国产成人综合天| 91视频精品在这里| 精品一区二区三区的国产在线播放| 国产精品一区二区三区99| 92精品国产成人观看免费| 欧美一区二区三区日韩视频| 久久久久青草大香线综合精品| 亚洲欧美日韩国产中文在线| 日韩精彩视频在线观看| 不卡欧美aaaaa| 欧美一区二区福利在线| 一区免费观看视频| 久久精品国产**网站演员| 成人免费高清在线| 欧美日韩在线亚洲一区蜜芽| 久久精品一区二区| 欧美国产精品v| 五月天欧美精品| 99热国产精品| 欧美精品一区二区三区高清aⅴ| 自拍偷在线精品自拍偷无码专区| 日本亚洲最大的色成网站www| 国产一区二区导航在线播放| 欧美日韩国产免费一区二区| 中文字幕精品在线不卡| 亚洲午夜免费电影| 91在线无精精品入口| 久久久久久久久久久久久女国产乱| 国产精品美女久久久久aⅴ国产馆 国产精品美女久久久久av爽李琼 国产精品美女久久久久高潮 | 免费精品99久久国产综合精品| 懂色av一区二区三区蜜臀| 欧美日韩激情一区二区| 欧美国产精品中文字幕| 美女精品一区二区| 91精品国产91久久久久久一区二区 | 亚洲影视在线播放| 91亚洲永久精品| 狠狠狠色丁香婷婷综合激情| 欧美一级高清片| 亚洲视频一区在线| 国产精品自拍一区| 精品国产凹凸成av人导航| 亚洲国产一区二区a毛片| 一本色道久久综合亚洲aⅴ蜜桃| 国产亚洲精品福利| 韩国v欧美v亚洲v日本v| 欧美电视剧免费观看| 日日夜夜免费精品视频| 精品视频资源站|