数模论坛

 找回密码
 注-册-帐-号
搜索
热搜: 活动 交友 discuz
查看: 28470|回复: 21

遗传算法教程

  [复制链接]
发表于 2004-6-5 12:47:48 | 显示全部楼层 |阅读模式
遗传算法作为一种全局随机寻优的方法,是很有其特点的。
有兴趣的朋友请在这儿讨论。 有交流才有进步!
遗传算法(Genetic Algorithm, GA)是近几年发展起来的一种崭新的全局优化算法,
它借用了生物遗传学的观点,通过自然选择、遗传、变异等作用机制,实现各个
个体的适应性的提高。这一点体现了自然界中"物竞天择、适者生存"进化过程。
1962年Holland教授首次提出了GA算法的思想,从而吸引了大批的研究者,迅速
推广到优化、搜索、机器学习等方面,并奠定了坚实的理论基础。
用遗传算法解决问题时,首先要对待解决问题的模型结构和参数进行编码,一般
用字符串表示,这个过程就将问题符号化、离散化了。也有在连续空
间定义的GA(Genetic Algorithm in Continuous Space, GACS),暂不讨论。
一个串行运算的遗传算法(Seguential Genetic Algoritm, SGA)按如下过程进行:
(1) 对待解决问题进行编码;
(2) 随机初始化群体X(0):=(x1, x2, … xn);
(3) 对当前群体X(t)中每个个体xi计算其适应度F(xi),适应度表示了该个体的性
能好坏;
(4) 应用选择算子产生中间代Xr(t);
(5) 对Xr(t)应用其它的算子,产生新一代群体X(t+1),这些算子的目的在于扩展
有限个体的覆盖面,体现全局搜索的思想;
(6) t:=t+1;如果不满足终止条件继续(3)。
GA中最常用的算子有如下几种:
(1) 选择算子(selection/reproduction): 选择算子从群体中按某一概率成对
选择个体,某个体xi被选择的概率Pi与其适应度值成正比。最通常的实现
方法是轮盘赌(roulette wheel)模型。
(2) 交叉算子(Crossover): 交叉算子将被选中的两个个体的基因链按概率pc进
行交叉,生成两个新的个体,交叉位置是随机的。其中Pc是一个系统参数。
(3) 变异算子(Mutation): 变异算子将新个体的基因链的各位按概率pm进行变异,
对二值基因链(0,1编码)来说即是取反。
上述各种算子的实现是多种多样的,而且许多新的算子正在不断地提出,以改
进GA的某些性能。系统参数(个体数n,基因链长度l,交叉概率Pc,变异概率Pm等)对
算法的收敛速度及结果有很大的影响,应视具体问题选取不同的值。
GA的程序设计应考虑到通用性,而且要有较强的适应新的算子的能力。OOP中的
类的继承为我们提供了这一可能。
定义两个基本结构:基因(ALLELE)和个体(INDIVIDUAL),以个体的集合作为群体
类TPopulation的数据成员,而TSGA类则由群体派生出来,定义GA的基本操作。
对任一个应用实例,可以在TSGA类上派生,并定义新的操作。
TPopulation类包含两个重要过程:
FillFitness: 评价函数,对每个个体进行解码(decode)并计算出其适应度值,
具体操作在用户类中实现。
Statistic: 对当前群体进行统计,如求总适应度sumfitness、平均适应度
average、最好个体fmax、最坏个体fmin等。
TSGA类在TPopulation类的基础上派生,以GA的系统参数为构造函数的参数,
它有4个重要的成员函数:
Select: 选择算子,基本的选择策略采用轮盘赌模型(如图2)。轮盘经任意
旋转停止后指针所指向区域被选中,所以fi值大的被选中的概率就大。
Crossover: 交叉算子,以概率Pc在两基因链上的随机位置交换子串。
Mutation: 变异算子,以概率Pm对基因链上每一个基因进行随机干扰(取反)。
Generate: 产生下代,包括了评价、统计、选择、交叉、变异等全部过程,每
运行一次,产生新的一代。


 楼主| 发表于 2004-6-5 12:48:06 | 显示全部楼层
SGA的结构及类定义如下(用C++编写):

typedef char ALLELE; // 基因类型
typedef struct{
ALLELE *chrom;
float fitness; // fitness of Chromosome
}INDIVIDUAL; // 个体定义

class TPopulation{ // 群体类定义
public:
int size; // Size of population: n
int lchrom; // Length of chromosome: l
float sumfitness, average;
INDIVIDUAL *fmin, *fmax;
INDIVIDUAL *pop;

TPopulation(int popsize, int strlength);
~TPopulation();
inline INDIVIDUAL &Individual(int i){ return pop;};
void FillFitness(); // 评价函数
virtual void Statistics(); // 统计函数
};

class TSGA : public TPopulation{ // TSGA类派生于群体类
public:
float pcross; // Probability of Crossover
float pmutation; // Probability of Mutation
int gen; // Counter of generation

TSGA(int size, int strlength, float pm=0.03, float pc=0.6):
TPopulation(size, strlength)
{gen=0; pcross=pc; pmutation=pm; } ;
virtual INDIVIDUAL& Select();
virtual void Crossover(INDIVIDUAL &parent1, INDIVIDUAL &parent2,
INDIVIDUAL &child1, INDIVIDUAL &child2);
virtual ALLELE Mutation(ALLELE alleleval);
virtual void Generate(); // 产生新的一代
};


};


用户GA类定义如下:
class TSGAfit : public TSGA{
public:
TSGAfit(int size,float pm=0.0333,float pc=0.6):TSGA(size,24,pm,pc){};
void print();
};

由于GA是一个概率过程,所以每次迭代的情况是不一样的;系统参数不同,迭代
情况也不同。在实验中参数一般选取如下:个体数n=50-200,变异概率Pm=0.03, 交叉
概率Pc=0.6。变异概率太大,会导致不稳定。
 楼主| 发表于 2004-6-5 12:48:29 | 显示全部楼层
程序实例:(基于标准C)


//主程序

#include "sga.h"

main(argc,argv)
int argc;
char *argv[];
{
    struct individual *temp;
    FILE   *fopen();
    void   copyright();
    char   *malloc();


    /* determine input and output from program args */
    numfiles = argc - 1;
    switch(numfiles)
    {
        case 0:
            infp = stdin;
            outfp = stdout;
            break;
        case 1:
            if((infp = fopen(argv[1],"r")) == NULL)
            {
                fprintf(stderr,"Input file %s not found\n",argv[1]);
                exit(-1);
            }
            outfp = stdout;
            break;
        case 2:
            if((infp = fopen(argv[1],"r")) == NULL)
            {
                fprintf(stderr,"Cannot open input file %s\n",argv[1]);
                exit(-1);
            }
            if((outfp = fopen(argv[2],"w")) == NULL)
            {
                fprintf(stderr,"Cannot open output file %s\n",argv[2]);
                exit(-1);
            }
            break;
        default:
            fprintf(stderr,"Usage is: sga [input file] [output file]\n");
            exit(-1);
    }


    /* print the author/copyright notice */
    copyright();

    if(numfiles == 0)
        fprintf(outfp," Number of GA runs to be performed-> ");
    fscanf(infp,"%d",&maxruns);

    for(run=1; run<=maxruns; run++)
    {
        /* Set things up */
        initialize();

        for(gen=0; gen<maxgen; gen++)
        {
            fprintf(outfp,"\nRUN %d of %d: GENERATION %d->%d\n",
                           run,maxruns,gen,maxgen);

            /*application dependent routines*/
            application();

            /* create a new generation */
            generation();


            /* compute fitness statistics on new populations */
            statistics(newpop);

            /* report results for new generation */
            report();

            /* advance the generation */
            temp = oldpop;
            oldpop = newpop;
            newpop = temp;
        }
        freeall();
    }
}


 楼主| 发表于 2004-6-5 12:48:48 | 显示全部楼层
//头文件sga.h
/*--------------------------------------------------------------------------
--*/
/* sga.h - global declarations for main(), all variable declared herein must
  */
