Changeset 34 for liacs/nc/laser-pulse-shaping
- Timestamp:
- Dec 16, 2009, 1:24:21 PM (15 years ago)
- 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.m2 @octave -q mcs.m1 pso.out: pso.m 2 @octave -q pso.m 3 3 -
liacs/nc/laser-pulse-shaping/pso.m
r32 r34 1 % Simulated Annealing low-autocorrelation program1 % Particle Swarm optimalisation 2 2 % BSDLicence 3 3 % 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 8 5 9 6 function number = randint(low,high) … … 24 21 endfunction 25 22 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 24 function fitness = SHGa(v) 25 for i = 1:length(v(1,:)) 26 fitness(i) = SHG(v(:,i)); 27 endfor 61 28 endfunction 62 29 63 %% Basic variables 64 iterations = [1:10:1000]; 65 repetitions = 20; 66 length = 20; 67 temperature = 1000; 30 % Parameters 31 iterations = 1; 32 parameters = 3; 33 local_swarm_size = 2; 34 local_swarms = 5; 35 wander = 0.5; 36 37 % 'Influence' of the envirionment with regards to solutions 38 % Trust the group global solution to be feasible 39 c_social = 0.4; 40 % Trust the neighbor solution to be feasible 41 c_cognitive = 0.4; 42 % Trust the own best solution to be feasible 43 c_ego = 0.2; 44 45 % Initiate all particles 46 flock_p = rand(parameters,local_swarm_size,local_swarms) .* (2 * pi); 47 flock_v = zeros(size(flock_p)); 48 49 50 % Global best placeholder 51 g_best = zeros(parameters,1); 52 g_fitness = 1; 53 % at (:,x) lives the neighbor best of local_swarm 'x' 54 n_best = zeros(parameters,local_swarms); 55 n_fitness = ones(local_swarms); 56 % at (:,p,x) leves the local best of particle 'p' in local_swarm 'x' 57 l_best = zeros(parameters,local_swarm_size,local_swarms); 58 l_fitness = ones(local_swarm_size, local_swarms); 68 59 69 60 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 62 for 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 78 72 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 81 80 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 84 107 endfor 85 108 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");93 109 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.