/* SGPC: Simple Genetic Programming in C (c) 1993 by Walter Alden Tackett and Aviram Carmi This code and documentation is copyrighted and is not in the public domain. All rights reserved. - This notice may not be removed or altered. - You may not try to make money by distributing the package or by using the process that the code creates. - You may not distribute modified versions without clearly documenting your changes and notifying the principal author. - The origin of this software must not be misrepresented, either by explicit claim or by omission. Since few users ever read sources, credits must appear in the documentation. - Altered versions must be plainly marked as such, and must not be misrepresented as being the original software. Since few users ever read sources, credits must appear in the documentation. - The authors are not responsible for the consequences of use of this software, no matter how awful, even if they arise from flaws in it. If you make changes to the code, or have suggestions for changes, let us know! (gpc@ipld01.hac.com) */ #ifndef lint static char getparams_c_rcsid[]="$Id: getparams.c,v 2.18 1993/04/23 01:56:25 gpc-avc Exp gpc-avc $"; #endif /* * * $Log: getparams.c,v $ * Revision 2.18 1993/04/23 01:56:25 gpc-avc * Added COMPRESS macro so that the compression command for checkpoint * files is easily changed in Makefile * * Revision 2.17 1993/04/22 07:39:12 gpc-avc * Removed old log messages * * Revision 2.16 1993/04/17 03:03:01 gpc-avc * Added a test to check if the load_from_file file exists * * */ #include #include #include #include "gpc.h" #include "malloc4.h" #ifdef ANSI_FUNC VOID getparams( int argc, char **argv, int *numpops, int *numgens, int *demes, int *demerows, int *demecols, int *start_gen, FILE **ckpt_file, pop_struct **pop, pop_struct ****grid ) #else VOID getparams(argc,argv,numpops,numgens,demes,demerows,demecols,start_gen, ckpt_file,pop,grid) int argc; char **argv; int *numpops; int *numgens; int *demes; int *demerows; int *demecols; int *start_gen; FILE **ckpt_file; pop_struct **pop; pop_struct ****grid; #endif { int i,j; int arg_offset = 0; int argp = 1; FILE *paramfile; char ckptfile[255]; static char usage[] = "%s: Usage: %s [-d rows cols] npops ngens paramfile | 'none'... [seed]\n%s: Usage: %s -r ckpt_filename\n"; if (argc < 2) { fprintf(stderr, usage, argv[0],argv[0],argv[0],argv[0]); exit(1); } if (!strcmp(argv[1],"-r")) { if (argc < 3) { fprintf(stderr, usage, argv[0],argv[0], argv[0],argv[0]); fprintf(stderr, "%s: Missing checkpoint file name.\n",argv[0]); exit(1); } if ((*ckpt_file = fopen(argv[2],"r")) != (FILE *)NULL) { recover_params (*ckpt_file,numpops,numgens,demes,demerows,demecols,pop, grid,start_gen); } else { fprintf(stderr, "%s: Checkpoint file not found: '%s'.\n", argv[0],argv[2]); exit(1); } } else { if (!strcmp(argv[1],"-d")) { if (argc < 4) { fprintf(stderr, usage, argv[0],argv[0], argv[0],argv[0]); fprintf(stderr, "%s: Missing deme rows or cols.\n",argv[0]); exit(1); } if (argc < 7) { fprintf(stderr, usage, argv[0],argv[0], argv[0],argv[0]); if (argc < 6) { fprintf(stderr, "%s: Missing number of populations or generations.\n", argv[0]); } else { fprintf(stderr, "%s: Missing parameter file name.\n",argv[0]); fprintf(stderr, "%s: Use 'none' as the file name in order to use defaults.\n", argv[0]); } exit(1); } *demes = TRUE; arg_offset += 3; argp++; } else { if (argc < 4) { fprintf(stderr, usage, argv[0],argv[0], argv[0],argv[0]); if (argc < 3) { fprintf(stderr, "%s: Missing number of populations or generations.\n", argv[0]); } else { fprintf(stderr, "%s: Missing parameter file name.\n",argv[0]); fprintf(stderr, "%s: Use 'none' as the file name in order to use defaults.\n", argv[0]); } exit(1); } *demes = FALSE; *demerows = 0; *demecols = 0; } if (*demes) { *demerows = -1; sscanf(argv[argp++],"%d",demerows); if (*demerows < 1) { fprintf(stderr, "%s: Deme rows has to be greater than 1: '%s'.\n", argv[0], argv[argp-1]); exit(1); } *demecols = -1; sscanf(argv[argp++],"%d",demecols); if (*demecols < 1) { fprintf(stderr, "%s: Deme cols has to be greater than 1: '%s'.\n", argv[0], argv[argp-1]); exit(1); } } *numpops = -1; sscanf(argv[argp++],"%d",numpops); if (*numpops < 1) { fprintf(stderr, "%s: Number of populations has to be greater than 1: '%s'.\n", argv[0], argv[argp-1]); exit(1); } *numgens = -1; sscanf(argv[argp++],"%d",numgens); if (*numgens < 1) { fprintf(stderr, "%s: Number of generations has to be greater than 1: '%s'.\n", argv[0], argv[argp-1]); exit(1); } *pop = (pop_struct *) malloc(*numpops*sizeof(pop_struct)); if (*demes) { *grid = (pop_struct ***) malloc2(*demerows,*demecols*sizeof(pop_struct *)); for (i=0;i<*demerows;i++) { for (j=0;j<*demecols;j++) { (*grid)[i][j] = (pop_struct *) malloc(*numpops*sizeof(pop_struct)); } } } if (argc < *numpops+3+arg_offset) { fprintf(stderr, "%s: Missing parameter file name.\n", argv[0]); fprintf(stderr, "%s: Use 'none' as the file name in order to use defaults.\n", argv[0]); exit(1); } for(i=0;i<*numpops;i++,argp++) { defaultparams(*pop,i); if (strcasecmp(argv[argp],"none")) { if ((paramfile = fopen(argv[argp],"r")) != (FILE *)NULL) { readparams(*pop,i,paramfile); fclose(paramfile); } else { fprintf(stderr, "%s: Parameter file not found: '%s'.\n", argv[0],argv[argp]); exit(1); } } } if (argc == (j=*numpops+4+arg_offset)) { long seed = -1; sscanf(argv[j-1],"%d",&seed); if (seed < 0) { fprintf(stderr, "%s: Bad seed value: '%s'.\n", argv[0], argv[j-1]); exit(1); } set_seed((unsigned long)seed); } if (argc > (j=*numpops+4+arg_offset)) { fprintf(stderr, usage, argv[0],argv[0], argv[0],argv[0]); fprintf(stderr, "%s: Too many arguments on command line.\n",argv[0]); exit(1); } } if (*demes) { for(i=0;i<*numpops;i++) { if ((*pop)[i].population_size%((*demerows) * (*demecols))) { fprintf(stderr, "population %d size (%d) must be evenly divisible by rows*cols (%d)\n", i,(*pop)[i].population_size, ((*demerows) * (*demecols))); exit(1); } } } for (i=0;i