Function Values most genetic algorithms

Function Values ​​most genetic algorithms

0 Preface

It has a function:

f(x) = x + 10*sin(5*x) + 7*cos(4*x);

It is easy to draw the image, as follows:

Function image

First requires selecting the maximum value of the function, the reader may already have very many ideas, this article describes how the genetic algorithm to solve such problems.

1. Introduction to Genetic Algorithms

If you do not care about the implementation details of the algorithm, then, genetic algorithms can use the following process description.

Genetic algorithm flow chart

This is a basic flow chart drawing natural evolution of biological populations and abstract obtained. The following are the various steps of the flowchart explained.

coding

As we all know, the organism has stored its genetic information of material - chromosomes, chromosome record of genetic information - a genetically determined trait performance of individual organisms, it may simply be considered a chromosome uniquely identify a biological individual .

Chromosome is essentially a protein, its structure is very complex, computer simulation is unrealistic. Therefore, genetic algorithm simply regarded as chromosomal sequence consisting of a string of binary digits (ie taken from the set of all elements of the sequence {0,1}).

Suppose organisms named A, chromosomal sequences which predetermined length is 8, then you can obtain an individual:

[0,1,0,0,1,0,1,1]

Initial population

Obviously not contain a biological population of only one individual, it can be specified the size of the population, in order to illustrate the principles of convenience, it is the provisions of its size 4. Initial population may be generated randomly. as follows:

[1,1,1,1,1,1,0,0]
[1,1,1,1,0,1,1,0]
[1,0,1,0,0,0,0,1]
[0,0,0,1,0,1,0,1]

Individual fitness assessments in the population

With the rise and fall of the existence of biological populations depends on its ability to adapt to the environment. For individual sequence representing binary bits, you can always find a function to evaluate the ability of individuals to adapt to the environment. Functions can be defined:

f(x) = int(x)
# x是输入的一个个体,如[0,1,0,0,1,0,1,1]
# 返回值是该二进制序列表示的整数

In addition the above-described function may also return a predetermined value (an integer), the higher the fitness of an individual. Obviously fitness is the highest individual:

[1,1,1,1,1,1,1,1]

select

Options selected for a higher probability of survival in a population of individuals, these individuals form a new population. Obviously the higher the higher the individual fitness of individual survival probability. So you can use roulette algorithm to elect a new population. Key principles of roulette algorithm is not of this article, is omitted, its implementation code, see below.

cross

Crossover is the abstraction of the genetic process. There are two individual organisms, these two reproductive individuals of the reproductive process, it will cross chromosomes exchange, generate new chromosomes, multiply come new individuals carrying the new chromosome.

It is provided with two individual organisms as follows:

[1,1,1,1,1,1,0,0]
[1,1,1,1,0,1,1,0]

Provided the two have a certain individual organism crossover probability, the crossover must occur at a certain position in the sequence of bits. Select this position is also random. Suppose the bit 5 (counting from 1) start to overlap, the following steps:

[1,1,1,1,1,1,0,0]
[1,1,1,1,0,1,1,0]
->
[1,1,1,1,1],[1,0,0]
[1,1,1,1,0],[1,1,0]
->
[1,1,1,1,1],[1,1,0]
[1,1,1,1,0],[1,0,0]
->
[1,1,1,1,1,1,1,0]
[1,1,1,1,0,1,0,0]

variation

Organisms in the natural environment affected by the environment, it may permanently alter the genetic information occurs, that is variation. Genetic algorithms provides that the mutation sequence of bits refers to the probability of a certain randomly selected in the sequence of a negated. Suppose mutated at position 2, the following steps:

[0,1,0,0,1,0,1,1]
->
[0],1,[0,0,1,0,1,1]
->
[0],0,[0,0,1,0,1,1]
->
[0,0,0,0,1,0,1,1]

summary

The above are executed in the respective steps shown in the flowchart until all individuals in a population are the same (i.e. converge), such as:

[1,0,1,0,0,0,0,1]
[1,0,1,0,0,0,0,1]
[1,0,1,0,0,0,0,1]
[1,0,1,0,0,0,0,1]

Or the main loop iteration exceeds a predetermined number of times, the algorithm stops output.

Getting Started with Matlab

This article describes the algorithm in Matlab, and therefore need to have some knowledge of Matlab. Getting help readers but Matlab is not our task, it is recommended that the manual can help readers gain a quick Matlab:
https://ww2.mathworks.cn/help/pdf_doc/matlab/getstart_zh_CN.pdf

Matlab is free software, and the performance of running the software have certain requirements, so you can use free open source and takes less resources Octave as a substitute, its official website as follows:
https://www.gnu.org/software/octave/

