📄 ga_io.c
字号:
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, 19*sizeof(int), &nread, NULL) || nread < 1) dief("Unable to read data. Error %d\n", GetLastError()); memcpy(&id, buffer, 19*sizeof(int)); 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_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; }#endif/********************************************************************** ga_entity_write() synopsis: Write an entity to disk. Note: Currently does not (and probably can not) store any of the userdata. parameters: population *pop entity *entity char *fname return: TRUE last updated: 07 Nov 2002 **********************************************************************/#ifndef WIN32boolean ga_entity_write(population *pop, entity *entity, char *fname) { int i; /* Loop variable. */ char buffer[BUFFER_SIZE]; /* String buffer. */ char *format_str="FORMAT: GAUL ENTITY 001"; /* Format tag. */ FILE *fp; /* Filehandle. *//* Checks. */ if ( !pop ) die("Null pointer to population structure passed."); if ( !entity ) die("Null pointer to entity structure passed."); if ( !fname ) die("Null pointer to filename passed.");/* * Open output file. */ if( !(fp=fopen(fname,"w")) ) dief("Unable to open entity file \"%s\" for output.", fname);/* * Write stuff. */ 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); gaul_write_entity_posix(fp, pop, entity); fwrite("END", sizeof(char), 4, fp); fclose(fp); return TRUE; }#elseboolean ga_entity_write(population *pop, entity *entity, char *fname) { int i; /* Loop variable. */ char buffer[BUFFER_SIZE]; /* String buffer. */ char *format_str="FORMAT: GAUL ENTITY 001"; /* Format tag. */ HANDLE file; /* Filehandle. */ DWORD nwrote; /* Number of bytes written. *//* Checks. */ if ( !pop ) die("Null pointer to population structure passed."); if ( !entity ) die("Null pointer to entity structure passed."); if ( !fname ) die("Null pointer to filename passed.");/* * Open output file. */ 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());/* * Write stuff. */ 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()); gaul_write_entity_win32(file, pop, entity); strncpy(buffer, "END\0", 4); if ( WriteFile(file, format_str, 4*sizeof(char), &nwrote, NULL)==0 ) dief("Error writing %d\n", GetLastError()); CloseHandle(file); return TRUE; }#endif/********************************************************************** ga_entity_read() synopsis: Read an entity from disk. Note: Currently does not (and probably can not) store any of the userdata. parameters: population *pop entity *entity char *fname return: TRUE last updated: 30 May 2002 **********************************************************************/#ifndef WIN32/* * UNIX/POSIX version: */entity *ga_entity_read(population *pop, char *fname) { char buffer[BUFFER_SIZE]; /* String buffer. */ char *format_str="FORMAT: GAUL ENTITY 001"; /* Format tag. */ char format_str_in[32]; /* Input format tag. */ FILE *fp; /* Filehandle. */ entity *entity; /* Input entity. *//* Checks. */ if ( !pop ) die("Null pointer to population structure passed."); if ( !fname ) die("Null pointer to filename passed.");/* * Open input file. */ if( !(fp=fopen(fname,"r")) ) dief("Unable to open entity file \"%s\" for input.", fname);/* * Read stuff. */ fread(format_str_in, sizeof(char), strlen(format_str), fp); if (strcmp(format_str, format_str_in)!=0) die("Incorrect format for entity file."); fread(buffer, sizeof(char), 64, fp); /* Ignored. */ entity = gaul_read_entity_posix(fp, pop); fread(buffer, sizeof(char), 4, fp); if (strcmp("END", buffer)!=0) die("Corrupt population file?"); fclose(fp); return entity; }#else/* * MS Windows version: */entity *ga_entity_read(population *pop, char *fname) { char buffer[BUFFER_SIZE]; /* String buffer. */ char *format_str="FORMAT: GAUL ENTITY 001"; /* Format tag. */ char format_str_in[32]; /* Input format tag. */ HANDLE file; /* Filehandle. */ entity *entity; /* Input entity. */ DWORD nread; /* Number of bytes read. *//* Checks. */ if ( !pop ) die("Null pointer to population structure passed."); if ( !fname ) die("Null pointer to filename passed.");/* * Open input 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());/* * Read stuff. */ if (!ReadFile(file, format_str_in, strlen(format_str)*sizeof(char), &nread, NULL) && nread < 1) dief("Unable to open entity file \"%s\" for input due to error %d.", fname, GetLastError()); if (strncmp(format_str, format_str_in, strlen(format_str))!=0) die("Incorrect format for entity file."); ReadFile(file, buffer, 64*sizeof(char), &nread, NULL); /* Ignored. */ entity = gaul_read_entity_win32(file, pop); if (!ReadFile(file, buffer, 4, &nread, NULL) && nread < 1) dief("Unable to open entity file \"%s\" for input due to error %d.", fname, GetLastError()); if (strcmp("END", buffer)!=0) die("Corrupt population file?"); CloseHandle(file); return entity; }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -