1 简介

介绍了遗传算法的基本原理和求解流程、详细阐述了Mdtlab遗传算法工具箱的使用方法.并通过使用遗传算法工具箱对一个红绿灯管理优化问题进行求解,验证了该工具箱在解决函数优化问题上的有效性和实用性。

2 部分代码

%% Starting point, clear everything in matlab
tic;
clear all;
close all;
clc;
%%问题表述
FitnessFunction=@(C,g,x,c) TDi(C,g,x,c); % 适应度功能
nLights=4; %红绿灯数量
nIntersections=1; % 交叉点数(静态为 1 个交叉点)
VarSize=[1 nIntersections*nLights]; % 基于交叉点数的决定染色体基因
greenMin= 10; % 绿灯的下限
greenMax= 60; % 绿灯的上限
Cyclemin=60; % CYCLE 的下限
Cyclemax=180 ;
RoadcapacityNSWE=[20,20,20,20]; % 道路容量分别
CarsNSWE=[20,20,11,17];
RoadCongestion1NSWE=RoadcapacityNSWE-CarsNSWE; % 根据空闲道路空间的拥堵
RoadCongestionNSWE=RoadCongestion1NSWE./RoadcapacityNSWE; % 体积/容量比
carpass=5;
%%遗传算法参数
MaxIt=25; %最大迭代次数
nPop=400; %人口规模
pc=0.5; %交叉百分比
nc=2*round(pc*nPop/2); % 后代数量(父母)
end
% Make 2 rows 1
popinv=popinv(:);
%% 合并人口
pop=[pop
popc
popm
popinv]; %#ok
% 根据 TotalDelay 对新人口进行排序
TotalDelay=[pop.TotalDelay];
[TotalDelay, SortOrder]=sort(TotalDelay);
pop=pop(SortOrder);
% 更新最坏成本
WorstDelay=max(WorstDelay,pop(end).TotalDelay);
% 保留给定数量的最佳人口
pop=pop(1:nPop);
TotalDelay=TotalDelay(1:nPop);
%存储有史以来最好的解决方案
BestSol=pop(1);
% 存储有史以来最好的成本
BestDelay(it)=BestSol.TotalDelay;
% 显示迭代信息
disp([' Iteration ' num2str(it) ': Best TotalDelay = ' num2str(BestDelay(it))]);
fprintf('\n')
disp('Green Timings:');
fprintf('\n')
disp([' North Green time = ' num2str(BestSol.GreenNSWE(1))'' ' seconds']);
fprintf('\n')
disp([' South Green time = ' num2str(BestSol.GreenNSWE(2))'' ' seconds']);
fprintf('\n')
disp([' West Green time = ' num2str(BestSol.GreenNSWE(3))'' ' seconds']);
fprintf('\n')
disp([' East Green time = ' num2str(BestSol.GreenNSWE(4))'' ' seconds']);
fprintf('\n')
%世代结束
end
disp(' ****************************************************************' );
disp(' CASE: Every 5 seconds 2 vehicles leaves the corresponding road ' );
disp(' Expected vehicles left through North road' );
disp(round(2*BestSol.GreenNSWE(1)/carpass));
disp(' Expected vehicles left through South road' );
disp(round(2*BestSol.GreenNSWE(2)/carpass));
disp(' Expected vehicles left through West road' );
disp(round(2*BestSol.GreenNSWE(3)/carpass));
disp(' Expected vehicles left through East road' );
disp(round(2*BestSol.GreenNSWE(4)/carpass));
fprintf('\n')
disp(' ****************************************************************' );
disp(['Cycle Time = ' num2str(current_cycle)'' ' seconds']);
%% 绘制输出结果
figure(1);
semilogy(BestDelay,'LineWidth',1);
% plot(BestCost,'LineWidth',2);
xlabel('迭代');
ylabel('总延迟时间');
grid on;
toc

3 仿真结果

 【优化布局】基于遗传算法求解红绿灯管理优化问题含Matlab源码_迭代

4 参考文献

[1]蒋冬初, 何飞, 向继文. 遗传算法求解函数优化问题的Matlab实现[J]. 吉首大学学报:自然科学版, 2005, 26(2):4.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

 【优化布局】基于遗传算法求解红绿灯管理优化问题含Matlab源码_函数优化_02