Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.41 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.  
  78. %(mu,lambda)
  79. function sorted = selectPopulation(offspring,populationSize)
  80.     sorted = sortrows(offspring,7);
  81.     sorted = sorted(1:populationSize,:);
  82. end
  83. %(mu+lambda)
  84. function sorted = selectPopulation2(offspring,parents,populationSize)
  85.     sorted = sortrows([parents ;offspring],7);
  86.     sorted = sorted(1:populationSize,:);
  87. end
  88.  
  89.  
  90. function off = mutation(pop, mu,populationSize,tau1,tau2,X,Y)
  91.     x= 1;
  92.     off = zeros(mu,7);
  93.    
  94.     for i =1:mu
  95.        p = pop(x,:);
  96.        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)))];
  97.        x=x+1;
  98.        if x>=populationSize
  99.            x=1;
  100.        end
  101.     end
  102.     for k = 1: length(pop)
  103.         Yind = zeros(101,1);
  104.         for i=1:length(X)
  105.             Yind(i)=func(off(k,1),off(k,2),off(k,3),X(i));
  106.         end
  107.         off(k,7) = immse(Y,Yind);
  108.     end
  109.    
  110. end
  111.  
  112. function f = func(a,b,c,x)
  113.   f = a*(x.^2-b*cos(c*pi*x));
  114. end
  115.  
  116. function offspring = intermediateCrossover(population,mu,populationSize)
  117.     x=1;
  118.     offspring =[];
  119.     for i = 1:mu
  120.         p1 = population(x,:);
  121.         if x <= 1
  122.             p2 = population((populationSize),:);        
  123.         else
  124.             p2 = population((x-1),:);      
  125.         end
  126.         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)];
  127.         temp = [offspring;newRow];
  128.         offspring = temp;
  129.         x = x+1;
  130.         if x >= populationSize
  131.             x=1;
  132.         end
  133.  
  134.     end
  135. end
  136.  
  137. function offspring = discreteCrossover(population,mu,populationSize)
  138.     x =1;
  139.     offspring = [];
  140.     for i=1:mu
  141.          p1 = population(x,:);
  142.          if x <= 1    
  143.             p2 = population(populationSize,:);
  144.             randomSelect = [0 0 0 0 0 0];
  145.             for g = 1:6            
  146.                 if rand() > 0.5
  147.                     randomSelect(g) = p1(g);
  148.                 else
  149.                     randomSelect(g) = p2(g);
  150.                 end
  151.             end        
  152.             temp = [offspring;randomSelect];
  153.             offspring = temp;
  154.         else
  155.             p2 = population(x-1,:);
  156.             randomSelect = [0 0 0 0 0 0];
  157.             for g = 1:6            
  158.                 if rand() > 0.5
  159.                     randomSelect(g) = p1(g);
  160.                 else
  161.                     randomSelect(g) = p2(g);
  162.                 end
  163.             end        
  164.             temp = [offspring;randomSelect];
  165.             offspring = temp;      
  166.          end
  167.           x = x+1;
  168.         if x >= populationSize
  169.             x=1;
  170.         end
  171.         offspring;
  172.     end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement