Ejemplo n.º 1
0
void SweeperLiveScene::startGeneration()
{
	// 显示当前时代;
	int nGeneration = GeneticAlgorithm::instance()->getGeneration();
	char szGeneration[32];
	sprintf(szGeneration, "Generation: %d", nGeneration);
	m_labelGeneration->setString(szGeneration);

	// 计数归零;
	m_generationCurrentTicks = 0;

	// 适应值归零,重新设计基因;
	for (int i=0; i<CONSTANTS.SweeperNumber; ++i)
	{
		auto sweeper = m_sweepers[i];

		// 设置随机位置;
		sweeper->setFixedPosition(this->randomFixedPosition());

		// 适应值归零;
		sweeper->resetFitness();

		// 设置基因;
		const auto &genes = GeneticAlgorithm::instance()->getGenome(i);
		sweeper->setGenome(genes);
	}
}
Ejemplo n.º 2
0
/******************************************************************************
	Function evolutionStep - controls one evolution step, is called for each
		generation, manages the deffirences between CGP and coevolution
	Takes parameters:
		@input - pointer to an array of all training vectors
		@geneticP - pointer to a struct of all CGP params
		@geneticArray - pointer to a vector of whole population
		@functions - pointer to an array of available functions
		@test - pointer to the test - in variant without coevolution is NULL
******************************************************************************/
void evolutionStep(TData* input, TCgpProperties* geneticP, vector<TIndividual>* geneticArray, TFuncAvailable* functions, TTest* test){

#ifdef COEVOLUTION
	//copy test vector from shared memory
	TCoevIndividual* testVect = NULL;
	testVect = (TCoevIndividual*)malloc(sizeof(struct test));
	pthread_mutex_lock(&test->test_sem);
	testVect->value = new vector<int>(*test->test.value);
	pthread_mutex_unlock(&test->test_sem);
#endif

	getActiveNodes(geneticArray, geneticP);
 
 	for(int ind = 0; ind < geneticP->individCount; ind++){
 		resetFitness(&geneticArray->at(ind));
#ifdef COEVOLUTION
 		for(int i = 0; i < geneticP->testSize; i++){
 			getValue(&geneticArray->at(ind), geneticP, input->data[testVect->value->at(i)]);
			getFitness(&geneticArray->at(ind), geneticP, input->data[testVect->value->at(i)]);			
 		}//for all tests
#else
		for(int i = 0; i < input->dataCount; i++){
			getValue(&geneticArray->at(ind), geneticP, input->data[i]);
			getFitness(&geneticArray->at(ind), geneticP, input->data[i]);
		}//for all inputs	
#endif
 	}//for all individuals

 #ifdef COEVOLUTION
 	//tidy up the test vector
 	delete(testVect->value);
 	free(testVect);
 #endif

	getParents(geneticArray, geneticP);
	createChildren(geneticArray, geneticP, functions);
}