A brief introduction to genetic algorithms
1 the introduction Genetic algorithm theory2.1 Biological basis of genetic algorithm 2.2 Theoretical basis of genetic algorithm 2.3 Basic concepts of genetic algorithm 2.4 Standard genetic algorithm 2.5 Characteristics of genetic algorithm 2.6 Improvement direction of genetic algorithm 3. Genetic algorithm flow 4 Key Parameters
Two, some source code
clear; clc; close all; %% Generate random OD matrix % OD () %% Genetic parameter set NUMPOP=200; % Initial population size irange_l=1; % problem solution interval irange_r=35;
LENGTH=24; % Binary encoding length ITERATION =10000; % Number of iterations CROSSOVERRATE =0.8; % hybrid rate SELECTRATE =0.4; % selection rate VARIATIONRATE =0.2; % OD = xlsread('OD.xlsx'); Suzhou Metro2OD travel matrix h = xlsread('Interval runtime.xlsx'); Suzhou Metro2Pop =m_InitPop(NUMPOP,irange_l,irange_r); pop_save=pop; fitness_concat = []; best_solution = []; % Start iterationfor time=1:ITERATION % Calculates the fitness of the initial population fitness=m_Fitness(POP, OD, h); fitness_concat = [fitness_concat;max(fitness)]; pop_T = pop';
[m,index] = max(m_Fitness(pop, OD, h));
best_solution = [best_solution;pop(:,index)']. Choose pop = % m_Select (fitness, pop, SELECTRATE); % code binpop = m_Coding (pop, LENGTH, irange_l); Cross kidsPop % = crossover (binpop NUMPOP, CROSSOVERRATE); % Variation kidsPop = Variation(kidsPop,VARIATIONRATE); % decoding kidsPop = m_Incoding (kidsPop irange_l); % update population pop=[pop kidsPop]; end Mu = 26; sigma = 10; N = round(normrnd(Mu, sigma, [35 35])); N = N + abs(min(N)); sum(sum(N)) if sum(sum(N)) > 35000 ; if sum(sum(N)) < 40000; xlswrite('test.xlsx',N,'Sheet1')
end
end
function fitness=m_Fitness(pop, OD, h)
%% Fitness Function
for n=1:size(pop,2)
a = pop(1,n);
b = pop(2,n);
f1 = pop(3,n);
f2 = pop(4,n); %% constraint condition, does not meet the constraint fitness value infinitesimal %%1A, B, F1,f20
if a == 0 || b == 0 || f1 == 0 || f2 == 0
fitness(n) = 1/1000000000;
continue;
end
%% 2A, B, F1, and F2 cannot be exceeded35
if a > 35 || b > 35 || f1 >35 || f2 >35
fitness(n) = 1/1000000000;
continue;
end
%% 3) constraints on train numberif (sum(h) * 120 + 1170) *( f1 - 16) + (sum(h(a: b- 1)) + (b - a + 1) * 30 + 120) * f2 > 0
fitness(n) = 1/1000000000;
continue;
end
%% 4) capacity constraint % constraint2 = []; %for j = 2:33
% constraint2(j) = (sum(sum(OD(1:j, j+1:35)))/(f1+f2)) * (sum(sum(OD(j+1:35.1:j)))/(f1+f2));
% end
% if max(constraint2)> 1 * 1460%fitness(n) = 1/1000000000;
% continue;
% end
%% 5) Minimum tracking intervalif f1 + f2 > 30
fitness(n) = 1/1000000000;
continue;
end
%% 5) Minimum departure intervalif f1 < 12
fitness(n) = 1/1000000000;
continue; End %% Main fitness function, set as the reciprocal of the objective function, that is, the minimum requirement of the objective function, then the smaller, the greater the fitness1/m_Fx(pop(:,n), OD); End function y=m_Fx(x, OD) %% Z = Q1 * t1d + Q2 * t2d y= (sum(sum(OD)) -sum (sum(OD) (x(OD)) %% Z = Q1 * t1d + Q2 * t2d y= (sum(OD)) -sum (sum(OD) (x(OD))1):x(2),x(1):x(2))))) * (30/x(3)) + sum(sum(OD(x(1):x(2),x(1):x(2* ())))30/(x(3)+x(4)));
end
Copy the code
3. Operation results
Matlab version and references
1 matlab version 2014A
Intelligent Optimization Algorithm and its MATLAB Examples (2nd edition), baoyang, Yu Jizhou, Yang Shan, Publishing House of Electronics Industry