int main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 16; unsigned i; // a chromosome randomizer eoInitPermutation <Chrom> random(CHROM_SIZE); // the population: eoPop<Chrom> pop; // Evaluation eoEvalFuncPtr<Chrom> eval( real_value ); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); std::cout << " Initial chromosome n°" << i << " : " << chrom << "..." << std::endl; random(chrom); eval(chrom); std::cout << " ... becomes : " << chrom << " after initialization" << std::endl; check_permutation(chrom); pop.push_back(chrom); } return 0; }
/* * Assumes the string is in a comma delimted format and has been "validated". */ void str_to_permutation(char* str, uint32* permutation, bool print) { char* p =str; char c = *p++; int i=1; uint32 v =0; while (c != '\0') { if (print) print_perm(stdout, permutation); if (c == ',') { check_permutation(permutation, i, v); permutation[i] = v; i++; v = 0; } else { v *= 10; v += c - '0'; } c = *p++; } check_permutation(permutation, i, v); permutation[i] = v; if (print) print_perm(stdout, permutation); uint32* inv_perm = alloc_permutation(permutation[0], false); for (i=1; i <= permutation[0]; i++) { if (permutation[i] == 0) { error("Invalid permutation: one of the values has not been assigned."); } if (inv_perm[permutation[i]] != 0) { error("Invalid permutation: one of the values is repeating."); } inv_perm[permutation[i]] = i; } free(inv_perm); }
int main() { const unsigned POP_SIZE = 3, CHROM_SIZE = 8; unsigned i; // a chromosome randomizer eoInitPermutation <Chrom> random(CHROM_SIZE); // the population: eoPop<Chrom> pop; // Evaluation //eoEvalFuncPtr<Chrom> eval( real_value ); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); //eval(chrom); pop.push_back(chrom); } // a shift mutation eoOrderXover<Chrom> cross; for (i = 0; i < POP_SIZE; ++i) std::cout << " Initial chromosome n�" << i << " : " << pop[i] << "..." << std::endl; cross(pop[0],pop[1]); cross(pop[1],pop[2]); for (i = 0; i < POP_SIZE; ++i) { std::cout << " Initial chromosome n�" << i << " becomes : " << pop[i] << " after orderXover" << std::endl; check_permutation(pop[i]); } return 0; }