Changeset 35 for liacs/nc


Ignore:
Timestamp:
Dec 16, 2009, 3:22:18 PM (15 years ago)
Author:
Rick van der Zwet
Message:

Make it work under MatLab

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

Legend:

Unmodified
Added
Removed
  • liacs/nc/laser-pulse-shaping/pso.m

    r34 r35  
    33% Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl>
    44% Modeled after http://en.wikipedia.org/wiki/Particle_swarm_optimization
    5 
    6 function number = randint(low,high)
    7     number = round(rand() * (high - low)) + low;
    8 endfunction
    9 
    10 function new = mutation(old)
    11     loc = randint(1,length(old));
    12     old(loc) = old(loc) * -1;
    13     new = old;
    14 endfunction
    15 
    16 function s = initseq(n)
    17     % Generate a random column s={-1,1}^n
    18     s = rand(n,1);
    19     s = round(s);
    20     s = s - (s == 0);
    21 endfunction
    22 
    23 % Iterate on multiple vectors
    24 function fitness = SHGa(v)
    25     for i = 1:length(v(1,:))
    26         fitness(i) = SHG(v(:,i));
    27     endfor
    28 endfunction
    295
    306% Parameters
     
    4622flock_p = rand(parameters,local_swarm_size,local_swarms) .* (2 * pi);
    4723flock_v = zeros(size(flock_p));
    48    
     24
    4925
    5026% Global best placeholder
     
    6844                l_fitness(p,s) = fitness(p);
    6945                l_best(:,p,s) = flock_p(:,p,s);
    70             endif
    71         endfor
     46            end
     47        end
    7248
    7349        % See if we got any better neighbor optimum
     
    7652                n_fitness(s) = l_fitness(p,s);
    7753                n_best(:,s) = l_best(:,p,s);
    78             endif
    79         endfor
    80     endfor
     54            end
     55        end
     56    end
    8157
    8258    % See wether we have a new global optimum
     
    8561          g_fitness = n_fitness(s);
    8662          g_best = n_best(s);
    87         endif
    88     endfor
     63        end
     64    end
    8965
    9066    % Update particles to new value
     
    9470    for s = 1:local_swarms
    9571        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
    107 endfor
    108 
    109 
    110 
    111 %plot(iterations,fitnesses);
     72            flock_v(:,p,s) = flock_v(:,p,s) * wander;
     73            flock_v(:,p,s) =+ g_best * (c_cognitive * r_cognitive);
     74            flock_v(:,p,s) =+ n_best(s) * (c_social * r_social);
     75            flock_v(:,p,s) =+ l_best(p,s) .* (c_ego *r_ego);
     76            flock_p(:,p,s) =+ flock_v(:,p,s);
     77        end
     78    end
     79end
     80i = 1:10;
     81y = i;
     82plot(i,y);
     83print -deps pso-fitness.eps;
    11284%title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem - repetitions %i',repetitions));
    11385%ylabel('fitness');
Note: See TracChangeset for help on using the changeset viewer.