Advertisement
Guest User

EA1

a guest
Jul 8th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.99 KB | None | 0 0
  1. numberOfCities = 10;
  2. populationSize = 250;
  3. n = 0.8;
  4. parentsSize = populationSize * n;
  5. t_max = 1000;
  6. mutationRate = 0.3;
  7. x = [0 2 6 7 15 12 14 9.5 7.5 0.5];
  8. y = [1 3 5 2.5 -0.5 3.5 10 7.5 9 10];
  9.  
  10. points = zeros(2, numberOfCities);
  11. points(1, :) = x;
  12. points(2, :) = y;
  13. population = zeros(populationSize,numberOfCities);
  14. selected_parents = zeros(parentsSize, numberOfCities);
  15. costs = zeros(populationSize, 3);
  16.  
  17. the_best_val = 1000000; %initial value
  18. the_best_path = zeros(numberOfCities, 1);
  19. the_best_iteration = 0;
  20.  
  21. % generate n paths where n=population_size
  22. for k = 1:populationSize    
  23. population(k,:) = randperm(numberOfCities);
  24. end
  25. for z = 1:t_max    
  26.     % calculate costs for each path combination in population  
  27.     % Evaluate the cost (the total distance to be traveled) of each individual.
  28.     for k = 1:populationSize
  29.         normalization = points(: ,population(k, numberOfCities)) - points(: ,population(k, 1));
  30.         single_cost = norm(points(: ,population(k, numberOfCities)) - points(: ,population(k, 1)));
  31.         for j=2:numberOfCities      
  32.             single_cost = single_cost + norm(points(: ,population(k, j-1)) - points(:, population(k,j)));
  33.         end
  34.         costs(k, 1) = single_cost;    
  35.         costs(k, 2) = k;    
  36.         costs(k, 3) = true;  
  37.     end
  38.     % proportional selection - roulette wheel  
  39.     % Choose n·P parents from the current population via proportional selection, where n ∈ (0, 1].
  40.     costs = sortrows(costs);    
  41.     max_val = max(costs(:,1));
  42.     ti = zeros(populationSize, 1);      
  43.     for i = 1: populationSize      
  44.         costs(i, 1);      
  45.         ti(i) = max_val - costs(i,1);  
  46.     end
  47.     ts = sum(ti);    
  48.     for i = 1: parentsSize  
  49.         r = ts.*rand();    
  50.         chosen_index = 1;
  51.         ti_sum = 0;    
  52.         for index = 1 : populationSize  
  53.             ti_sum = ti_sum + ti(i);    
  54.             if(ti_sum >= r)          
  55.                 index;          
  56.                 chosen_index = costs(index, 2);    
  57.                 break    
  58.             end
  59.         end
  60.         selected_parents(i, :) = population(chosen_index, :);
  61.     end
  62.     %parents_costs = zeros(parentsSize,3);
  63.     %for k = 1:parentsSize  
  64.     %    single_cost = norm(points(: ,selected_parents(k, numberOfCities)) - points(: ,selected_parents(k, 1)));  
  65.     %    for j=2:numberOfCities    
  66.     %        single_cost = single_cost + norm(points(: ,selected_parents(k, j-1)) - points(:, selected_parents(k,j)));    
  67.     %    end
  68.     %    parents_costs(k,1) = single_cost;    
  69.     %    parents_costs(k,2) = k;      
  70.     %    parents_costs(k,3) = true;  
  71.     %end
  72.    
  73.     %crossover  
  74.     % Select randomly two parents to create offspring using crossover operator
  75.     child_popul = zeros(parentsSize, numberOfCities);  
  76.     for i = 1:parentsSize      
  77.         random2parents = datasample(selected_parents, 2);    %random 2 parents
  78.         parent1 = random2parents(1, :);  
  79.         parent2 = random2parents(2, :);  
  80.         child = zeros(1, numberOfCities);    
  81.         child(1) = parent1(1);      
  82.         search = parent1(1);        
  83.         while(true)        
  84.             found = false;        
  85.             for j = 1:numberOfCities              
  86.                 if(parent2(j) == search)          
  87.                     if(child(j) ~= 0)                  
  88.                         break;                
  89.                     end
  90.                     child(j)=parent1(j);            
  91.                     search=parent1(j);            
  92.                     found=true;            
  93.                 end
  94.             end
  95.             if(~found)            
  96.                 break;      
  97.             end
  98.         end
  99.         for j = 1:numberOfCities      
  100.             if(child(j)==0)      
  101.                 child(j)=parent1(j);      
  102.             end
  103.         end
  104.         child_popul(i, :) = child;  
  105.     end
  106.     %mutation
  107.     % Apply mutation operators for changes in randomly selected offspring.
  108.  
  109.     for i = 1:child_popul      
  110.         if rand() <= mutationRate  
  111.             mut_child = child_popul(i, :);
  112.             mut_points = randi([1 numberOfCities],2,1);    
  113.             temp = mut_child(mut_points(1));        
  114.             mut_child(mut_points(1))=mut_child(mut_points(2));
  115.             mut_child(mut_points(2))=temp;      
  116.             child_popul(i, :) = mut_child;    
  117.         end
  118.     end
  119.     %calc children costs
  120.     children_costs = zeros(parentsSize,2);  
  121.     for k = 1:parentsSize      
  122.         single_cost = norm(points(: ,child_popul(k, numberOfCities)) - points(: ,child_popul(k, 1)));  
  123.         for j=2:numberOfCities          
  124.             single_cost = single_cost + norm(points(: ,child_popul(k, j-1)) - points(:, child_popul(k,j)));  
  125.         end
  126.         children_costs(k,1) = single_cost;    
  127.         children_costs(k,2) = k;    
  128.         children_costs(k,3) = false;    
  129.     end
  130.     [a, b] = size(children_costs);  
  131.     family = costs;    
  132.     for i = 1 : a    
  133.         family(i+populationSize, :) = children_costs(i, :);
  134.     end
  135.     family_sorted = sortrows(family);    
  136.     if(family_sorted(1, 1) < the_best_val)      
  137.         the_best_val = family_sorted(1,1)      
  138.         the_best_iteration = z      
  139.         if family_sorted(1, 3)            
  140.             the_best_path = population(family_sorted(1,2), :);    
  141.         else
  142.             the_best_path = child_popul(family_sorted(1, 2), :);  
  143.         end
  144.     end
  145.     new_population = zeros(populationSize, numberOfCities);    
  146.     for i = 1: populationSize  
  147.         if family_sorted(i, 3)
  148.             new_population(i, :) = population(i, :);    
  149.         else
  150.             new_population(i, :) = child_popul(family_sorted(i, 2), :);  
  151.         end
  152.     end
  153. population = new_population;
  154. the_best_val
  155. end
  156. hold on scatter(x,y,'X')
  157. plot([x(the_best_path(numberOfCities)), x(the_best_path(1))], [y(the_best_path(numberOfCities)), y(the_best_path(1))])
  158. for i=2:numberOfCities    
  159.     plot([x(the_best_path(i-1)), x(the_best_path(i))], [y(the_best_path(i-1)), y(the_best_path(i))])
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement