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

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

?? gatsp.cpp

?? 開發游戲人工智能的王道書
?? 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一区二区三区免费野_久草精品视频
日韩欧美高清一区| 免费看欧美女人艹b| 成人99免费视频| 国产精品久久久爽爽爽麻豆色哟哟| 国产一区二区精品久久99| 国产亚洲污的网站| 成人白浆超碰人人人人| 亚洲精品第1页| 91精品在线观看入口| 麻豆视频一区二区| 久久久不卡网国产精品二区| 国产成人啪免费观看软件| 国产精品久久影院| 欧美视频三区在线播放| 久久成人综合网| 亚洲国产精品国自产拍av| 91免费在线播放| 日韩福利视频导航| 亚洲国产精品精华液ab| 欧美日韩在线观看一区二区 | 蜜臀av在线播放一区二区三区| 欧美不卡一区二区三区| 国产91精品一区二区麻豆网站 | 久久激五月天综合精品| 久久影视一区二区| 91香蕉视频mp4| 日韩av午夜在线观看| 国产三级精品视频| 欧美色图天堂网| 国产精品一区久久久久| 一区二区三区在线免费视频| 欧美一级免费大片| 成人18视频在线播放| 日韩电影一区二区三区| 专区另类欧美日韩| 91麻豆精品国产91久久久使用方法| 国产高清无密码一区二区三区| 一区二区三区四区不卡视频| 欧美精品一区二区三区四区| 91美女福利视频| 国产一区二区影院| 日韩在线一区二区| **网站欧美大片在线观看| 欧美一区二区三区在线视频| 一本久久综合亚洲鲁鲁五月天| 久久国产免费看| 亚洲成人午夜电影| 亚洲欧洲日产国产综合网| 欧美一区二区三区在| 91精品办公室少妇高潮对白| 国产精品小仙女| 免费精品视频最新在线| 亚洲一区二区欧美日韩 | 日韩欧美色电影| 欧美日韩一区二区三区四区五区| 国产成人亚洲综合色影视| 蜜臀av性久久久久蜜臀av麻豆 | 国产午夜精品一区二区三区四区 | 成人精品国产福利| 另类综合日韩欧美亚洲| 亚洲精品中文字幕在线观看| 久久午夜电影网| 日韩欧美国产一区二区在线播放 | 亚洲一区欧美一区| 亚洲欧美色综合| 国产精品毛片无遮挡高清| 精品卡一卡二卡三卡四在线| 91精品国产综合久久精品| 欧洲一区在线电影| 欧美亚洲国产怡红院影院| 99国产精品久久久久久久久久| 国产成人精品亚洲午夜麻豆| 国产在线乱码一区二区三区| 美女精品一区二区| 久久99精品一区二区三区| 久久精品国产99| 黄一区二区三区| 国产精品一线二线三线精华| 国产精品白丝av| 国产成人av电影在线播放| 国产福利一区在线观看| 国产福利一区在线| 不卡免费追剧大全电视剧网站| www.综合网.com| 91女神在线视频| 欧美日韩高清不卡| 日韩欧美一级在线播放| 精品国产一区二区三区久久久蜜月| 欧美xfplay| 欧美激情一区二区三区不卡 | 亚洲观看高清完整版在线观看 | 色婷婷国产精品久久包臀| 99国产精品国产精品久久| 91国偷自产一区二区开放时间 | 99久久精品国产毛片| 日本道色综合久久| 欧美另类久久久品| 日韩欧美国产一区二区在线播放| 欧美精品一区二区三区高清aⅴ| 久久精品一区二区三区不卡| 国产精品国产三级国产专播品爱网| 综合久久综合久久| 日韩黄色片在线观看| 韩国女主播一区| 波多野结衣亚洲| 欧美三级日韩三级| 精品日韩在线观看| 综合久久综合久久| 免费人成网站在线观看欧美高清| 国产盗摄视频一区二区三区| 99久久免费国产| 欧美精选在线播放| 日本一区二区动态图| 亚洲资源在线观看| 国产一区二区三区蝌蚪| 色综合久久久久综合体桃花网| 欧美日韩高清一区二区不卡| 久久久久国产一区二区三区四区| 国产精品传媒视频| 久久精品免费观看| 91黄色免费看| 国产欧美日本一区视频| 亚洲一区二区三区四区在线免费观看 | 日产精品久久久久久久性色| 国产精品一级片| 欧美群妇大交群中文字幕| 久久久国产精华| 日韩精品亚洲一区| 99re8在线精品视频免费播放| 日韩一级免费观看| 亚洲男人天堂一区| 国产麻豆视频一区二区| 欧美视频在线观看一区二区| 国产三级欧美三级日产三级99| 一区二区三区中文字幕精品精品| 国产美女精品在线| 国产综合色精品一区二区三区| 精品国产成人系列| 六月丁香婷婷久久| 91亚洲永久精品| 日韩欧美一卡二卡| 亚洲午夜一区二区三区| 国产69精品久久久久777| 欧美一区二区三区视频免费| 亚洲另类春色国产| 99视频一区二区| 欧美—级在线免费片| 精品无人区卡一卡二卡三乱码免费卡| 欧美性色黄大片| 亚洲三级在线看| 99在线热播精品免费| 亚洲国产精品av| 久久99精品久久只有精品| 欧美精品视频www在线观看| 日韩一区中文字幕| 成人av电影免费观看| 欧美国产精品中文字幕| 国产mv日韩mv欧美| 久久久国产综合精品女国产盗摄| 久久se这里有精品| 日韩亚洲欧美成人一区| 日韩成人午夜电影| 日韩欧美国产综合| 老司机精品视频一区二区三区| 欧美丰满美乳xxx高潮www| 亚洲综合成人在线视频| 欧美怡红院视频| 亚州成人在线电影| 欧美浪妇xxxx高跟鞋交| 五月天久久比比资源色| 欧美日韩高清在线播放| 视频一区在线播放| 欧美一区二区三区成人| 久久精品国产一区二区| 欧美成人r级一区二区三区| 久久国产综合精品| 久久亚洲春色中文字幕久久久| 国产一区二区美女诱惑| 国产女主播视频一区二区| 成人av片在线观看| 亚洲免费色视频| 欧美图片一区二区三区| 日本亚洲最大的色成网站www| 日韩一区二区电影在线| 国产伦精品一区二区三区免费 | 毛片一区二区三区| 久久久国产午夜精品| av激情综合网| 亚洲综合视频在线观看| 在线成人小视频| 九九国产精品视频| 国产精品人人做人人爽人人添| 91啪在线观看| 免费观看91视频大全| 国产亚洲一区二区三区四区| 色哟哟一区二区| 久久99国产精品久久99果冻传媒| 日本一区二区三区在线观看| 色狠狠综合天天综合综合| 日本一不卡视频|