t_cplx f_julia(t_coeff col, double x, double y) { double r; t_cplx z; double theta; (void)col; r = sqrt (sqrt (x * x + y * y)); theta = atan2 (y, x) * .5; if (random_bit ()) theta += M_PI; z.r = r * cos(theta); z.i = r * sin(theta); return (z); }
vector<bool> Hybrid::do_crossover(Chromosome& c1, Chromosome& c2) { vector<bool> crossed(flm_conf.chromosome_length, false); // Do two point crossover in first part int smoothing_beginning = flm_conf.chromosome_length - flm_conf.smooth_len; pair<int, int> range = pick_range(smoothing_beginning); for (int i = range.first; i < range.second; i++) { swap(c1[i], c2[i]); crossed[i] = true; } // Do uniform crossover in the smoothing part for (int j = smoothing_beginning; j < flm_conf.chromosome_length; j++) { if (random_bit()) { swap(c1[j], c2[j]); crossed[j] = true; } } return crossed; }
Genotype::Genotype(const ID_type nID, const std::pair<Genotype*, Genotype*> parents) : ID(nID), decision_chromosome(), link_chromosome(), fitness(parents.first->fitness), value(0.0), generator(nID) { real_type mutation_rate = 0.2, crossover_rate = 0.5; int i, j, ii, jj; //breed new chromosomes from parents, use hardcoded mutation and crossover rates fitness->add(ID, this); //what does crossover rate mean? //generate 2 random numbers for decision_chromosome crossover std::uniform_int_distribution<> random_int(0, decision_chromosome.size() - 1); std::bernoulli_distribution crossover(crossover_rate); if( crossover(generator) ) { i = random_int(generator); //take beginning of chromosome from first parent ... for(ii=i; ii>=0; --ii) decision_chromosome[ii] = parents.first->decision_chromosome[ii]; //... and the rest from the second parent for(ii=decision_chromosome.size()-1; ii>i; --ii) decision_chromosome[ii] = parents.second->decision_chromosome[ii]; } //decide whether to mutate std::bernoulli_distribution mutate(mutation_rate); random_int = std::uniform_int_distribution<>(0, decision_chromosome.size() - 1); if( mutate(generator) ) ~decision_chromosome[random_int(generator)]; //flip a bit //generate 2 random numbers for link_chromosome crossover if( crossover(generator) ) { ii = random_int(generator); //take beginning of chromosome from first parent ... for(i=i; i>=0; --i) //over genetic nodes for(j=link_chromosome[0].size()-1; j>=0; --j) //over each connectivity vector link_chromosome[i][j] = parents.first->link_chromosome[i][j]; //... and the rest from the second parent for(i=decision_chromosome.size()-1; i>ii; --i) for(j=link_chromosome[0].size()-1; j>=0; --j) link_chromosome[i][j] = parents.second->link_chromosome[i][j]; } //decide whether to mutate //mutations must occur in pairs to preserve K=2 connectivity random_int = std::uniform_int_distribution<>(0, link_chromosome.size() - 1); i = random_int(generator); //which bitset random_int = std::uniform_int_distribution<>(0, link_chromosome[0].size() - 1); j = random_int(generator); //which false bit to flip std::bernoulli_distribution random_bit(0.5); bool first = random_bit(generator); //which true bit to flip (there are only 2) ii = link_chromosome[0].size() - 1; if( mutate(generator) ) { for(ii; ii>=0; --ii) { if( link_chromosome[i][ii] ) { //true bit found if(first) { link_chromosome[i][ii] = false; break; } else first = true; //flip next true bit } } while(link_chromosome[i][j]) { //make sure bit is false to start with j = random_int(generator); } //may reflip the same bit, but it doesn't matter; happens rarely link_chromosome[i][j] = true; } //if } //constructor