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

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

?? cgabob.cpp

?? 《游戲編程中的人工智能技術》一書中123章源代碼
?? CPP
字號:
#include "CgaBob.h"



//--------------------------RouletteWheelSelection-----------------
//
//	selects a member of the population by using roulette wheel 
//	selection as described in the text.
//------------------------------------------------------------------
SGenome& CgaBob::RouletteWheelSelection()
{
	double fSlice	= RandFloat() * m_dTotalFitnessScore;
	
	double cfTotal	= 0.0;
	
	int	SelectedGenome = 0;
	
	for (int i=0; i<m_iPopSize; ++i)
	{
		
		cfTotal += m_vecGenomes[i].dFitness;
		
		if (cfTotal > fSlice) 
		{
			SelectedGenome = i;
			break;
		}
	}
	
	return m_vecGenomes[SelectedGenome];
}

//----------------------------Mutate---------------------------------
//	iterates through each genome flipping the bits acording to the
//	mutation rate
//--------------------------------------------------------------------
void CgaBob::Mutate(vector<int> &vecBits)
{
	for (int curBit=0; curBit<vecBits.size(); curBit++)
	{
		//do we flip this bit?
		if (RandFloat() < m_dMutationRate)
		{
			//flip the bit
			vecBits[curBit] = !vecBits[curBit];
		}
	}//next bit
}

//----------------------------Crossover--------------------------------
//	Takes 2 parent vectors, selects a midpoint and then swaps the ends
//	of each genome creating 2 new genomes which are stored in baby1 and
//	baby2.
//---------------------------------------------------------------------
void CgaBob::Crossover( const vector<int> &mum,
						const vector<int> &dad,
						vector<int>		  &baby1,
						vector<int>		  &baby2)
{
	//just return parents as offspring dependent on the rate
	//or if parents are the same
	if ( (RandFloat() > m_dCrossoverRate) || (mum == dad)) 
	{
		baby1 = mum;
		baby2 = dad;

		return;
	}
	
	//determine a crossover point
	int cp = RandInt(0, m_iChromoLength - 1);

	//swap the bits
	for (int i=0; i<cp; ++i)
	{
		baby1.push_back(mum[i]);
		baby2.push_back(dad[i]);
	}

	for (i=cp; i<mum.size(); ++i)
	{
		baby1.push_back(dad[i]);
		baby2.push_back(mum[i]);
	}
}
//-----------------------------------Run----------------------------------
//
//	This is the function that starts everything. It is mainly another 
//	windows message pump using PeekMessage instead of GetMessage so we
//	can easily and dynamically make updates to the window while the GA is 
//	running. Basically, if there is no msg to be processed another Epoch
//	is performed.
//------------------------------------------------------------------------
void CgaBob::Run(HWND hwnd)
{
	//The first thing we have to do is create a random population
	//of genomes
	CreateStartPopulation();
	
	m_bBusy = true;
		
}
//----------------------CreateStartPopulation---------------------------
//
//-----------------------------------------------------------------------
void CgaBob::CreateStartPopulation()
{
	//clear existing population
	m_vecGenomes.clear();
	
	for (int i=0; i<m_iPopSize; i++)
	{
		m_vecGenomes.push_back(SGenome(m_iChromoLength));
	}

	//reset all variables
	m_iGeneration		 = 0;
	m_iFittestGenome	 = 0;
	m_dBestFitnessScore  = 0;
	m_dTotalFitnessScore = 0;
}
//--------------------------------Epoch---------------------------------
//
//	This is the workhorse of the GA. It first updates the fitness
//	scores of the population then creates a new population of
//	genomes using the Selection, Croosover and Mutation operators
//	we have discussed
//----------------------------------------------------------------------
void CgaBob::Epoch()
{
	
	UpdateFitnessScores();

	//Now to create a new population
	int NewBabies = 0;

	//create some storage for the baby genomes 
	vector<SGenome> vecBabyGenomes;

	while (NewBabies < m_iPopSize)
	{
		//select 2 parents
		SGenome mum = RouletteWheelSelection();
		SGenome dad = RouletteWheelSelection();

		//operator - crossover
		SGenome baby1, baby2;
		Crossover(mum.vecBits, dad.vecBits, baby1.vecBits, baby2.vecBits);

		//operator - mutate
		Mutate(baby1.vecBits);
		Mutate(baby2.vecBits);

		//add to new population
		vecBabyGenomes.push_back(baby1);
		vecBabyGenomes.push_back(baby2);

		NewBabies += 2;
	}

	//copy babies back into starter population
	m_vecGenomes = vecBabyGenomes;

	//increment the generation counter
	++m_iGeneration;
}

