一段遺傳演算法的代碼

來源:互聯網
上載者:User

figure(1);
fplot('variable.*sin(10*pi*variable)+2.0',[-1,2]);   %畫出函數曲線
%定義遺傳演算法參數
NIND=40;        %個體數目(Number of individuals)
MAXGEN=25;      %最大遺傳代數(Maximum number of generations)
PRECI=20;       %變數的二進位位元(Precision of variables)
GGAP=0.9;       %代溝(Generation gap)
trace=zeros(2, MAXGEN);                        %尋優結果的初始值
FieldD=[20;-1;2;1;0;1;1];                      %地區描述器(Build field descriptor)
Chrom=crtbp(NIND, PRECI);                      %初始種群
gen=0;                                         %代計數器
variable=bs2rv(Chrom, FieldD);                 %計算初始種群的十進位轉換
ObjV=variable.*sin(10*pi*variable)+2.0;        %計算目標函數值
while gen<MAXGEN
   FitnV=ranking(-ObjV);                                  %分配適應度值(Assign fitness values)        
   SelCh=select('sus', Chrom, FitnV, GGAP);               %選擇
   SelCh=recombin('xovsp', SelCh, 0.7);                   %重組
   SelCh=mut(SelCh);                                      %變異
   variable=bs2rv(SelCh, FieldD);                         %子代個體的十進位轉換
   ObjVSel=variable.*sin(10*pi*variable)+2.0;             %計運算元代的目標函數值
   [Chrom ObjV]=reins(Chrom, SelCh, 1, 1, ObjV, ObjVSel); %重插入子代的新種群
   variable=bs2rv(Chrom, FieldD);
   gen=gen+1;                                             %代計數器增加
   %輸出最優解及其序號,並在目標函數映像中標出,Y為最優解,I為種群的序號
   [Y, I]=max(ObjV);hold on;
   plot(variable(I), Y, 'bo');
   trace(1, gen)=max(ObjV);                               %遺傳演算法效能跟蹤
   trace(2, gen)=sum(ObjV)/length(ObjV);
end
variable=bs2rv(Chrom, FieldD);                            %最優個體的十進位轉換
hold on  grid;
plot(variable,ObjV,'b*');
figure(2);
plot(trace(1,:));
hold on;
plot(trace(2,:),'-.');grid
legend('解的變化','種群均值的變化')

 

 

 

 

