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

📄 boolfktenv.c

📁 Simple GA code (Pascal code from Goldberg, D. E. (1989), Genetic Algorithms in Search, Optimization,
💻 C
📖 第 1 页 / 共 2 页
字号:
double doBiasedMultiplexerAction(char *state, int act, int *correct){  int i,place,increment,zeroone=0,placelocal=0;  char action='\0';  for(i=0,place=0; i<boolFuncProps->bmpX; i++){    if(state[i]=='1')      place += (int)(1<<(boolFuncProps->bmpX-i-1));  }  if(place >= ((int)(1<<(boolFuncProps->bmpX)))/2)    zeroone=1;  increment=((int)(1<<(boolFuncProps->bmpY))) - 1 + boolFuncProps->bmpY;  for(placelocal=0, i=0; i< boolFuncProps->bmpY; i++){    if(state[boolFuncProps->bmpX + place * increment + i]=='1')      placelocal += (int)(1<<(boolFuncProps->bmpY-i-1));  }  if(zeroone==0){    if(placelocal==0){      action='0';    }else{      placelocal--;      action=state[boolFuncProps->bmpX + place*increment + boolFuncProps->bmpY + placelocal];    }  }else{    if(placelocal == ((int)(1<<boolFuncProps->bmpY))-1){      action='1';    }else{      action=state[boolFuncProps->bmpX + place*increment + boolFuncProps->bmpY + placelocal];    }  }  /* printf("%s -> %c\n",state,action);*/ if((action=='1'&& act==1) || (action=='0' && act==0)){    *correct=1;    return boolFuncProps->paymentRange;  }else{    *correct=0;    return 0;  }}/** * Concatenated Multiplexer Function */double doConcatenatedMultiplexerAction(char *state, int act, int *correct){  int place=boolFuncProps->multiplexerBits;  int nrMPs = (int)((boolFuncProps->conditionLength)/(boolFuncProps->multiplexerBits + (1<<boolFuncProps->multiplexerBits)));  int length = boolFuncProps->multiplexerBits + (1<<boolFuncProps->multiplexerBits);  int i,j, nOne;  double reward;   *correct=1;  reward=0;  nOne=0;  for(i=0; i<nrMPs; i++) {    /* get the place of the spot referenced by the first index bits */    for(j=0, place=length*i+boolFuncProps->multiplexerBits; j < boolFuncProps->multiplexerBits; j++) {      if(state[length*i+j]=='1')	place += (int)(1<<((boolFuncProps->multiplexerBits)-1-j));    }    /* determine the corresponding reward and set 'correct' */    if(boolFuncProps->concatenatedMultiplexer==1) {      if((act==1 && state[place]=='1') || (act==0 && state[place]=='0')) {	/* the right action was chosen */	if(boolFuncProps->payoffLandscape)	  reward += 1000;      }else{	/* the wrong action was chosen */	*correct=0;	if(!boolFuncProps->payoffLandscape)	  break; /* if one is wrong and no payoff Landscape, all is wrong */      }    }else{      if(state[place]=='1') {	nOne++;      }    }  }  if(boolFuncProps->concatenatedMultiplexer==2) {    if((nOne>1 && act==1) || (nOne<2 && act==0)) {/*type with only one action*/      /*printf("Is correct %s - %s: %d\n",state, act, nOne);*/      *correct = 1;      reward = 1000;    }else{      *correct = 0;      reward = 0;    }    if(boolFuncProps->payoffLandscape) {      if(act==1)	reward = 1000 * nOne;      else	reward = 1000 * (nrMPs-nOne);    }  }else{    if(*correct && !boolFuncProps->payoffLandscape)      reward = 1000;    /* printf("%s with act %s was assigned %d with reward %f\n",state, act, *correct, reward);*/  }  return reward;}/* * Initialize the environment -> do nothing in the multiplexer environment  */int initEnv(FILE *fp){  assert((boolFuncProps = (struct booleanEnv *) calloc(1,sizeof(struct booleanEnv)))!=0);  boolFuncProps->constantFunction = CONSTANT_FUNCTION;  boolFuncProps->randomFunction = RANDOM_FUNCTION;  boolFuncProps->parityFunction = PARITY_FUNCTION;  boolFuncProps->multiplexerFunction = MULTIPLEXER_FUNCTION;  boolFuncProps->concatenatedMultiplexer = CONCATENATED_MULTIPLEXER;  boolFuncProps->biasedMultiplexer = BIASED_MULTIPLEXER;  boolFuncProps->countOnesFunction = COUNT_ONES_FUNCTION;  boolFuncProps->addNoiseToAction = ADD_NOISE_TO_ACTION;  boolFuncProps->actionNoiseMu = ACTION_NOISE_MU;  boolFuncProps->actionNoiseSigma = ACTION_NOISE_SIGMA;  boolFuncProps->samplingBias = SAMPLING_BIAS;  boolFuncProps->paymentRange = PAYMENT_RANGE;  boolFuncProps->conditionLength = CONDITION_LENGTH;  boolFuncProps->paritySize = PARITY_SIZE;  boolFuncProps->multiplexerBits = MULTIPLEXER_BITS;  boolFuncProps->payoffLandscape = PAYOFF_LANDSCAPE;  boolFuncProps->bmpX = BMP_X;  boolFuncProps->bmpY = BMP_Y;  boolFuncProps->countOnesSize = COUNT_ONES_SIZE;  boolFuncProps->countOnesType = COUNT_ONES_TYPE;  return 1;}/** * Print the environment constants to file. */void fprintEnv(FILE *outfile){  fprintf(outfile, "# constantFunction %d; randomFunction %d; parityFunction %d; multiplexerFunction %d; concatenatedMP %d; biasedMP %d; countOnesFunction %d; addNoiseToAction %f; actionNoiseMu %f; actionNoiseSigma %f; samplingBias %f;\n", boolFuncProps->constantFunction, boolFuncProps->randomFunction, boolFuncProps->parityFunction, boolFuncProps->multiplexerFunction, boolFuncProps->concatenatedMultiplexer, boolFuncProps->biasedMultiplexer, boolFuncProps->countOnesFunction, boolFuncProps->addNoiseToAction, boolFuncProps->actionNoiseMu, boolFuncProps->actionNoiseSigma, boolFuncProps->samplingBias);   fprintf(outfile, "# paymentRange %d; conditionLength %d; paritySize %d; multiplexerBits %d; payoffLandscape %d; bmpX %d; bmpY %d; countOnesSize %d; countOnesType %d;\n", boolFuncProps->paymentRange, boolFuncProps->conditionLength, boolFuncProps->paritySize, boolFuncProps->multiplexerBits, boolFuncProps->payoffLandscape,boolFuncProps->bmpX, boolFuncProps->bmpY, boolFuncProps->countOnesSize, boolFuncProps->countOnesType); }/** * Resets the all function selections to zero. */void newFunctionChoice(){  boolFuncProps->constantFunction = 0;  boolFuncProps->randomFunction = 0;  boolFuncProps->multiplexerFunction = 0;  boolFuncProps->concatenatedMultiplexer = 0;  boolFuncProps->biasedMultiplexer = 0;  boolFuncProps->countOnesFunction = 0;}/** * Tries to set parameter 'type' to value 'value. If 'type' does not exist, 0 is returned. */int setEnvParam(char *type, double value){  if(strcmp(type,"constantFunction")==0){    if((int)value){      boolFuncProps->paymentRange = PAYMENT_RANGE;      newFunctionChoice();    }    boolFuncProps->constantFunction = (int)value;  }else if(strcmp(type,"randomFunction")==0){    if((int)value){      boolFuncProps->paymentRange = PAYMENT_RANGE;      newFunctionChoice();    }    boolFuncProps->randomFunction = (int)value;  }else if(strcmp(type,"parityFunction")==0){    if((int)value){      boolFuncProps->paymentRange = PAYMENT_RANGE;      newFunctionChoice();    }    boolFuncProps->parityFunction = (int)value;  }else if(strcmp(type,"multiplexerFunction")==0){    if((int)value){      boolFuncProps->paymentRange = PAYMENT_RANGE;      if(boolFuncProps->payoffLandscape) /* redetermine payment range if set */	boolFuncProps->paymentRange = (1<<((boolFuncProps->multiplexerBits)+1))*100+200;      newFunctionChoice();    }    boolFuncProps->multiplexerFunction = (int)value;  }else if(strcmp(type,"concatenatedMultiplexer")==0) {    if((int)value) {      boolFuncProps->paymentRange = PAYMENT_RANGE;      if(boolFuncProps->payoffLandscape) /* redetermine payment range if set */	/* size of condition divided by size of one multiplexer */	boolFuncProps->paymentRange = 1000*(boolFuncProps->conditionLength) / (boolFuncProps->multiplexerBits + (1<<boolFuncProps->multiplexerBits));       newFunctionChoice();    }    boolFuncProps->concatenatedMultiplexer = (int)value;  }else if(strcmp(type,"biasedMultiplexer")==0){    if((int)value) {      boolFuncProps->paymentRange = PAYMENT_RANGE;      newFunctionChoice();    }    boolFuncProps->biasedMultiplexer = (int)value;  }else if(strcmp(type,"countOnesFunction")==0){    if((int)value){      boolFuncProps->paymentRange = PAYMENT_RANGE;      newFunctionChoice();    }    boolFuncProps->countOnesFunction = (int)value;  }else if(strcmp(type,"addNoiseToAction")==0){    boolFuncProps->addNoiseToAction = value;  }else if(strcmp(type,"actionNoiseMu")==0){    boolFuncProps->actionNoiseMu = value;  }else if(strcmp(type,"actionNoiseSigma")==0){    boolFuncProps->actionNoiseSigma = value;  }else if(strcmp(type,"samplingBias")==0){    boolFuncProps->samplingBias = value;  }else if(strcmp(type,"paymentRange")==0){    printf("Payment Range is set internally!\n");    /*boolFuncProps->paymentRange = (int)value;*/  }else if(strcmp(type,"conditionLength")==0){    boolFuncProps->conditionLength = (int)value;  }else if(strcmp(type,"paritySize")==0){    boolFuncProps->paritySize = (int)value;  }else if(strcmp(type,"multiplexerBits")==0){    boolFuncProps->multiplexerBits = (int)value;    if(boolFuncProps->payoffLandscape) { /* redetermine payment range if set */      if(boolFuncProps->multiplexerFunction)	boolFuncProps->paymentRange = (1<<((boolFuncProps->multiplexerBits)+1))*100+200;      else if(boolFuncProps->concatenatedMultiplexer)	boolFuncProps->paymentRange = 1000*(boolFuncProps->conditionLength)/	  (boolFuncProps->multiplexerBits + (1<<boolFuncProps->multiplexerBits));     }  }else if(strcmp(type,"payoffLandscape")==0){    boolFuncProps->payoffLandscape = (int)value;    if(boolFuncProps->payoffLandscape) { /* redetermine payment range if set */      if(boolFuncProps->multiplexerFunction)	boolFuncProps->paymentRange = (1<<((boolFuncProps->multiplexerBits)+1))*100+200;      else if(boolFuncProps->concatenatedMultiplexer)	boolFuncProps->paymentRange = 1000*(boolFuncProps->conditionLength)/	  (boolFuncProps->multiplexerBits + (1<<boolFuncProps->multiplexerBits));     }  }else if(strcmp(type,"bmpX")==0){    boolFuncProps->bmpX = (int)value;  }else if(strcmp(type,"bmpY")==0){    boolFuncProps->bmpY = (int)value;  }else if(strcmp(type,"countOnesSize")==0){    boolFuncProps->countOnesSize = (int)value;  }else if(strcmp(type,"countOnesType")==0){    boolFuncProps->countOnesType = (int)value;  }else{    printf("%s not supported in current environment\n",type);    return 0;  }  return 1;}/** * Default to free any alocated memory.  */void freeEnv(){  free(boolFuncProps);  return;}

⌨️ 快捷键说明

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