Changeset 32 for liacs/nc/low-correlation/sa.m
- Timestamp:
- Dec 13, 2009, 9:41:09 PM (15 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
liacs/nc/low-correlation/sa.m
r31 r32 1 % Monte-Carlo Search Algoritm onlow-autocorrelation program1 % Simulated Annealing low-autocorrelation program 2 2 % BSDLicence 3 3 % Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl> … … 7 7 % autocorrelation(best_20); 8 8 9 function [fitness,value] = mcs(length, iterations) 9 function number = randint(low,high) 10 number = round(rand() * (high - low)) + low; 11 endfunction 12 13 function new = mutation(old) 14 loc = randint(1,length(old)); 15 old(loc) = old(loc) * -1; 16 new = old; 17 endfunction 18 19 function s = initseq(n) 20 % Generate a random column s={-1,1}^n 21 s = rand(n,1); 22 s = round(s); 23 s = s - (s == 0); 24 endfunction 25 26 function [fitness,value] = sa(length, temperature, stopLimit) 10 27 best_fitness = 0; 11 for i = 1:iterations12 % Generate a random column s={-1,1}^n13 n = length; 14 s = rand(n,1);15 s = round(s);16 s = s - (s == 0);17 18 % Find whether we are better than everything else19 fitness = autocorrelation( s);28 s = initseq(length); 29 stopCounter = 0; 30 31 while (stopCounter < stopLimit) 32 % Generate new mutation 33 newseq = mutation(s); 34 35 % Better is always accept 36 fitness = autocorrelation(newseq); 20 37 if (fitness > best_fitness) 21 38 best_value = s; 22 39 best_fitness = fitness; 40 s = newseq; 41 fitness 42 disp(rot90(newseq,-1)); 43 stopCounter = 0; 44 else 45 % Make the next 'move' less atractive 46 temperature =- 1; 47 48 stopCounter += 1; 49 % Accept on an certain probability 50 if (temperature < 0) 51 break; 52 else 53 if(randint(0,temperature) > temperature / 4) 54 s = newseq; 55 endif 56 endif 23 57 endif 24 endfor 58 endwhile 59 value = best_value; 25 60 fitness = best_fitness; 26 value = best_value;27 61 endfunction 28 62 29 % Basic variables30 iterations = [1:10: 200];63 %% Basic variables 64 iterations = [1:10:1000]; 31 65 repetitions = 20; 32 66 length = 20; 67 temperature = 1000; 33 68 34 69 … … 39 74 for rep = 1:repetitions 40 75 printf('Iter:%i, Rep:%i\n',iteration,rep); 41 [new_fitness, value] = mcs(length,iteration);76 [new_fitness, value] = sa(length,iteration,temperature); 42 77 fitness = [fitness, new_fitness]; 43 78 … … 50 85 51 86 plot(iterations,fitnesses); 52 title(sprintf(' Monte-Carlo Searchon Low-Corretation set - repetitions %i',repetitions));87 title(sprintf('Simulated Annealing on Low-Corretation set - repetitions %i',repetitions)); 53 88 ylabel('fitness'); 54 89 xlabel('iterations'); 55 90 grid on; 56 91 legend(sprintf('Length %i',length)); 57 print(" mcs-fitness.eps","-depsc2");92 print("sa-fitness.eps","-depsc2"); 58 93
Note:
See TracChangeset
for help on using the changeset viewer.