【优化控制】基于遗传算法实现红绿灯优化管理附matlab代码

简介: 【优化控制】基于遗传算法实现红绿灯优化管理附matlab代码

1 内容介绍

城市交通拥堵阻碍城市发展:(1)减少市民可用于工作时间;(2)造成环境污染;(3)难以应变道路紧急情况.特种车辆在城市中执行紧急任务时,由于现阶段灯控系统未能对其做出区分,无法动态引导特种车辆到路口之间的交通流,并在其到达路口时设置绿灯,造成特种车辆通行过程中常常遇到阻碍.红绿灯作为城市交通管理的工具,根据感知到的路口周边车辆调整红灯时间和绿灯时间,可以优化交通控制,解决路口交通拥堵以及实现特种车辆执行紧急任务时一路"绿灯"畅行.对于灯控路口拥堵问题的研究.

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('\n')

   disp('Green Timings in seconds:');

   disp(['  North Green time = ' num2str(BestSol.GreenNSWE(1))]);

   fprintf('\n')

   disp(['  South Green time = ' num2str(BestSol.GreenNSWE(2))]);

   fprintf('\n')

   disp(['  West Green time = ' num2str(BestSol.GreenNSWE(3))]);

   fprintf('\n')

   disp(['  East Green time = ' num2str(BestSol.GreenNSWE(4))]);

   fprintf('\n')


%% 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('\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 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('\n')

   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 运行结果

4 参考文献

[1]赵功勋, 郭海滨, 苏利. 基于遗传算法的工程项目资源均衡优化及其MATLAB实现[J]. 工程经济, 2016, 26(12):6.

[2]叶文斌. 基于红绿灯优化城市交通控制设计与仿真[D]. 华东师范大学, 2015.

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

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


相关文章
|
3月前
|
存储 传感器 分布式计算
针对大尺度L1范数优化问题的MATLAB工具箱推荐与实现
针对大尺度L1范数优化问题的MATLAB工具箱推荐与实现
|
3月前
|
算法 定位技术 计算机视觉
【水下图像增强】基于波长补偿与去雾的水下图像增强研究(Matlab代码实现)
【水下图像增强】基于波长补偿与去雾的水下图像增强研究(Matlab代码实现)
165 0
|
3月前
|
算法 机器人 计算机视觉
【图像处理】水下图像增强的颜色平衡与融合技术研究(Matlab代码实现)
【图像处理】水下图像增强的颜色平衡与融合技术研究(Matlab代码实现)
137 0
|
3月前
|
机器学习/深度学习 算法 机器人
使用哈里斯角Harris和SIFT算法来实现局部特征匹配(Matlab代码实现)
使用哈里斯角Harris和SIFT算法来实现局部特征匹配(Matlab代码实现)
199 8
|
3月前
|
机器学习/深度学习 编解码 算法
基于OFDM技术的水下声学通信多径信道图像传输研究(Matlab代码实现)
基于OFDM技术的水下声学通信多径信道图像传输研究(Matlab代码实现)
210 8
|
3月前
|
机器学习/深度学习 算法 机器人
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
354 0
|
3月前
|
新能源 Java Go
【EI复现】参与调峰的储能系统配置方案及经济性分析(Matlab代码实现)
【EI复现】参与调峰的储能系统配置方案及经济性分析(Matlab代码实现)
144 0
|
3月前
|
机器学习/深度学习 数据采集 测试技术
基于CEEMDAN-VMD-BiLSTM的多变量输入单步时序预测研究(Matlab代码实现)
基于CEEMDAN-VMD-BiLSTM的多变量输入单步时序预测研究(Matlab代码实现)
119 8
|
3月前
|
机器学习/深度学习 算法 自动驾驶
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
213 8

热门文章

最新文章