- Timestamp:
- Dec 16, 2009, 5:26:00 PM (15 years ago)
- Location:
- liacs/nc/low-correlation
- Files:
-
- 3 added
- 3 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
liacs/nc/low-correlation/mcs-call.m
r34 r38 5 5 % Brute-force result of length 20 6 6 % best_20 = [-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,1,1,1,1,1]; 7 % autocorrelation(best_20);8 9 function [fitness,value] = mcs(length, iterations)10 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);20 if (fitness > best_fitness)21 best_value = s;22 best_fitness = fitness;23 endif24 endfor25 fitness = best_fitness;26 value = best_value;27 endfunction28 7 29 8 % Basic variables … … 32 11 length = 20; 33 12 34 35 13 % Plot the stuff 36 14 fitnesses = []; … … 38 16 fitness = []; 39 17 for rep = 1:repetitions 40 printf('Iter:%i, Rep:%i\n',iteration,rep);18 fprintf('Iter:%i, Rep:%i\n',iteration,rep); 41 19 [new_fitness, value] = mcs(length,iteration); 42 20 fitness = [fitness, new_fitness]; … … 44 22 % Little hack to display output nicely 45 23 % disp(rot90(value,-1)); 46 endfor 47 24 end 48 25 fitnesses = [fitnesses,mean(fitness)]; 49 end for26 end 50 27 51 28 plot(iterations,fitnesses); … … 55 32 grid on; 56 33 legend(sprintf('Length %i',length)); 57 print( "mcs-fitness.eps","-depsc2");34 print('mcs-fitness.eps','-depsc2'); 58 35 -
liacs/nc/low-correlation/mcs.m
r31 r38 21 21 best_value = s; 22 22 best_fitness = fitness; 23 end if24 end for23 end 24 end 25 25 fitness = best_fitness; 26 26 value = best_value; 27 endfunction 28 29 % Basic variables 30 iterations = [1:10:200]; 31 repetitions = 20; 32 length = 20; 33 34 35 % Plot the stuff 36 fitnesses = []; 37 for iteration = iterations 38 fitness = []; 39 for rep = 1:repetitions 40 printf('Iter:%i, Rep:%i\n',iteration,rep); 41 [new_fitness, value] = mcs(length,iteration); 42 fitness = [fitness, new_fitness]; 43 44 % Little hack to display output nicely 45 % disp(rot90(value,-1)); 46 endfor 47 48 fitnesses = [fitnesses,mean(fitness)]; 49 endfor 50 51 plot(iterations,fitnesses); 52 title(sprintf('Monte-Carlo Search on Low-Corretation set - repetitions %i',repetitions)); 53 ylabel('fitness'); 54 xlabel('iterations'); 55 grid on; 56 legend(sprintf('Length %i',length)); 57 print("mcs-fitness.eps","-depsc2"); 58 27 end -
liacs/nc/low-correlation/sa-call.m
r34 r38 2 2 % BSDLicence 3 3 % Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl> 4 5 % Brute-force result of length 206 % best_20 = [-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,1,1,1,1,1];7 % autocorrelation(best_20);8 9 function number = randint(low,high)10 number = round(rand() * (high - low)) + low;11 endfunction12 13 function new = mutation(old)14 loc = randint(1,length(old));15 old(loc) = old(loc) * -1;16 new = old;17 endfunction18 19 function s = initseq(n)20 % Generate a random column s={-1,1}^n21 s = rand(n,1);22 s = round(s);23 s = s - (s == 0);24 endfunction25 26 function [fitness,value] = sa(length, temperature, stopLimit)27 best_fitness = 0;28 s = initseq(length);29 stopCounter = 0;30 31 while (stopCounter < stopLimit)32 % Generate new mutation33 newseq = mutation(s);34 35 % Better is always accept36 fitness = autocorrelation(newseq);37 if (fitness > best_fitness)38 best_value = s;39 best_fitness = fitness;40 s = newseq;41 fitness42 disp(rot90(newseq,-1));43 stopCounter = 0;44 else45 % Make the next 'move' less atractive46 temperature =- 1;47 48 stopCounter += 1;49 % Accept on an certain probability50 if (temperature < 0)51 break;52 else53 if(randint(0,temperature) > temperature / 4)54 s = newseq;55 endif56 endif57 endif58 endwhile59 value = best_value;60 fitness = best_fitness;61 endfunction62 4 63 5 %% Basic variables … … 67 9 temperature = 1000; 68 10 11 iteration_history = []; 12 fitness_history = []; 69 13 70 14 % Plot the stuff 71 fitnesses = [];72 15 for iteration = iterations 73 16 fitness = []; 74 17 for rep = 1:repetitions 75 printf('Iter:%i, Rep:%i\n',iteration,rep);18 fprintf('Iter:%i, Rep:%i\n',iteration,rep); 76 19 [new_fitness, value] = sa(length,iteration,temperature); 77 20 fitness = [fitness, new_fitness]; … … 79 22 % Little hack to display output nicely 80 23 % disp(rot90(value,-1)); 81 endfor82 83 fitness es = [fitnesses,mean(fitness)];84 end for24 end 25 iteration_history = [iteration_history,iteration]; 26 fitness_history = [fitness_history,mean(fitness)]; 27 end 85 28 86 plot(iteration s,fitnesses);29 plot(iteration_history,fitness_history); 87 30 title(sprintf('Simulated Annealing on Low-Corretation set - repetitions %i',repetitions)); 88 31 ylabel('fitness'); … … 90 33 grid on; 91 34 legend(sprintf('Length %i',length)); 92 print( "sa-fitness.eps","-depsc2");35 print('sa-fitness.eps','-depsc2'); 93 36 -
liacs/nc/low-correlation/sa.m
r32 r38 2 2 % BSDLicence 3 3 % Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl> 4 5 % Brute-force result of length 206 % best_20 = [-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,1,1,1,1,1];7 % autocorrelation(best_20);8 9 function number = randint(low,high)10 number = round(rand() * (high - low)) + low;11 endfunction12 13 function new = mutation(old)14 loc = randint(1,length(old));15 old(loc) = old(loc) * -1;16 new = old;17 endfunction18 19 function s = initseq(n)20 % Generate a random column s={-1,1}^n21 s = rand(n,1);22 s = round(s);23 s = s - (s == 0);24 endfunction25 4 26 5 function [fitness,value] = sa(length, temperature, stopLimit) … … 39 18 best_fitness = fitness; 40 19 s = newseq; 41 fitness 42 disp(rot90(newseq,-1)); 20 % disp(rot90(newseq,-1)); 43 21 stopCounter = 0; 44 22 else … … 46 24 temperature =- 1; 47 25 48 stopCounter +=1;26 stopCounter =+ 1; 49 27 % Accept on an certain probability 50 28 if (temperature < 0) … … 53 31 if(randint(0,temperature) > temperature / 4) 54 32 s = newseq; 55 endif56 endif57 endif58 endwhile33 end 34 end 35 end 36 end 59 37 value = best_value; 60 38 fitness = best_fitness; 61 endfunction 62 63 %% Basic variables 64 iterations = [1:10:1000]; 65 repetitions = 20; 66 length = 20; 67 temperature = 1000; 68 69 70 % Plot the stuff 71 fitnesses = []; 72 for iteration = iterations 73 fitness = []; 74 for rep = 1:repetitions 75 printf('Iter:%i, Rep:%i\n',iteration,rep); 76 [new_fitness, value] = sa(length,iteration,temperature); 77 fitness = [fitness, new_fitness]; 78 79 % Little hack to display output nicely 80 % disp(rot90(value,-1)); 81 endfor 82 83 fitnesses = [fitnesses,mean(fitness)]; 84 endfor 85 86 plot(iterations,fitnesses); 87 title(sprintf('Simulated Annealing on Low-Corretation set - repetitions %i',repetitions)); 88 ylabel('fitness'); 89 xlabel('iterations'); 90 grid on; 91 legend(sprintf('Length %i',length)); 92 print("sa-fitness.eps","-depsc2"); 93 39 end
Note:
See TracChangeset
for help on using the changeset viewer.