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

📄 xcs.c

📁 Simple GA code (Pascal code from Goldberg, D. E. (1989), Genetic Algorithms in Search, Optimization,
💻 C
📖 第 1 页 / 共 3 页
字号:
  fprintf(tabFile, "\n");  free(specPos);}/** * Writes out averages and the corresponding standard deviations over all executed experiments.  */void writeAveragePerformance(struct XCS *xcs, double **averages){  char *tabFileName;  int i,pos,j, exp;  FILE *tabFile;  double *av, *stdev;  assert((av = (double *)(calloc( (8 + getConditionLength()) *       ((int)(xcs->maxNrSteps/xcs->testFrequency)+1), sizeof(double))))!=0);  assert((stdev = (double *)(calloc( (8 + getConditionLength()) *       ((int)(xcs->maxNrSteps/xcs->testFrequency)+1), sizeof(double))))!=0);  /* generate output file for statistics (averages and standard deviaitons) */  assert((tabFileName = (char *)(calloc( strlen(xcs->tabOutFile)+5, sizeof(char))))!=0);  strcpy(tabFileName,xcs->tabOutFile);  tabFileName[strlen(xcs->tabOutFile)]='.';  tabFileName[strlen(xcs->tabOutFile)+1]='t';  tabFileName[strlen(xcs->tabOutFile)+2]='a';  tabFileName[strlen(xcs->tabOutFile)+3]='b';  if ((tabFile = fopen(tabFileName, "wt")) == NULL) {    fprintf(stderr, "Cannot open file");    fprintf(stderr, tabFileName);    return;  }  free(tabFileName);  /* Print parameter settings */  fprintf(tabFile, "# The following averages and standard deviations were determined\n");  fprintXCS(tabFile, xcs);  fprintEnv(tabFile);  for(exp=0; exp < xcs->nrExps; exp++) {    for(i=0; i < (8+getConditionLength())*((int)(xcs->maxNrSteps/xcs->testFrequency)+1); i++) {      av[i] += averages[exp][i];    }  }  for(i=0; i < (8+getConditionLength())*((int)(xcs->maxNrSteps/xcs->testFrequency)+1); i++) {    av[i] /= xcs->nrExps;  }  for(exp=0; exp < xcs->nrExps; exp++) {    for(i=0; i < (8+getConditionLength())*((int)(xcs->maxNrSteps/xcs->testFrequency)+1); i++) {      stdev[i] += (av[i]-averages[exp][i])*(av[i]-averages[exp][i]);    }  }  for(i=0; i < (8+getConditionLength())*((int)(xcs->maxNrSteps/xcs->testFrequency)+1); i++) {    stdev[i] = sqrt(stdev[i]/xcs->nrExps);  }  /* print successively all averages and starndard deviations for each monitored step in one line */  for(i=0; i<(int)(xcs->maxNrSteps/xcs->testFrequency)+1; i++) {    fprintf(tabFile, "%d ", (1+i)*xcs->testFrequency);    pos = i*(8+getConditionLength());    for(j=0; j<8+getConditionLength(); j++) {      if(j==2) {/*this is the population size */	fprintf(tabFile, "%f %f ", av[pos+j]/xcs->maxPopSize, stdev[pos+j]/xcs->maxPopSize);      }else{	fprintf(tabFile, "%f %f ", av[pos+j], stdev[pos+j]);      }    }    fprintf(tabFile, "\n");  }  fclose(tabFile);  free(av);  free(stdev);}/*######################## xcs parameter initialization and handling #######################*//** * Set parameters in the XCS parameter struct and for the environment -  * reading parameters from the specified parameter file. */void setParameterValues(FILE *parameterFile, struct XCS *xcs){  char s[500], type[100], value[100];  int i, j, xs, cLength;    cLength = getConditionLength();  setMacroValues(xcs);  if(parameterFile){    while(fscanf(parameterFile,"%[^\n]\n",s)==1){      if(s[0]!='#' && s[0]!='\n'){  /* '#' is the comment sign, empty lines are not treated */ 	xs=strlen(s);	for(i=0; i<xs && s[i]!=' ' && s[i]!='\t' && i<100; i++)	  type[i]=s[i];	if(i==100){	  printf("Error in parameter File - input too long!");	}else{	  type[i]='\0';	  while(s[i]==' ' || s[i]=='\t')	    i++;	  for(j=0 ; i<xs && s[i]!=' ' && s[i] !='\t' && j < 100; i++,j++)	    value[j]=s[i];	  if(j==100){	    printf("Error in parameter File - input2 too long!");	  }else{	    value[j]='\0';	    	    if(j!=0){	      /*printf("set %s to %s, ",type, value);*/	      /* Check for all possible inputs here */	      if(strcmp(type,"tabOutFile")==0){		free(xcs->tabOutFile);		assert(( xcs->tabOutFile = (char *)calloc(strlen(value)+1, sizeof(char)))!=NULL);		strcpy(xcs->tabOutFile, value);	      }else if(strcmp(type,"nrExps")==0){		xcs->nrExps = getIntValue(value);	      }else if(strcmp(type,"maxNrSteps")==0){		xcs->maxNrSteps = getIntValue(value);	      }else if(strcmp(type,"testFrequency")==0){		xcs->testFrequency = getIntValue(value);	      }else if(strcmp(type,"maxPopSize")==0){		xcs->maxPopSize = getIntValue(value);	      }else if(strcmp(type,"alpha")==0){		xcs->alpha = getDoubleValue(value);	      }else if(strcmp(type,"beta")==0){		xcs->beta = getDoubleValue(value);	      }else if(strcmp(type,"gamma")==0){		xcs->gamma = getDoubleValue(value);	      }else if(strcmp(type,"epsilon0")==0){		xcs->epsilon0 = getDoubleValue(value);	      }else if(strcmp(type,"nu")==0){		xcs->nu = getIntValue(value);	      }else if(strcmp(type,"thetaGA")==0){		xcs->thetaGA = getIntValue(value);	      }else if(strcmp(type,"fitnessReduction")==0){		xcs->fitnessReduction = getDoubleValue(value);	      }else if(strcmp(type,"tournamentSize")==0){		xcs->tournamentSize = getDoubleValue(value);	      }else if(strcmp(type,"forceDifferentInTournament")==0){		xcs->forceDifferentInTournament = getDoubleValue(value);	      }else if(strcmp(type,"selectTolerance")==0){		xcs->selectTolerance = getDoubleValue(value);	      }else if(strcmp(type,"crossoverType")==0){		xcs->crossoverType = getIntValue(value);	      }else if(strcmp(type,"chiGA")==0){		xcs->chiGA = getDoubleValue(value);	      }else if(strcmp(type,"muGA")==0){		xcs->muGA = getDoubleValue(value);	      }else if(strcmp(type,"doGeneralizationMutation")==0){		xcs->doGeneralizationMutation = getIntValue(value);	      }else if(strcmp(type,"doNicheMutation")==0){		xcs->doNicheMutation = getIntValue(value);	      }else if(strcmp(type,"doMAM")==0){		xcs->doMAM = getIntValue(value);	      }else if(strcmp(type,"doGAErrorBasedSelect")==0){		xcs->doGAErrorBasedSelect = getIntValue(value);	      }else if(strcmp(type,"delta")==0){		xcs->delta = getDoubleValue(value);	      }else if(strcmp(type,"thetaDel")==0){		xcs->thetaDel = getIntValue(value);	      }else if(strcmp(type,"deletionType")==0){		xcs->deletionType = getIntValue(value);	      }else if(strcmp(type,"dontCareProb")==0){		xcs->dontCareProb = getDoubleValue(value);	      }else if(strcmp(type,"doGASubsumption")==0){		xcs->doGASubsumption = getIntValue(value);	      }else if(strcmp(type,"doActionSetSubsumption")==0){		xcs->doActionSetSubsumption = getIntValue(value);	      }else if(strcmp(type,"thetaSub")==0){		xcs->thetaSub = getIntValue(value);	      }else if(strcmp(type,"exploreProb")==0){		xcs->exploreProb = getDoubleValue(value);	      }else if(strcmp(type,"teletransportation")==0){		xcs->teletransportation = getIntValue(value);	      }else if(strcmp(type,"initializePopulation")==0){		xcs->initializePopulation = getIntValue(value);	      }else{		/* if no string matches try to pass it to the environmental settings */		if(!setEnvParam(type, getDoubleValue(value))) 		  printf("Could not resolve line %s\n", s);	      }	    }	  }	}      }    }  }  return;}/** * Sets the xcs parameter values to the (default) macro values.  */void setMacroValues(struct XCS *xcs){  assert(( xcs->tabOutFile = (char *)calloc(strlen(TAB_OUT_FILE)+1, sizeof(char)))!=NULL);  strcpy(xcs->tabOutFile, TAB_OUT_FILE);  xcs->nrExps = NR_EXPS;  xcs->maxNrSteps = MAX_NR_STEPS;  xcs->testFrequency = TEST_FREQUENCY;  xcs->maxPopSize = MAX_POP_SIZE;  xcs->alpha = ALPHA;  xcs->beta = BETA;  xcs->gamma = GAMMA;  xcs->epsilon0 = EPSILON_0;  xcs->nu = (int)NU;  xcs->thetaGA = THETA_GA;  xcs->fitnessReduction = FITNESS_REDUCTION;  xcs->tournamentSize = TOURNAMENT_SIZE;  xcs->forceDifferentInTournament = FORCE_DIFFERENT_IN_TOURNAMENT;  xcs->selectTolerance = SELECT_TOLERANCE;  xcs->crossoverType = CROSSOVER_TYPE;  xcs->chiGA = CHI_GA;  xcs->muGA = MU_GA;  xcs->doGeneralizationMutation = DO_GENERALIZATION_MUTATION;  xcs->doNicheMutation = DO_NICHE_MUTATION;  xcs->doMAM = DO_MAM;  xcs->doGAErrorBasedSelect = DO_GA_ERROR_BASED_SELECT;  xcs->delta = DELTA;  xcs->thetaDel = THETA_DEL;  xcs->deletionType = DELETION_TYPE;  xcs->dontCareProb = DONT_CARE_PROB;  xcs->doGASubsumption = DO_GA_SUBSUMPTION;  xcs->doActionSetSubsumption = DO_ACTION_SET_SUBSUMPTION;  xcs->thetaSub = THETA_SUB;  xcs->exploreProb = EXPLORE_PROB;  xcs->teletransportation = TELETRANSPORTATION;  xcs->initializePopulation = INITIALIZE_POPULATION;}/** * Constructs a new XCS parameter struct and copies the old parameters.  */struct XCS *copyXCS(struct XCS *xcsOld){  struct XCS *xcs;  assert((xcs=( struct XCS *)calloc(1,sizeof(struct XCS)))!=NULL);  assert(( xcs->tabOutFile = (char *)calloc(strlen(xcsOld->tabOutFile)+1, sizeof(char)))!=NULL);  strcpy(xcs->tabOutFile, xcsOld->tabOutFile);  xcs->nrExps = xcsOld->nrExps;  xcs->maxNrSteps = xcsOld->maxNrSteps;  xcs->testFrequency = xcsOld->testFrequency;  xcs->maxPopSize = xcsOld->maxPopSize;  xcs->alpha = xcsOld->alpha;  xcs->beta = xcsOld->beta;  xcs->gamma = xcsOld->gamma;  xcs->epsilon0 = xcsOld->epsilon0;  xcs->nu = xcsOld->nu;  xcs->thetaGA = xcsOld->thetaGA;  xcs->fitnessReduction = xcsOld->fitnessReduction;  xcs->tournamentSize = xcsOld->tournamentSize;  xcs->forceDifferentInTournament = xcsOld->forceDifferentInTournament;  xcs->selectTolerance = xcsOld->selectTolerance;  xcs->crossoverType = xcsOld->crossoverType;  xcs->chiGA = xcsOld->chiGA;  xcs->muGA = xcsOld->muGA;  xcs->doGeneralizationMutation = xcsOld->doGeneralizationMutation;  xcs->doNicheMutation = xcsOld->doNicheMutation;  xcs->doMAM = xcsOld->doMAM;  xcs->doGAErrorBasedSelect = xcsOld->doGAErrorBasedSelect;  xcs->delta = xcsOld->delta;  xcs->thetaDel = xcsOld->thetaDel;  xcs->deletionType = xcsOld->deletionType;  xcs->dontCareProb = xcsOld->dontCareProb;  xcs->doGASubsumption = xcsOld->doGASubsumption;  xcs->doActionSetSubsumption = xcsOld->doActionSetSubsumption;  xcs->thetaSub = xcsOld->thetaSub;  xcs->exploreProb = xcsOld->exploreProb;  xcs->teletransportation = xcsOld->teletransportation;  xcs->initializePopulation = xcsOld->initializePopulation;  return xcs;}/** * Frees the XCS parameter struct. */void freeXCS(struct XCS *xcs){  free(xcs->tabOutFile);  free(xcs);}/** * Prints all XCS parameters  */void fprintXCS(FILE *outfile, struct XCS *xcs){  fprintf(outfile, "# tabOutFile %s nrExps %d maxNrSteps %d testFrequency %d maxPopSize %d\n", xcs->tabOutFile, xcs->nrExps, xcs->maxNrSteps, xcs->testFrequency, xcs->maxPopSize);  fprintf(outfile, "# alpha %f beta %f gamma %f epsilon0 %f nu %d\n", xcs->alpha, xcs->beta, xcs->gamma, xcs->epsilon0, xcs->nu);  fprintf(outfile, "# thetaGA %d fitnessReduction %f tournamentSize %4.2f selectTolerance %4.6f forceDifferentInTournament %4.2f crossoverType %d chiGA %f muGA %f doGeneralizationMutation %d doNicheMutation %d delta %f thetaDel %d deletionType %d \n", xcs->thetaGA, xcs->fitnessReduction, xcs->tournamentSize, xcs->selectTolerance, xcs->forceDifferentInTournament, xcs->crossoverType, xcs->chiGA, xcs->muGA, xcs->doGeneralizationMutation, xcs->doNicheMutation, xcs->delta, xcs->thetaDel, xcs->deletionType);  fprintf(outfile,"# doMAM %d doGAErrorBasedSelect %d dontCareProb %f doGASubsumption %d doActionSetSubsumption %d \n", xcs->doMAM, xcs->doGAErrorBasedSelect, xcs->dontCareProb, xcs->doGASubsumption, xcs->doActionSetSubsumption);  fprintf(outfile, "# thetaSub %d exploreProb %f teletransportation %d initializePopulation %d\n", xcs->thetaSub, xcs->exploreProb, xcs->teletransportation, xcs->initializePopulation);}

⌨️ 快捷键说明

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