static entity *struggle_adaptation(population *pop, entity *child) { entity *adult; /* Adapted solution. */ int allele; /* Randomly selected allele. */ /* * We must generate a new solution by copying the original solution. * This function copys all genomic, and if appropriate, phenomic data. * It is never safe to adapt the solution in place. */ adult = ga_entity_clone(pop, child); /* Make point mutation. */ allele = random_int(strlen(target_text)); ((char *)adult->chromosome[0])[allele]++; struggle_score(pop, adult); if (adult->fitness > child->fitness) return adult; /* Searching in that previous direction didn't help. */ ((char *)adult->chromosome[0])[allele]-=2; struggle_score(pop, adult); if (adult->fitness > child->fitness) return adult; /* We must already be at a maxima. */ ((char *)adult->chromosome[0])[allele]++; adult->fitness = child->fitness; return adult; }
entity *test_adaptation(population *pop, entity *child){ entity *adult; /* Adapted solution. */ double * img; Image * fc; int i; /* * We must generate a new solution by copying the original solution. * This function copys all genomic, and if appropriate, phenomic data. * It is never safe to adapt the solution in place. */ adult = ga_entity_clone(pop, child); img = (double *)adult->chromosome[0]; for(i = 0;i<TSIZE(amp);i++){ real_in->r[i] = img[i]; real_in->c[i] = img[i+TSIZE(amp)]; real_in->image[i] = norm(real_in,i); } freeimg(real_out); if(get_algorithm(opts,&my_log) == HIO){ real_out = basic_hio_iteration(amp, real_in, support,opts,&my_log); }else if(get_algorithm(opts,&my_log) == RAAR){ real_out = basic_raar_iteration(amp,exp_sigma, real_in, support,opts,&my_log); }else if(get_algorithm(opts,&my_log) == HPR){ real_out = basic_hpr_iteration(amp, real_in, support,opts,&my_log); } adult->fitness = 0; fc = image_fft(real_out); for(i = 0;i<TSIZE(amp);i++){ img[i] = real_out->r[i]; img[i+TSIZE(amp)] = real_out->c[i]; adult->fitness -= fabs(fc->image[i] - amp->image[i]); } freeimg(fc); return adult; }
GAULFUNC int ga_tabu( population *pop, entity *initial, const int max_iterations ) { int iteration=0; /* Current iteration number. */ int i, j; /* Index into putative solution array. */ entity *best; /* Current best solution. */ entity **putative; /* Current working solutions. */ entity *tmp; /* Used to swap working solutions. */ entity **tabu_list; /* Tabu list. */ int tabu_list_pos=0; /* Index into the tabu list. */ /* Checks. */ if (!pop) die("NULL pointer to population structure passed."); if (!pop->evaluate) die("Population's evaluation callback is undefined."); if (!pop->mutate) die("Population's mutation callback is undefined."); if (!pop->rank) die("Population's ranking callback is undefined."); if (!pop->tabu_params) die("ga_population_set_tabu_params(), or similar, must be used prior to ga_tabu()."); if (!pop->tabu_params->tabu_accept) die("Population's tabu acceptance callback is undefined."); /* Prepare working entities. */ best = ga_get_free_entity(pop); /* The best solution so far. */ if ( !(putative = s_malloc(sizeof(entity *)*pop->tabu_params->search_count)) ) die("Unable to allocate memory"); for (i=0; i<pop->tabu_params->search_count; i++) { putative[i] = ga_get_free_entity(pop); /* The 'working' solutions. */ } /* Allocate and clear an array for the tabu list. */ if ( !(tabu_list = s_malloc(sizeof(vpointer)*pop->tabu_params->list_length)) ) die("Unable to allocate memory"); for (i=0; i<pop->tabu_params->list_length; i++) { tabu_list[i] = NULL; } /* Do we need to generate a random starting solution? */ if (!initial) { plog(LOG_VERBOSE, "Will perform tabu-search with random starting solution."); initial = ga_get_free_entity(pop); ga_entity_seed(pop, best); } else { plog(LOG_VERBOSE, "Will perform tabu-search with specified starting solution."); ga_entity_copy(pop, best, initial); } /* * Ensure that initial solution is scored. */ if (best->fitness==GA_MIN_FITNESS) pop->evaluate(pop, best); plog( LOG_VERBOSE, "Prior to the first iteration, the current solution has fitness score of %f", best->fitness ); /* * Do all the iterations: * * Stop when (a) max_iterations reached, or * (b) "pop->iteration_hook" returns FALSE. */ while ( (pop->iteration_hook?pop->iteration_hook(iteration, best):TRUE) && iteration<max_iterations ) { iteration++; /* * Generate and score new solutions. */ for (i=0; i<pop->tabu_params->search_count; i++) { pop->mutate(pop, best, putative[i]); pop->evaluate(pop, putative[i]); } /* * Sort new solutions (putative[0] will have highest rank). * We assume that there are only a small(ish) number of * solutions and, therefore, a simple bubble sort is adequate. */ for (i=1; i<pop->tabu_params->search_count; i++) { for (j=pop->tabu_params->search_count-1; j>=i; j--) { if ( pop->rank(pop, putative[j], pop, putative[j-1]) > 0 ) { /* Perform a swap. */ tmp = putative[j]; putative[j] = putative[j-1]; putative[j-1] = tmp; } } } /* * Save best solution if it is an improvement, otherwise * select the best non-tabu solution (if any). * If appropriate, update the tabu list. */ if ( pop->rank(pop, putative[0], pop, best) > 0 ) { tmp = best; best = putative[0]; putative[0] = tmp; if (tabu_list[tabu_list_pos] == NULL) { tabu_list[tabu_list_pos] = ga_entity_clone(pop, best); } else { ga_entity_blank(pop, tabu_list[tabu_list_pos]); ga_entity_copy(pop, tabu_list[tabu_list_pos], best); } tabu_list_pos++; if (tabu_list_pos >= pop->tabu_params->list_length) tabu_list_pos=0; } else { if ( -1 < (j = gaul_check_tabu_list(pop, putative, tabu_list)) ) { tmp = best; best = putative[j]; putative[j] = tmp; if (tabu_list[tabu_list_pos] == NULL) { tabu_list[tabu_list_pos] = ga_entity_clone(pop, best); } else { ga_entity_blank(pop, tabu_list[tabu_list_pos]); ga_entity_copy(pop, tabu_list[tabu_list_pos], best); } tabu_list_pos++; if (tabu_list_pos >= pop->tabu_params->list_length) tabu_list_pos=0; } } /* * Save the current best solution in the initial entity, if this * is now the best found so far. */ if ( pop->rank(pop, best, pop, initial) > 0 ) { ga_entity_blank(pop, initial); ga_entity_copy(pop, initial, best); } /* * Use the iteration callback. */ plog( LOG_VERBOSE, "After iteration %d, the current solution has fitness score of %f", iteration, best->fitness ); } /* Iteration loop. */ /* * Cleanup. */ ga_entity_dereference(pop, best); for (i=0; i<pop->tabu_params->search_count; i++) { ga_entity_dereference(pop, putative[i]); } for (i=0; i<pop->tabu_params->list_length; i++) { if (tabu_list[i] != NULL) ga_entity_dereference(pop, tabu_list[i]); } s_free(putative); s_free(tabu_list); return iteration; }