Changeset 34


Ignore:
Timestamp:
Dec 16, 2009, 1:24:21 PM (15 years ago)
Author:
Rick van der Zwet
Message:

First pso attempt commit :-)

Location:
liacs/nc/laser-pulse-shaping
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • liacs/nc/laser-pulse-shaping/Makefile

    r28 r34  
    1 mcs.out: mcs.m
    2         @octave -q mcs.m
     1pso.out: pso.m
     2        @octave -q pso.m
    33   
  • liacs/nc/laser-pulse-shaping/pso.m

    r32 r34  
    1 % Simulated Annealing low-autocorrelation program
     1% Particle Swarm optimalisation
    22% BSDLicence
    33% Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl>
    4 
    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);
     4% Modeled after http://en.wikipedia.org/wiki/Particle_swarm_optimization
    85
    96function number = randint(low,high)
     
    2421endfunction
    2522
    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 mutation
    33         newseq = mutation(s);
    34 
    35         % Better is always accept
    36         fitness = autocorrelation(newseq);
    37         if (fitness > best_fitness)
    38             best_value = s;
    39             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
    57         endif
    58     endwhile
    59     value = best_value;
    60     fitness = best_fitness;
     23% Iterate on multiple vectors
     24function fitness = SHGa(v)
     25    for i = 1:length(v(1,:))
     26        fitness(i) = SHG(v(:,i));
     27    endfor
    6128endfunction
    6229
    63 %% Basic variables
    64 iterations = [1:10:1000];
    65 repetitions = 20;
    66 length = 20;
    67 temperature = 1000;
     30% Parameters
     31iterations = 1;
     32parameters = 3;
     33local_swarm_size = 2;
     34local_swarms = 5;
     35wander = 0.5;
     36
     37% 'Influence' of the envirionment with regards to solutions
     38% Trust the group global solution to be feasible
     39c_social  = 0.4;
     40% Trust the neighbor solution to be feasible
     41c_cognitive = 0.4;
     42% Trust the own best solution to be feasible
     43c_ego = 0.2;
     44
     45% Initiate all particles
     46flock_p = rand(parameters,local_swarm_size,local_swarms) .* (2 * pi);
     47flock_v = zeros(size(flock_p));
     48   
     49
     50% Global best placeholder
     51g_best = zeros(parameters,1);
     52g_fitness = 1;
     53% at (:,x) lives the neighbor best of local_swarm 'x'
     54n_best = zeros(parameters,local_swarms);
     55n_fitness = ones(local_swarms);
     56% at (:,p,x) leves the local best of particle 'p' in local_swarm 'x'
     57l_best = zeros(parameters,local_swarm_size,local_swarms);
     58l_fitness = ones(local_swarm_size, local_swarms);
    6859
    6960
    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];
     61% Code not optimised for performance, but for readablility
     62for i = 1:iterations
     63    for s = 1:local_swarms
     64        fitness = SHGa(flock_p(:,:,s));
     65        % See if we got any better local optimum
     66        for p = 1:local_swarm_size
     67            if fitness(p) < l_fitness(p,s)
     68                l_fitness(p,s) = fitness(p);
     69                l_best(:,p,s) = flock_p(:,p,s);
     70            endif
     71        endfor
    7872
    79         % Little hack to display output nicely
    80         % disp(rot90(value,-1));
     73        % See if we got any better neighbor optimum
     74        for p = 1:local_swarm_size
     75            if l_fitness(p,s) < n_fitness(s)
     76                n_fitness(s) = l_fitness(p,s);
     77                n_best(:,s) = l_best(:,p,s);
     78            endif
     79        endfor
    8180    endfor
    82    
    83     fitnesses = [fitnesses,mean(fitness)];
     81
     82    % See wether we have a new global optimum
     83    for s = 1:local_swarms
     84        if n_fitness(s) < g_fitness
     85          g_fitness = n_fitness(s);
     86          g_best = n_best(s);
     87        endif
     88    endfor
     89
     90    % Update particles to new value
     91    r_cognitive = rand();
     92    r_social = rand();
     93    r_ego = rand();
     94    for s = 1:local_swarms
     95        for p = 1:local_swarm_size
     96            flock_v(:,p,s) = (flock_v(:,p,s) .* wander) + \
     97                (g_best .* (c_cognitive * r_cognitive)) + \
     98                (n_best(s) .* (c_social *r_social)) + \
     99                (l_best(p,s) .* (c_ego *r_ego));
     100            s
     101            p
     102            flock_p(:,p,s)
     103            flock_p(:,p,s) += flock_v(:,p,s);
     104            flock_p(:,p,s)
     105        endfor
     106    endfor
    84107endfor
    85108
    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");
    93109
     110
     111%plot(iterations,fitnesses);
     112%title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem - repetitions %i',repetitions));
     113%ylabel('fitness');
     114%xlabel('iterations');
     115%grid on;
     116%legend(sprintf('Length %i',length));
     117%print("sa-fitness.eps","-depsc2");
     118
Note: See TracChangeset for help on using the changeset viewer.