/*         also be defined as extern variables in external.h !!!
  */
/*--------------------------------------------------------------------------
--*/
#define LINELENGTH 80                                    /* width of printou
t */
#define BITS_PER_BYTE 8            /* number of bits per byte on this machin
e */
#define UINTSIZE (BITS_PER_BYTE*sizeof(unsigned))    /* # of bits in unsigne
d */
#include <stdio.h>
/* file pointers */
FILE *outfp, *infp;
/* Global structures and variables */
struct individual
{
    unsigned *chrom;                  /* chromosome string for the individua
l */
    double   fitness;                            /* fitness of the individua
l */
    int      xsite;                               /* crossover site at matin
g */
    int      parent[2];                  /* who the parents of offspring wer
e */
    int      *utility;           /* utility field can be used as pointer to
a */
                /* dynamically allocated, application-specific data structur
e */
};
struct bestever
{
    unsigned *chrom;        /* chromosome string for the best-ever individua
l */
    double   fitness;                  /* fitness of the best-ever individua
l */
    int      generation;                      /* generation which produced i
t */
};
struct individual *oldpop;                  /* last generation of individual
s */
struct individual *newpop;                  /* next generation of individual
s */
struct bestever bestfit;                         /* fittest individual so fa
r */
double sumfitness;                    /* summed fitness for entire populatio
n */
double max;                                  /* maximum fitness of populatio
n */
double avg;                                  /* average fitness of populatio
n */
double min;                                  /* minumum fitness of populatio
n */
float  pcross;                                    /* probability of crossove
r */
float  pmutation;                                  /* probability of mutatio
n */
int    numfiles;                                      /* number of open file
s */
int    popsize;                                            /* population siz
e */
int    lchrom;                     /* length of the chromosome per individua
l */
int    chromsize;            /* number of bytes needed to store lchrom strin
g */
int    gen;                                      /* current generation numbe
r */
int    maxgen;                                   /* maximum generation numbe
r */
int    run;                                             /* current run numbe
r */
int    maxruns;                             /* maximum number of runs to mak
e */
int    printstrings = 1;     /* flag to print chromosome strings (default on
) */
int    nmutation;                             /* number of mutations */
int    ncross;                               /* number of crossovers */
/* Application-dependent declarations go after here... */

 楼主| 发表于 2004-6-5 12:49:28 | 显示全部楼层
//头文件external.h
/*--------------------------------------------------------------------------
--*/
/* external.h - external global declarations from sga.h.
  */
/*--------------------------------------------------------------------------
--*/
#define LINELENGTH 80                                    /* width of printou
t */
#define BITS_PER_BYTE 8            /* number of bits per byte on this machin
e */
#define UINTSIZE (BITS_PER_BYTE*sizeof(unsigned))    /* # of bits in unsigne
d */
#include <stdio.h>
/* file pointers */
FILE *outfp, *infp;
/* Global structures and variables */
struct individual
{
    unsigned *chrom;                  /* chromosome string for the individua
l */
    double   fitness;                            /* fitness of the individua
l */
    int      xsite;                               /* crossover site at matin
g */
    int      parent[2];                  /* who the parents of offspring wer
e */
    int      *utility;           /* utility field can be used as pointer to
a */
                /* dynamically allocated, application-specific data structur
e */
};
struct bestever
{
    unsigned *chrom;        /* chromosome string for the best-ever individua
l */
    double   fitness;                  /* fitness of the best-ever individua
l */
    int      generation;                      /* generation which produced i
t */
};
extern struct individual *oldpop;           /* last generation of individual
s */
extern struct individual *newpop;           /* next generation of individual
s */
extern struct bestever bestfit;                  /* fittest individual so fa
r */
extern double sumfitness;             /* summed fitness for entire populatio
n */
extern double max;                           /* maximum fitness of populatio
n */
extern double avg;                           /* average fitness of populatio
n */
extern double min;                           /* minumum fitness of populatio
n */
extern float  pcross;                             /* probability of crossove
r */
extern float  pmutation;                           /* probability of mutatio
n */
extern int    numfiles;                               /* number of open file
s */
extern int    popsize;                                     /* population siz
e */
extern int    lchrom;              /* length of the chromosome per individua
l */
extern int    chromsize;     /* number of bytes needed to store lchrom strin
g */
extern int    gen;                               /* current generation numbe
r */
extern int    maxgen;                            /* maximum generation numbe
r */
extern int    run;                                      /* current run numbe
r */
extern int    maxruns;                      /* maximum number of runs to mak
e */
extern int    printstrings;  /* flag to print chromosome strings (default on
) */
extern int    nmutation;                      /* number of mutations  */
extern int    ncross;                        /* number of crossovers  */
/* Application-dependent external declarations go after here...  */


 楼主| 发表于 2004-6-5 12:49:48 | 显示全部楼层