//---------------------------UpdateFitnessScores------------------------
//	updates the genomes fitness with the new fitness scores and calculates
//	the highest fitness and the fittest member of the population.
//	Also sets m_pFittestGenome to point to the fittest. If a solution
//	has been found (fitness == 1 in this example) then the run is halted
//	by setting m_bBusy to false
//-----------------------------------------------------------------------
void CgaBob::UpdateFitnessScores()
{
	m_iFittestGenome		= 0;
	m_dBestFitnessScore		= 0;
	m_dTotalFitnessScore	= 0;

	CBobsMap TempMemory;
	 
	//update the fitness scores and keep a check on fittest so far
	for (int i=0; i<m_iPopSize; ++i)
	{
		//decode each genomes chromosome into a vector of directions
		vector<int> vecDirections = Decode(m_vecGenomes[i].vecBits);

		//get it's fitness score
		m_vecGenomes[i].dFitness = m_BobsMap.TestRoute(vecDirections, TempMemory);

		//update total
		m_dTotalFitnessScore += m_vecGenomes[i].dFitness;

		//if this is the fittest genome found so far, store results
		if (m_vecGenomes[i].dFitness > m_dBestFitnessScore)
		{
			m_dBestFitnessScore = m_vecGenomes[i].dFitness;

			m_iFittestGenome = i;

			m_BobsBrain = TempMemory;

			//Has Bob found the exit?
			if (m_vecGenomes[i].dFitness == 1)
			{
				//is so, stop the run
				m_bBusy = false;
			}
		}

		TempMemory.ResetMemory();
	
	}//next genome
}

//---------------------------Decode-------------------------------------
//
//	decodes a vector of bits into a vector of directions (ints)
//
//	0=North, 1=South, 2=East, 3=West
//-----------------------------------------------------------------------
vector<int> CgaBob::Decode(const vector<int> &bits)
{
	vector<int>	directions;

	//step through the chromosome a gene at a time
	for (int gene=0; gene < bits.size(); gene += m_iGeneLength)
	{
		//get the gene at this position
		vector<int> ThisGene;
		
		for (int bit = 0; bit < m_iGeneLength; ++bit)
		{
			ThisGene.push_back(bits[gene+bit]);
		}

		//convert to decimal and add to list of directions
		directions.push_back(BinToInt(ThisGene));
	}

	return directions;
}

//-------------------------------BinToInt-------------------------------
//	converts a vector of bits into an integer
//----------------------------------------------------------------------
int	CgaBob::BinToInt(const vector<int> &vec)
{
	int val = 0;
	int multiplier = 1;
	
	for (int cBit=vec.size(); cBit>0; cBit--)
	{
		val += vec[cBit-1] * multiplier;
		
		multiplier *= 2;
	}

	return val;
}



