Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.81 KB | None | 0 0
  1. % % % % % % %pradines chromosomu populiacijos generavimas
  2. % % % % % % chroms=genbin(6,5)
  3. % % % % % % for i=1:5, rval(i)=bin2real(chroms(i,:),2,4);
  4. % % % % % % end;
  5. % % % % % % rval
  6. % % % % % %
  7. % % % % % % %Fitneso apskaiciavimas
  8. % % % % % % [fit,fitot]=fitness('genf',chroms,2,4);
  9. % % % % % % fit
  10. % % % % % % fitot
  11. % % % % % %
  12. % % % % % % %Fitneso procentalumas
  13. % % % % % % percent=fit/sum(fit)*100;
  14. % % % % % % %percent
  15. % % % % % % %sum(percent)
  16. % % % % % %
  17. % % % % % % %Nauja populiacija
  18. % % % % % % cross=selectga('genf',chroms,2,4)
  19. % % % % % % [fit,fitot]=fitness('genf',cross,2,4)
  20. % % % % % % %percent=fit/sum(fit)*100;
  21. % % % % % % %percent
  22. % % % % % %
  23. % % % % % % %Newgen kazkas nezinau po crossoverio
  24. % % % % % % disp('newgen yra cia')
  25. % % % % % % newgen=matesome(cross,.6)
  26. % % % % % % [fit,fitot]=fitness('genf',newgen,2,4)
  27. % % % % % %
  28. % % % % % % %mutacija
  29. % % % % % % disp('mutacija')
  30. % % % % % % mutate(newgen,.005)
  31.  
  32. %main programike, kad nedegtu mums siknike
  33. disp('main programa')
  34. %[x f]=optga('genf',[2 4],6,5,5,.005,.6)
  35. [x f]=optga1('genf',[0.001 0.3],6,5,10,.5,.8)
  36.  
  37. %Sugeneruoja random populiacija
  38. function chromosome=genbin(bitl,numchrom)
  39. % generation of a binary population
  40. % word length bitl
  41. % size of the population numchrom
  42. maxchros=2^bitl;
  43. if numchrom >= maxchros
  44.     numchrom = maxchros;
  45. end
  46. chromosome = round(rand(numchrom,bitl));
  47. end
  48.  
  49. %dvejetaine chromosoma pakeicia i desimtaini skaiciu(????)
  50. function deci=bin2real(chrom,a,b)
  51. % binary chromosome -> decimal number in the range from a to b
  52. s=size(chrom);
  53. [pop bitlength] = size(chrom);
  54. maxchrom=2^bitlength-1;
  55. % weights of binary digits
  56. realel=chrom.*((2*ones(1,bitlength)).^fliplr([0:bitlength-1]));
  57. % sumation
  58. tot=sum(realel);
  59. % range
  60. deci=a+tot*(b-a)/maxchrom;
  61. end
  62.  
  63. %Patrankos suvis
  64. function target = genf(x)
  65. % target value of x
  66. %target = pi*x.*x.*(.666667*x+2);
  67. target = 10 + (1/((x-0.160).^2 + 0.1))*sin(1/x);
  68. end
  69.  
  70. %Apskaiciuojamas populiacijos fitneso lygis, reikia rasti
  71. %labiausiai "fitnesuota" chromosoma ir ja pasirinkti reprodukcijai
  72. %fitneso procentinis lygis nurodo, kad yra daugiau galimybiu patekti i
  73. %kazkokia chromosomu populiacija
  74. function [fit,fitot]=fitness(kriter,chrom,a,b)
  75. % Fitness for the set of chromosomes in the range between a and b
  76. % 'kriter' – the title of the external target function
  77. [pop bitl]=size(chrom);
  78. for k=1:pop
  79.     v(k)=bin2real(chrom(k,:),a,b);
  80.     fit(k)=feval(kriter,v(k));
  81. end
  82. fitot=sum(fit);
  83. end
  84.  
  85. %Rekombinuoja nauja populiacija su "fitnesiskiausiom" chromosomom tam, kad
  86. %gautu nauja, patobulinta populiacija
  87. function newchrom=selectga(kriter,chrom,a,b)
  88. % the title of the target function - ‘kriter’
  89. [pop bitlength]=size(chrom);
  90. fit=[];
  91. % the assessment of chromosomes
  92. [fit,fitot] = fitness(kriter,chrom,a,b);
  93. for chromnum=1:pop
  94.     sval(chromnum)=sum(fit(1,1:chromnum));
  95. end
  96. % roulette
  97. parname=[];
  98. for i=1:pop
  99.     rval=floor(fitot*rand);
  100.     if rval<sval(1)
  101.         parname=[parname 1];
  102.     else
  103.         for j=1:pop-1
  104.             sl=sval(j);
  105.             su=sval(j)+fit(j+1);
  106.             if (rval>=sl) & (rval<=su)
  107.                 parname=[parname j+1];
  108.             end
  109.         end
  110.     end
  111. end
  112. newchrom(1:pop,:)=chrom(parname,:);
  113. end
  114.  
  115. function chrom1=matesome(chrom,kiek)
  116. % crossover procedure
  117. % chrom – the initial set of genes
  118. % chrom1 – genes after the crossover
  119. % kiek – crossover degree from 0 to 1
  120. mateind = [ ];
  121. chrom1 = chrom;
  122. [pop bitlength] = size(chrom);
  123. ind = 1:pop;
  124. u = floor(pop*kiek);
  125. if floor(u/2) ~= u/2
  126.     u = u-1;
  127. end
  128. % random crossover
  129. while length(mateind)~=u
  130.     i = round(rand*pop);
  131.     if i == 0
  132.         i = 1;
  133.     end
  134.     if ind(i) ~= -1
  135.         mateind = [mateind i];
  136.         i = -1;
  137.     end
  138. end
  139. % crossover at a random point
  140. for i = 1:2:u-1
  141.     splitpos = floor(rand*bitlength);
  142.     if splitpos == 0
  143.         splitpos = 1;
  144.     end
  145.     i1 = mateind(i);
  146.     i2 = mateind(i+1);
  147.     tempgene = chrom(i1,splitpos+1:bitlength);
  148.     chrom1(i1,splitpos+1:bitlength)=chrom(i2,splitpos+1:bitlength);
  149.     chrom1(i2,splitpos+1:bitlength)=tempgene;
  150. end
  151. end
  152.  
  153. %mutacija
  154. function chrom=mutate(chrom,mu)
  155. % mutation of chromosomes
  156. % mu – the mutation strength; usually assumed as a small positive number
  157. [pop bitlength] = size(chrom);
  158. for i=1:pop
  159.     for j=1:bitlength
  160.         if rand <= mu
  161.             if chrom(i,j) == 1
  162.                 chrom(i,j) = 0;
  163.             else
  164.                 chrom(i,j) = 1;
  165.             end
  166.         end
  167.     end
  168. end
  169. end
  170.  
  171. function [xval,maxf]=optga(fun,range,bits,pop,gens,mu,matenum)
  172. % GA optimization
  173. % fun – the target function
  174. % range[x1 x2] – the range of x: from x1 to x2
  175. % bits – the number of bits in a word
  176. % pop – the number of chromosomes in the population
  177. % gens – the number of generations
  178. % mu – the mutation parameter
  179. newpop=[ ];
  180. a=range(1); b=range(2);
  181. newpop=genbin(bits,pop)
  182. for i=1:gens
  183.     selpop = selectga(fun,newpop,a,b);
  184.     newgen = matesome(selpop,matenum);
  185.     newgen1 = mutate(newgen,mu);
  186.     newpop = newgen;
  187. end
  188. [fit,fitot] = fitness(fun,newpop,a,b)
  189. [maxf,mostfit] = max(fit)
  190. xval = bin2real(newpop(mostfit,:),a,b);
  191. end
  192.  
  193.  
  194. %dizaino savaite buvo labai gera, nes ismokome kurti burtazodi be ranku
  195. function [xval,maxf]=optga1(fun,range,bits,pop,gens,mu,matenum)
  196. mu
  197. matenum
  198.  
  199. Y = ones(1,pop);
  200. axis ([0 gens range(1) range(2)])
  201. newpop=[ ];
  202. a=range(1); b=range(2);
  203. newpop=genbin(bits,pop);
  204. for i=1:gens
  205.     for iii = 1:pop
  206.         XXX(iii) = bin2real(newpop(iii,:),a,b);
  207.     end
  208.     YYY = i*Y;
  209.     [fit,fitot] = fitness(fun,newpop,a,b);
  210.     fit
  211.     plot(YYY,XXX,'o')
  212.     hold on
  213.     selpop = selectga(fun,newpop,a,b);
  214.     newgen = matesome(selpop,matenum);
  215.     newgen1 = mutate(newgen,mu);
  216.     newpop = newgen;
  217. end
  218. [fit,fitot] = fitness(fun,newpop,a,b)
  219. [maxf,mostfit] = max(fit)
  220. xval = bin2real(newpop(mostfit,:),a,b);
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement