Manual simulation calculation example of genetic algorithm (easy to understand) (including genetic algorithm principle, genetic algorithm code)

Here are a few good learning resources on Genetic Algorithms

  1. Genetic Algorithm Introduction and Manual Simulation Calculation Example (Text Version)
    Genetic Algorithm Introduction and Manual Simulation Calculation Example (Video Explanation Version)

  2. Introduction to the principle of genetic algorithm (including binary encoding, decoding principle, algorithm implementation, video explanation version) and analyzes some ideas of selection, crossover and mutation

Study notes:

Introduction to Genetic Algorithms

Genetic Algorithm (GA) is a kind of random search algorithm evolved by referring to the law of evolution in the biological world ( survival of the fittest and genetic mechanism of survival of the fittest ). It was first proposed by Professor Holland of the University of Michigan in the 1970s, and originated from the research on natural and artificial adaptive systems in the 1960s. Replication of this algorithm is very suitable for dealing with complex and nonlinear optimization problems that are difficult to solve by traditional optimization methods.

Genetic Algorithm Process

ps concept:
gene: corresponding to the components of each solution;
individual: a single individual, which is equivalent to a solution to the optimization problem to be solved;
population: a group composed of many individuals;
fitness: a measure of the adaptability of a species to the living environment;
Selection: Select several individuals from the population with a certain probability, so that their genes have the opportunity to be passed on to the next generation. Generally speaking, the selection process is a process of survival of the fittest based on fitness;
crossover: the DNA at the same position of two chromosomes is cut off, and the two strings before and after are crossed and combined to form two new chromosomes, that is, genetic recombination or Hybridization;
mutation: to mutate some crossed individuals in the population with a certain probability to produce new chromosomes, that is, to change genes.

insert image description here

As can be seen from the genetic algorithm flow chart, the main operation process of the genetic algorithm using the above three genetic operators (selection operator, crossover operator, mutation operator) is as follows:

  1. Initialize the population. Set the iteration number (evolutionary algebra) counter t←0; set the maximum iteration number (evolutionary algebra) T; randomly generate M individuals as the initial population P (0).
  2. Calculate the fitness value of each individual in the population. Calculate the fitness of each individual in the tth generation population P(t).
  3. Choose an operation. Apply the selection operator to the population.
  4. cross operation. The crossover operator is applied to the population, and the crossover operation is performed according to the probability Pc.
  5. mutation operation. The mutation operator is applied to the population, and the mutation operation is performed according to the probability Pm. The tth generation population P(t) is selected, crossed, and mutated to obtain the next generation (t+1 generation) population P(t+1).
  6. Termination condition judgment. If t≤T, then: t←t+1, go to step 2; if t>T, the individual with the best fitness obtained in the evolution process is output as the satisfactory solution or optimal solution of the problem, and the calculation is terminated .

** PS: The idea behind the heuristic algorithm is the same - the evolution of the best , that is, according to a certain strategy to make the poor solution develop in a good direction, so that the good solution becomes better. Among them, the bottom guarantee mechanism can be used , that is, if the solution of the next generation is worse than the previous generation, it will not be updated; if the solution of the next generation is better than the previous generation, it will be updated. Guarantee mechanism + update strategy + heuristic idea = optimal solution/approximate optimal solution

%% 遗传算法Matlab代码(此时示例的目标函数为f(x)=x.^2,求最小值)
%% 采用二进制编码方式;选择算子采用轮盘赌方式;交叉算子采用两点交叉法;变异算子采用基本位变异算子(即对于二进制编码符号串所表示的个体,若需要进行变异操作的某一基因座上的原有基因值为0,则将其变为1%% 参考up https://www.bilibili.com/video/BV14a411C7s8?spm_id_from=333.880.my_history.page.click&vd_source=77722e5d7039d559546697b243150ac0

%% 主程序
function GA()

clc
clear
popsize = 30;       % 种群大小
chromlength = 20;   % 串的长度(个体长度)
pc = 0.6;           % 交叉概率
pm = 0.2;           % 变异概率
xlim = [0,50];    % 求解范围
Maxiter = 200;            % 迭代次数
% x = zeros(1,Maxiter);     % 记录每代个体最优位置
% y = zeros(1,Maxiter);     % 记录每代最优个体对应的函数值

pop = round(rand(popsize,chromlength)); % 随机产生初始种群  直接生成的二进制串
% rand(popsize,chromlength)生成一个由介于 01 之间,即取值(0,1)的均匀分布的随机数组成的 popsize×chromlength 矩阵
% round(X) 将X的每个元素四舍五入为最近的整数。在对等情况下,即有元素的小数部分恰为 0.5 时,round 函数会偏离零四舍五入到具有更大幅值的整数。
% 这样随机生成的初试种群的值 非01
decpop = bintodec(pop,popsize,chromlength,xlim); % 计算初始解对应的十进制
fx = calobjvalue(decpop);  % 计算初始解的函数值,形状为1×popsize的矩阵
plotfig(decpop,fx,xlim,1); % 画出初始函数图
[y(1),I] = min(fx);        %[M,I] = mix(A) 返回 A 中最小值,以及最小值对应的索引。
x(1) = decpop(I);          % 获取最小值对应的变量x

for i = 2:Maxiter
    decpop = bintodec(pop,popsize,chromlength,xlim); % 计算上一代解对应的十进制
    fx = calobjvalue(decpop); % 计算上一代解的函数值
    fitvalue = calfitvalue(fx); % 适应度映射
    newpop = copyx(pop,fitvalue,popsize); % 复制
    newpop = crossover(newpop,pc,popsize,chromlength); %交叉
    newpop = mutation(newpop,pm,popsize,chromlength); % 变异
    % 这时的newpop是经过复制交叉变异产生的新一代群体
    % 下面进行选择择优保留(即优胜劣汰)
    newdecpop = bintodec(newpop,popsize,chromlength,xlim);
    new_fx = calobjvalue(newdecpop); % 计算新解目标函数
    new_fitvalue = calfitvalue(new_fx); % 计算新群体中所有个体的适应度
    index = find(new_fitvalue < fitvalue);  % find(X) 返回一个包含数组X中每个非零元素的线性索引的向量。即若新解的值小于更新前解的值,获取新解的索引(本示例是求最小值)
    
    pop(index,:) = newpop(index,:) % 更新得到最新解
    decpop = bintodec(pop,popsize,chromlength,xlim); % 计算新解的十进制
    fx = calobjvalue(decpop); % 计算结果
    plotfig(decpop,fx,xlim,i); % 绘制新解的图
    % 找出更新后的个体最优函数值
    [bestindividual,bestindex] = min(fx);
    y(i) = bestindividual; % 记录每一代的最优函数值
    x(i) = decpop(bestindex); % 十进制解
    subplot(1,2,2);
    plot(1:i,y);
    title('适应度进化曲线');
    i = i+1;
end
[ymin,min_index] = min(y);
disp(['最优解对应的位置为:', num2str(x(min_index))])
disp(['最优解为:', num2str(ymin)])
end

%% 计算适应度
function fitvalue = calfitvalue(fx)
% 这里求最小值,并且函数值又都大于等于0,所以直接使用函数值本身作为适应度值
% 不同的问题构造适应度函数的方法不同
fitvalue = fx;
end

%% 复制操作 按适应度大小映射为概率,进行轮盘赌复制
% 轮盘赌基本思想:适应度越高的解,按道理越应该高概率的进行复制,且复制的份数应该越多
function newx = copyx(pop,fitvalue,popsize) % 传进来二进制串和对应适应度值
% 利用轮盘赌策略对个体进行复制
newx = pop; % 只是起到申请size为pop大小空间的作用,newx之后要更新的
i = 1; j = 1;
p = fitvalue / sum(fitvalue);  % 对于每个个体,计算对应适应度.即被选择的概率
Cs = cumsum(p);            
% cumsum(A) 从 A 中的第一个其大小不等于 1 的数组维度开始返回 A 的累积和
% 如p为 0.2 0.1 0.4 0.3 则对应 Cs 为0.2 0.3 0.7 1.0
R = sort(rand(popsize,1)); % 每个个体的复制概率,升序排序
while j <= popsize
    if R(j) < Cs(i)
        newx(j,:) = pop(i,:);
        j = j+1;
    else
        i = i+1;
    end
end
end

%% 交叉操作 1234,以一定概率决定是否交叉。若交叉,则两者选择随机一个段进行交叉
function newx = crossover(pop,pc,popsize,chromlength)
% 12 34 56交叉方式,随机选择交叉位点
% 注意个体数为奇数偶数的区别
i = 2;
newx = pop; % 申请空间
while i+2 <= popsize
    % 将第i与i-1进行随机位点交叉
    if rand < pc
        x1 = pop(i-1,:);  % 第i-1个个体
        x2 = pop(i,:);    % 第i个个体
        r = randperm(chromlength,2); % 返回范围内两个整数   p = randperm(n,k)返回行向量,其中包含在 1 到n之间随机选择的k个唯一整数。
        r1 = min(r); r2 = max(r); % 交叉复制的位点
        newx(i-1,:) = [x1(1:r1-1),x2(r1:r2),x1(r2+1:end)];
        newx(i,:) = [x2(1:r1-1),x1(r1:r2),x2(r2+1:end)];
    end
    i = i + 2; % 更新i   2,4,6,8...
end
end

%% 变异 按照一定概率决定该个体是否变异,若变异,随机选择一个位点进行变异:按位取反
function newx = mutation(pop,pm,popsize,chromlength)
i = 1;
while i <= popsize
    if rand < pm
        r = randperm(chromlength,1);
        pop(i,r) = ~ pop(i,r);  % 按位取反
    end
    i = i+1;
end

newx = pop; % 将变异后的结果返回
end

%% 二进制转十进制函数
function dec = bintodec(pop,popsize,chromlength,xlim)
dec = zeros(1,chromlength);
index = chromlength-1:-1:0;  % chromlength=10 chromlength-1:-1:09 8 7 6 ... 0
for i = 1:popsize
    dec(i) = sum(pop(i,:).*(2.^index)); % 将二进制数转成十进制
end
dec = xlim(1) + dec*(xlim(2) - xlim(1)) / (2 ^ chromlength -1); % 获得二进制串对应的实值解
end

%% 绘制图像
function plotfig(decpop,fx,xlim,k)
f = @(x) (x.^2);
x = xlim(1):0.05:xlim(2);
y = f(x);
subplot(1,2,1);
plot(x,y,decpop,fx,'o');
title(['第',num2str(k),'次迭代进化'])
pause(0.1); % pause(n) 暂停执行 n 秒,然后继续执行。必须启用暂停,此调用才能生效。
end

%% 目标函数
function fx = calobjvalue(decpop) % 参数为十进制
f = @(x) (x.^2);
fx = f(decpop);
end



The above program results
insert image description here
genetic algorithm to solve the TSP problem code example

Guess you like

Origin blog.csdn.net/qq_41238751/article/details/126819070