//------------------------- Render -------------------------------
//
//	given a surface to render to this function renders the map
//	and the best path if relevant. cxClient/cyClient are the 
//	dimensions of the client window.
//----------------------------------------------------------------
void CgaBob::Render(int cxClient, int cyClient, HDC surface)
{
	//render the map
	m_BobsMap.Render(cxClient, cyClient, surface);

	//render the best route
	m_BobsBrain.MemoryRender(cxClient, cyClient, surface);

	//Render additional information
	string s = "Generation: " + itos(m_iGeneration);
	TextOut(surface, 5, 0, s.c_str(), s.size());
	
	if (!m_bBusy)
	{
		string Start = "Press Return to start a new run";
		
		TextOut(surface, cxClient/2 - (Start.size() * 3), cyClient - 20, Start.c_str(), Start.size());
	}
	
	else
		
	{
		string Start = "Space to stop";
		
		TextOut(surface, cxClient/2 - (Start.size() * 3), cyClient - 20, Start.c_str(), Start.size());
	}
	
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
色8久久精品久久久久久蜜| 日本一区二区三区在线观看| 免费观看一级特黄欧美大片| 日韩午夜三级在线| 国产一区二区精品久久| 国产精品女同互慰在线看| 91女厕偷拍女厕偷拍高清| 亚洲国产精品视频| 欧美一卡2卡3卡4卡| 狠狠色狠狠色综合日日91app| 国产色91在线| 色激情天天射综合网| 图片区小说区区亚洲影院| 日韩精品一区二区在线| 成人免费高清视频在线观看| 亚洲精品美腿丝袜| 日韩午夜精品视频| 国产91丝袜在线播放九色| 亚洲另类色综合网站| 亚洲福利国产精品| 精品国产乱子伦一区| 成人av电影在线| 亚洲成av人片一区二区梦乃| 精品久久国产字幕高潮| eeuss鲁片一区二区三区在线观看| 亚洲国产wwwccc36天堂| 久久你懂得1024| 色哟哟一区二区在线观看| 免费成人av在线播放| 国产精品欧美一区喷水| 欧美剧情片在线观看| 国产麻豆精品theporn| 亚洲老司机在线| 精品久久久久一区二区国产| 91在线视频官网| 麻豆精品一区二区av白丝在线| 国产精品久久精品日日| 欧美高清视频www夜色资源网| 国产精品一卡二卡在线观看| 一区二区三区四区精品在线视频| 精品嫩草影院久久| 日本韩国欧美在线| 麻豆成人av在线| 亚洲区小说区图片区qvod| 免费成人在线视频观看| 中文无字幕一区二区三区| 欧美日韩电影一区| 成人午夜精品在线| 蜜臀va亚洲va欧美va天堂 | 亚洲成人av免费| 久久久亚洲综合| 欧美日韩你懂的| 成人黄色软件下载| 久久精品国产一区二区三 | 国产aⅴ综合色| 偷拍日韩校园综合在线| 中文字幕中文乱码欧美一区二区| 日韩欧美一二三四区| 日本精品一区二区三区四区的功能| 激情深爱一区二区| 午夜视频一区二区| 亚洲天堂福利av| 久久精品夜夜夜夜久久| 欧美一区二区视频网站| 色婷婷一区二区三区四区| 国产成人免费视频网站| 裸体歌舞表演一区二区| 欧美日韩国产三级| 91浏览器入口在线观看| 国产成人免费视频网站高清观看视频 | 色综合网站在线| 国产传媒一区在线| 免费久久99精品国产| 亚洲一二三专区| 中文字幕综合网| 亚洲国产成人自拍| 久久综合久久鬼色| 日韩欧美一二三四区| 欧美精品一二三| 在线观看一区二区视频| 99久久精品国产导航| 国产成人精品一区二区三区网站观看| 免费日韩伦理电影| 日韩精品一二区| 亚洲地区一二三色| 亚洲伊人伊色伊影伊综合网| 国产精品久久久久9999吃药| 国产午夜亚洲精品理论片色戒| 日韩精品一区二区三区在线观看| 91麻豆精品国产综合久久久久久| 欧美日韩免费在线视频| 一区二区三区国产精华| 亚洲同性gay激情无套| 欧美高清一级片在线观看| 久久久电影一区二区三区| 欧美成人激情免费网| 欧美一区二区三区思思人| 欧美高清dvd| 在线播放日韩导航| 制服丝袜亚洲网站| 欧美一区二区三级| 日韩亚洲欧美一区| 欧美xxxx老人做受| 亚洲精品一区二区在线观看| 精品国产亚洲在线| 久久久精品综合| 国产欧美一区二区三区在线看蜜臀| 久久久久综合网| 国产日韩欧美综合一区| 欧美极品少妇xxxxⅹ高跟鞋| 日本一区二区三区四区| 国产精品第13页| 亚洲人成在线观看一区二区| 亚洲精选视频在线| 亚洲国产日韩在线一区模特| 亚洲午夜电影在线观看| 香港成人在线视频| 青椒成人免费视频| 精品一区二区三区久久| 国产乱子伦一区二区三区国色天香| 国产毛片精品一区| 亚洲色图一区二区| 亚洲综合区在线| 婷婷丁香激情综合| 久久se精品一区二区| 国产精品系列在线观看| 成人动漫av在线| 在线观看视频91| 91精品婷婷国产综合久久| 精品国产网站在线观看| 欧美激情综合网| 亚洲激情综合网| 日本中文字幕一区二区视频| 激情伊人五月天久久综合| 成人免费黄色大片| 欧洲另类一二三四区| 日韩亚洲欧美在线| 日本一区二区三级电影在线观看| 亚洲欧美日韩在线播放| 日韩精品一区第一页| 国产一区欧美一区| 91碰在线视频| 91精品黄色片免费大全| 久久久九九九九| 一区二区三区高清| 麻豆91在线播放免费| 成人精品免费看| 精品1区2区3区| 久久这里只有精品6| 亚洲蜜臀av乱码久久精品| 日本网站在线观看一区二区三区| 国产九色精品成人porny| 91在线精品一区二区三区| 日日夜夜免费精品| 国产精品中文有码| 91成人网在线| 精品少妇一区二区三区免费观看 | 国产精品久久看| 香蕉久久一区二区不卡无毒影院| 激情综合一区二区三区| 色综合久久综合网97色综合| 日韩视频一区二区| 自拍视频在线观看一区二区| 蜜桃精品视频在线观看| 99久久精品情趣| 日韩精品一区二区三区中文精品| 亚洲欧美一区二区视频| 日本一不卡视频| 91最新地址在线播放| 欧美v国产在线一区二区三区| 亚洲色图一区二区三区| 久久99热这里只有精品| 91亚洲午夜精品久久久久久| 欧美mv日韩mv亚洲| 一区二区三区欧美日| 国产制服丝袜一区| 欧美日韩你懂的| 中文字幕一区二区5566日韩| 老司机一区二区| 在线视频亚洲一区| 国产欧美日产一区| 美腿丝袜亚洲一区| 亚洲成人激情综合网| 国产99一区视频免费| 欧美一区二区黄| 一区二区三区精密机械公司| 国产成人在线影院 | 久久午夜电影网| 午夜视黄欧洲亚洲| 成人高清视频免费观看| 日韩精品一区二区三区视频在线观看 | 日韩在线卡一卡二| 91视频国产观看| 久久精品一二三| 麻豆精品视频在线观看免费| 欧美在线视频日韩| 18成人在线视频| 国产剧情一区二区三区| 欧美一区二区日韩| 午夜精品影院在线观看|