paryz17

ES / AI

Nov 11th, 2019
747
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ATTACHMENT 1.
  2.  
  3. %By Katarzyna Chowańska & Mateusz Urbanek
  4.  
  5. clear all; close all; clc;
  6. data = importdata('AIdata12.dat');
  7.  
  8. % Setting initial values.
  9. u=100;
  10. lambda = u * 5;
  11. epsilon = 10^(-5);
  12. it_max = 200;
  13.  
  14. xF=data(:,1);
  15. yF=data(:,2);
  16.  
  17. n=6;
  18. t1=(1/(sqrt(2*n)));
  19. t2=1/(sqrt(2*sqrt(n)));
  20.  
  21. bP=1;
  22. bO=0;
  23. it = 0;
  24.  
  25. time1=cputime;
  26.  
  27. %Creating first population of the Parents.
  28. for i=1:u
  29.   for j=1:6
  30.     if j<4
  31.       P(i,j) = -10+(20*rand);
  32.      else
  33.       P(i,j) = 10*rand;
  34.     endif
  35.   endfor
  36. endfor
  37.  
  38. %Entering main loop
  39. while abs(bP-bO)>epsilon
  40.  
  41. %Creating population of the Offsprings.
  42.   for i=1:lambda
  43.     r=randi(u);
  44.     q=randn*t2;
  45.       for j=1:6
  46.         if j<4
  47.           O(i,j) = P(r,j) + randn*P(r,j+3);
  48.         else
  49.           O(i,j) = P(r,j) * exp(randn*t1)*exp(q);
  50.         endif
  51.       endfor
  52.   endfor
  53.  
  54.   fitP = zeros(u,1);
  55.   fitO = zeros(lambda,1);
  56.  
  57. %Calculating MSE for the Parents.
  58.   for i=1:u
  59.     for j=1:101
  60.       fitP(i,1)=fitP(i,1)+power(yF(j)-(P(i,1)*(xF(j)^2-P(i,2)*cos(P(i,3)*pi*xF(j)))),2);  
  61.     end
  62.       P(i,7)=fitP(i,1)/101;
  63.    end
  64.  
  65. %Calculating MSE for the Offsprings.
  66.   for i=1:lambda
  67.     for j=1:101
  68.       fitO(i,1)=fitO(i,1)+power(yF(j)-(O(i,1)*(xF(j)^2-O(i,2)*cos(O(i,3)*pi*xF(j)))),2);
  69.     end
  70.       O(i,7)=fitO(i,1)/101;
  71.    end
  72.    
  73. %Sorting rows by MSE to find Best Parent and Best Offspring
  74.   P=sortrows(P,7);
  75.   O=sortrows(O,7);
  76.   bP=P(1,7);
  77.   bO=O(1,7);
  78.  
  79. %Merging set of Offsprings to set of Parents and sorting it by MSE again.
  80.   P=vertcat(P,O);
  81.  
  82.   P=sortrows(P,7);
  83. %Cutting of members with the worst MSE - creating new population of Parents with "u" members.
  84.   P = P(1:end-(lambda),:);
  85.  
  86. %Increasing number of iterations and checking if it hasn't exceed maximum set at the beggining.
  87.   it = it+1;
  88.   if it == it_max
  89.     break;
  90.   end
  91. end
  92.  
  93. %Saving and printing time which was needed to execute algorithm.
  94. time2=cputime-time1;
  95. printf('Execute time: %.2fs\n', cputime-time1);
  96.  
  97. %Printing fitness of result.
  98. fit=P(1,7);
  99. disp("Fitness:");
  100. disp(fit);
  101. disp("");
  102.  
  103. %Printing number of iterations needed to obtain result
  104. disp("Iterations: ");
  105. disp(it);
  106.  
  107. %Printing result (parameters a, b, c)
  108. par_a = P(1,1);
  109. par_b = P(1,2);
  110. par_c = P(1,3);
  111.  
  112. disp("a: ");
  113. disp(par_a);
  114. disp("b: ");
  115. disp(par_b);
  116. disp("c: ");
  117. disp(par_c);
  118.  
  119. %Calulating values y for each value x from data file using function with parameters obtained by algorithm.
  120. for i=1:101
  121. y(i)=(P(1,1)*(power(xF(i),2)-P(1,2)*cos(P(1,3)*pi*xF(i))));
  122. end
  123.  
  124. hold on
  125.  
  126. %Plotting values x and values y from file.
  127. plot(xF,yF,'r');
  128.  
  129. %Plotting values x from file with values y calculated by us.
  130. plot(xF,y,'b');
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×