コード例 #1
0
void GeneticPopulation::evolve() {

	std::sort(population.begin(), population.end());

	calculateStats();
	Genomes newPopulation;

	assert((bestTopN * bestCopies) % 2 == 0);
	assert(population.size() % 2 == 0);

	pickBest(bestTopN, bestCopies, newPopulation);

	while (newPopulation.size() < population.size()) {
		Genome parent1 = pickRoulette();
		Genome parent2 = pickRoulette();

		Weights child1, child2;

		crossover(parent1.weights, parent2.weights, child1, child2);

		mutate(child1);
		mutate(child2);

		newPopulation.push_back(Genome(child1, 0));
		newPopulation.push_back(Genome(child2, 0));
	}

	population = newPopulation;
}
コード例 #2
0
ファイル: GenAlg.cpp プロジェクト: KeithMcPherson/Tetris-AI
//-----------------------------------Epoch()-----------------------------
//
//	takes a population of chromosones and runs the algorithm through one
//	 cycle.
//	Returns a new population of chromosones.
//
//-----------------------------------------------------------------------
vector<Genome> GenAlg::Epoch(vector<Genome> &old_pop)
{
	//assign the given population to the classes population
  mPop = old_pop;

  //reset the appropriate variables
  Reset();

  //sort the population (for scaling and elitism)
  sort(mPop.begin(), mPop.end());

  //calculate best, worst, average and total fitness
	CalculateBestWorstAvTot();
  
  //create a temporary vector to store new chromosones
	vector <Genome> vecNewPop;

	//Now to add a little elitism we shall add in some copies of the
	//fittest genomes. Make sure we add an EVEN number or the roulette
  //wheel sampling will crash
	if (!(NUM_COPIES_ELITE * NUM_ELITE % 2))
	{
		GrabNBest(NUM_ELITE, NUM_COPIES_ELITE, vecNewPop);
	}
	

	//now we enter the GA loop
	
	//repeat until a new population is generated
	while (vecNewPop.size() < mPopSize)
	{
		//grab two chromosones
		Genome mum = GetChromoRoulette();
		Genome dad = GetChromoRoulette();

		//create some offspring via crossover
		vector<double>		baby1, baby2;

		Crossover(mum.weights, dad.weights, baby1, baby2);

		//now we mutate
		Mutate(baby1);
		Mutate(baby2);

		//now copy into vecNewPop population
		vecNewPop.push_back(Genome(baby1, 0));
		vecNewPop.push_back(Genome(baby2, 0));
	}

	//finished so assign new pop back into m_vecPop
	mPop = vecNewPop;

	return mPop;
}
コード例 #3
0
ファイル: GenAlg.cpp プロジェクト: KeithMcPherson/Tetris-AI
//-----------------------------------constructor-------------------------
//
//	sets up the population with random floats
//
//-----------------------------------------------------------------------
GenAlg::GenAlg(int	  popsize,
                 double	MutRat,
                 double	CrossRat,
                 int	  numweights) :	mPopSize(popsize),
                                      mMutationRate(MutRat),
										                  mCrossoverRate(CrossRat),
										                  mChromoLength(numweights),
										                  mTotalFitness(0),
										                  mGeneration(0),
										                  mFittestGenome(0),
										                  mBestFitness(0),
										                  mWorstFitness(99999999),
										                  mAverageFitness(0)
{
	//initialise population with chromosomes consisting of random
	//weights and all fitnesses set to zero
	for (int i=0; i<mPopSize; ++i)
	{
		mPop.push_back(Genome());

		for (int j=0; j<mChromoLength; ++j)
		{
			mPop[i].weights.push_back(RandomClamped());
		}
	}
}
コード例 #4
0
ファイル: world.cpp プロジェクト: firestack/cell-simulation
bool World::initialize()
{
    m_border.setRadius(m_radius);
    m_border.setOrigin(m_radius, m_radius);
    m_border.setFillColor(sf::Color(32, 32, 32, 255));
    m_border.setOutlineColor(sf::Color(128, 128, 128, 255));
    m_border.setOutlineThickness(10.0f);
    m_border.setPointCount(100);

    m_vertexQuadArray.setPrimitiveType(sf::Quads);
    m_vertexLineArray.setPrimitiveType(sf::Lines);

    m_debugText = new sf::Text();
    m_debugText->setFont(*Content::font);
    m_debugText->setPosition(0.0f, 100.0f);
    m_debugText->setCharacterSize(16);

    for (int32 i = 0; i < 250; i++) {
        Cell* newCell = new Cell(1, Genome(), randomWorldPoint(), *this);
        newCell->setMass(100.0f);
        m_entities.push_back(newCell);
    }

    for (int32 i = 0; i < 500; i++)
       m_entities.push_back(new Food(randomWorldPoint(), *this));

    m_spatialHash.buildArray(m_vertexQuadArray, sf::Quads);
    m_spatialHash.buildArray(m_vertexLineArray, sf::Lines);

    updateEntityText();

    return true;
}
コード例 #5
0
ファイル: world.cpp プロジェクト: firestack/cell-simulation
void World::onDeath(Entity* entity)
{
    if (entity->getType() == type::Cell) {

        Cell* cell = (Cell*)entity;
        if (cell) {

            //std::stringstream sb;
            //sb << "entity died at generation: " << cell->getGeneration();

            //Console::write(sb.str());

            if (Cell::m_cellCount <= 50) {
                m_entities.push_back(new Cell(1, Genome(), randomWorldPoint(), *this));
            }
        }

    }
    else if (entity->getType() == type::Resource) {

        Resource* resource = (Resource*)entity;
        if (resource->getResourceType() == type::Food) {
            m_entities.push_back(new Food(randomWorldPoint(), *this));
        }
    }
}
コード例 #6
0
GeneticPopulation::GeneticPopulation(unsigned populationSize, unsigned numberOfWeights) {
	for (unsigned i = 0; i < populationSize; ++i) {
		Weights weights(numberOfWeights);
		for (Weight& weight : weights) {
			weight = randomReal(-1, 1);
		}
		population.push_back(Genome(weights));
	}
}
コード例 #7
0
Genome BlanketResolver::resolveBlanket(Genome* blanket) {
	unsigned int head = BlanketResolver::findHeadIndex(blanket);
	std::vector<bool> used(blanket->genomeLength(), false);
	return Genome(
		BlanketResolver::resolve(
			BlanketResolver::getBlanketGenomes(blanket),
			used,
			head
		),
		blanket->getIndex<Genome*>(head)->getSpeciesNode()
	);
}
コード例 #8
0
// Create Population Method
void GeneticAlgorithm::CreatePopulation() {
	// Clear existing population
	mGenomes.clear();
	// Loop through the population
	for (int iGenome = 0; iGenome < mPopulationSize; iGenome++) {
		// Add the genome
		mGenomes.push_back(Genome(mChromosomeLength));
	}
	// Reset all variables
	mGeneration        = 0;
	mFittestGenome     = 0;
	mBestFitnessScore  = 0;
	mTotalFitnessScore = 0;
}
コード例 #9
0
std::vector<Genome> GenAlg::new_generation(std::vector<Genome> &last_generation)
{
	generation_count++;
	
	reset();
	
	std::sort(last_generation.begin(), last_generation.end(), myComparer);
	
	calculateStatistics();
	
	std::vector<Genome> new_population;
	
	//get elites
	GetBest(my_params.num_elite, my_params.num_copies_of_elite, last_generation, new_population);
	
	//genetic algorithm loop
	while(new_population.size() < pop_size)
	{
		Genome mother = getRandomChromosome();
		Genome father = getRandomChromosome();
		
		std::vector<float> child1, child2;
		
		crossover(mother.weights, father.weights, child1, child2);
		
		mutate(child1);
		mutate(child2);
		
		new_population.push_back(Genome(child1, 0));
		new_population.push_back(Genome(child2, 0));
	}
	
	population = new_population;
	
	return population;
}
コード例 #10
0
GenAlg::GenAlg(parameters &my_params , int number_weights) : my_params(my_params)
{
	pop_size = my_params.num_sweepers;
	mutation_rate = my_params.mutation_rate;
	crossover_rate = my_params.crossover_rate;
	weights_per_chromo = number_weights;
	total_fitness = 0;
	best_fitness = 0;
	average_fitness = 0;
	worst_fitness = 99999999;
	generation_count = 0;
	
	//generate random weights for initial population
	for(int i = 0; i < pop_size; ++i)
	{
		population.push_back(Genome());
		
		for(int j = 0; j < weights_per_chromo; ++j)
		{
			population[i].weights.push_back(small_rand());
		}
	}
}
コード例 #11
0
ファイル: Genome.cpp プロジェクト: Abdul59/speedseq
// Application includes
#include "Genome.hh"

