GA Error Code Issue

7 views (last 30 days)
Shaik Dawood Hussain
Shaik Dawood Hussain on 20 Aug 2017
Commented: Walter Roberson on 20 Aug 2017
Hi Guys, I have been running the traffic light simulation using GA as provided by stelios krasadakis in here : https://www.mathworks.com/matlabcentral/fileexchange/60578-traffic-light-management-algorithm-through-genetic-algorithms?focused=7144075&tab=function 1.I am trying to run the first part of GA initiation codes as per below :
%%Starting point, clear everything in matlab
tic;
clear all;
close all;
clc;
%%Problem Formulation
FitnessFunction=@myff; % 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 kitrino
while i<=nPop
% Initialize Individual
pop(i).GreenNSWE=randi([greenMin greenMax],VarSize);
if(sum(pop(i).GreenNSWE)>current_cycle)
continue;
end
% Individual Evaluation from Fitness Function
for j=1:nLights
% Measure Delay for each traffic light with current congestion
pop(i).TotalDelay(j)=FitnessFunction(current_cycle,pop(i).GreenNSWE(j),RoadCongestionNSWE(j),RoadcapacityNSWE(j));
end
% Summation of Total Delays quotients
pop(i).TotalDelay= real(sum(pop(i).TotalDelay));
i=i+1;
end
% Sort Population
TotalDelay=[pop.TotalDelay];
[TotalDelay, SortOrder]=sort(TotalDelay);
pop=pop(SortOrder);
% Store Best Solution
BestSol=pop(1);
% Store Best Fitness
BestDelay=pop(1).TotalDelay;
% Worst Fitness
WorstDelay=pop(end).TotalDelay;
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')
2.My defined fitness function is :
function TotalDelay=myff(r)
C = r(1);
g = r(2);
x = r(3);
c = r(4);
a=(1-(g/C))^2;
p=1-((g/C)*x);
d1i=(0.38*C*a)/p;
a2=173*(x^2);
ri1=sqrt( (x-1) + (x-1)^2 + ((16*x)/c) );
d2i=a2*ri1;
TotalDelay=(d1i+d2i);
end
3. I am getting the below error, when i run the codes : "??? Error using ==> ga at 238 Fitness function must be a function handle."
4. Can you guys enlightened me, where i did wrong in running the said codes.
Thanks and regards, Dawood
  1 Comment
Walter Roberson
Walter Roberson on 20 Aug 2017
Your code does not call ga(). That code implements its own genetic algorithm.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!