std::pair<G, G> crossover(Random& rng, const G& mom, const G& dad) { assert(mom.size() == dad.size()); const size_t gene_size = mom.size(); std::numeric_limits<typename G::value_type> value_limits; G child_a(gene_size, value_limits.max()); G child_b(gene_size, value_limits.max()); const size_t rand_1 = rng.natural(gene_size); size_t rand_2 = rng.natural(gene_size); while (rand_2 == rand_1) { rand_2 = rng.natural(gene_size); } const size_t cut_a = std::min(rand_1, rand_2); const size_t cut_b = std::max(rand_1, rand_2); // Copy a chunk from parent1->child_a && parent2->child_b for (size_t i = cut_a; i < cut_b; ++i) { child_a[i] = mom[i]; } child_a[cut_b] = mom[cut_b]; for (size_t i = cut_a; i < cut_b; ++i) { child_b[i] = dad[i]; } child_b[cut_b] = dad[cut_b]; fill_in_order(dad, child_a, cut_a, cut_b); fill_in_order(mom, child_b, cut_a, cut_b); return make_pair(*(G*)&child_a, *(G*)&child_b); }
int main(int argc, char* argv[]){ flags(argc,argv); int qid_send = create_queue(); int qid_receive = create_queue(); int pid = fork(); Msg msg_check; key_t key = KEY; int shmid = shm_create(key); strcpy(msg_check.text, CODE); shm_write_process(msg_check); if(pid > 0){ // Parent parent_a(qid_send, qid_receive); }else if(pid == 0){ // Child child_a(qid_send, qid_receive); } else if(pid < 0){ // Fail printf("Failed to create process!\n", pid); exit(-1); } } // fim-main
std::pair<G, G> crossover(Random& rng, const G& mom, const G& dad) { assert(mom.size() == dad.size()); const size_t size = mom.size(); G child_a(size); G child_b(size); for (size_t i = 0; i < size; ++i) { if (rng.flip()) { child_a[i] = mom[i]; child_b[i] = dad[i]; } else { child_a[i] = dad[i]; child_b[i] = mom[i]; } } assert(child_a.size() == mom.size()); assert(child_a.size() == child_b.size()); return make_pair(child_a, child_b); }