Changeset 39
- Timestamp:
- Dec 16, 2009, 8:37:47 PM (15 years ago)
- Location:
- liacs/nc/low-correlation
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
liacs/nc/low-correlation/sa-call.m
r38 r39 4 4 5 5 %% Basic variables 6 iterations = [1:10:1000]; 7 repetitions = 20; 6 iterations = 100000; 8 7 length = 20; 9 temperature = 1000;10 8 11 iteration_history = []; 12 fitness_history = []; 9 10 % Always use the same innitial values 11 s = initseq(length); 13 12 14 13 % Plot the stuff 15 for iteration = iterations 16 fitness = []; 17 for rep = 1:repetitions 18 fprintf('Iter:%i, Rep:%i\n',iteration,rep); 19 [new_fitness, value] = sa(length,iteration,temperature); 20 fitness = [fitness, new_fitness]; 21 22 % Little hack to display output nicely 23 % disp(rot90(value,-1)); 24 end 25 iteration_history = [iteration_history,iteration]; 26 fitness_history = [fitness_history,mean(fitness)]; 27 end 14 [iteration_history, fitness_history] = sa(s,iterations); 28 15 29 16 plot(iteration_history,fitness_history); 30 title(sprintf('Simulated Annealing on Low-Corretation set - repetitions %i',repetitions));17 title(sprintf('Simulated Annealing on Low-Corretation set')); 31 18 ylabel('fitness'); 32 19 xlabel('iterations'); … … 34 21 legend(sprintf('Length %i',length)); 35 22 print('sa-fitness.eps','-depsc2'); 36 -
liacs/nc/low-correlation/sa.m
r38 r39 3 3 % Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl> 4 4 5 function [fitness,value] = sa(length, temperature, stopLimit) 6 best_fitness = 0; 7 s = initseq(length); 8 stopCounter = 0; 5 function [iteration_history,fitness_history] = sa(seq, stopLimit) 6 fitness = 0; 7 temperature = stopLimit; 9 8 10 while (stopCounter < stopLimit) 9 iteration_history = []; 10 fitness_history = []; 11 12 for iteration = 1:stopLimit 11 13 % Generate new mutation 12 newseq = mutation(s); 14 newseq = mutation(seq); 15 16 new_fitness = autocorrelation(newseq); 13 17 14 18 % Better is always accept 15 fitness = autocorrelation(newseq); 16 if (fitness > best_fitness) 17 best_value = s; 18 best_fitness = fitness; 19 s = newseq; 19 if (new_fitness > fitness) 20 fitness = new_fitness; 20 21 % disp(rot90(newseq,-1)); 21 stopCounter =0;22 temperature = temperature - 10; 22 23 else 23 24 % Make the next 'move' less atractive 24 temperature = -1;25 temperature = temperature + 1; 25 26 26 stopCounter =+ 1;27 27 % Accept on an certain probability 28 if (temperature < 0)28 if (temperature < 1) 29 29 break; 30 30 else 31 if(randint(0,temperature) > temperature / 4) 32 s = newseq; 31 % XXX: Some more cleaver cooling would be great 32 if( e^((fitness - new_fitness) / temperature) > rand()) 33 seq = newseq; 33 34 end 34 35 end 35 end36 end37 value = best_value;38 fitness = best_fitness;36 end 37 iteration_history = [ iteration_history, iteration ]; 38 fitness_history = [ fitness_history, fitness ]; 39 end 39 40 end
Note:
See TracChangeset
for help on using the changeset viewer.