/*======================================================================== PISA (www.tik.ee.ethz.ch/pisa/) ======================================================================== Computer Engineering (TIK) ETH Zurich ======================================================================== SPAM - Set Preference Algorithm for Multiobjective Optimization authors: Johannes Bader, johannes.bader@tik.ee.ethz.ch Eckart Zitzler, eckart.zitzler@tik.ee.ethz.ch Marco Laumanns, laumanns@tik.ee.ethz.ch revision by: Stefan Bleuler, stefan.bleuler@tik.ee.ethz.ch Dimo Brockhoff, dimo.brockhoff@tik.ee.ethz.ch last change: 01.12.2008 ======================================================================== */ #include #include #include #include #include "spam.h" #include "population.h" int read_pop(char *filename, pop *pp, int size, int dim ) /* Reads individuals from file into pop */ { int i, j; int entries = 0; char tag[4]; FILE *fp; int result; assert(dim >= 0); assert(pp != NULL); fp = fopen(filename, "r"); assert(fp != NULL); fscanf(fp, "%d", &entries); if (entries == 0) /* file has not been written yet */ { return (1); /* signalling that reading failed */ } if( entries != size*(dim+1) ) { fprintf(stderr,"The number of entries (%d) is not equal to " "size*(dim+1) [%d*(%d+1)]", entries, size, dim ); } assert(entries == size * (dim + 1)); assert( size <= pp->maxsize ); assert( dim == pp->dim ); for (j = 0; j < size; j++) { /* reading index of individual */ result = fscanf(fp, "%d", &(pp->ind_array[j]->index)); pp->size++; for (i = 0; i < dim; i++) { /* reading objective values of ind */ double value; result = fscanf(fp, "%le", &value); pp->ind_array[j]->f_u[i] = value; pp->ind_array[j]->f_s[i] = value; if (result == EOF) /* file not completely written */ { fclose(fp); return (1); /* signalling that reading failed */ } } } /* after all data elements: "END" expected */ fscanf(fp, "%s", tag); if (strcmp(tag, "END") != 0) { fclose(fp); return (1); /* signalling that reading failed */ } else /* "END" ok */ { fclose(fp); /* delete file content if reading successful */ fp = fopen(filename, "w"); assert(fp != NULL); fprintf(fp, "0"); fclose(fp); return (0); /* signalling that reading was successful */ } } void write_pop(char* filename, pop* pp, int size) /* Writes a pop or PISA_to a given filename. */ { int i; FILE *fp; assert(0 <= size ); assert( size <= pp->size); fp = fopen(filename, "w"); assert(fp != NULL); fprintf(fp, "%d\n", size); /* number of elements */ for (i = 0; i < size; i++) { fprintf(fp, "%d\n", pp->ind_array[i]->index); } fprintf(fp, "END"); assert(fp != NULL); fclose(fp); } int check_file(char* filename) { int control_element = 1; FILE *fp; fp = fopen(filename, "r"); if( fp == NULL ) { printf("\nThe file >%s< does not exist!\n",filename); } assert(fp != NULL); fscanf(fp, "%d", &control_element); fclose(fp); if (0 == control_element) return (0); /* file is ready for writing */ else return (1); /* file is not ready for writing */ }