Example #1
0
boolean ga_qsort_test(void)
#endif
  {
  int		i;		/* Loop variable */
  population	*pop=NULL;	/* Test population */

  pop = ga_population_new(50000, 4, 32);

/* Randomly assigned fitnesses */
  for (i=0; i<50000; i++)
    {
    pop->entity_array[i]->fitness=(double) rand()/RAND_MAX;
    pop->entity_iarray[i]=pop->entity_array[i];
    }
  pop->size=50000;

  plog(LOG_NORMAL, "Sorting random list.");
  sort_population(pop);

  plog(LOG_NORMAL, "Sorting ordered list.");
  sort_population(pop);

/* Reverse population */
  for (i=0; i<50000/2; i++)
    swap_e(pop->entity_iarray[i],pop->entity_iarray[24999-i]);

  plog(LOG_NORMAL, "Sorting reverse-ordered list.");
  sort_population(pop);

/* Write list */
/*
  for (i=0; i<50000; i++)
    printf("%6d: %f\n", i, pop->entity_iarray[i]->fitness);
*/

#ifdef GA_QSORT_COMPILE_MAIN
  exit(EXIT_SUCCESS);
#else
  return TRUE;
#endif
  }
Example #2
0
GAULFUNC population *ga_population_read(char *fname)
  {
  population	*pop=NULL;		/* New population structure. */
  HANDLE        file;			/* File handle. */
  int		i;			/* Loop variables. */
  char		buffer[BUFFER_SIZE];	/* String buffer. */
  int		id[GA_POPULATION_HOOK_COUNT];   /* Array of hook indices. */
  int		count=0;		/* Number of unrecognised hook functions. */
  char		*format_str="FORMAT: GAUL POPULATION 004";	/* Format tag. */
  int		size, stable_size, num_chromosomes, len_chromosomes;	/* Input data. */
  DWORD		nread;			/* Number of bytes read. */

/* Checks. */
  if ( !fname ) die("Null pointer to filename passed.");

/*
 * Open output file.
 */
  if ( (file = CreateFile(fname, GENERIC_READ,
        0, 0, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE )
    dief("Unable to open entity file \"%s\" for input due to error %d.",
         fname, GetLastError());

/*
 * Program info.
 */
  if (!ReadFile(file, buffer, strlen(format_str), &nread, NULL) || nread != strlen(format_str))
    dief("Unable to read data.  Error %d\n", GetLastError());

  if (strncmp(format_str, buffer, strlen(format_str))!=0)
    {
    CloseHandle(file);
    die("Invalid file format");
    }

  /* Presently ignored. */
  if (!ReadFile(file, buffer, 64*sizeof(char), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());

/*
 * Population info.
 */
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&size, buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&stable_size, buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&num_chromosomes, buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&len_chromosomes, buffer, sizeof(int));

/*
 * Allocate a new population structure.
 */
  pop = ga_population_new(stable_size, num_chromosomes, len_chromosomes);

/*
 * Got a population structure?
 */
  if ( !pop ) die("Unable to allocate population structure.");

/*
 * GA parameters.
 */
  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->crossover_ratio), buffer, sizeof(double));
  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->mutation_ratio), buffer, sizeof(double));
  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->migration_ratio), buffer, sizeof(double));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->allele_mutation_prob), buffer, sizeof(double));
  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->allele_min_integer), buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->allele_max_integer), buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->allele_min_double), buffer, sizeof(double));
  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->allele_max_double), buffer, sizeof(double));
  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->scheme), buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->elitism), buffer, sizeof(int));
  if (!ReadFile(file, buffer, sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&(pop->island), buffer, sizeof(int));

/*
 * Callback handling.  Note that user-implemented functions currently
 * can't be handled in these files.
 * id = -1 - Unknown, external function.
 * id = 0  - NULL function.
 * id > 0  - GAUL defined function.
 */
  if (!ReadFile(file, buffer, GA_POPULATION_HOOK_COUNT * sizeof(int), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  memcpy(&id, buffer, GA_POPULATION_HOOK_COUNT * sizeof(int));

  pop->generation_hook        = (GAgeneration_hook)  ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_GENERATION_HOOK]);
  pop->iteration_hook         = (GAiteration_hook)   ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_ITERATION_HOOK]);

  pop->data_destructor        = (GAdata_destructor)      ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_DATA_DESTRUCTOR]);
  pop->data_ref_incrementor   = (GAdata_ref_incrementor) ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_DATA_REF_INCREMENTOR]);

  pop->population_data_destructor = (GAdata_destructor) ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_POPULATION_DATA_DESTRUCTOR]);
  pop->population_data_copy   = (GAdata_copy)           ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_POPULATION_DATA_COPY]);

  pop->chromosome_constructor = (GAchromosome_constructor) ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CHROMOSOME_CONSTRUCTOR]);
  pop->chromosome_destructor  = (GAchromosome_destructor)  ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CHROMOSOME_DESTRUCTOR]);
  pop->chromosome_replicate   = (GAchromosome_replicate)   ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CHROMOSOME_REPLICATE]);
  pop->chromosome_to_bytes    = (GAchromosome_to_bytes)    ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CHROMOSOME_TO_BYTES]);
  pop->chromosome_from_bytes  = (GAchromosome_from_bytes)  ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CHROMOSOME_FROM_BYTES]);
  pop->chromosome_to_string   = (GAchromosome_to_string)   ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CHROMOSOME_TO_STRING]);

  pop->evaluate               = (GAevaluate)       ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_EVALUATE]);
  pop->seed                   = (GAseed)           ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_SEED]);
  pop->adapt                  = (GAadapt)          ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_ADAPT]);
  pop->select_one             = (GAselect_one)     ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_SELECT_ONE]);
  pop->select_two             = (GAselect_two)     ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_SELECT_TWO]);
  pop->mutate                 = (GAmutate)         ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_MUTATE]);
  pop->crossover              = (GAcrossover)      ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_CROSSOVER]);
  pop->replace                = (GAreplace)        ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_REPLACE]);
  pop->rank                   = (GArank)           ga_funclookup_id_to_ptr(id[GA_POPULATION_HOOK_OFFSET_RANK]);

/*
 * Warn user of any unhandled data.
 */
  for (i=0; i < GA_POPULATION_HOOK_COUNT; i++)
    if (id[i] == -1) count++;

  if (count>0)
    plog(LOG_NORMAL, "Unable to handle %d hook function%sspecified in population structure.", count, count==1?" ":"s ");

/*
 * Entity info.
 */
  for (i=0; i<size; i++)
    {
    gaul_read_entity_win32(file, pop);
    }

/*
 * Footer info.
 */
  if (!ReadFile(file, buffer, 4*sizeof(char), &nread, NULL) || nread < 1)
    dief("Unable to read data.  Error %d\n", GetLastError());
  if (strcmp("END", buffer)!=0) die("Corrupt population file?");

/*
 * Close file.
 */
  CloseHandle(file);

  plog(LOG_DEBUG, "Have read %d entities into population.", pop->size);

  return pop;
  }
Example #3
0
GAULFUNC population *ga_genesis_bitstring(	const int		population_size,
			const int		num_chromo,
			const int		len_chromo,
			GAgeneration_hook	generation_hook,
			GAiteration_hook	iteration_hook,
			GAdata_destructor	data_destructor,
			GAdata_ref_incrementor	data_ref_incrementor,
			GAevaluate		evaluate,
			GAseed			seed,
			GAadapt			adapt,
			GAselect_one		select_one,
			GAselect_two		select_two,
			GAmutate		mutate,
			GAcrossover		crossover,
			GAreplace		replace,
			vpointer		userdata )
  {
  population	*pop;	/* The new population structure. */

  plog(LOG_VERBOSE, "Genesis is beginning!");

/*
 * Initialise OpenMP code.
 */
  ga_init_openmp();

/*
 * Allocate and initialise a new population.
 * This call also sets this as the active population.
 */
  if ( !(pop = ga_population_new( population_size, num_chromo, len_chromo )) )
    return NULL;

/*
 * Assign population's user data.
 */
  pop->data = userdata;

/*
 * Define some callback functions.
 */
  pop->generation_hook = generation_hook;
  pop->iteration_hook = iteration_hook;

  pop->data_destructor = data_destructor;
  pop->data_ref_incrementor = data_ref_incrementor;

  pop->chromosome_constructor = ga_chromosome_bitstring_allocate;
  pop->chromosome_destructor = ga_chromosome_bitstring_deallocate;
  pop->chromosome_replicate = ga_chromosome_bitstring_replicate;
  pop->chromosome_to_bytes = ga_chromosome_bitstring_to_bytes;
  pop->chromosome_from_bytes = ga_chromosome_bitstring_from_bytes;
  pop->chromosome_to_string = ga_chromosome_bitstring_to_string;

  pop->evaluate = evaluate;
  pop->seed = seed;
  pop->adapt = adapt;
  pop->select_one = select_one;
  pop->select_two = select_two;
  pop->mutate = mutate;
  pop->crossover = crossover;
  pop->replace = replace;

/*
 * Seed the population.
 */
#if 0
  if (seed==NULL)
    {
    plog(LOG_VERBOSE, "Entity seed function not defined.  Genesis can not occur.  Continuing anyway.");
    }
  else
    {
    ga_population_seed(pop);
    plog(LOG_VERBOSE, "Genesis has occured!");
    }
#endif

  return pop;
  }