⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga_io.c

📁 关于遗传算法的一些见地。特别是关于简单遗传程序设计的实现。
💻 C
📖 第 1 页 / 共 3 页
字号:
 */  if ((file = CreateFile(fname, GENERIC_WRITE,        0, 0, CREATE_NEW, 0, 0)) == INVALID_HANDLE_VALUE)    dief("Unable to open entity file \"%s\" for output due to error %d.",         fname, GetLastError());/* * Program info. */  if ( WriteFile(file, format_str, strlen(format_str)*sizeof(char), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  for (i=0; i<64; i++) buffer[i]='\0';  snprintf(buffer, 64, "%s %s", GA_VERSION_STRING, GA_BUILD_DATE_STRING);  if ( WriteFile(file, format_str, 64*sizeof(char), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());/* * Population info. */  if ( WriteFile(file, &(pop->size), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->stable_size), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->num_chromosomes), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->len_chromosomes), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());/* * GA parameters. */  if ( WriteFile(file, &(pop->crossover_ratio), sizeof(double), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->mutation_ratio), sizeof(double), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->migration_ratio), sizeof(double), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->allele_mutation_prob), sizeof(double), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->allele_min_integer), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->allele_max_integer), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->allele_min_double), sizeof(double), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->allele_max_double), sizeof(double), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->scheme), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->elitism), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  if ( WriteFile(file, &(pop->island), sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());/* * 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. */  id[0] = ga_funclookup_ptr_to_id((void *)pop->generation_hook);  id[1] = ga_funclookup_ptr_to_id((void *)pop->iteration_hook);  /*ga_funclookup_ptr_to_id((void *)pop->data_destructor);*/  /*ga_funclookup_ptr_to_id((void *)pop->data_ref_incrementor);*/  /* GAUL doesn't define any functions for either of these. */  if (pop->data_destructor)    id[2] = -1;  else    id[2] = 0;  if (pop->data_ref_incrementor)    id[3] = -1;  else    id[3] = 0;  id[4] = ga_funclookup_ptr_to_id((void *)pop->chromosome_constructor);  id[5] = ga_funclookup_ptr_to_id((void *)pop->chromosome_destructor);  id[6] = ga_funclookup_ptr_to_id((void *)pop->chromosome_replicate);  id[7] = ga_funclookup_ptr_to_id((void *)pop->chromosome_to_bytes);  id[8] = ga_funclookup_ptr_to_id((void *)pop->chromosome_from_bytes);  id[9] = ga_funclookup_ptr_to_id((void *)pop->chromosome_to_string);  id[10] = ga_funclookup_ptr_to_id((void *)pop->evaluate);  id[11] = ga_funclookup_ptr_to_id((void *)pop->seed);  id[12] = ga_funclookup_ptr_to_id((void *)pop->adapt);  id[13] = ga_funclookup_ptr_to_id((void *)pop->select_one);  id[14] = ga_funclookup_ptr_to_id((void *)pop->select_two);  id[15] = ga_funclookup_ptr_to_id((void *)pop->mutate);  id[16] = ga_funclookup_ptr_to_id((void *)pop->crossover);  id[17] = ga_funclookup_ptr_to_id((void *)pop->replace);  id[18] = ga_funclookup_ptr_to_id((void *)pop->rank);  if ( WriteFile(file, id, 19*sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());/* * Warn user of any unhandled data. */  for (i=0; i<19; 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<pop->size; i++)    {    gaul_write_entity_win32(file, pop, pop->entity_iarray[i]);    }/* * Footer info. */  strncpy(buffer, "END\0", 4);   if ( WriteFile(file, format_str, 4*sizeof(char), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());/* * Close file. */  CloseHandle(file);  return TRUE;  }#endif/**********************************************************************  ga_population_read()  synopsis:	Reads entire population and it's genetic data back		from disk.   Some things can't be restored.  See		ga_population_write() for details.  parameters:	char *fname		Filename to read from.  return:	population *pop		New population structure.  last updated: 24 Feb 2005 **********************************************************************/#ifndef WIN32population *ga_population_read(char *fname)  {  population	*pop=NULL;		/* New population structure. */  FILE          *fp;			/* File handle. */  int		i;			/* Loop variables. */  char		buffer[BUFFER_SIZE];	/* String buffer. */  int		id[19];			/* Array of hook indices. */  int		count=0;		/* Number of unrecognised hook functions. */  char		*format_str="FORMAT: GAUL POPULATION 003";	/* Format tag. */  char		format_str_in[32]="";	/* Input format tag. (Empty initialiser to avoid valgrind warning...) */  int		size, stable_size, num_chromosomes, len_chromosomes;	/* Input data. *//* Checks. */  if ( !fname ) die("Null pointer to filename passed.");/* * Open output file. */  if( !(fp=fopen(fname,"r")) )    dief("Unable to open population file \"%s\" for input.", fname);/* * Program info. */  fread(format_str_in, sizeof(char), strlen(format_str), fp);  if (strcmp(format_str, format_str_in)!=0)    {    fclose(fp);    die("Invalid file format");    }  fread(buffer, sizeof(char), 64, fp);	/* Presently ignored. *//* * Population info. */  fread(&size, sizeof(int), 1, fp);  fread(&stable_size, sizeof(int), 1, fp);  fread(&num_chromosomes, sizeof(int), 1, fp);  fread(&len_chromosomes, sizeof(int), 1, fp);/* * 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. */  fread(&(pop->crossover_ratio), sizeof(double), 1, fp);  fread(&(pop->mutation_ratio), sizeof(double), 1, fp);  fread(&(pop->migration_ratio), sizeof(double), 1, fp);  fread(&(pop->allele_mutation_prob), sizeof(double), 1, fp);  fread(&(pop->allele_min_integer), sizeof(int), 1, fp);  fread(&(pop->allele_max_integer), sizeof(int), 1, fp);  fread(&(pop->allele_min_double), sizeof(double), 1, fp);  fread(&(pop->allele_max_double), sizeof(double), 1, fp);  fread(&(pop->scheme), sizeof(int), 1, fp);  fread(&(pop->elitism), sizeof(int), 1, fp);  fread(&(pop->island), sizeof(int), 1, fp);/* * 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. */  fread(id, sizeof(int), 19, fp);  pop->generation_hook        = (GAgeneration_hook)  ga_funclookup_id_to_ptr(id[0]);  pop->iteration_hook         = (GAiteration_hook)   ga_funclookup_id_to_ptr(id[1]);  pop->data_destructor        = (GAdata_destructor)      ga_funclookup_id_to_ptr(id[2]);  pop->data_ref_incrementor   = (GAdata_ref_incrementor) ga_funclookup_id_to_ptr(id[3]);  pop->chromosome_constructor = (GAchromosome_constructor) ga_funclookup_id_to_ptr(id[4]);  pop->chromosome_destructor  = (GAchromosome_destructor)  ga_funclookup_id_to_ptr(id[5]);  pop->chromosome_replicate   = (GAchromosome_replicate)   ga_funclookup_id_to_ptr(id[6]);  pop->chromosome_to_bytes    = (GAchromosome_to_bytes)    ga_funclookup_id_to_ptr(id[7]);  pop->chromosome_from_bytes  = (GAchromosome_from_bytes)  ga_funclookup_id_to_ptr(id[8]);  pop->chromosome_to_string   = (GAchromosome_to_string)   ga_funclookup_id_to_ptr(id[9]);  pop->evaluate               = (GAevaluate)       ga_funclookup_id_to_ptr(id[10]);  pop->seed                   = (GAseed)           ga_funclookup_id_to_ptr(id[11]);  pop->adapt                  = (GAadapt)          ga_funclookup_id_to_ptr(id[12]);  pop->select_one             = (GAselect_one)     ga_funclookup_id_to_ptr(id[13]);  pop->select_two             = (GAselect_two)     ga_funclookup_id_to_ptr(id[14]);  pop->mutate                 = (GAmutate)         ga_funclookup_id_to_ptr(id[15]);  pop->crossover              = (GAcrossover)      ga_funclookup_id_to_ptr(id[16]);  pop->replace                = (GAreplace)        ga_funclookup_id_to_ptr(id[17]);  pop->rank                   = (GArank)           ga_funclookup_id_to_ptr(id[18]);/* * Warn user of any unhandled data. */  for (i=0; i<19; 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_posix(fp, pop);    }/* * Footer info. */  fread(buffer, sizeof(char), 4, fp);   if (strcmp("END", buffer)!=0)    die("Corrupt population file?");/* * Close file. */  fclose(fp);  plog(LOG_DEBUG, "Have read %d entities into population.", pop->size);  return pop;  }#elsepopulation *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[19];			/* Array of hook indices. */  int		count=0;		/* Number of unrecognised hook functions. */  char		*format_str="FORMAT: GAUL POPULATION 003";	/* 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());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -