- Timestamp:
- Dec 16, 2009, 3:22:18 PM (15 years ago)
- Location:
- liacs/nc/laser-pulse-shaping
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
liacs/nc/laser-pulse-shaping/pso.m
r34 r35 3 3 % Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl> 4 4 % 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 endfunction9 10 function new = mutation(old)11 loc = randint(1,length(old));12 old(loc) = old(loc) * -1;13 new = old;14 endfunction15 16 function s = initseq(n)17 % Generate a random column s={-1,1}^n18 s = rand(n,1);19 s = round(s);20 s = s - (s == 0);21 endfunction22 23 % Iterate on multiple vectors24 function fitness = SHGa(v)25 for i = 1:length(v(1,:))26 fitness(i) = SHG(v(:,i));27 endfor28 endfunction29 5 30 6 % Parameters … … 46 22 flock_p = rand(parameters,local_swarm_size,local_swarms) .* (2 * pi); 47 23 flock_v = zeros(size(flock_p)); 48 24 49 25 50 26 % Global best placeholder … … 68 44 l_fitness(p,s) = fitness(p); 69 45 l_best(:,p,s) = flock_p(:,p,s); 70 end if71 end for46 end 47 end 72 48 73 49 % See if we got any better neighbor optimum … … 76 52 n_fitness(s) = l_fitness(p,s); 77 53 n_best(:,s) = l_best(:,p,s); 78 end if79 end for80 end for54 end 55 end 56 end 81 57 82 58 % See wether we have a new global optimum … … 85 61 g_fitness = n_fitness(s); 86 62 g_best = n_best(s); 87 end if88 end for63 end 64 end 89 65 90 66 % Update particles to new value … … 94 70 for s = 1:local_swarms 95 71 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 79 end 80 i = 1:10; 81 y = i; 82 plot(i,y); 83 print -deps pso-fitness.eps; 112 84 %title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem - repetitions %i',repetitions)); 113 85 %ylabel('fitness');
Note:
See TracChangeset
for help on using the changeset viewer.