Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ATTACHMENT 1.
- %By Katarzyna Chowańska & Mateusz Urbanek
- clear all; close all; clc;
- data = importdata('AIdata12.dat');
- % Setting initial values.
- u=100;
- lambda = u * 5;
- epsilon = 10^(-5);
- it_max = 200;
- xF=data(:,1);
- yF=data(:,2);
- n=6;
- t1=(1/(sqrt(2*n)));
- t2=1/(sqrt(2*sqrt(n)));
- bP=1;
- bO=0;
- it = 0;
- time1=cputime;
- %Creating first population of the Parents.
- for i=1:u
- for j=1:6
- if j<4
- P(i,j) = -10+(20*rand);
- else
- P(i,j) = 10*rand;
- endif
- endfor
- endfor
- %Entering main loop
- while abs(bP-bO)>epsilon
- %Creating population of the Offsprings.
- for i=1:lambda
- r=randi(u);
- q=randn*t2;
- for j=1:6
- if j<4
- O(i,j) = P(r,j) + randn*P(r,j+3);
- else
- O(i,j) = P(r,j) * exp(randn*t1)*exp(q);
- endif
- endfor
- endfor
- fitP = zeros(u,1);
- fitO = zeros(lambda,1);
- %Calculating MSE for the Parents.
- for i=1:u
- for j=1:101
- 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);
- end
- P(i,7)=fitP(i,1)/101;
- end
- %Calculating MSE for the Offsprings.
- for i=1:lambda
- for j=1:101
- 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);
- end
- O(i,7)=fitO(i,1)/101;
- end
- %Sorting rows by MSE to find Best Parent and Best Offspring
- P=sortrows(P,7);
- O=sortrows(O,7);
- bP=P(1,7);
- bO=O(1,7);
- %Merging set of Offsprings to set of Parents and sorting it by MSE again.
- P=vertcat(P,O);
- P=sortrows(P,7);
- %Cutting of members with the worst MSE - creating new population of Parents with "u" members.
- P = P(1:end-(lambda),:);
- %Increasing number of iterations and checking if it hasn't exceed maximum set at the beggining.
- it = it+1;
- if it == it_max
- break;
- end
- end
- %Saving and printing time which was needed to execute algorithm.
- time2=cputime-time1;
- printf('Execute time: %.2fs\n', cputime-time1);
- %Printing fitness of result.
- fit=P(1,7);
- disp("Fitness:");
- disp(fit);
- disp("");
- %Printing number of iterations needed to obtain result
- disp("Iterations: ");
- disp(it);
- %Printing result (parameters a, b, c)
- par_a = P(1,1);
- par_b = P(1,2);
- par_c = P(1,3);
- disp("a: ");
- disp(par_a);
- disp("b: ");
- disp(par_b);
- disp("c: ");
- disp(par_c);
- %Calulating values y for each value x from data file using function with parameters obtained by algorithm.
- for i=1:101
- y(i)=(P(1,1)*(power(xF(i),2)-P(1,2)*cos(P(1,3)*pi*xF(i))));
- end
- hold on
- %Plotting values x and values y from file.
- plot(xF,yF,'r');
- %Plotting values x from file with values y calculated by us.
- plot(xF,y,'b');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement