i want to remove the error from this matlab code

조회 수: 3 (최근 30일)
VIPUL SAWAKE
VIPUL SAWAKE 2016년 12월 20일
댓글: Walter Roberson 2016년 12월 20일
Matlab Code
%%start.m
global volume headway redtime;
volume=800; %Vehicles per hour
headway=2; %Spacing between cars on internal movements
redtime =2; %All red time for intersections
NumCars=fix(volume*.5); %Number of cars generated in each direction
gencar() %Generate vehicles
%%gencar.m
%outputs NumCars number of cars with shifted exponential distribution first generate spacing between cars
arrival = headway - (3600/volume -headway)*log(rand(4,NumCars));
%add spacing to get arrival times
for j = 2:NumCars
arrival(1:4,j)= arrival(1:4,j-1)+arrival(1:4,j);
end
%%inctime.m
%Traffic flow is simulated and control signals generated
%Time is incremented to the next signal choice, once the optimal signal
is
%found
%ACO parameters
iterations=75; %Number of iterations before choosing a switch
%time
numants=10; %number of ants used
evaporation =.8; %pheromone evaporation rate
local=1; %1 if local search is to be used, 0 otherwise
locstep = 5; %How often local search is performed
localneighbor=4; %Size of local search neighborhood
heur=1; %1 if heuristic information is to be used, 0
%otherwise
elitist =1; %1 if elitist ant is to be used, 0 otherwise
rank =1; %1 if rank-based update is to be used, 0
%otherwise
N=10; %number of ants used in rank-based
totalsig = zeros(1,400); %array containing signal switch times
totalqueue = zeros(4,400); %array containing queue on each
%movement at switch times
88
await=zeros(4,NumCars); %array containing delay of each
%vehicle
t=0; %start time
initial = zeros(4,1); %number of cars in each direction at
%start of each phase
totalsig(1) = t; %totalsig is vector of switching times
totalqueue(:,1)=initial; %Array of queue length at each change
% in each direction
totalwait=zeros(4,1); %Array of vehicle wait times
green = [1,0,1,0]'; %1 if green on a movement otherwise 0
next=ones(4,1); %Index of next arrival
l = volume/3600; %lambda, vehicle arrival rate
inc=1; %Number of signal transitions
currentwait=zeros(4,1); %The current wait of vehicles waiting
% at the queue
cycle = zeros(4,1);
while t<min(arrival(:,size(arrival,2)))-200 %Loops until stopping time
greendir= mod(green(1)+1,2)+1; %1 if movement 1 is green,
%otherwise 2
reddir= mod(greendir,2)+1; %1 if movement 1 is red ,
%otherwise 2
ACO() %run ACO to find optimal
%signal
ccc = [t1,t2,t3];
sigset(1) = t;
sigset(2) = sigset(1)+gsetting(1);
sigset(3) = sigset(2)+gsetting(2);
movecars() %The chosen path is evaluated and vehicle’s wait
time is computed
t = sigset(2); %Advance time to next signal change
green=~green; %switch green to other direction
totalsig(inc) = t; %keep track of signal
totalqueue(:,inc)=initial; %keep track of queue lengths
inc=inc+1;
end
89
%%ACO.m
%Pheromone levels are initialized, then ants construct new solutions on
%each iteration
prob = cat(3,cat(1,ones(1,26),zeros(25,26)),ones(26,26,1));
prob2=prob; %prob2 used with heuristic
%prob is a 3-d array consisting of 2, 26 by 26 matrices, dimension. The
%matrix in the first slot is for the next time change and
%the matrix in the second slot is for the time change after that. The
%ij-th slot of the 2nd matrix gives the probability of an ant moving
%from time t+i to time t+i+j
%It is updated after each iteration
weight = 1:26;
weight = exp(-abs(weight - (max(green.*initial)*2 - 2)+6)/5);
%Weight is used for heuristic information
gminwait=10000;
for iteration = 1:iterations %based on ants
updatepher() %Ants create solution and find optimal path
end
%%updatepher.m
% At each iteration ants create and evaluate candidate solutions, here
% they deposit pheromone, based on ACO parameters, and pheromone is
% evaporated.
minwait=10000;
if local == 1 && mod(iteration,locstep )==0 %Do local search on 5th
%iteration
locmin(1)=max(1,gsetting(1)-6-localneighbor);
locmax(1)=min(26,gsetting(1)-6+localneighbor);
locmin(2)=max(1,gsetting(2)-6-localneighbor);
locmax(2)=min(26,gsetting(2)-6+localneighbor);
locprob = {ones(1,locmax(1)-locmin(1)+1),ones(locmax(1)...
locmin(1)+1,locmax(2)-locmin(2)+1)};
optcen()
antwait =waittime; %waittime of each ant
aset = setting; %signal setting of each ant
90
for a = 2:numants %Number of ants
optcen() %calc waittime for each ant
antwait = [antwait ;waittime];
aset = [aset; setting];
end
else
optcen2()
antwait =waittime; %wait time of each ant
aset = setting; %signal setting of each ant
for a = 2:numants %Number of ants
optcen2() %calc wait time for each ant
antwait = [antwait ;waittime];
aset = [aset; setting];
end
end
%Ranking
sorted = sortrows([antwait,aset]);
aset = sorted(:,2:3);
if minwait < gminwait %Pick best solution so far
gminwait = minwait;
goptsig = optsig;
gsetting = [goptsig(2)-goptsig(1),goptsig(3)-goptsig(2)] ;
end
if ~(mod(iteration, localneighbor )==0 && local ==1) %If not using
%local search or not local search step
prob = evaporation*prob; %Evaporation of pheromone
if rank == 1
for r = 1:size(aset,1)
for s = 1:size(aset,2) %Add weight to best solution of last
%group
if s ==1
prob(1,aset(r,s)-4-2,s) = prob(1, aset(r,s)-4-2, s)...
+(max(0,(N-r))/(antwait(r)));
else
prob(aset(r,s-1)-4-2,aset(r,s)-4-2,s)=...
prob(aset(r,s-1)-4-2,aset(r,s)-4-2,s)+(max(0,(N-r))/(antwait(r)));
end
end
end
else
for r = 1:size(aset,1)
for s = 1:size(aset,2) %Deposit pheromone on paths
91
if s ==1
prob(1,aset(r,s)-4-2,s) = ...
prob(1, aset(r,s)-4-2, s) +(1/antwait(r));
else
prob(aset(r,s-1)-4-2,aset(r,s)-4-2,s)=...
prob(aset(r,s-1)-4-2,aset(r,s)-4-2,s)+(1/antwait(r));
end
end
end
end
if elitist ==1 %add elitist ant's pheromone
%deposit
prob(1,gsetting(1)-4-2,1) = prob(1,gsetting(1)-4-2,1) +...
(10)/ gminwait ;
prob(gsetting(1)-4-2,gsetting(2)-4-2,2) = ...
prob(gsetting(1)-4-2,gsetting(2)-4-2,2) +10/ gminwait ;
end
if heur==1 %include heuristics
prob2 = cat(3,cat(1,prob(1,:,1).*weight,zeros(25,26)),prob(:,:,2));
else
prob2=prob;
end
end
optcen.m
%Each ant creates a candidate solution randomly based on pheromone
%deposits and then the expected delay is computed to evaluate the
%solution
%This is used on local search step
global volume;
%Randomly pick next solution based on pheromone
sumprob1 = sum(locprob{1},2);
sumprob2 = sum(locprob{2},2) ;
sumprob = {sumprob1,sumprob2};
s=1;j=1;offset=0;
setting=zeros(1,2);
signal=zeros(2,1);
while s < 3
signal(s)=rand*sumprob{s}(j) ; %Choose signal switch times based on
92
%pheromone
i =1;
while signal(s) > sum(locprob{s}(j+offset,1:i)) %pick the
%signal
i = i+1;
end
j=i-locmin(s)+1;
j=1;
setting(s)=i+3+locmin(s)+redtime;
s = s+1;
end
%sigset is the next two switch times
%setting is length of next two phases
sigset = [t];
t1= initial;
for i = 1:size(setting,2)
sigset = cat(2, sigset, sigset(i)+setting(i));
end
%t1 is vehicles in queue initially
%t2 is expected number of vehicles in queue at next signal change
%t3 is expected number of vehicles in queue at second signal change
%On green movements
t2 = green.*max(0 , t1 - floor((setting(1)-redtime)/headway +1)+...
volume/3600* min(headway*(t1-1)/(1-
(volume/3600)*headway),setting(1)));
%On red movements
t2 = t2 + ~green.*(t1+volume/3600*setting(1));
t3 = ~green.*max(0 , t2 - floor((setting(2)-redtime)/headway +1)+...
volume/3600* min(headway*(t2-1)/(1-
(volume/3600)*headway),setting(2)));
t3 = t3 + green.*(t2+volume/3600*setting(2));
compwait() %Compute wait time of signal choice
if waittime < minwait
minwait = waittime;
optsig = sigset;
optsetting = setting;
end
93
optcen2.m
%Each ant creates a candidate solution randomly based on pheromone
%deposits
%and then the expected delay is computed to evaluate the solution
%This is used when not using local search
sumprob = sum(prob2,2); %Randomly pick next solution based on
%pheromone
s=1;j=1; setting=zeros(1,2);
signal=zeros(2,1);
while s < 3
signal(s)=rand*sumprob(j,1,s) ; %Choose signal switch times based on
%pheromone
i = 1;
sp = prob2(j,1:i,s);
while signal(s) > sp %pick the signal
i = i+1;
sp = sp + prob2(j,i,s);
end
j=i;
setting(s)=i+4+redtime;
s = s+1;
end
%sigset is the next two switch times
%setting is length of next two phases
sigset = t;
t1= initial;
for i = 1:size(setting,2)
sigset = [ sigset, sigset(i)+setting(i)];
end
%On green movements
t2 = green.*max(0 , t1 - floor((setting(1)-redtime)/headway +1)+...
volume/3600* min(headway*(t1-1)/(1-
(volume/3600)*headway),setting(1)));
%On red movements
t2 = t2 + ~green.*(t1+volume/3600*setting(1));
t3 = ~green.*max(0 , t2 - floor((setting(2)-redtime)/headway +1)+...
volume/3600* min(headway*(t2-1)/(1-
(volume/3600)*headway),setting(2)));
t3 = t3 + green.*(t2+volume/3600*setting(2));
compwait() %Compute expected wait time of signal
if waittime < minwait
minwait = waittime;
optsig = sigset;
optsetting = setting;
end
94
%%Compwait.m
%Computes the expected wait time of a signal
cycle = zeros(4,1);
%computes lower bound on extra wait time
M = 30/headway+1-30*volume/3600;
q= t3 +(~green)*7*volume/3600;
for k =1:4
while t3(k) > M - (~green(k))*7*volume/3600 + cycle(k)*...
(M - 7*volume/3600) &&cycle(k) <5
cycle(k) = cycle(k) +1;
q(k) = t3(k) -(M - (~green(k))*7*volume/3600 + (cycle(k)-1)*...
(M - 7*volume/3600));
if cycle(k) == 5 && iteration ==70
t
end
end
end
%compute initial vehicles released, new arrivals released and not
%released
initrel = green.* min(1 + fix((setting(1)-redtime)/headway),t1)+...
~green.* min(1 + fix((setting(2)-redtime)/headway),t2);
newrel = green.*max(0,min(1+fix((setting(1)-redtime)/headway)-t1,...
l * (t1-1)*headway/(1- l * headway)))+...
~green.*max(0,min(1+fix((setting(2)-redtime)/headway)-t2,...
l * (t2-1)*headway/(1- l * headway)));
notrel = green.*(t1 - initrel)+...
~green.*(t2-initrel);
%compute wait time of each group of vehicles
inwait = initrel.*(initrel-1)*headway/2;
newwait = green.*newrel*headway.*(t1+1)/2+...
~green.*newrel*headway.*(t2+1)/2;
notrwait = green.*notrel*setting(1)+...
~green.*notrel*setting(2);
nnrwait = green.*max(0,l* (setting(1)-redtime- newrel*headway).^2/2.*...
sign(((t1-1)*headway)/(1-l * headway)-setting(1)+redtime))+...
~green.*max(0,l* (setting(2)-redtime- newrel*headway).^2/2.*...
sign(((t2-1)*headway)/(1-l * headway)-setting(2)+redtime));
new = green.*max(0,l * q*headway/(1- l * headway));
wait3 = (~green).*t3*(5+2)+...
cycle*(30+5+2)^2*l/2+M*(M-1)/2.*cycle+... %wait time from uncleared
%cycles
max(0,q.*(q-1)*headway/2) +... %cars at beginning of final
%cycle
new*headway.*(q+1)/2; %cars
95
redwait = (green).*(t2*setting(2) +...
(volume/3600).*(setting(2).^2)/2 )+...
(~green).*(t1*setting(1) +...
(volume/3600).*(setting(1).^2)/2 );
%Total expected wait time
wait = inwait+newwait+notrwait+nnrwait+wait3+redwait+currentwait;
%Average expected wait time
waittime =(sum(wait))/((sigset(3)-sigset(1))*volume/900+sum(initial));
%%Movecars.m
%Move cars according to the optimal control
setting = sigset(2)-sigset(1);
queue =initial;
for k =greendir:2:greendir+2 %clear green cars on green
%movements
m=next(k); n=0;
for j=0:initial(k)-1 %loop through vehicles in queue to
%determine if relased or not
if headway* j <=setting(1)-redtime %Vehicle relased
await(k,m+j)=await(k,m+j)+headway*(j);
n=n+1;
end
end
m=m+n;
next(k)=m; %next is now the index of the next vehicle
%to be released
initial(k)=0;
while arrival(k,m) <sigset(2)-redtime %new arrivals while
%signal is still green
if arrival(k,m)<sigset(1)+(queue(k)-1)*headway %if arrives
%before traffic has been released
if queue(k)*headway <= setting(1)-redtime %if initial
%queue will be released on current signal
await(k,m)=sigset(1)+ headway*queue(k) - arrival(k,m);
next(k)=next(k)+1;
queue(k)=queue(k)+1;
96
else
%If queue is too long then calculation begins on next (red)
%phase
initial(k)=initial(k)+1;
queue(k)=queue(k)+1;
end
else %if vehicle arrives when queue is empty then
%no wait
await(k,m)=0;
next(k)=m+1;
end
m=m+1;
end
if arrival(k,next(k)+initial(k))<sigset(2) && ...
arrival(k,next(k)+initial(k))>sigset(2)-2
initial(k) = initial(k)+1;
end
currentwait(k) = ...
sum(max(0,(-arrival(k,next(k):next(k)+initial(k))+sigset(2))));
end
for k=reddir:2:reddir+2 %Loop over red directions
m=next(k);
initial(k)=0; %reset initial
while arrival(k,m) <sigset(2)
await(k,m)= sigset(2) - arrival(k,m); %add wait time to
%vehicles in queue
initial(k)=initial(k)+1; %Count vehicles in
%queue
m=m+1;
end
currentwait(k) = sum(await(k,next(k):next(k)+max(0,(initial(k)-1))));
%Update current wait
end
97
%%Fullact.m
%Fully actuated control
t=0; %start time
totalsig = t; %totalsig is vector of switching times
totalwait=zeros(4,1); %Arrary of vehcile wait times
green = [1,0,1,0]'; next=ones(4,1);
greendir= mod(green(1)+1,2)+1;
reddir= mod(greendir,2)+1;
initial = zeros(4,1); %number of cars in each direction at
%start of optimization
totalqueue=initial; %Array of quenelenth at each change
%in each direction
inc =1;
chg=zeros(4,1);
currentwait=zeros(4,1);
nextarrival = ones(4,1);
changegreen=0;
first=1;
fulltime=0;
while t<min(arrival(:,size(arrival,2)))-100
sigset = [t];
nextchangefullact()
t=t +redtime;
if endflag ==1
for k=reddir:2:reddir+2
m=nextarrival(k);
while arrival(k,m) <t
m=m+1;
end
end
end
goptsig =[sigset,t,t+7];
sigset = [sigset, goptsig(2),goptsig(3)];
movecars()
green=~green;
greendir= mod(green(1)+1,2)+1;
reddir= mod(greendir,2)+1;
totalsig = [totalsig, t];
totalqueue=[totalqueue,initial];
first =0;
[totalsig;totalqueue];
end
98
%%nextchangefullact.m
%Inputs are current quene
%arrival times
%green
%Determines next time fully actuated controller changes the signal
changegreen=0; %Is 1 if conditions for changing signal are met
endflag =0; %Is 1 if signal should be changed
time =0; %Length of signal
for k =greendir:2:greendir+2 %clear green cars
time(k) = max(0,(initial(k)-1))*headway;
end
if max(time) >30 %if time to release cars in
%queue is longer than max green time
mintime = [time(greendir),time(greendir+2)];
[maxtime,argmax]=max(mintime);
[mintime,argmin]=min(mintime);
nextarrival(greendir+2*(argmax-1))=...
nextarrival(greendir+2*(argmax-1))+16;
while arrival(greendir+2*(argmin-1),...
nextarrival(greendir+2*(argmin-1))) < t+30
nextarrival(greendir+2*(argmin-1))=...
nextarrival(greendir+2*(argmin-1))+1;
end
time=30;
endflag=1;
t=t+30;
else
nextarrival = nextarrival+initial.*green; %All cars in queue are
%released
end
while endflag ~= 1
endclear = [0,0]; %Is 1 if there are no more cars
%arriving
if max(time) ~=0 %If there are cars in queue
while and(endclear(1),endclear(2)) ~=1
if arrival(greendir,nextarrival(greendir))<...
t+time(greendir)+2 %If car arrives before queue is released
time(greendir) = time(greendir)+2;
nextarrival(greendir)=nextarrival(greendir)+1;
else
99
endclear(1)=1;
end
if arrival(greendir+2, nextarrival(greendir+2))< ...
t+time(greendir+2)+2 %If car arrives before queue is released
time(greendir+2) = time(greendir+2)+2;
nextarrival(greendir+2)=nextarrival(greendir+2)+1;
else
endclear(2)=1;
end
if max(time)>29 %If cars keep arriving until
%the max green time
endflag=1;
endclear(1)=1;
endclear(2)=1;
t=t+30;
end
end
mintime = [time(greendir),time(greendir+2)]; %Which green
%signal requires less green time
[mintime,argmin]=min(mintime);
%Let cars pass on shorter green signal if other direction is
%still green
while arrival(greendir+2*(argmin-1),...
nextarrival(greendir+2*(argmin-1)))< t+max(time)*(1-endflag)
nextarrival(greendir+2*(argmin-1))=...
nextarrival(greendir+2*(argmin-1))+1;
end
else %If queue is empty
time = max(time); %Make time a scalar
if arrival(greendir,nextarrival(greendir))< t+2 %If car arrives
%before minimum green if up
time = time+2;
nextarrival(greendir)=nextarrival(greendir)+1;
if arrival(greendir+2,nextarrival(greendir+2))< t+2
nextarrival(greendir+2)=nextarrival(greendir+2)+1;
end
elseif arrival(greendir+2, nextarrival(greendir+2))< t+2
time = time+2;
nextarrival(greendir+2)=nextarrival(greendir+2)+1;
else
endflag=1;
end
100
if fulltime+ time >28
endflag =1;
t = t +time;
end
%end
end
fulltime=max(max(time),1)+fulltime; %Check if reached minimum green
%time
if fulltime <= 5
endflag=0;
end
if endflag ~=1
t=t+max(max(time),1) ;
time =0;
end
if endflag ==1
time =max(time);
if arrival(reddir,nextarrival(reddir)) <t + time ...
||arrival(reddir+2,nextarrival(reddir+2)) <t + time||fulltime+ time >28
changegreen=1;
fulltime =0;
else
endflag =0;
t= t+1;
end
end
end

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 20일
편집: Walter Roberson 2016년 12월 20일
Unfortunately you gave no indication of how the code is to be used. We can guess that it is launched by start but what should we do after that?
I do see problems in updatephr that have to do with attempting to access prob at locations retrieved from aset where the values in aset exceed the size of the prob array. The values in aset appear to be determined by a variable named setting. However, with your use of scripts instead of functions and with your use of global variables, it is difficult to determine which is the relevant place that setting is assigned to, especially as we do not know the order the routines are to be run.
I pulled the files out of your posting and saved them, and cleaned up some obvious syntax errors, some of which might have been accidents of posting. I attach the files I created here.
  댓글 수: 2
VIPUL SAWAKE
VIPUL SAWAKE 2016년 12월 20일
thank you so much sir but i hav a file of matlab code i will mail u sir can u please remove the query from source code and execute it
Walter Roberson
Walter Roberson 2016년 12월 20일
The thesis does not describe how to run the code.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

VIPUL SAWAKE
VIPUL SAWAKE 2016년 12월 20일

VIPUL SAWAKE
VIPUL SAWAKE 2016년 12월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by