【優(yōu)化控制】基于遺傳算法實現(xiàn)紅綠燈優(yōu)化管理附matlab代碼
【優(yōu)化控制】基于遺傳算法實現(xiàn)紅綠燈優(yōu)化管理附matlab代碼
TT_Matlab
博主簡介:擅長智能優(yōu)化算法、神經(jīng)網(wǎng)絡(luò)預(yù)測、信號處理、元胞自動機(jī)、圖像處理、路徑規(guī)劃、無人機(jī)等多種領(lǐng)域的Matlab仿真,完整matlab代碼或者程序定制加qq1575304183。
1 內(nèi)容介紹
城市交通擁堵阻礙城市發(fā)展:(1)減少市民可用于工作時間;(2)造成環(huán)境污染;(3)難以應(yīng)變道路緊急情況.特種車輛在城市中執(zhí)行緊急任務(wù)時,由于現(xiàn)階段燈控系統(tǒng)未能對其做出區(qū)分,無法動態(tài)引導(dǎo)特種車輛到路口之間的交通流,并在其到達(dá)路口時設(shè)置綠燈,造成特種車輛通行過程中常常遇到阻礙.紅綠燈作為城市交通管理的工具,根據(jù)感知到的路口周邊車輛調(diào)整紅燈時間和綠燈時間,可以優(yōu)化交通控制,解決路口交通擁堵以及實現(xiàn)特種車輛執(zhí)行緊急任務(wù)時一路"綠燈"暢行.對于燈控路口擁堵問題的研究.
2 仿真代碼
%% Starting point, clear everything in matlab
tic;
clear all;
close all;
clc;
%% Problem Formulation
FitnessFunction=@(C,g,x,c) TDi(C,g,x,c); % FitnessFunction
nLights=4; % Number of Traffic Lights
nIntersections=1; % Number of Intersections (static as 1 intersection)
VarSize=[1 nIntersections*nLights]; % Decision Chromosome genes based on number of Intersections
greenMin= 10; % Lower bound of GREEN LIGHT
greenMax= 60; % Upper bound of GREEN LIGHT
Cyclemin=60; % Lower bound of CYCLE
Cyclemax=180 ;
RoadcapacityNSWE=[20,20,20,20]; % Road Capacity for NSWE respectivelly
CarsNSWE=[20,20,11,17];
RoadCongestion1NSWE=RoadcapacityNSWE-CarsNSWE; % congestion according to free road spaces
RoadCongestionNSWE=RoadCongestion1NSWE./RoadcapacityNSWE; % Volume/Capacity RATIO
carpass=5;
%% Genetic Algorithm Parameters
MaxIt=25; % Maximum Number of Iterations
nPop=400; % Population Size
pc=0.5; % Crossover Percentage
nc=2*round(pc*nPop/2); % Number of Offsprings (parents)
pm=0.02; % Mutation Percentage
nm=round(pm*nPop); % Number of Mutants
mu=0.1; % Mutation Rate
pinv=0.2;
ninv=round(pinv*nPop);
beta=8; % Selection Pressure
%% Initialization
% Individual Structure
empty_individual.GreenNSWE=[];
empty_individual.TotalDelay=[];
% Population Structure
pop=repmat(empty_individual,nPop,1);
% Initialize Population
i=1;
current_cycle=160-12; %estw kiklos 160 seconds - 12 seconds gia ;
disp([’FIRST Population..........Best TotalDelay = ’ num2str(BestDelay)]);
fprintf(’ ’)
disp(’Green Timings in seconds:’);
disp([’ North Green time = ’ num2str(BestSol.GreenNSWE(1))]);
fprintf(’ ’)
disp([’ South Green time = ’ num2str(BestSol.GreenNSWE(2))]);
fprintf(’ ’)
disp([’ West Green time = ’ num2str(BestSol.GreenNSWE(3))]);
fprintf(’ ’)
disp([’ East Green time = ’ num2str(BestSol.GreenNSWE(4))]);
fprintf(’ ’)
%% Loop For Number of Iterations
count=0;
for it=1:MaxIt
% TERMINATION CRITERIA IF NEEDED
% if(it>2)
% if(BestDelay(it-1)==BestDelay(it-2))
% count=count+1;
% else
% count=0;
% end
% end
% if(count>5)
% disp(’5 Generations without evolution. Process Stopped !’);
% break;
% end
% Calculate Selection Probabilities
P=exp(-beta*TotalDelay/WorstDelay);
P=P/sum(P);
%% Crossover
popc=repmat(empty_individual,nc/2,2);
k=1;
while k<=nc/2
% Select Parents Indices from roulette wheel
i1=RouletteWheelSelection(P);
i2=RouletteWheelSelection(P);
% Select Parents
p1=pop(i1);
p2=pop(i2);
popc(k,1).GreenNSWE=p1.GreenNSWE;
popc(k,2).GreenNSWE=p2.GreenNSWE;
popc(k,1).TotalDelay=p1.TotalDelay;
popc(k,2).TotalDelay=p2.TotalDelay;
% Select random crossover point
i=randi([1 3]);
% crossover randomness
if(i==1)
popc1=popc(k,1).GreenNSWE(2);
popc(k,1).GreenNSWE(2)= popc(k,2).GreenNSWE(2);
popc(k,2).GreenNSWE(2)=popc1;
popc1=popc(k,1).GreenNSWE(3);
popc(k,1).GreenNSWE(3)= popc(k,2).GreenNSWE(3);
popc(k,2).GreenNSWE(3)=popc1;
popc1=popc(k,1).GreenNSWE(4);
popc(k,1).GreenNSWE(4)= popc(k,2).GreenNSWE(4);
popc(k,2).GreenNSWE(4)=popc1;
elseif(i==2)
popc1=popc(k,1).GreenNSWE(3);
popc(k,1).GreenNSWE(3)= popc(k,2).GreenNSWE(3);
popc(k,2).GreenNSWE(3)=popc1;
popc1=popc(k,1).GreenNSWE(4);
popc(k,1).GreenNSWE(4)= popc(k,2).GreenNSWE(4);
popc(k,2).GreenNSWE(4)=popc1;
else
popc1=popc(k,1).GreenNSWE(4);
popc(k,1).GreenNSWE(4)= popc(k,2).GreenNSWE(4);
popc(k,2).GreenNSWE(4)=popc1;
end
% check if new green times are out constraints 10-60s. If it is
% get it to closer min or max
popc(k,1).GreenNSWE=max(popc(k,1).GreenNSWE,greenMin);
popc(k,1).GreenNSWE=min(popc(k,1).GreenNSWE,greenMax);
popc(k,2).GreenNSWE=max(popc(k,2).GreenNSWE,greenMin);
popc(k,2).GreenNSWE=min(popc(k,2).GreenNSWE,greenMax);
if(sum(popc(k,1).GreenNSWE)>current_cycle || sum(popc(k,2).GreenNSWE)>current_cycle)
continue;
end
% Evaluate Generated Offsprings for each traffic light according to
% the corresponding traffic congestion
for j=1:nLights
popc(k,1).TotalDelay(j)=FitnessFunction(current_cycle, popc(k,1).GreenNSWE(j), RoadCongestionNSWE(j),RoadcapacityNSWE(j));
popc(k,2).TotalDelay(j)=FitnessFunction(current_cycle, popc(k,2).GreenNSWE(j), RoadCongestionNSWE(j),RoadcapacityNSWE(j));
end
% TOTAL DELAY which correspongs to the summation of of 4 lights quotients
popc(k,1).TotalDelay= real(sum(popc(k,1).TotalDelay));
popc(k,2).TotalDelay= real(sum(popc(k,2).TotalDelay));
k=k+1; %step
end
% Make 2 rows 1
popc=popc(:);
% Sort popc matrix according to TotalDelay
TotalDelay=[popc.TotalDelay];
[TotalDelay, SortOrder]=sort(TotalDelay);
popc=popc(SortOrder);
%% Mutation
% Create empty Matrix with length the number of mutants
popm=repmat(empty_individual,nm,1);
k=1;
while k<=nm
% Select Parent population
i=randi([1 nPop]); %nPop value 100
p=pop(i);
% Apply Mutation
nVar=4;
nmu=ceil(mu*nVar);
j=randi([1 nVar]);
prosimo=randi([-1 1]);
sigma=prosimo*0.02*(greenMax-greenMin);
mutated=p.GreenNSWE(j)+sigma;
popm(k).GreenNSWE = p.GreenNSWE;
popm(k).GreenNSWE(j)=mutated;
popm(k).GreenNSWE=max(popm(k).GreenNSWE,greenMin);
popm(k).GreenNSWE=min(popm(k).GreenNSWE,greenMax);
if(sum(popm(k).GreenNSWE)>current_cycle)
continue;
end
for j=1:nLights
% Evaluate Mutant
popm(k).TotalDelay(j)=FitnessFunction(current_cycle, popm(k).GreenNSWE(j), RoadCongestionNSWE(j),RoadcapacityNSWE(j));
end
% Summation of delay quotients
popm(k).TotalDelay=real(sum(popm(k).TotalDelay));
k=k+1; %step
end
%% INVERSION
% Create empty Matrix
popinv=repmat(empty_individual,nm,1);
k=1;
while k<=ninv
% Select Parent population
i=randi([1 nPop]);
p=pop(i);
% Apply Inversion
nVar=numel(p.GreenNSWE);
randomgene1=randi([1 4]);
randomgene2=randi([1 4]);
y=p.GreenNSWE;
popinv(k).GreenNSWE=y;
x=popinv(k).GreenNSWE(randomgene1);
popinv(k).GreenNSWE(randomgene1)=popinv(k).GreenNSWE(randomgene2);
popinv(k).GreenNSWE(randomgene2)=x;
popinv(k).GreenNSWE=max(popinv(k).GreenNSWE,greenMin);
popinv(k).GreenNSWE=min(popinv(k).GreenNSWE,greenMax);
if(sum(popinv(k).GreenNSWE)>current_cycle)
continue;
end
for j=1:nLights
% Evaluate Mutant
popinv(k).TotalDelay(j)=FitnessFunction(current_cycle, popinv(k).GreenNSWE(j), RoadCongestionNSWE(j),RoadcapacityNSWE(j));
end
% Summation of delay quotients
popinv(k).TotalDelay=real(sum(popinv(k).TotalDelay));
k=k+1;
end
% Make 2 rows 1
popinv=popinv(:);
%% Merge Population
pop=[pop
popc
popm
popinv]; %#ok
% Sort New Population according to TotalDelay
TotalDelay=[pop.TotalDelay];
[TotalDelay, SortOrder]=sort(TotalDelay);
pop=pop(SortOrder);
% Update Worst Cost
WorstDelay=max(WorstDelay,pop(end).TotalDelay);
% Keep the Best Population from the given number
pop=pop(1:nPop);
TotalDelay=TotalDelay(1:nPop);
% Store Best Solution Ever Found
BestSol=pop(1);
% Store Best Cost Ever Found
BestDelay(it)=BestSol.TotalDelay;
% Show Iteration Information
disp([’ Iteration ’ num2str(it) ’: Best TotalDelay = ’ num2str(BestDelay(it))]);
fprintf(’ ’)
disp(’Green Timings:’);
fprintf(’ ’)
disp([’ North Green time = ’ num2str(BestSol.GreenNSWE(1))’’ ’ seconds’]);
fprintf(’ ’)
disp([’ South Green time = ’ num2str(BestSol.GreenNSWE(2))’’ ’ seconds’]);
fprintf(’ ’)
disp([’ West Green time = ’ num2str(BestSol.GreenNSWE(3))’’ ’ seconds’]);
fprintf(’ ’)
disp([’ East Green time = ’ num2str(BestSol.GreenNSWE(4))’’ ’ seconds’]);
fprintf(’ ’)
%end of generation
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(’ ’)
disp(’ ****************************************************************’ );
disp([’Cycle Time = ’ num2str(current_cycle)’’ ’ seconds’]);
%% Results / Plots
figure(1);
semilogy(BestDelay,’LineWidth’,2);
% plot(BestCost,’LineWidth’,2);
xlabel(’Iteration’);
ylabel(’Total Delay’);
grid on;
toc
3 運行結(jié)果
4 參考文獻(xiàn)
[1]趙功勛, 郭海濱, 蘇利. 基于遺傳算法的工程項目資源均衡優(yōu)化及其MATLAB實現(xiàn)[J]. 工程經(jīng)濟(jì), 2016, 26(12):6.
[2]葉文斌. 基于紅綠燈優(yōu)化城市交通控制設(shè)計與仿真[D]. 華東師范大學(xué), 2015.
博主簡介:擅長智能優(yōu)化算法、神經(jīng)網(wǎng)絡(luò)預(yù)測、信號處理、元胞自動機(jī)、圖像處理、路徑規(guī)劃、無人機(jī)等多種領(lǐng)域的Matlab仿真,相關(guān)matlab代碼問題可私信交流。
部分理論引用網(wǎng)絡(luò)文獻(xiàn),若有侵權(quán)聯(lián)系博主刪除。
-
2023年各省最新電價一覽!8省中午執(zhí)行谷段電價! 2023-01-03
-
PPT導(dǎo)出高分辨率圖片的四種方法 2022-09-22
-
2023年最新!國家電網(wǎng)27家省級電力公司負(fù)責(zé)人大盤點 2023-03-14
-
全國消防救援總隊主官及簡歷(2023.2) 2023-02-10
-
盤點 l 中國石油大慶油田現(xiàn)任領(lǐng)導(dǎo)班子 2023-02-28
-
我們的前輩!歷屆全國工程勘察設(shè)計大師完整名單! 2022-11-18
-
關(guān)于某送變電公司“4·22”人身死亡事故的快報 2022-04-26
