天天看點

簡單的遺傳算法源代碼

導讀:

  文章由算法源碼吧(www.sfcode.cn)收集

  這是一個非常簡單的遺傳算法源代碼,是由Denis Cormier (North Carolina State University)開發的,Sita S.Raghavan (University of North Carolina at Charlotte)修正。代碼保證盡可能少,實際上也不必查錯。對一特定的應用修正此代碼,使用者隻需改變常數的定義并且定義“評價函數”即可。注意代碼 的設計是求最大值,其中的目标函數隻能取正值;且函數值和個體的适應值之間沒有差別。該系統使用比率選擇、精華模型、單點雜交和均勻變異。如果用 Gaussian變異替換均勻變異,可能得到更好的效果。代碼沒有任何圖形,甚至也沒有螢幕輸出,主要是保證在平台之間的高可移植性。讀者可以從ftp.uncc.edu, 目錄 coe/evol中的檔案prog.c中獲得。要求輸入的檔案應該命名為‘gadata.txt’;系統産生的輸出檔案為‘galog.txt’。輸入的 檔案由幾行組成:數目對應于變量數。且每一行提供次序——對應于變量的上下界。如第一行為第一個變量提供上下界,第二行為第二個變量提供上下界,等等。

  

  #include

  #define POPSIZE 50

  #define MAXGENS 1000

  #define NVARS 3

  #define PXOVER 0.8

  #define PMUTATION 0.15

  #define TRUE 1

  #define FALSE 0

  int generation;

  int cur_best;

  FILE *galog;

  struct genotype

  {

  double gene[NVARS];

  double fitness;

  double upper[NVARS];

  double lower[NVARS];

  double rfitness;

  double cfitness;

  };

  struct genotype population[POPSIZE+1];

  struct genotype newpopulation[POPSIZE+1];

  void initialize(void);

  double randval(double, double);

  void evaluate(void);

  void keep_the_best(void);

  void elitist(void);

  void select(void);

  void crossover(void);

  void Xover(int,int);

  void swap(double *, double *);

  void mutate(void);

  void report(void);

  void initialize(void)

  FILE *infile;

  int i, j;

  double lbound, ubound;

  if ((infile = fopen("gadata.txt","r"))==NULL)

  fprintf(galog,"\nCannot open input file!\n");

  exit(1);

  }

  for (i = 0; i

  fscanf(infile, "%lf",&lbound);

  fscanf(infile, "%lf",&ubound);

  for (j = 0; j

  population[j].fitness = 0;

  population[j].rfitness = 0;

  population[j].cfitness = 0;

  population[j].lower[i] = lbound;

  population[j].upper[i]= ubound;

  population[j].gene[i] = randval(population[j].lower[i],

  population[j].upper[i]);

  fclose(infile);

  double randval(double low, double high)

  double val;

  val = ((double)(rand()%1000)/1000.0)*(high - low) + low;

  return(val);

  void evaluate(void)

  int mem;

  int i;

  double x[NVARS+1];

  for (mem = 0; mem

  x[i+1] = population[mem].gene[i];

  population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];

  void keep_the_best()

  cur_best = 0;

  if (population[mem].fitness >population[POPSIZE].fitness)

  cur_best = mem;

  population[POPSIZE].fitness = population[mem].fitness;

  population[POPSIZE].gene[i] = population[cur_best].gene[i];

  void elitist()

  double best, worst;

  int best_mem, worst_mem;

  best = population[0].fitness;

  worst = population[0].fitness;

  if(population[i].fitness >population[i+1].fitness)

  if (population[i].fitness >= best)

  best = population[i].fitness;

  best_mem = i;

  if (population[i+1].fitness <= worst)

  worst = population[i+1].fitness;

  worst_mem = i + 1;

  else

  if (population[i].fitness <= worst)

  worst = population[i].fitness;

  worst_mem = i;

  if (population[i+1].fitness >= best)

  best = population[i+1].fitness;

  best_mem = i + 1;

  if (best >= population[POPSIZE].fitness)

  population[POPSIZE].gene[i] = population[best_mem].gene[i];

  population[POPSIZE].fitness = population[best_mem].fitness;

  population[worst_mem].gene[i] = population[POPSIZE].gene[i];

  population[worst_mem].fitness = population[POPSIZE].fitness;

  void select(void)

  int mem, i, j, k;

  double sum = 0;

  double p;

  sum += population[mem].fitness;

  population[mem].rfitness = population[mem].fitness/sum;

  population[0].cfitness = population[0].rfitness;

  for (mem = 1; mem

  population[mem].cfitness = population[mem-1].cfitness +

  population[mem].rfitness;

  p = rand()%1000/1000.0;

  if (p

  newpopulation[i] = population[0];

  if (p >= population[j].cfitness &&

  p

  newpopulation[i] = population[j+1];

  population[i] = newpopulation[i];

  void crossover(void)

  int i, mem, one;

  int first = 0;

  double x;

  x = rand()%1000/1000.0;

  if (x

  ++first;

  if (first % 2 == 0)

  Xover(one, mem);

  one = mem;

  void Xover(int one, int two)

  int point;

  if(NVARS >1)

  if(NVARS == 2)

  point = 1;

  point = (rand() % (NVARS - 1)) + 1;

  swap(&population[one].gene[i], &population[two].gene[i]);

  void swap(double *x, double *y)

  double temp;

  temp = *x;

  *x = *y;

  *y = temp;

  void mutate(void)

  double lbound, hbound;

  lbound = population[i].lower[j];

  hbound = population[i].upper[j];

  population[i].gene[j] = randval(lbound, hbound);

  void report(void)

  double best_val;

  double avg;

  double stddev;

  double sum_square;

  double square_sum;

  double sum;

  sum = 0.0;

  sum_square = 0.0;

  sum += population[i].fitness;

  sum_square += population[i].fitness * population[i].fitness;

  avg = sum/(double)POPSIZE;

  square_sum = avg * avg * POPSIZE;

  stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));

  best_val = population[POPSIZE].fitness;

  fprintf(galog, "\n%5d, %6.3f, %6.3f, %6.3f \n\n", generation,

  best_val, avg, stddev);

  void main(void)

  if ((galog = fopen("galog.txt","w"))==NULL)

  generation = 0;

  fprintf(galog, "\n generation best average standard \n");

  fprintf(galog, " number value fitness deviation \n");

  initialize();

  evaluate();

  keep_the_best();

  while(generation

  generation++;

  select();

  crossover();

  mutate();

  report();

  elitist();

  fprintf(galog,"\n\n Simulation completed\n");

  fprintf(galog,"\n Best member: \n");

  fprintf (galog,"\n var(%d) = %3.3f",i,population[POPSIZE].gene[i]);

  fprintf(galog,"\n\n Best fitness = %3.3f",population[POPSIZE].fitness);

  fclose(galog);

  printf("Success\n");

本文轉自feisky部落格園部落格,原文連結:http://www.cnblogs.com/feisky/archive/2008/04/11/1586624.html,如需轉載請自行聯系原作者

繼續閱讀