//初始化
/*--------------------------------------------------------------------------

--*/
/* initial.c - functions to get things set up and initialized
  */
/*--------------------------------------------------------------------------

--*/
#include "external.h"
initialize()
/* Initialization Coordinator */
{
    /* get basic problem values from input file */
    initdata();
    /* define chromosome size in terms of machine bytes, ie  */
    /* length of chromosome in bits (lchrom)/(bits-per-byte) */
    /* chromsize must be known for malloc() of chrom pointer */
    chromsize = (lchrom/UINTSIZE);
    if(lchrom%UINTSIZE) chromsize++;
    /* malloc space for global data structures */
    initmalloc();
    /* initialize application dependent variables*/
    app_init();
    /* initialize random number generator */
    randomize();
    /* initialize global counters/values */
    nmutation = 0;
    ncross = 0;
    bestfit.fitness = 0.0;
    bestfit.generation = 0;
    /* initialize the populations and report statistics */
    initpop();
    statistics(oldpop);
    initreport();
}
initdata()
/* data inquiry and setup */
{
    char  answer[2];
    if(numfiles == 0)
    {
        fprintf(outfp,"\n ------- SGA Data Entry and Initialization -------\

n");
        fprintf(outfp," Enter the population size ------------> ");
    }
    fscanf(infp,"%d", &popsize);
    if((popsize%2) != 0)
      {
        fprintf(outfp, "Sorry! only even population sizes are allowed. \n In
crem
nt
ing popsize by one.\n");
        popsize++;
      };
    if(numfiles == 0)
        fprintf(outfp," Enter chromosome length --------------> ");
    fscanf(infp,"%d", &lchrom);
    if(numfiles == 0)
        fprintf(outfp," Print chromosome strings? (y/n) ------> ");
    fscanf(infp,"%s",answer);
    if(strncmp(answer,"n",1) == 0) printstrings = 0;
    if(numfiles == 0)
        fprintf(outfp," Enter maximum number of generations --> ");
    fscanf(infp,"%d", &maxgen);
    if(numfiles == 0)
        fprintf(outfp," Enter crossover probability ----------> ");
    fscanf(infp,"%f", &pcross);
    if(numfiles == 0)
        fprintf(outfp," Enter mutation probability -----------> ");
    fscanf(infp,"%f", &pmutation);
    /* any application-dependent global input */
    app_data();
}
initpop()
/* Initialize a population at random */
{
    int j, j1, k, stop;
    unsigned mask = 1;
    for(j = 0; j < popsize; j++)
    {
        for(k = 0; k < chromsize; k++)
        {
            oldpop[j].chrom[k] = 0;
            if(k == (chromsize-1))
                stop = lchrom - (k*UINTSIZE);
            else
                stop = UINTSIZE;
            /* A fair coin toss */
            for(j1 = 1; j1 <= stop; j1++)
            {
               oldpop[j].chrom[k] = oldpop[j].chrom[k]<<1;
               if(flip(0.5))
                  oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;
            }
        }
        oldpop[j].parent[0] = 0; /* Initialize parent info. */
        oldpop[j].parent[1] = 0;
        oldpop[j].xsite = 0;
        objfunc(&(oldpop[j]));  /* Evaluate initial fitness */
    }
}
initreport()
/* Initial report */
{
    void   skip();
    skip(outfp,1);
    fprintf(outfp," SGA Parameters\n");
    fprintf(outfp," -------------------------------------------------\n");
    fprintf(outfp," Total Population size              =   %d\n",popsize);
    fprintf(outfp," Chromosome length (lchrom)         =   %d\n",lchrom);
    fprintf(outfp," Maximum # of generations (maxgen)  =   %d\n",maxgen);
    fprintf(outfp," Crossover probability (pcross)     = %f\n", pcross);
    fprintf(outfp," Mutation  probability (pmutation)  = %f\n", pmutation);
    skip(outfp,1);
    /* application dependant report */
    app_initreport();
    fflush(outfp);
}
 楼主| 发表于 2004-6-5 12:50:04 | 显示全部楼层
//繁衍
/*--------------------------------------------------------------------------
--*/
/* generate.c - create a new generation of individuals
  */
/*--------------------------------------------------------------------------
--*/
#include "external.h"
generation()
{
  int mate1, mate2, jcross, j = 0;
  /* perform any preselection actions necessary before generation */
  preselect();
  /* select, crossover, and mutation */
  do
    {
      /* pick a pair of mates */
      mate1 = select();
      mate2 = select();
      /* Crossover and mutation */
      jcross = crossover(oldpop[mate1].chrom, oldpop[mate2].chrom,
                         newpop[j].chrom, newpop[j+1].chrom);
      mutation(newpop[j].chrom);
      mutation(newpop[j+1].chrom);
      /* Decode string, evaluate fitness, & record */
      /* parentage date on both children */
      objfunc(&(newpop[j]));
      newpop[j].parent[0] = mate1+1;
      newpop[j].xsite = jcross;
      newpop[j].parent[1] = mate2+1;
      objfunc(&(newpop[j+1]));
      newpop[j+1].parent[0] = mate1+1;
      newpop[j+1].xsite = jcross;
      newpop[j+1].parent[1] = mate2+1;
      /* Increment population index */
      j = j + 2;
    }
  while(j < (popsize-1));
}

 楼主| 发表于 2004-6-5 12:50:44 | 显示全部楼层


//一些不重要的函数
/*--------------------------------------------------------------------------
--*/
/* utility.c - utility routines, contains copyright,  repchar, skip
  */
/*--------------------------------------------------------------------------
--*/
#include "external.h"
void copyright()
{
    void repchar(), skip();
    int iskip;
    int ll = 59;
    iskip = (LINELENGTH - ll)/2;
    skip(outfp,1);
    repchar(outfp," ",iskip); repchar(outfp,"-",ll); skip(outfp,1);
    repchar(outfp," ",iskip);
    fprintf(outfp,"|        SGA-C (v1.1) - A Simple Genetic Algorithm
  |\n");
    repchar(outfp," ",iskip);
    fprintf(outfp,"|     (c) David E. Goldberg 1986, All Rights Reserved
  |\n");
    repchar(outfp," ",iskip);
    fprintf(outfp,"|        C version by Robert E. Smith, U. of Alabama
  |\n");
    repchar(outfp," ",iskip);
    fprintf(outfp,"|    v1.1 modifications by Jeff Earickson, Boeing Company
  |\n");
    repchar(outfp," ",iskip); repchar(outfp,"-",ll); skip(outfp,2);
}
void repchar (outfp,ch,repcount)
/* Repeatedly write a character to stdout */
FILE *outfp;
char *ch;
int repcount;
{
    int j;
    for (j = 1; j <= repcount; j++) fprintf(outfp,"%s", ch);
}
void skip(outfp,skipcount)
/* Skip skipcount lines */
FILE *outfp;
int skipcount;
{
    int j;
    for (j = 1; j <= skipcount; j++) fprintf(outfp,"\n");
}
int ithruj2int(i,j,from)
/* interpret bits i thru j of a individual as an integer      */
/* j MUST BE greater than or equal to i AND j-i < UINTSIZE-1  */
/* from is a chromosome, represented as an array of unsigneds */
int i,j;
unsigned *from;
{
    unsigned mask, temp;
    int bound_flag;
    int iisin, jisin;
    int i1, j1, out;
    if(j < i)
    {
        fprintf(stderr,"Error in ithruj2int: j < i\n");
        exit(-1);
    }
    if(j-i+1 > UINTSIZE)
    {
        fprintf(stderr,"Error in ithruj2int: j-i+1 > UINTSIZE\n");
        exit(-1);
    }
    iisin = i/UINTSIZE;
    jisin = j/UINTSIZE;
    i1 = i - (iisin*UINTSIZE);
    j1 = j - (jisin*UINTSIZE);
        if(i1 == 0)
        {
                iisin = iisin-1;
                i1 = i - (iisin*UINTSIZE);
        };
        if(j1 == 0)
        {
                jisin = jisin-1;
                j1 = j - (jisin*UINTSIZE);
        };
    /* check if bits fall across a word boundary */
    if(iisin == jisin)
        bound_flag = 0;
    else
        bound_flag = 1;
    if(bound_flag == 0)
    {
        mask = 1;
        mask = (mask<<(j1-i1+1))-1;
        mask = mask<<(i1-1);
        out = (from[iisin]&mask)>>(i1-1);
        return(out);
    }
    else
    {
        mask = 1;
        mask = (mask<<j1)-1;
        temp = from[jisin]&mask;
        mask = 1;
        mask = (mask<<(UINTSIZE-i1+1))-1;
        mask = mask<<(i1-1);
        out = ((from[iisin]&mask)>>(i1-1)) | (temp<<(UINTSIZE-i1+1));
        return(out);
    }
}

 楼主| 发表于 2004-6-5 12:51:02 | 显示全部楼层
//适应度统计
/*--------------------------------------------------------------------------
--*/
/* statistic.c - compute the fitness statistics
  */
/*--------------------------------------------------------------------------
--*/
#include "external.h"
statistics(pop)
/* Calculate population statistics */
struct individual *pop;
{
    int i, j;
    sumfitness = 0.0;
    min = pop[0].fitness;
    max = pop[0].fitness;
    /* Loop for max, min, sumfitness */
    for(j = 0; j < popsize; j++)
    {
        sumfitness = sumfitness + pop[j].fitness;               /* Accumulat
e */
        if(pop[j].fitness > max) max = pop[j].fitness;         /* New maximu
m */
        if(pop[j].fitness < min) min = pop[j].fitness;         /* New minimu
m */
        /* new global best-fit individual */
        if(pop[j].fitness > bestfit.fitness)
          {
            for(i = 0; i < chromsize; i++)
              bestfit.chrom      = pop[j].chrom;
            bestfit.fitness    = pop[j].fitness;
            bestfit.generation = gen;
          }
      }
    /* Calculate average */
    avg = sumfitness/popsize;
    /* get application dependent stats */
    app_stats(pop);
}

 楼主| 发表于 2004-6-5 12:51:16 | 显示全部楼层
//选择
/*--------------------------------------------------------------------------
--*/
/* rselect.c:  roulette wheel selection.
  */
/*--------------------------------------------------------------------------
--*/
#include "external.h"
select_memory()
{
}
select_free()
{
}
preselect()
{
    int j;
    sumfitness = 0;
    for(j = 0; j < popsize; j++) sumfitness += oldpop[j].fitness;
}
int select()
/* roulette-wheel selection 轮盘赌选择*/
{
    extern float randomperc();
    float sum, pick;
    int i;
    pick = randomperc();
    sum = 0;
    if(sumfitness != 0)
    {
        for(i = 0; (sum < pick) && (i < popsize); i++)
            sum += oldpop.fitness/sumfitness;
    }
    else
        i = rnd(1,popsize);
    return(i-1);
}
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

小黑屋|手机版|Archiver|数学建模网 ( 湘ICP备11011602号 )

GMT+8, 2024-3-29 05:44 , Processed in 0.056234 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表