#include "genetic_algorithm.hpp" #include #include #include /* Prints the population strings in a line */ void print_population(unsigned char population_in[]) { int iterator = 0; int member_count = 0; char cur_member; while (member_count < N){ cur_member = population_in[member_count]; while (iterator < L){ if (cur_member & 0x80){ printf("1"); } else{ printf("0"); } cur_member = cur_member << 1; iterator++; } member_count++; iterator = 0; printf(" "); } printf("\n"); } /* Fitness is determined by the number of 1's in the bitstring. */ int get_fitness(unsigned char string_in){ int count = 0; unsigned char temp = string_in; while (temp){ if (temp & 0x01){ count++; } temp = temp >> 1; } return count; } /* Randomly initialize the first population */ void init_population(unsigned char* population){ int i; for ( i=0; i