int free_offsprings(void) { int i=1; for (i=1; i<=K; i++) free_chromosome(offsprings[i]); return 1; }
int test_regression_evaluate(void) { int i; int res; float f = 100.0; float **func_input; float solution_score = 5.0; struct tree *t; struct node **chromosome = malloc(sizeof(struct node *) * 5); struct data *d = csv_load_data(TEST_DATA, 1, ","); /* initialize func_input */ func_input = malloc(sizeof(float *) * 2); for (i = 0; i < 2; i++) { func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows); } /* build chromosome */ chromosome[0] = node_new_constant(FLOAT, &f); chromosome[1] = node_new_input((char *) "x"); chromosome[2] = node_new_func(MUL, 2); chromosome[3] = node_new_func(RAD, 1); chromosome[4] = node_new_func(SIN, 1); /* build tree */ t = malloc(sizeof(struct tree)); t->root = NULL; t->size = 5; t->depth = -1; t->score = NULL; t->chromosome = chromosome; /* evaluate tree */ res = regression_evaluate(t, func_input, d, (char *) "y"); mu_check(res == 0); mu_check(fltcmp(t->score, &solution_score) == 0); mu_check(t->hits == 361); /* clean up */ free_chromosome(chromosome, 5); tree_destroy(t); data_destroy(d); free_mem_arr(func_input, 2, free); return 0; }
int local_optimization(int n, int edge[][SIZE+1]) { int i, j, k; int improved = 1; int modify = 0; int cost = (offsprings[n]->cost = calc_cost(offsprings[n], edge)); int permutation[SIZE+1]; Chromosome *c; init_chromosome(&c); for (i=0; i<=SIZE; i++) c->ch[i] = offsprings[n]->ch[i]; int num = 0; init_permutation(permutation); while (improved) { improved = 0; for (i=1; i<=SIZE; i++) { int tmp = 0; c->ch[permutation[i]] = (1 - c->ch[permutation[i]]) % 2; tmp = calc_cost(c, edge); if (tmp > cost) { offsprings[n]->ch[permutation[i]] = c->ch[permutation[i]]; improved = 1; cost = tmp; offsprings[n]->cost = tmp; } else c->ch[permutation[i]] = (1 - c->ch[permutation[i]]) % 2; } } free_chromosome(c); return 1; }
int local_optimization(int n, int edge[][SIZE+1]) { int i, j, k; int loop = 0; int improved = 1; int modify = 0; int cost, num; int idx = -1; unsigned long seed = get_nano_seconds(); srand(seed); Chromosome *tmp; init_chromosome(&tmp, edge); for (i=0; i<=SIZE; i++) tmp->ch[i] = offsprings[n]->ch[i]; for (num=0; num<HUBS; num++) { int r = rand() % N + 1; int neighbors[SIZE+1]; int n_count = 0; for (i=1; i<= SIZE; i++) { if (edge[r][i] != 0) neighbors[++n_count] = i; } neighbors[0] = r; while (improved) { improved = 0; cost = calc_cost(offsprings[n], edge); for (i=0; i<= n_count; i++) { tmp->ch[neighbors[i]] = (1 - (tmp->ch[neighbors[i]])) % 2; calc_cost(tmp, edge); if (tmp->cost > cost) { cost = tmp->cost; idx = i; improved = 1; } tmp->ch[neighbors[i]] = (1 - (tmp->ch[neighbors[i]])) % 2; } if (improved) { offsprings[n]->ch[neighbors[idx]] = (1 - (offsprings[n]->ch[neighbors[idx]])) % 2; tmp->ch[neighbors[idx]] = (1 - (tmp->ch[neighbors[idx]])) % 2; } } } free_chromosome(tmp); return 1; }
/** * Main method of genetic algorithm. * @param config Struct with configuraiton parameters like population size, number of gens, etc... * @see struct Chromosome_configuration */ int genetic_main(Ptr_config config) { #ifdef TRACE printf("---> genetic_main.\n"); fflush(stdout); #endif const int TOTAL_CHROM = config->total_chrom; const int MAX_ITER = config->max_iter; const int MAX_REP_MEJOR = config->max_iter_best; const int CHROMOSOME_LENGTH = config->chrom_length; int rep_best, fin, ale1, ale2, eti1, eti2, iter, i, validos, np; double best; Ptr_Chromosome *List_Chromosome; // Array of chromosmes. Population. rep_best = 10; fin = 0; ale1 = 0; ale2 = 0; eti1 = 0; eti2 = 0; iter = 0; i = 0; validos = 0; /**< Percentage of valid chromosomes */ // Time measurement variables struct timeval *tv; struct timezone *tz; long tf, ti, si, sf; double tiempo, mut; tv = (struct timeval *)malloc(sizeof(struct timeval)); tz = NULL; gettimeofday(tv, tz); si = (tv->tv_sec); ti = (tv->tv_usec); ti = si * 1000000 + ti; srand (time (NULL)); /* * ########################################## * ###### GENERATE INITIAL POPULATION ####### * ########################################## */ List_Chromosome = (Ptr_Chromosome*)malloc(TOTAL_CHROM * sizeof(Ptr_Chromosome)); // Canditate loop to be paralelized with OpenMP for ( i = 0 ; i < TOTAL_CHROM ; i++ ) { List_Chromosome[i] = create_chromosome(i, CHROMOSOME_LENGTH); } rep_best = 0; best = -1.0; //se le da un valor que no tiene ninguno para que no coincida // ######### MAIN LOOP ########## while ( fin == 0 ) { /* * ########################################### * ###### EVALUATION AND CLASSIFICATION ###### * ########################################### */ // Canditate loop to be paralelized with OpenMP for ( i = 0 ; i < TOTAL_CHROM ; i++ ) { if ( ((List_Chromosome[i])->evaluation == BAD_CHROM) && (good_chromosome(List_Chromosome[i], CHROMOSOME_LENGTH) == 1) ) { List_Chromosome[i]->evaluation = evaluate_chromosome(List_Chromosome[i], CHROMOSOME_LENGTH); }; }; classify_chromosome(List_Chromosome, TOTAL_CHROM); best = List_Chromosome[0]->evaluation; /* * ##################################### * ###### CHROMOSOME REPRODUCTION ###### * ##################################### */ #ifdef TRACE printf("Reproduccion de los cromosomas\n"); fflush(stdout); #endif // Canditate loop to be paralelized with OpenMP for ( i = TOTAL_CHROM / 2 ; i < TOTAL_CHROM - 1 ; i = i + 2 ) { // Elimination of the worst half part of the population eti1 = List_Chromosome[i]->id; eti2 = List_Chromosome[i + 1]->id; ale1 = (int)(rand() % (TOTAL_CHROM / 2)); ale2 = (int)(rand() % (TOTAL_CHROM / 2)); free_chromosome(List_Chromosome[i]); free_chromosome(List_Chromosome[i + 1]); List_Chromosome[i] = NULL; List_Chromosome[i + 1] = NULL; crossover(List_Chromosome[ale1], List_Chromosome[ale2], &List_Chromosome[i], &List_Chromosome[i + 1], CHROMOSOME_LENGTH, ONE_POINT_CROSS); List_Chromosome[i]->id = eti1; List_Chromosome[i + 1]->id = eti2; } /* * #################################### * ####### MUTATION ######## * #################################### */ mut = (double)(rand() % (100)); if ( mut < 10 ) //el 10% de que haya mutacion { ale1 = (int)(rand() % (TOTAL_CHROM / 2)); mutate(List_Chromosome[ale1], CHROMOSOME_LENGTH, BIT_STRING_MUTATION); }; iter++; // CHECK END CONDITIONS if ((best == (List_Chromosome[0])->evaluation) && (best != BAD_CHROM)) rep_best++; else { rep_best = 0; best = (List_Chromosome[0])->evaluation; }; if ( (rep_best == MAX_REP_MEJOR) || (iter == MAX_ITER) ) fin = 1; /* * ##################################################################### * ###### EVALUACION Y CLASIFICACION DESPUES DE CRUCE Y MUTACION. ###### * ##################################################################### */ // Canditate loop to be paralelized with OpenMP for ( i = 0 ; i < TOTAL_CHROM ; i++ ) { if ( ((List_Chromosome[i])->evaluation == BAD_CHROM) && (good_chromosome(List_Chromosome[i], CHROMOSOME_LENGTH) == 1) ) { List_Chromosome[i]->evaluation = evaluate_chromosome(List_Chromosome[i], CHROMOSOME_LENGTH); }; }; classify_chromosome(List_Chromosome, TOTAL_CHROM); // PRINT 3 BEST CHROMOSOMES printf("%d ITERATION. 3 BEST CHROMOSOMES:\n", iter); for ( i = 0 ; i < 3 ; i++) { show_chromosome(List_Chromosome[i], CHROMOSOME_LENGTH); } };// ########### END OF MAIN LOOP ########### // TIME MEASUREMENT gettimeofday(tv, tz); sf = (tv->tv_sec); tf = (tv->tv_usec); tf = sf * 1000000 + tf; tiempo = (1.0 * tf - 1.0 * ti) / 1000000.0; printf("\nExecution time T=%lf s.\n", tiempo); fflush(stdout); // CALCULATE PERCENTAGE OF VALID CHROMOSOMES. for ( i = 0 ; i < TOTAL_CHROM ; i++ ) if ( List_Chromosome[i]->evaluation != BAD_CHROM ) validos++; printf("%d%% of valid chromosomes.\n", (validos * 100) / TOTAL_CHROM); // Free memory for ( i = 3 ; i < TOTAL_CHROM ; i++ ) free_chromosome(List_Chromosome[i]); List_Chromosome = realloc(List_Chromosome, 3 * sizeof(struct Chromosome)); }
int test_regression_traverse(void) { int i; int res; float f1 = 1.0; float f2 = 2.0; float f3; float *flt_ptr; float zero = 0.0; float **func_input; struct node *result_node; struct node **chromosome = malloc(sizeof(struct node *) * 3); struct stack *stack = stack_new(); struct data *d = csv_load_data(TEST_DATA, 1, ","); /* initialize func_input */ func_input = malloc(sizeof(float *) * 2); for (i = 0; i < 2; i++) { func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows); } /* ADD */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(ADD, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 + f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* SUB */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(SUB, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 - f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* MUL */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(MUL, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 * f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* DIV */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(DIV, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 / f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* DIV - FAIL TEST - DIVIDE BY ZERO */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &zero); chromosome[2] = node_new_func(DIV, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); mu_check(res == -1); free_chromosome(chromosome, 3); /* POW */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(POW, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) pow(f1, f2); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* LOG */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(LOG, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) log(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* EXP */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(EXP, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) exp(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* RAD */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(RAD, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) f1 * (float) (PI / 180.0); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* SIN */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(SIN, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) sin(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* COS */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(COS, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) cos(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* FAIL TEST - UNKNOWN FUNCTION */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(99, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); mu_check(res == -2); free_chromosome(chromosome, 2); /* clean up */ stack_destroy(stack, free); free(chromosome); data_destroy(d); free_mem_arr(func_input, 2, free); return 0; }