CRTBP.m - Create an initial population
 
  This function creates a binary population of given size and structure.
 
  Syntax: [Chrom Lind BaseV] = crtbp(Nind, Lind, Base)
 
  Input Parameters:
 
   Nind - Either a scalar containing the number of individuals
          in the new population or a row vector of length two
          containing the number of individuals and their length.
 
   Lind - A scalar containing the length of the individual
         chromosomes.
 
   Base - A scalar containing the base of the chromosome
          elements or a row vector containing the base(s)
         of the loci of the chromosomes.
 
  Output Parameters:
 
   Chrom - A matrix containing the random valued chromosomes
          row wise.
 
   Lind - A scalar containing the length of the chromosome.
 
   BaseV - A row vector containing the base of the
         chromosome loci.
 
  Author: Andrew Chipperfield
  Date: 19-Jan-94
 
  Tested under MATLAB v6 by Alex Shenfield (20-Jan-03)

       BS2RV.m - Binary string to real vector
 
  This function decodes binary chromosomes into vectors of reals. The
  chromosomes are seen as the concatenation of binary strings of given
  length, and decoded into real numbers in a specified interval using
  either standard binary or Gray decoding.
 
  Syntax:       Phen = bs2rv(Chrom,FieldD)
 
  Input parameters:
 
                Chrom    - Matrix containing the chromosomes of the current
                           population. Each line corresponds to one
                           individual's concatenated binary string
                  representation. Leftmost bits are MSb and
                   rightmost are LSb.
 
                FieldD   - Matrix describing the length and how to decode
                  each substring in the chromosome. It has the
                  following structure:
 
     [len;  (num)
      lb;  (num)
      ub;  (num)
      code;  (0=binary     | 1=gray)
      scale;  (0=arithmetic | 1=logarithmic)
      lbin;  (0=excluded   | 1=included)
      ubin];  (0=excluded   | 1=included)
 
       where
     len   - row vector containing the length of
         each substring in Chrom. sum(len)
          should equal the individual length.
     lb,
     ub    - Lower and upper bounds for each
          variable.
     code  - binary row vector indicating how each
          substring is to be decoded.
     scale - binary row vector indicating where to
          use arithmetic and/or logarithmic
          scaling.
     lbin,
     ubin  - binary row vectors indicating whether
          or not to include each bound in the
          representation range
 
  Output parameter:
 
                Phen     - Real matrix containing the population phenotypes.
 
  Author: Carlos Fonseca,  Updated: Andrew Chipperfield,  
  Date: 08/06/93,      Date: 26-Jan-94,
 
  Tested under MATLAB v6 by Alex Shenfield (17-Jan-03)       RANKING.M      (RANK-based fitness assignment)
 
  This function performs ranking of individuals.
 
  Syntax:  FitnV = ranking(ObjV, RFun, SUBPOP)
 
  This function ranks individuals represented by their associated
  cost, to be *minimized*, and returns a column vector FitnV
  containing the corresponding individual fitnesses. For multiple
  subpopulations the ranking is performed separately for each
  subpopulation.
 
  Input parameters:
     ObjV      - Column vector containing the objective values of the
                 individuals in the current population (cost values).
     RFun      - (optional) If RFun is a scalar in [1, 2] linear ranking is
                 assumed and the scalar indicates the selective pressure.
                 If RFun is a 2 element vector:
                 RFun(1): SP - scalar indicating the selective pressure
                 RFun(2): RM - ranking method
                          RM = 0: linear ranking
                          RM = 1: non-linear ranking
                 If RFun is a vector with length(Rfun) > 2 it contains
                 the fitness to be assigned to each rank. It should have
                 the same length as ObjV. Usually RFun is monotonously
                 increasing.
                 If RFun is omitted or NaN, linear ranking
                 and a selective pressure of 2 are assumed.
     SUBPOP    - (optional) Number of subpopulations
                 if omitted or NaN, 1 subpopulation is assumed
 
  Output parameters:
     FitnV     - Column vector containing the fitness values of the
                 individuals in the current population.
                
 
  Author:     Hartmut Pohlheim (Carlos Fonseca)
  History:    01.03.94     non-linear ranking
              10.03.94     multiple populations
              21.01.03     updated for MATLAB v6 by Alex Shenfield       SELECT.M          (universal SELECTion)
 
  This function performs universal selection. The function handles
  multiple populations and calls the low level selection function
  for the actual selection process.
 
  Syntax:  SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP)
 
  Input parameters:
     SEL_F     - Name of the selection function
     Chrom     - Matrix containing the individuals (parents) of the current
                 population. Each row corresponds to one individual.
     FitnV     - Column vector containing the fitness values of the
                 individuals in the population.
     GGAP      - (optional) Rate of individuals to be selected
                 if omitted 1.0 is assumed
     SUBPOP    - (optional) Number of subpopulations
                 if omitted 1 subpopulation is assumed
 
  Output parameters:
     SelCh     - Matrix containing the selected individuals.
 
  Author:     Hartmut Pohlheim
  History:    10.03.94     file created
              22.01.03     tested under MATLAB v6 by Alex Shenfield       RECOMBIN.M       (RECOMBINation high-level function)
 
  This function performs recombination between pairs of individuals
  and returns the new individuals after mating. The function handles
  multiple populations and calls the low-level recombination function
  for the actual recombination process.
 
  Syntax:  NewChrom = recombin(REC_F, OldChrom, RecOpt, SUBPOP)
 
  Input parameters:
     REC_F     - String containing the name of the recombination or
                 crossover function
     Chrom     - Matrix containing the chromosomes of the old
                 population. Each line corresponds to one individual
     RecOpt    - (optional) Scalar containing the probability of
                 recombination/crossover occurring between pairs
                 of individuals.
                 if omitted or NaN, 1 is assumed
     SUBPOP    - (optional) Number of subpopulations
                 if omitted or NaN, 1 subpopulation is assumed
 
  Output parameter:
     NewChrom  - Matrix containing the chromosomes of the population
                 after recombination in the same format as OldChrom.
 
   Author:    Hartmut Pohlheim
   History:   18.03.94     file created
              22.01.03     tested under MATLAB v6 by Alex Shenfield
                           (NOTE : doesn't work with low level recmut.m)       MUT.m
 
  This function takes the representation of the current population,
  mutates each element with given probability and returns the resulting
  population.
 
  Syntax: NewChrom = mut(OldChrom,Pm,BaseV)
 
  Input parameters:
 
   OldChrom - A matrix containing the chromosomes of the
       current population. Each row corresponds to
       an individuals string representation.
 
   Pm  - Mutation probability (scalar). Default value
       of Pm = 0.7/Lind, where Lind is the chromosome
       length is assumed if omitted.
 
   BaseV  - Optional row vector of the same length as the
       chromosome structure defining the base of the
       individual elements of the chromosome. Binary
       representation is assumed if omitted.
 
  Output parameter:
 
   NewChrom - A Matrix containing a mutated version of
       OldChrom.
 
  Author: Andrew Chipperfield
  Date: 25-Jan-94
 
  Tested under MATLAB v6 by Alex Shenfield (21-Jan-03)       REINS.M        (RE-INSertion of offspring in population replacing parents)
 
  This function reinserts offspring in the population.
 
  Syntax: [Chrom, ObjVCh] = reins(Chrom, SelCh, SUBPOP, InsOpt, ObjVCh, ObjVSel)
 
  Input parameters:
     Chrom     - Matrix containing the individuals (parents) of the current
                 population. Each row corresponds to one individual.
     SelCh     - Matrix containing the offspring of the current
                 population. Each row corresponds to one individual.
     SUBPOP    - (optional) Number of subpopulations
                 if omitted or NaN, 1 subpopulation is assumed
     InsOpt    - (optional) Vector containing the insertion method parameters
                 ExOpt(1): Select - number indicating kind of insertion
                           0 - uniform insertion
                           1 - fitness-based insertion
                           if omitted or NaN, 0 is assumed
                 ExOpt(2): INSR - Rate of offspring to be inserted per
                           subpopulation (% of subpopulation)
                           if omitted or NaN, 1.0 (100%) is assumed
     ObjVCh    - (optional) Column vector containing the objective values
                 of the individuals (parents - Chrom) in the current
                 population, needed for fitness-based insertion
                 saves recalculation of objective values for population
     ObjVSel   - (optional) Column vector containing the objective values
                 of the offspring (SelCh) in the current population, needed for
                 partial insertion of offspring,
                 saves recalculation of objective values for population
 
  Output parameters:
     Chrom     - Matrix containing the individuals of the current
                 population after reinsertion.
     ObjVCh    - if ObjVCh and ObjVSel are input parameters, then column
                 vector containing the objective values of the individuals
                 of the current generation after reinsertion.
           
  Author:     Hartmut Pohlheim
  History:    10.03.94     file created
              19.03.94     parameter checking improved
              26.01.03     tested under MATLAB v6 by Alex Shenfield

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.