source: liacs/nc/low-correlation/mcs.m@ 32

Last change on this file since 32 was 31, checked in by Rick van der Zwet, 15 years ago

Monto Carlo Search implementation completed

File size: 1.5 KB
RevLine 
[28]1% Monte-Carlo Search Algoritm on low-autocorrelation program
2% BSDLicence
3% Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl>
4
[30]5% Brute-force result of length 20
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
[31]9function [fitness,value] = mcs(length, iterations)
10 best_fitness = 0;
11 for i = 1:iterations
12 % Generate a random column s={-1,1}^n
13 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 else
19 fitness = autocorrelation(s);
20 if (fitness > best_fitness)
21 best_value = s;
22 best_fitness = fitness;
23 endif
24 endfor
25 fitness = best_fitness;
26 value = best_value;
27endfunction
[30]28
[31]29% Basic variables
30iterations = [1:10:200];
31repetitions = 20;
32length = 20;
[28]33
[31]34
35% Plot the stuff
36fitnesses = [];
37for 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
[28]44 % Little hack to display output nicely
[31]45 % disp(rot90(value,-1));
46 endfor
47
48 fitnesses = [fitnesses,mean(fitness)];
49endfor
[28]50
[31]51plot(iterations,fitnesses);
52title(sprintf('Monte-Carlo Search on Low-Corretation set - repetitions %i',repetitions));
53ylabel('fitness');
54xlabel('iterations');
55grid on;
56legend(sprintf('Length %i',length));
57print("mcs-fitness.eps","-depsc2");
58
Note: See TracBrowser for help on using the repository browser.