Changeset 32


Ignore:
Timestamp:
Dec 13, 2009, 9:41:09 PM (15 years ago)
Author:
Rick van der Zwet
Message:

SA - fitness calculations

Location:
liacs/nc/low-correlation
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • liacs/nc/low-correlation/sa.m

    r31 r32  
    1 % Monte-Carlo Search Algoritm on low-autocorrelation program
     1% Simulated Annealing low-autocorrelation program
    22% BSDLicence
    33% Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl>
     
    77% autocorrelation(best_20);
    88
    9 function [fitness,value] = mcs(length, iterations)
     9function number = randint(low,high)
     10    number = round(rand() * (high - low)) + low;
     11endfunction
     12
     13function new = mutation(old)
     14    loc = randint(1,length(old));
     15    old(loc) = old(loc) * -1;
     16    new = old;
     17endfunction
     18
     19function 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);
     24endfunction
     25
     26function [fitness,value] = sa(length, temperature, stopLimit)
    1027    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);
     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);
    2037        if (fitness > best_fitness)
    2138            best_value = s;
    2239            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
    2357        endif
    24     endfor
     58    endwhile
     59    value = best_value;
    2560    fitness = best_fitness;
    26     value = best_value;
    2761endfunction
    2862
    29 % Basic variables
    30 iterations = [1:10:200];
     63%% Basic variables
     64iterations = [1:10:1000];
    3165repetitions = 20;
    3266length = 20;
     67temperature = 1000;
    3368
    3469
     
    3974    for rep = 1:repetitions
    4075        printf('Iter:%i, Rep:%i\n',iteration,rep);
    41         [new_fitness, value] =  mcs(length,iteration);
     76        [new_fitness, value] =  sa(length,iteration,temperature);
    4277        fitness = [fitness, new_fitness];
    4378
     
    5085
    5186plot(iterations,fitnesses);
    52 title(sprintf('Monte-Carlo Search on Low-Corretation set - repetitions %i',repetitions));
     87title(sprintf('Simulated Annealing on Low-Corretation set - repetitions %i',repetitions));
    5388ylabel('fitness');
    5489xlabel('iterations');
    5590grid on;
    5691legend(sprintf('Length %i',length));
    57 print("mcs-fitness.eps","-depsc2");
     92print("sa-fitness.eps","-depsc2");
    5893
Note: See TracChangeset for help on using the changeset viewer.