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

📄 xcsmacros.c

📁 Simple GA code (Pascal code from Goldberg, D. E. (1989), Genetic Algorithms in Search, Optimization,
💻 C
字号:
/*/       (XCS-C 1.2)/	------------------------------------/	Learning Classifier System based on accuracy//     by /     Martin V. Butz/     Illinois Genetic Algorithms Laboratory (IlliGAL)/     University of Illinois at Urbana/Champaign/     butz@illigal.ge.uiuc.edu//     Last modified: 09-30-2003//     Utility functions like random number generator and double/int/string conversion.*/#include <stdio.h>#include <math.h>#include <string.h>#include <time.h>#include "xcsMacros.h"long _Q = _M/_A;     /* M / A */long _R = _M%_A;     /* M mod A */long _seed;          /* a number between 1 and m-1 */int haveUniNum=0;double uniNum=0;/* returns a floating-point random number generated according to uniform distribution from [0,1) */double urand(){  long lo,hi,test;    hi   = _seed / _Q;  lo   = _seed % _Q;  test = _A*lo - _R*hi;    if (test>0)    _seed = test;  else    _seed = test+_M;  return (double)(_seed)/_M;}/* returns a floating-point random number generated according to a normal distribution with  * mean 0 and standard deviation 1 */double nrand(){  float x1, x2, w;  if(haveUniNum){    haveUniNum=0;    return uniNum;  }else{    do {      x1 = 2.0 * urand() - 1.0;      x2 = 2.0 * urand() - 1.0;      w = x1 * x1 + x2 * x2;    } while ( w >= 1.0 );        w = sqrt( (-2.0 * log( w ) ) / w );    uniNum = x1 * w;    haveUniNum=1;    return x2 * w;  }  }/* sets the random seed */long  setSeed(long newSeed){  return (_seed = newSeed);}/** * randomizes the pseudo random generator  */void randomize(void){  setSeed((long)(time(NULL)%10000+1));}/** * returns int value of string - does not check for any incorrect format */int getIntValue(char *string){  int i, l, value=0;  l = strlen(string);  for(i=0; i<l; i++){    if(string[i]=='.' || string[i]==',')      break;    value = value*10 + (int)string[i]-(int)('0');  }  return value;}/** * returns double value of string - does not check for any incorrect format!  */double getDoubleValue(char *string){  int i, l;  double value=0., divider=0;  l = strlen(string);  for(i=0; i<l; i++){    if(string[i]=='.' || string[i]==',')      divider = 10.;    else if(divider==0)      value = value*10. + (double)((int)string[i]-(int)('0'));    else{      value += (double)((int)string[i]-(int)('0'))/divider;      divider*=10.;    }  }  return value;}

⌨️ 快捷键说明

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