void CGeneticSystem::stepGeneration(){ //stepGeneration2(); //return; std::vector<CChromo> newPopulation(m_populationSize*2); for(int i = 0; i < m_populationSize*2; i++) { CChromo p(m_noCitys , m_distMatrix); newPopulation[i] = p; } int newPopulationSize = 0; SortPopulation(m_ChromoPopulation,false); computeFitness(); for(int i = 0; i < m_populationSize; i++) { for(int j = 0; j < m_noCitys; j++) { int test = m_ChromoPopulation[i].getGene(j); newPopulation[i].setGene(j,test ); } newPopulationSize++; } while(newPopulationSize < 2*m_populationSize) { int idx1 =0; //tournamentSelection(); int idx2 =0; //tournamentSelection(); while(idx1 == idx2) { idx2= tournamentSelection(); idx1= tournamentSelection(); } CChromo &pfather=m_ChromoPopulation[idx2]; CChromo &pMother=m_ChromoPopulation[idx1]; CChromo p_offspring1(m_noCitys,m_distMatrix) , p_offspring2(m_noCitys,m_distMatrix); pMother.crossover(&pfather, &p_offspring1, &p_offspring2); newPopulation[newPopulationSize] = p_offspring1; newPopulationSize++; if(newPopulationSize >= newPopulation.size()) break; newPopulation[newPopulationSize] = p_offspring2; newPopulationSize++; } mutatePopulation(newPopulation); SortPopulation(newPopulation , true); //ass for(int i = 0; i < m_populationSize-2; i++) //keep last best { m_ChromoPopulation[i] = newPopulation[i]; } SortPopulation(m_ChromoPopulation , true); updateBestSoFarPath(); }
int main(int argc, char** argv) { //parparser parser(argc, argv); //long inititalPosition = parser.get("x").asLong(); srand(time(0)); //----------------------------------------------------------------------------- MPICHECK(MPI_Init(&argc, &argv)); //----------------------------------------------------------------------------- int commSize = 0; int rank = 0; MPICHECK(MPI_Comm_size(MPI_COMM_WORLD, &commSize)); MPICHECK(MPI_Comm_rank(MPI_COMM_WORLD, &rank)); const int nextProc = rank == commSize - 1 ? 0 : rank + 1; const int prevProc = rank == 0 ? commSize - 1 : rank - 1; vector<Individ> population(islandSize); for (int i = 0; i < islandSize; ++i) { population[i].init(problemSize); } for (int i = 1; i <= iterationsThreshold; ++i) { select(population); crossoverPopulation(population); mutatePopulation(population); if (i % migrationFreq == 0) { migrate(population, nextProc, prevProc); } cout << getBestFitness(population, rank, commSize) << "\n"; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- MPICHECK(MPI_Finalize()); return 0; }
void CGeneticSystem::stepGeneration2() { std::vector<CChromo> newPopulation(m_populationSize*2); for(int i = 0; i < m_populationSize*2; i++) { CChromo p(m_noCitys , m_distMatrix); newPopulation[i] = p; } int newPopulationSize = 0; SortPopulation(m_ChromoPopulation , false); computeFitness(); for(int i = 0; i < m_populationSize; i++) { for(int j = 0; j < m_noCitys; j++) { int test = m_ChromoPopulation[i].getGene(j); newPopulation[i].setGene(j,test ); } newPopulationSize++; } while (newPopulationSize < 2*m_populationSize) { int idx1 = tournamentSelection(); int idx2 = tournamentSelection(); CChromo offspring = m_ChromoPopulation[idx1].CrossOver2(&m_ChromoPopulation[idx2]); newPopulation[newPopulationSize] = offspring; newPopulationSize++; } mutatePopulation(newPopulation); SortPopulation(newPopulation , true); for(int i = 0; i < m_populationSize; i++) { m_ChromoPopulation[i] = newPopulation[i]; } SortPopulation(m_ChromoPopulation,true); updateBestSoFarPath(); }
int main(int argc, char **argv){ tspsMap_t map; tspsConfig_t config; tspsPopulation_t population; unsigned long int numGenerations = 0; int mpiNumProcs = 0; double start, end; //starting MPI directives MPI_Init(NULL,NULL); MPI_Comm_size(MPI_COMM_WORLD,&mpiNumProcs); MPI_Comm_rank(MPI_COMM_WORLD,&mpiId); logg("* Starting tspgen...\n"); // parse the command line args if(readConfig(&config, argc, argv) != TSPS_RC_SUCCESS){ return TSPS_RC_FAILURE; } // parse the map logg("* Parsing map...\n"); if(parseMap(&map) != TSPS_RC_SUCCESS){ logg("Error! Unable to read map 'maps/brazil58.tsp'!\n"); return TSPS_RC_FAILURE; } // initialize random seed: srand ( time(NULL)*mpiId ); logg("* Generating population...\n"); if(generatePopulation(&population, &config) != TSPS_RC_SUCCESS){ logg("Error! Failed to create a new random population!"); return TSPS_RC_FAILURE; } // start a timer (mpi_barrier + mpi_wtime) MPI_Barrier(MPI_COMM_WORLD); start = MPI_Wtime(); logg("* Initializing reproduction loop...\n"); while(1){ numGenerations++; calculateFitnessPopulation(&population, &map); if(numGenerations % 500 == 0){ logg("- Generation %d...\n", numGenerations); printIndividual(&population.individuals[0], "Current Top Individual"); } sortPopulation(&population); if(config.numGenerations > 0 && numGenerations == config.numGenerations){ logg("* Max number of generations [%d] reached!\n", config.numGenerations); break; } crossoverPopulation(&population, &config); mutatePopulation(&population, &config); // migrate population at every n generation if(numGenerations % config.migrationRate == 0){ migrateIndividuals(&population, mpiId, mpiNumProcs, &config); } } // join all the populations joinPopulations(&population, mpiId, mpiNumProcs); sortPopulation(&population); // get the best inidividual and print it printIndividual(&population.individuals[0], "Top Individual"); printIndividual(&population.individuals[config.populationSize-1], "Worst (of the top ones) Individual"); // stop the timer MPI_Barrier(MPI_COMM_WORLD); /* IMPORTANT */ end = MPI_Wtime(); if(mpiId == 0) { /* use time on master node */ printf("* Runtime = %f\n", end-start); } logg("* tspgen finished!\n"); free(population.individuals); free(map.nodes); MPI_Finalize(); return TSPS_RC_SUCCESS; }
int optimization(struct GA *ga, struct CGoL *cgol){ printf("\nIn optimization\n"); printf("\ncurrentGeneration: %d\n", (ga -> currentGeneration) + 1); printf("\nmaxGeneration: %d\n", ga -> maxGeneration); int again = 0; //Mutate initial population if((ga -> currentGeneration) == 0){ printf("\nBest Solution at start\n"); printSolution(&(ga -> bestSolution), (ga -> solutionHeight), (ga -> solutionWidth), 0); printf("\nWith a fitness of: (%d)\n",(ga -> bestSolution.fitness)); sleep(1); mutatePopulation((ga -> population), (ga -> maxSolution), (ga -> solutionWidth), (ga -> solutionHeight), INITIAL_MUTATION_RATE); } int p; //Run each solution through CGoL to determine their fitness //Don't show the board... int seedTooLarge; int i; for(i = 0; i < (ga -> maxSolution); i++){ seedTooLarge = runCGoL(&(ga -> population[i]), (ga -> solutionWidth), (ga -> solutionHeight), (ga -> fitnessSpecifier), cgol, 0); if(seedTooLarge == 0) return seedTooLarge; //Temp fix } //Sort population by fitness sortPopulationByFitness((ga -> population), (ga -> maxSolution)); //Allocate memory for a new blank population for the next generation struct Solution *newPopulation = ga_initializePopulation((ga -> maxSolution), (ga -> solutionWidth), (ga -> solutionHeight)); //For each new solution perform the following by working with the last population //Selection and Crossover for(i = 0; i < (ga -> maxSolution); i++){ int parentA, parentB; //Selection to find each parent parentA = selection_Roulette((ga -> population), (ga -> maxSolution)); parentB = selection_Roulette((ga -> population), (ga -> maxSolution)); //perform crossover at different parts for each line (newPopulation)[i] = *crossover(&(ga -> population[parentA]), &(ga -> population[parentB]), (ga -> solutionWidth), (ga -> solutionHeight)); } //For each solution in the new population //Mutation mutatePopulation(newPopulation, (ga -> maxSolution), (ga -> solutionWidth), (ga -> solutionHeight), (ga -> mutationRate)); //Determine to continue or set the bestSeed and finish if((ga -> currentGeneration) < ((ga -> maxGeneration)-1)){ again = 1; }else{ //Testing past bestSolution vs current leading solution if(ga -> bestSolution.fitness < ga -> population[(ga -> maxSolution) - 1].fitness){ printf("\nga -> bestSolution.fitness < ga -> population[0].fitness\n"); //free the old bestSolution ga_freeSolution(&(ga -> bestSolution), (ga -> solutionHeight)); //Copy values and return a new solution (ga -> bestSolution) = *copySolution(&((ga -> population)[(ga -> maxSolution) - 1]), (ga -> solutionWidth), (ga -> solutionHeight)); } printf("\nBest Solution at finish\n"); printSolution(&(ga -> bestSolution), (ga -> solutionHeight), (ga -> solutionWidth), 0); printf("\nWith a fitness of: (%d)\n",(ga -> bestSolution.fitness)); sleep(1); } //Free memory from the old population ga_freePopulation((ga -> population), (ga -> maxSolution), (ga -> solutionHeight)); //Set ga.population to point to the new population (ga -> population) = newPopulation; //Advance currentGeneration (ga -> currentGeneration)++; return again; }