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

📄 getpar~1.c

📁 压缩包内内容有:《遗传算法——理论、应用与软件实现》中所附源程序C或C++代码文件及其可执行文件以及遗传算法相关自由软件及代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
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 <stdio.h>
#include <malloc.h>
#include <errno.h>
#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<argc;i++) {
    printf("%s ",argv[i]);
  }
  printf("\n\n");
  printf("Random seed:            %ld\n",(unsigned long)get_seed());
  printf("Number of populations:  %d\n",*numpops);
  printf("Number of generations:  %d\n",*numgens);
  if (*demes) {
    printf("Number of deme rows:    %d\n",*demerows);
    printf("Number of deme columns: %d\n",*demecols);
  }
  if (CHECKPOINT_FREQUENCY) {
    sprintf(ckptfile,"gpc_%ld_%d.ckpt",gethostid(),getpid());
    printf("Checkpointing to:       %s\n",ckptfile);
  }
  if (*start_gen) {
    printf("Restarting from gen:    %d\n", *start_gen+1);
  }
  printf("\n");

  argp = arg_offset+3;
  for(i=0;i<*numpops;i++,argp++) {
    if (*start_gen) {
      printf("Reading parameters for population %d from: %s\n\n",
	     i,argv[2]);
    }
    else if (strcasecmp(argv[argp],"none")) {
      printf("Reading parameters for population %d from: %s\n\n",
	     i,argv[argp]);
    }
    else {
      printf("Using default parameters for population %d\n\n",i);
    }
    writeparams(*pop,i,stdout);
    printf("\n");
  }

}


#ifdef ANSI_FUNC

VOID defaultparams(
  pop_struct 	*pop,
  int		i
  )
#else

VOID defaultparams(pop,i)
  pop_struct 	*pop;
  int		i;
#endif
{
  FILE	*paramfile;

  set_seed((unsigned long)11287);
  CHECKPOINT_FREQUENCY			= 0;
  START_GEN				= 0;

  pop[i].population_size 		= 100;
  pop[i].steady_state			= FALSE;
  pop[i].load_from_file 		= (char *) malloc(132*sizeof(char));
  pop[i].load_from_file[0] 		= '\0';
  pop[i].max_depth_for_new_trees 	= 6;
  pop[i].max_depth_after_crossover 	= 17; 
  pop[i].max_mutant_depth 		= 4;
  pop[i].grow_method 			= GROW;
  pop[i].selection_method 		= FITNESSPROP;
  pop[i].tournament_K 			= 7;
  pop[i].deme_search_radius_sigma	= 1.0;
  pop[i].crossover_func_pt_fraction 	= 0.695;
  pop[i].crossover_any_pt_fraction 	= 0.195;
  pop[i].fitness_prop_repro_fraction	= 0.095;
  pop[i].parsimony_factor 		= 0.0;

  if ((paramfile = fopen("default.in","r")) != (FILE *)NULL) {
    readparams(pop,i,paramfile);
    fclose(paramfile);
  }
}

#ifdef ANSI_FUNC

VOID readparams(
  pop_struct 	*pop,
  int		i,
  FILE 		*f

⌨️ 快捷键说明

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