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

📄 ga_io.c

📁 关于遗传算法的一些见地。特别是关于简单遗传程序设计的实现。
💻 C
📖 第 1 页 / 共 3 页
字号:
/**********************************************************************  ga_io.c **********************************************************************  ga_io - Disk I/O routines.  Copyright ©2003-2005, Stewart Adcock <stewart@linux-domain.com>  All rights reserved.  The latest version of this program should be available at:  http://gaul.sourceforge.net/  This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.  Alternatively, if your project  is incompatible with the GPL, I will probably agree to requests  for permission to use the terms of any other license.  This program is distributed in the hope that it will be useful, but  WITHOUT ANY WARRANTY WHATSOEVER.  A full copy of the GNU General Public License should be in the file  "COPYING" provided with this distribution; if not, see:  http://www.gnu.org/ **********************************************************************  Synopsis:     Routines for reading/writing GA data from/onto disk.  To do:	Fix the really inefficient entity format (i.e. remove len).		Remove excessive use of malloc()/free() in posix versions.		Support for longer chromosomes in win32 versions.		It is fair to say that the file formats are embarrasingly poor.		Alternative optimisation function parameters not saved. **********************************************************************/#include "gaul/ga_core.h"#define BUFFER_SIZE	1024/**********************************************************************  gaul_write_entity_posix()  synopsis:	Writes an entity to a filepointer using a binary format.		Note: Currently does not (and probably can not) store		any of the userdata.  parameters:  return:  last updated: 29 May 2002 **********************************************************************/#ifndef WIN32static void gaul_write_entity_posix(FILE *fp, population *pop, entity *entity)  {  byte		*buffer=NULL;		/* Buffer for genetic data. */  unsigned int	len, max_len=0;		/* Length of buffer. */  fwrite(&(entity->fitness), sizeof(double), 1, fp);  len = (int) pop->chromosome_to_bytes(pop, entity, &buffer, &max_len);  fwrite(&len, sizeof(unsigned int), 1, fp);  fwrite(buffer, sizeof(byte), len, fp);  if (max_len!=0) s_free(buffer);  return;  }#endif/**********************************************************************  gaul_write_entity_win32()  synopsis:	Writes an entity to a filepointer using a binary format.		Note: Currently does not (and probably can not) store		any of the userdata.  parameters:  return:  last updated: 29 May 2002 **********************************************************************/#ifdef WIN32static void gaul_write_entity_win32(HANDLE file,                                 population *pop, entity *entity)  {  byte		buffer[BUFFER_SIZE];	/* Buffer for genetic data. */  unsigned int	len, max_len=0;		/* Length of buffer. */  byte		*bufptr;		/* Pointer into buffer. */  DWORD		nwrote;			/* Number of bytes written. */  memcpy(buffer, &(entity->fitness), sizeof(double));  bufptr = buffer+sizeof(double)+sizeof(int);  len = (int) pop->chromosome_to_bytes(pop, entity, &bufptr, &max_len);  memcpy(buffer+sizeof(double), &len, sizeof(int));  if ( WriteFile(file, buffer, len+sizeof(double)+sizeof(int), &nwrote, NULL)==0 )    dief("Error writing %d\n", GetLastError());  return;  }#endif/**********************************************************************  gaul_read_entity_posix()  synopsis:	Reads an entity from a filepointer using a binary		format.  parameters:	FILE *fp	File handle from which to read entity.		population *pop	Population to add new entity to.  return:	entity*	An entity as read from disk.  last updated: 30 May 2002 **********************************************************************/#ifndef WIN32static entity *gaul_read_entity_posix(FILE *fp, population *pop)  {  byte		*buffer=NULL;	/* Buffer for genetic data. */  unsigned int	len;		/* Length of buffer. */  entity	*entity;	/* New entity read from disk. */  entity = ga_get_free_entity(pop);  fread(&(entity->fitness), sizeof(double), 1, fp);  fread(&len, sizeof(unsigned int), 1, fp);  buffer = s_malloc(sizeof(byte)*len);  fread(buffer, sizeof(byte), len, fp);  pop->chromosome_from_bytes(pop, entity, buffer);  s_free(buffer);  return entity;  }#endif/**********************************************************************  gaul_read_entity_win32()  synopsis:	Reads an entity from a filepointer using a binary		format.  parameters:	FILE *fp	File handle from which to read entity.		population *pop	Population to add new entity to.  return:	entity*	An entity as read from disk.  last updated: 16 Aug 2003 **********************************************************************/#ifdef WIN32static entity *gaul_read_entity_win32(HANDLE file, population *pop)  {  byte		buffer[BUFFER_SIZE];	/* Buffer for genetic data. */  unsigned int	len;		/* Length of buffer. */  entity	*entity;	/* New entity read from disk. */  DWORD		nread;		/* Number of bytes read. */  entity = ga_get_free_entity(pop);  if (!ReadFile(file, buffer, sizeof(double), &nread, NULL) || nread < 1)    dief("Unable to read data.  Error %d\n", GetLastError());  memcpy(&(entity->fitness), buffer, sizeof(double));  if (!ReadFile(file, buffer, sizeof(unsigned int), &nread, NULL) || nread < 1)    dief("Unable to read data.  Error %d\n", GetLastError());  memcpy(&len, buffer, sizeof(unsigned int));  if (!ReadFile(file, buffer, len*sizeof(byte), &nread, NULL) || nread < 1)    dief("Unable to read data.  Error %d\n", GetLastError());  pop->chromosome_from_bytes(pop, entity, buffer);  return entity;  }#endif/**********************************************************************  ga_population_write()  synopsis:	Writes entire population and it's genetic data to disk,		using a binary format.		Note: Currently does not (and probably can not) store		any of the userdata.  parameters:  return:  last updated: 24 Feb 2005 **********************************************************************/#ifndef WIN32boolean ga_population_write(population *pop, char *fname)  {  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. *//* Checks. */  if ( !pop ) die("Null pointer to population structure passed.");  if ( !fname ) die("Null pointer to filename passed.");/* * Open output file. */  if( !(fp=fopen(fname,"w")) )    dief("Unable to open population file \"%s\" for output.", fname);/* * Program info. */  fwrite(format_str, sizeof(char), strlen(format_str), fp);  for (i=0; i<64; i++) buffer[i]='\0';  snprintf(buffer, 64, "%s %s", GA_VERSION_STRING, GA_BUILD_DATE_STRING);  fwrite(buffer, sizeof(char), 64, fp);/* * Population info. */  fwrite(&(pop->size), sizeof(int), 1, fp);  fwrite(&(pop->stable_size), sizeof(int), 1, fp);  fwrite(&(pop->num_chromosomes), sizeof(int), 1, fp);  fwrite(&(pop->len_chromosomes), sizeof(int), 1, fp);/* * GA parameters. */  fwrite(&(pop->crossover_ratio), sizeof(double), 1, fp);  fwrite(&(pop->mutation_ratio), sizeof(double), 1, fp);  fwrite(&(pop->migration_ratio), sizeof(double), 1, fp);  fwrite(&(pop->allele_mutation_prob), sizeof(double), 1, fp);  fwrite(&(pop->allele_min_integer), sizeof(int), 1, fp);  fwrite(&(pop->allele_max_integer), sizeof(int), 1, fp);  fwrite(&(pop->allele_min_double), sizeof(double), 1, fp);  fwrite(&(pop->allele_max_double), sizeof(double), 1, fp);  fwrite(&(pop->scheme), sizeof(int), 1, fp);  fwrite(&(pop->elitism), sizeof(int), 1, fp);  fwrite(&(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. */  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);  fwrite(id, sizeof(int), 19, fp);/* * 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_posix(fp, pop, pop->entity_iarray[i]);    }/* * Footer info. */  fwrite("END", sizeof(char), 4, fp); /* * Close file. */  fclose(fp);  return TRUE;  }#elseboolean ga_population_write(population *pop, char *fname)  {  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. */  DWORD		nwrote;			/* Number of bytes written. *//* Checks. */  if ( !pop ) die("Null pointer to population structure passed.");  if ( !fname ) die("Null pointer to filename passed.");/* * Open output file.

⌨️ 快捷键说明

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