/* Initialisation d'un L-System a partir d'une chaine de caracteres ou sont stockes les parametres */ Lsystem initLsystem(String strLsystem) { Lsystem lsystem; lsystem.name = NULL; lsystem.variables = NULL; lsystem.constants = NULL; lsystem.start = NULL; lsystem.rules = NULL; lsystem.generations = NULL; lsystem.position = NULL; char delims[] = "\n"; lsystem.name = strtok( strLsystem, delims ); lsystem.angle = atoi(strtok( NULL, delims )); lsystem.variables = strtok( NULL, delims ); lsystem.constants = strtok( NULL, delims ); lsystem.start = strtok( NULL, delims ); String result = NULL; result = strtok( NULL, delims ); while( result != NULL ) { lsystem.rules=addRule(lsystem.rules,result); result = strtok( NULL, delims ); } lsystem.generations = (Generations*)malloc(sizeof(Generations)); lsystem.generations = newGeneration(lsystem.generations,lsystem); lsystem.position = (Position*)malloc(sizeof(Position)); lsystem.position = newPosition(lsystem.position); return lsystem; }
void mainView::setConnections() { connect(bQuit, SIGNAL(clicked()), this, SLOT(quit())); connect(bStart, SIGNAL(clicked()), this, SLOT(startGame())); // connect(bStart, SIGNAL(clicked()), this, SLOT (StartMyThread())); connect(bRnd, SIGNAL(clicked ()), this, SLOT (setValue ())); // connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int))); connect(timer, SIGNAL(timeout()), lifeWgt, SLOT(newGeneration())); connect(timer, SIGNAL(timeout()), this, SLOT(updateLabels())); //QObject::connect (&m_thread, SIGNAL(currentValue(int)), lcd, SLOT(display(int)) ); //QObject::connect (&m_thread, SIGNAL(finished()), qApp, SLOT(quit())); }
GameWidget::GameWidget(QWidget *parent) : QWidget(parent), timer(new QTimer(this)), generations(-1), universeSize(50) { timer->setInterval(300); m_masterColor = "#000"; universe = new bool[(universeSize + 2) * (universeSize + 2)]; next = new bool[(universeSize + 2) * (universeSize + 2)]; connect(timer, SIGNAL(timeout()), this, SLOT(newGeneration())); memset(universe, false, sizeof(bool)*(universeSize + 2) * (universeSize + 2)); memset(next, false, sizeof(bool)*(universeSize + 2) * (universeSize + 2)); }
int main(int argc, const char *argv[]) { // 'true' satisfaccion restricciones duras bool type = true; // 'true' selecciona mejores bool cloneSelType = true; // 'true' remplaza los mas malos bool replaceType = true; // 'true' Se clonan mediante formula bool cloneType = true; timespec ts, te; clock_gettime(CLOCK_REALTIME, &ts); population[POP].fitness = 10000; if(!checkFile(argc,argv)) return 0; changeParam(argv); // cout << "ClonationFactor: "<<clonationFactor << endl; // cout << "ClonationRate: " << clonationRate << endl; // cout << "ReplaceRate: " << replaceRate << endl; inputReading(argv[1]); generation = 0; initPopulation(type); evaluation(population, type); while(generation<GENS){ cleanPops(); selection(clonationRate, population); clonation(tmpPop, cloneType); hypermutation(); evaluation(clonePop, type); cloneSelection(clonePop, cloneSelType); cloneInsertion(); newGeneration(replaceRate, replaceType); evaluation(population, type); generation++; } clock_gettime(CLOCK_REALTIME, &te); // printFile(argv[1],population[0].gene,population[0].fitness,(te.tv_sec-ts.tv_sec), (te.tv_nsec-ts.tv_nsec)); // printFile(population[0].fitness); cout << replaceRate << " " << population[0].fitness << " " << (te.tv_sec-ts.tv_sec)<<"."<<(te.tv_nsec-ts.tv_nsec) << endl; return 0; }
void GeneticPopulation::produceNextGeneration() { cout << "In produce next generation loop...\n"; //This clears the link history so future links with the same toNode and fromNode will have different IDs Globals::getSingleton()->clearNodeHistory(); Globals::getSingleton()->clearLinkHistory(); int numParents = int(generations[generations.size() - 1/*onGeneration*/]->getIndividualCount()); //check that no org has fitness <= zero for (int a = 0; a < numParents; a++) { PRINT(generations[generations.size() - 1/*onGeneration*/]->getIndividual(a)->getFitness()); if (generations[generations.size() - 1/*onGeneration*/]->getIndividual(a)->getFitness() < 1e-6) { throw CREATE_LOCATEDEXCEPTION_INFO("ERROR: Fitness must be a positive number!\n"); } } double totalFitness = 0; //get sum of adjusted fitness for all species together for (int a = 0; a < (int) species.size(); a++) { totalFitness += species[a]->getAdjustedFitness(); } int totalOffspring = 0; //give each species a fitness proportional to its adjusted fitness for (int a = 0; a < (int) species.size(); a++) { double adjustedFitness = species[a]->getAdjustedFitness(); int offspring = int(adjustedFitness / totalFitness * numParents); totalOffspring += offspring; species[a]->setOffspringCount(offspring); } //cout << "Pausing\n"; //int result = system("PAUSE"); //(void) result; //Some offspring were truncated. Give these N offspring to the species that had the best N individuals while (totalOffspring < numParents) { for (int a = 0; totalOffspring < numParents && a < generations[generations.size() - 1/*onGeneration*/]->getIndividualCount(); a++) { shared_ptr<GeneticIndividual> ind = generations[generations.size() - 1/*onGeneration*/]->getIndividual(a); shared_ptr<GeneticSpecies> gs = getSpecies(ind->getSpeciesID()); gs->setOffspringCount(gs->getOffspringCount() + 1); totalOffspring++; /* //Try to give 2 offspring to the very best individual if it only has one offspring. //This fixes the problem where the best indiviudal sits in his own species //and duplicates every generation. if(a==0&&gs->getOffspringCount()==1&&totalOffspring<numParents) { gs->setOffspringCount(gs->getOffspringCount()+1); totalOffspring++; }*/ } } //report stats on species for (int a = 0; a < (int) species.size(); a++) { cout << "Species ID: " << species[a]->getID() << " Age: " << species[a]->getAge() << " last improv. age: " << species[a]->getAgeOfLastImprovement() << " Fitness: " << species[a]->getFitness() << "*" << species[a]->getMultiplier() << "=" << species[a]->getAdjustedFitness() << " Size: " << int(species[a]->getIndividualCount()) << " Offspring: " << int(species[a]->getOffspringCount()) << endl; } //jmc: This is the new generation container vector<shared_ptr<GeneticIndividual> > babies; double totalIndividualFitness = 0; //jmc: clear the flag of whether they have reproduced (I think that is what that flag does.) for (int a = 0; a < (int) species.size(); a++) { species[a]->setReproduced(false); } int smallestSpeciesSizeWithElitism = int(Globals::getSingleton()->getParameterValue("SmallestSpeciesSizeWithElitism")); static double mutateSpeciesChampionProbability = Globals::getSingleton()->getParameterValue("MutateSpeciesChampionProbability"); double ForceCopyGenerationChampionParamVal = Globals::getSingleton()->getParameterValue("ForceCopyGenerationChampion"); bool forceCopyGenerationChampion = (ForceCopyGenerationChampionParamVal > Globals::getSingleton()->getRandom().getRandomDouble()); //getRandom between but not including 0 and 1 (Avida RNG and NEAT) for (int a = 0; a < generations[generations.size() - 1/*onGeneration*/]->getIndividualCount(); a++) //Go through and add the species champions (including ties for champ, which jmc added) { shared_ptr<GeneticIndividual> ind = generations[generations.size() - 1/*onGeneration*/]->getIndividual(a); shared_ptr<GeneticSpecies> species = getSpecies(ind->getSpeciesID()); // if (!species->isReproduced()) //original code assumed it would only store the champ of each species, so if this flag works. but it doesn't work to preserve ties for champ. if (!species->isReproduced() //jmc: if the species has not been checked yet to determine the fitness of its champ || ind->getFitness() == species->getBestIndividual()->getFitness()) //or if the fitness of this org equals the species champ { species->setReproduced(true); //This is the first and best organism of this species to be added, so it's the species champion //of this generation if (ind->getFitness() > species->getBestIndividual()->getFitness()) { //We have a new all-time species champion! species->setBestIndividual(ind); cout << "Species " << species->getID() << " has a new champ with fitness " << species->getBestIndividual()->getFitness() << endl; } if ((a == 0 && forceCopyGenerationChampion) || (species->getOffspringCount() >= smallestSpeciesSizeWithElitism)) { //Copy species champion. bool mutateChampion; if (Globals::getSingleton()->getRandom().getRandomDouble() < mutateSpeciesChampionProbability) mutateChampion = true; else { mutateChampion = false; } babies.push_back(shared_ptr<GeneticIndividual>(new GeneticIndividual(ind, mutateChampion, babies.size()))); species->decrementOffspringCount(); } if (a == 0) { species->updateAgeOfLastImprovement(); } } totalIndividualFitness += ind->getFitness(); } double averageFitness = totalIndividualFitness / generations[generations.size() - 1/*onGeneration*/]->getIndividualCount(); cout << "Generation " << int(onGeneration) << ": " << "overall_average = " << averageFitness << endl; #ifdef PRINT_GENETIC_PERTUBATION_INFO //print the gen number to this file ofstream mutationEffects_file; mutationEffects_file.open("mutationEffects.txt", ios::app); mutationEffects_file << "\nGeneration " << int(onGeneration + 1) << ": " << endl; //plus 1 cause we print this line to the file before this gens data mutationEffects_file.close(); ofstream mutationAndCrossoverEffects_file; mutationAndCrossoverEffects_file.open("mutationAndCrossoverEffects.txt", ios::app); mutationAndCrossoverEffects_file << "\nGeneration " << int(onGeneration + 1) << ": " << endl; //plus 1 cause we print this line to the file before this gens data mutationAndCrossoverEffects_file.close(); ofstream crossoverEffects_file; crossoverEffects_file.open("crossoverEffects.txt", ios::app); crossoverEffects_file << "\nGeneration " << int(onGeneration + 1) << ": " << endl; //plus 1 cause we print this line to the file before this gens data crossoverEffects_file.close(); #endif cout << "Champion fitness: " << generations[generations.size() - 1/*onGeneration*/]->getIndividual(0)->getFitness() << endl; if (generations[generations.size() - 1/*onGeneration*/]->getIndividual(0)->getUserData()) { cout << "Champion data: " << generations[generations.size() - 1/*onGeneration*/]->getIndividual(0)->getUserData()->summaryToString() << endl; } cout << "# of Species: " << int(species.size()) << endl; cout << "compat threshold: " << Globals::getSingleton()->getParameterValue("CompatibilityThreshold") << endl; for (int a = 0; a < (int) species.size(); a++) { //cout << "jmc: Making babies for species" << a << endl; species[a]->makeBabies(babies); } //cout << "jmc: done making babies\n"; if ((int) babies.size() != generations[generations.size() - 1/*onGeneration*/]->getIndividualCount()) { cout << "Population size changed! (or there may have been a bad (or negative, or zero) fitness value for an organism?)\n"; throw CREATE_LOCATEDEXCEPTION_INFO("Population size changed!"); } cout << "Making new generation\n"; shared_ptr<GeneticGeneration> newGeneration(generations[generations.size() - 1/*onGeneration*/]->produceNextGeneration(babies, onGeneration + 1)); //cout << "Done Making new generation!\n"; /*for(int a=0;a<4;a++) { for(int b=0;b<4;b++) { cout << babies[a]->getCompatibility(babies[b]) << '\t'; } cout << endl; }*/ generations.push_back(newGeneration); onGeneration++; }
Address Universe::allocateOops(int nOops) { return newGeneration()->allocateOops(nOops); }
BPRef Universe::allocateBytes(int nBytes) { return newGeneration()->allocateBytes(nBytes); }