int solve(Graph &G, vector<int> &path){ vector<Genome> population; REP(i, G.size) path.push_back(i); for(int i = 0; i < POPULATION_SIZE; i++) { random_shuffle(path.begin(), path.end()); Genome new_genome(path, G); population.push_back(new_genome); } sort(population.begin(), population.end(), genome_compare); for(int i = 0; i < EVOLUTION_TIMES; i++) { for(int l = 0; l < POPULATION_SIZE / 2; l++) { int r1 = rand() % POPULATION_SIZE; int r2 = rand() % POPULATION_SIZE; while(r1 == r2) r2 = rand() % POPULATION_SIZE; population[r1].breed(population[r2], population); } sort(population.begin(), population.end(), genome_compare); population.erase(population.begin() + POPULATION_SIZE, population.end()); printf("evolution %i: top: %d last: %d\n", i, population[0].score, population[POPULATION_SIZE-1].score); //printResult(0, top_genome.genes); //printResult(0, population[POPULATION_SIZE-1].genes); } path.clear(); REP(i, G.size) path.push_back(population[0].genes[i]); return population[0].score; }
Genome mutate() { vector<int> result(genes); if(rand() % 10 < 6) { int random = rand() % result.size(); swap(result[random], result[(random+1)%result.size()]); } else { int first = rand() % genes.size(); int second = first + rand() % (genes.size()-first); reverse(result.begin()+first, result.begin()+second+1); } Genome new_genome(result, G); return new_genome; }
void pool::import_fromfile(std::string filename){ std::ifstream input; input.open(filename); if (!input.is_open()){ std::cerr << "cannot open file '" << filename << "' !"; return ; } this->species.clear(); try { // current state unsigned int innovation_num; input >> innovation_num; this->innovation.set_innovation_number(innovation_num); input >> this->generation_number; input >> this->max_fitness; // network information input >> this->network_info.input_size >> this->network_info.output_size >> this->network_info.bias_size; this->network_info.functional_nodes = this->network_info.input_size + this->network_info.output_size + this->network_info.bias_size; std::string rec; input >> rec; if (rec == "rec") this->network_info.recurrent = true; if (rec == "nonrec") this->network_info.recurrent = false; // population information this->speciating_parameters.read(input); // mutation parameters this->mutation_rates.read(input); // species information unsigned int species_number; input >> species_number; this->species.clear(); for (unsigned int c = 0; c < species_number; c++){ specie new_specie; #ifdef GIVING_NAMES_FOR_SPECIES input >> new_specie.name; #endif input >> new_specie.top_fitness; input >> new_specie.average_fitness; input >> new_specie.staleness; unsigned int specie_population; input >> specie_population; for (unsigned int i=0; i<specie_population; i++){ genome new_genome(this->network_info, this->mutation_rates); input >> new_genome.fitness; input >> new_genome.adjusted_fitness; input >> new_genome.global_rank; new_genome.mutation_rates.read(input); unsigned int gene_number; input >> new_genome.max_neuron >> gene_number; for (unsigned int j=0; j<gene_number; j++){ gene new_gene; input >> new_gene.innovation_num; input >> new_gene.from_node; input >> new_gene.to_node; input >> new_gene.weight; input >> new_gene.enabled; new_genome.genes[new_gene.innovation_num] = new_gene; } new_specie.genomes.push_back(new_genome); } this->species.push_back(new_specie); } } catch (std::string error_message){ std::cerr << error_message; } input.close(); }