Genome Genome::genomes[NGS] = {Genome("NCBI36"),Genome("GRCh37")};

Genome::Genome(string name) : n_chr_(0)
{
  string org_name = name;
  for(int i = 0;i < name.length();i++) name[i] = tolower(name[i]);
  if (name == "hg18" || name == "ncbi36") {
    gname_       = "NCBI36";
    other_gname_ = "hg18";
    n_chr_      = 24;
    cnames_[0]  =  "1"; clens_[0]  = 247249719;
    cnames_[1]  =  "2"; clens_[1]  = 242951149;
    cnames_[2]  =  "3"; clens_[2]  = 199501827;
    cnames_[3]  =  "4"; clens_[3]  = 191273063;
    cnames_[4]  =  "5"; clens_[4]  = 180857866;
    cnames_[5]  =  "6"; clens_[5]  = 170899992;
    cnames_[6]  =  "7"; clens_[6]  = 158821424;
    cnames_[7]  =  "8"; clens_[7]  = 146274826;
    cnames_[8]  =  "9"; clens_[8]  = 140273252;
    cnames_[9]  = "10"; clens_[9]  = 135374737;
    cnames_[10] = "11"; clens_[10] = 134452384;
    cnames_[11] = "12"; clens_[11] = 132349534;
    cnames_[12] = "13"; clens_[12] = 114142980;
    cnames_[13] = "14"; clens_[13] = 106368585;
    cnames_[14] = "15"; clens_[14] = 100338915;
    cnames_[15] = "16"; clens_[15] =  88827254;
    cnames_[16] = "17"; clens_[16] =  78774742;
    cnames_[17] = "18"; clens_[17] =  76117153;
コード例 #12
0
Genome NonNoisyGeneticSource::addNoise(Genome * target) {
	return Genome(target);
}