Octave syntax rules using the Matlab basically the same, incompatibilities below:
https://en.wikibooks.org/wiki/MATLAB_Programming/Differences_between_Octave_and_MATLAB

Annexed hereto code on Octave run through, not tested in Matlab, if there is a need to use Matlab to run the code, so you need to check compatibility.

Algorithm

function retval = main (input1, input2)
  % 主函数,input1,input2,以及retval都没有实际作用
  clear all;
  close all;
  clc;
  
  NP = 50; % 种群大小
  L = 16; % 染色体序列长度
  Pc = 0.8; % 交叉概率
  Pm = 0.1; % 变异概率
  G = 50; % 主循环迭代次数
  Xs = 10; % 所求函数的定义域的最大值
  Xx = 0; % 所求函数的定义域的最小值
  S = 10; % 什么意思自己悟吧,我只是不想硬编码成10
  
  pop = initPop(NP,L);
  
  for i = 1:G
    dpop = decode(pop);
    xpop = getX(dpop,L,Xx,Xs);
    fit = fitValue(xpop);
    if all(fit == fit(1)) % 如果收敛,停止主循环
      break;
    endif
    [maxFit,maxIndex] = max(fit);
    maxX(i) = getX(decode(pop(maxIndex,:)),L,Xx,Xs);
    maxY(i) = maxFit;
    pop = select(pop,fit,xpop);    
    pop = crossOver(pop,Pc);    
    pop = mutation(pop,Pm);
  endfor
  
  
  x = Xx:(Xs-Xx)/(2^L-1)/S:Xs;
  y = targetFunc(x);  
  subplot(2,1,1);
  plot(x,y); % 绘制函数图像
  hold on;
  plot(maxX,maxY,'r*'); % 绘制历次迭代生成的最大值点
  
  [px,py] = size(maxY);
  x = 1:py;
  y = maxY;  
  subplot(2,1,2);
  plot(x,y); % 绘制历次生成的最大值点关于迭代次数的折线图
  hold off;
  
endfunction


function pop = initPop(NP,L)
  pop = round(rand(NP,L));
endfunction

function res = targetFunc(x)
  res = x + 10*sin(5*x) + 7*cos(4*x);
endfunction

function res = decode(pop)
  % 用于将二进制位序列转化成整数
  [px,py] = size(pop);
  res = ones(px,1);
  for j = 1:py
    res(:,j) = 2.^(py-j)*pop(:,j);
  endfor
  res = sum(res,2);
endfunction

function res = getX(dpop,L,Xx,Xs)
  % 缩放二进制位序列表示成的整数到
  % 定义域中
  res = dpop./(2^L-1).*(Xs-Xx);
endfunction

function res = fitValue(xpop)
  res = targetFunc(xpop);
endfunction

function newpop = select(pop,fit,xpop)
  % 赌轮盘算法的实现
  [maxFit,maxIndex] = max(fit);
  [minFit,minIndex] = min(fit);
  [px,py] = size(pop);
  newpop = ones(px,py);
  fit = (fit-minFit)./(maxFit-minFit);
  fit = fit./sum(fit);
  cumfit = cumsum(fit);
  ms = sort(rand(px,1));
  fiti = 1;
  newi = 1;
  while newi <= px
    if ms(newi) < cumfit(fiti)
      newpop(newi,:) = pop(fiti,:);
      newi = newi + 1;
    else
      fiti = fiti + 1;
    endif
  endwhile
endfunction

function newpop = crossOver(pop,Pc)
  % 交叉操作
  [px,py] = size(pop);
  newpop = ones(size(pop));
  for i = 1:2:px-1
    if rand<Pc
      cpoint = ceil(rand*py);
      newpop(i,:) = [pop(i,1:cpoint),pop(i+1,cpoint+1:py)];
      newpop(i+1,:) = [pop(i+1,1:cpoint),pop(i,cpoint+1:py)];
     else
      newpop(i,:) = pop(i,:);
      newpop(i+1,:) = pop(i+1,:);
    endif
  endfor
endfunction

function newpop = mutation(pop,Pm)
  % 变异操作
  [px,py] = size(pop);
  newpop = ones(px,py);
  for i = 1:px
    if rand<Pm
      mpoint=ceil(rand*py);
      newpop(i,mpoint) = 1 - pop(i,mpoint);
      newpop(i,:) = pop(i,:);
    else
      newpop(i,:) = pop(i,:);
    endif
  endfor
endfunction

Drawing results were as follows:

Drawing results

Guess you like

Origin www.cnblogs.com/pkuimyy/p/11585310.html