Advertisement
Guest User

EA2

a guest
Jul 9th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.40 KB | None | 0 0
  1. maxGen = 50;
  2. populationSize = 100;
  3. mu = 100;
  4. e = 0.2;
  5. population = zeros(populationSize,7); %a b c aStd bStd cStd MSE
  6.  
  7.  
  8. fileID = fopen('model26.txt');
  9. C = textscan(fileID,'%f %f');
  10. fclose(fileID);
  11. asd = C(1);
  12. asdd = C(2);
  13. X = [asd{:}];
  14. Y = [asdd{:}];
  15.  
  16. tau1 = 1 / sqrt(2 * populationSize);
  17. tau2 = 1 / sqrt(2 * sqrt(populationSize));
  18.  
  19. errors=[];
  20.  
  21. %START Timer
  22. tic
  23.  
  24. %Generation of the initial population
  25. for k = 1:populationSize    
  26. population(k,1:3) = normrnd(0,1,[1,3]);
  27. population(k,4:6) = abs(normrnd(0,1,[1,3]));
  28. Yind = zeros(101,1);
  29. for i=1:length(X)
  30.     Yind(i)=func(population(k,1),population(k,2),population(k,3),X(i));
  31. end
  32. %MSE for each individual
  33. population(k,7) = immse(Y,Yind);
  34. end
  35.  
  36.  
  37. for iter = 1:maxGen
  38.     %recombination
  39.     %tmpPop = population;    
  40.     %tmpPop = intermediateCrossover(population,mu,populationSize);
  41.     tmpPop = discreteCrossover(population,mu,populationSize);    
  42.    
  43.     %mutation and evaluatuon mse for each individual
  44.     offspring = mutation(tmpPop,mu,populationSize,tau1,tau2,X,Y);
  45.    
  46.     %offspring selection
  47.     %population = selectPopulation(offspring,populationSize);
  48.     population = selectPopulation2(offspring,population,populationSize);
  49.     iter=iter+1;
  50.     errors=[errors population(1,7)]
  51.     iter
  52.     if population(1,7)<e
  53.         break
  54.     end
  55.    
  56. end
  57.  
  58. final = population(1,1:7);
  59. disp("a: "+final(1)+", b: "+final(2)+", c: "+final(3)+", MSE: "+final(1,7))
  60. YY = zeros(101,1);
  61. for i=1:length(X)
  62.     YY(i)=func(final(1,1),final(1,2),final(1,3),X(i));
  63. end
  64.  
  65. t = toc
  66. %
  67. figure(1);
  68. plot(X,Y)
  69. title('Plot')
  70. hold on
  71. plot(X,YY)
  72. legend('Real values','Computed')
  73. hold off
  74. figure(2);
  75. title('MSE')
  76. plot(errors)
  77. function sorted = selectPopulation(offspring,populationSize)
  78.     sorted = sortrows(offspring,7);
  79.     sorted = sorted(1:populationSize,:);
  80. end
  81.  
  82. function sorted = selectPopulation2(offspring,parents,populationSize)
  83.     sorted = sortrows([parents ;offspring],7);
  84.     sorted = sorted(1:populationSize,:);
  85. end
  86.  
  87.  
  88. function off = mutation(pop, mu,populationSize,tau1,tau2,X,Y)
  89.     x= 1;
  90.     off = zeros(populationSize,7);
  91.    
  92.     for i =1:mu
  93.        p = pop(x,:);
  94.        off(i,1:6) = [(p(1)+normrnd(0,p(4))) (p(2)+normrnd(0,p(5))) (p(3)+normrnd(0,p(6))) (p(4)*exp(normrnd(0,tau1))*exp(normrnd(0,tau2))) (p(5)*exp(normrnd(0,tau1))*exp(normrnd(0,tau2))) (p(6)*exp(normrnd(0,tau1))*exp(normrnd(0,tau2)))];
  95.        x=x+1;
  96.        if x>=populationSize
  97.            x=1;
  98.        end
  99.     end
  100.     for k = 1: length(pop)
  101.         Yind = zeros(101,1);
  102.         for i=1:length(X)
  103.             Yind(i)=func(off(k,1),off(k,2),off(k,3),X(i));
  104.         end
  105.         off(k,7) = immse(Y,Yind);
  106.     end
  107.    
  108. end
  109.  
  110. function f = func(a,b,c,x)
  111.   f = a*(x.^2-b*cos(c*pi*x));
  112. end
  113.  
  114. function offspring = intermediateCrossover(population,mu,populationSize)
  115.     x=1;
  116.     offspring =[];
  117.     for i = 1:mu
  118.         p1 = population(x,:);
  119.         if x <= 1
  120.             p2 = population((populationSize),:);        
  121.         else
  122.             p2 = population((x-1),:);      
  123.         end
  124.         newRow = [((p1(1)+p2(1))/2) ((p1(2)+p2(2))/2) ((p1(3)+p2(3))/2) ((p1(4)+p2(4))/2) ((p1(5)+p2(5))/2) ((p1(5)+p2(5))/2)];
  125.         temp = [offspring;newRow];
  126.         offspring = temp;
  127.         x = x+1;
  128.         if x >= populationSize
  129.             x=1;
  130.         end
  131.  
  132.     end
  133. end
  134.  
  135. function offspring = discreteCrossover(population,mu,populationSize)
  136.     x =1;
  137.     offspring = [];
  138.     for i=1:mu
  139.          p1 = population(x,:);
  140.          if x <= 1    
  141.             p2 = population(populationSize,:);
  142.             randomSelect = [0 0 0 0 0 0];
  143.             for g = 1:6            
  144.                 if rand() > 0.5
  145.                     randomSelect(g) = p1(g);
  146.                 else
  147.                     randomSelect(g) = p2(g);
  148.                 end
  149.             end        
  150.             temp = [offspring;randomSelect];
  151.             offspring = temp;
  152.         else
  153.             p2 = population(x-1,:);
  154.             randomSelect = [0 0 0 0 0 0];
  155.             for g = 1:6            
  156.                 if rand() > 0.5
  157.                     randomSelect(g) = p1(g);
  158.                 else
  159.                     randomSelect(g) = p2(g);
  160.                 end
  161.             end        
  162.             temp = [offspring;randomSelect];
  163.             offspring = temp;      
  164.          end
  165.           x = x+1;
  166.         if x >= populationSize
  167.             x=1;
  168.         end
  169.         offspring;
  170.     end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement