Changeset 39 for liacs


Ignore:
Timestamp:
Dec 16, 2009, 8:37:47 PM (15 years ago)
Author:
Rick van der Zwet
Message:

Make it run properly (iterations vs fitness)

Location:
liacs/nc/low-correlation
Files:
3 edited

Legend:

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

    r38 r39  
    44
    55%% Basic variables
    6 iterations = [1:10:1000];
    7 repetitions = 20;
     6iterations = 100000;
    87length = 20;
    9 temperature = 1000;
    108
    11 iteration_history = [];
    12 fitness_history = [];
     9
     10% Always use the same innitial values
     11s = initseq(length);
    1312
    1413% 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);
    2815
    2916plot(iteration_history,fitness_history);
    30 title(sprintf('Simulated Annealing on Low-Corretation set - repetitions %i',repetitions));
     17title(sprintf('Simulated Annealing on Low-Corretation set'));
    3118ylabel('fitness');
    3219xlabel('iterations');
     
    3421legend(sprintf('Length %i',length));
    3522print('sa-fitness.eps','-depsc2');
    36 
  • liacs/nc/low-correlation/sa.m

    r38 r39  
    33% Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl>
    44
    5 function [fitness,value] = sa(length, temperature, stopLimit)
    6     best_fitness = 0;
    7     s = initseq(length);
    8     stopCounter = 0;
     5function [iteration_history,fitness_history] = sa(seq, stopLimit)
     6    fitness = 0;
     7    temperature = stopLimit;
    98
    10     while (stopCounter < stopLimit)
     9    iteration_history = [];
     10    fitness_history = [];
     11
     12    for iteration = 1:stopLimit
    1113        % Generate new mutation
    12         newseq = mutation(s);
     14        newseq = mutation(seq);
     15
     16        new_fitness = autocorrelation(newseq);
    1317
    1418        % 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;
    2021            % disp(rot90(newseq,-1));
    21             stopCounter = 0;
     22            temperature = temperature - 10;
    2223        else
    2324            % Make the next 'move' less atractive
    24             temperature =- 1;
     25            temperature = temperature + 1;
    2526
    26             stopCounter =+ 1;
    2727            % Accept on an certain probability
    28             if (temperature < 0)
     28            if (temperature < 1)
    2929                break;
    3030            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;
    3334               end
    3435           end
    35        end
    36    end
    37     value = best_value;
    38     fitness = best_fitness;
     36        end
     37        iteration_history = [ iteration_history, iteration ];
     38        fitness_history = [ fitness_history, fitness ];
     39    end
    3940end
Note: See TracChangeset for help on using the changeset viewer.