Example #1
0
int stringPopulation()
{
	Population pop;
	pop.setTarget(55.5);
	bool found = false;
	double prev = -1;
	int i = 0, gen = 0;;
	while(!found) {
		pop.update();
		if(pop.fittestGenome().second>0.999) {
			found = true;
			std::cout << "Solution found.\n";
			continue;
		}
		if(pop.fittestGenome().second>prev||i % 30000 == 0) {
			prev = pop.fittestGenome().second;
			std::cout << "\nGeneration: " << ++gen << " Target: " << pop.getTarget() << std::endl;
			std::string answer;
			for(auto& a:pop.getPopulation()) {
				answer = a.get();
				while(answer.find_first_of(':')!=std::string::npos)answer.replace(answer.find_first_of(':'),1,1,'+');
				while(answer.find_first_of(';')!=std::string::npos)answer.replace(answer.find_first_of(';'),1,1,'-');
				while(answer.find_first_of('<')!=std::string::npos)answer.replace(answer.find_first_of('<'),1,1,'*');
				while(answer.find_first_of('=')!=std::string::npos)answer.replace(answer.find_first_of('='),1,1,'/');
				std::cout << answer << std::endl;
			}
			answer = pop.fittestGenome().first;
			while(answer.find_first_of(':')!=std::string::npos)answer.replace(answer.find_first_of(':'),1,1,'+');
			while(answer.find_first_of(';')!=std::string::npos)answer.replace(answer.find_first_of(';'),1,1,'-');
			while(answer.find_first_of('<')!=std::string::npos)answer.replace(answer.find_first_of('<'),1,1,'*');
			while(answer.find_first_of('=')!=std::string::npos)answer.replace(answer.find_first_of('='),1,1,'/');
			std::cout << answer << std::endl;
			std::cout << "\nFittest individual: " << answer << " " << pop.fittestGenome().second << std::endl;
			std::cout << "Population size: " << pop.getPopulation().size() << std::endl;
			std::cin.get();
			i = 0;
		}
		gen++;

		i++;
		if(i % 1000 == 0)std::cout << ".";
	}
	std::cout << "\nGeneration: " << ++gen << " Target: " << pop.getTarget() << std::endl;
	std::string answer;
	for(auto& a:pop.getPopulation()) {
		answer = a.get();
		while(answer.find_first_of(':')!=std::string::npos)answer.replace(answer.find_first_of(':'),1,1,'+');
		while(answer.find_first_of(';')!=std::string::npos)answer.replace(answer.find_first_of(';'),1,1,'-');
		while(answer.find_first_of('<')!=std::string::npos)answer.replace(answer.find_first_of('<'),1,1,'*');
		while(answer.find_first_of('=')!=std::string::npos)answer.replace(answer.find_first_of('='),1,1,'/');
		std::cout << answer << std::endl;
	}
	answer = pop.fittestGenome().first;
	while(answer.find_first_of(':')!=std::string::npos)answer.replace(answer.find_first_of(':'),1,1,'+');
	while(answer.find_first_of(';')!=std::string::npos)answer.replace(answer.find_first_of(';'),1,1,'-');
	while(answer.find_first_of('<')!=std::string::npos)answer.replace(answer.find_first_of('<'),1,1,'*');
	while(answer.find_first_of('=')!=std::string::npos)answer.replace(answer.find_first_of('='),1,1,'/');
	std::cout << answer << std::endl;
	std::cout << "\nFittest individual: " << answer << " " << pop.fittestGenome().second << std::endl;
	std::cout << "Population size: " << pop.getPopulation().size() << std::endl;
	std::cin.get();
	return 0;
}