algorithm - How to implement Roulette Wheel Selection and Rank Sleection on Matlab code for the Traveling Salesman Problom? -


i have assignment coding genetic algorithm traveling salesman problem. i've written code giving correct results using tournament selection. problem is, have wheel , rank , results incorrect.

here code using tournament selection:

clc; clear all; close all;  nofcities = 30; initialpopulationsize = nofcities*nofcities; generations = nofcities*ceil(nofcities/10);  cities = floor(rand([nofcities 2])*100+1);  figure; hold on; scatter(cities(:,1), cities(:,2), 5, 'b','fill'); line(cities(:,1), cities(:,2)); line(cities([1 end],1), cities([1 end],2)); axis([0 110 0 110]);  population = zeros(initialpopulationsize ,nofcities);  i=1:initialpopulationsize     population(i,:) = randperm(nofcities); end  distancematrix = zeros(nofcities); i=1:nofcities     j=1:nofcities         if (i==j)             distancematrix(i,j)=0;         else             distancematrix(i,j) = sqrt((cities(i,1)-cities(j,1))^2+(cities(i,2)-cities(j,2))^2);         end     end end  u=1:generations          tourdistance = zeros(initialpopulationsize ,1);     i=1:initialpopulationsize         j=1:length(cities)-1            tourdistance(i) = tourdistance(i) + distancematrix(population(i,j),population(i,j+1));        end     end     i=1:initialpopulationsize          tourdistance(i) = tourdistance(i) + distancematrix(population(i,end),population(i,1));     end      min(tourdistance)      newpopulation = zeros(initialpopulationsize,nofcities);      k=1:initialpopulationsize         child = zeros(1,nofcities);         %tournament start         i=1:5            tournamentparent1(i) = ceil(rand()*initialpopulationsize);         end         p1 = find(tourdistance == min(tourdistance([tournamentparent1])));         parent1 = population(p1(1), :);         i=1:5            tournamentparent2(i) = ceil(rand()*initialpopulationsize);         end         p2 = find(tourdistance == min(tourdistance([tournamentparent2])));         parent2 = population(p2(1), :);         %tournament end         %crossover         startpos = ceil(rand()*(nofcities/2));         endpos = ceil(rand()*(nofcities/2)+10);          i=1:nofcities            if (i>startpos && i<endpos)                 child(i) = parent1(i);            end         end          i=1:nofcities             if (isempty(find(child==parent2(i))))                 j=1:nofcities                     if (child(j) == 0)                         child(j) = parent2(i);                         break;                     end                 end             end         end          newpopulation(k,:) = child;     end      %mutation     mutationrate = 0.015;     i=1:initialpopulationsize        if (rand() < mutationrate)            pos1 = ceil(rand()*nofcities);            pos2 = ceil(rand()*nofcities);            mutation1 = newpopulation(i,pos1);            mutation2 = newpopulation(i,pos2);            newpopulation(i,pos1) = mutation2;            newpopulation(i,pos2) = mutation1;                   end     end      population = newpopulation;     u end  figure; hold on; scatter(cities(:,1), cities(:,2), 5, 'b','fill'); line(cities(population(i,:),1), cities(population(i,:),2)); line(cities([population(i,1) population(i,end)],1), cities([population(i,1) population(i,end)],2)); axis([0 110 0 110]);  %close all; 

what want replace tournament code wheel , rank code.

here wrote wheel selection:

 fitness = tourdistance./sum(tourdistance);         wheel = cumsum(fitness);         parent1 = population(find(wheel >= rand(),1),:);         parent2 = population(find(wheel >= rand(),1),:); 

for wheel selection work, should start designing fitness measure fitter individuals having bigger value. in contrast distance better individuals having smaller value. approach cumsum should work.

where issue ranking selection?


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -