Changeset 36
- Timestamp:
- Dec 16, 2009, 4:21:58 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
liacs/nc/laser-pulse-shaping/pso.m
r35 r36 4 4 % Modeled after http://en.wikipedia.org/wiki/Particle_swarm_optimization 5 5 6 % Parameters 7 iterations = 1; 8 parameters = 3; 9 local_swarm_size = 2; 10 local_swarms = 5; 11 wander = 0.5; 6 % Dimention settings 7 parameters = 80; 8 9 % If global optimum does not change this many steps bail out 10 iteration_break = 5; 11 max_iterations = 1000; 12 max_time = 5 * 60; % in sec 13 14 % Flock properties 15 local_swarm_size = 10; 16 local_swarms = 50; 17 18 %% Particle properties 19 % Speed of walking around to a certain direction 20 wander = 0.4; 12 21 13 22 % 'Influence' of the envirionment with regards to solutions … … 18 27 % Trust the own best solution to be feasible 19 28 c_ego = 0.2; 29 30 % Variables used for plotting 31 fitness_history = []; 32 fitness_iterations = []; 20 33 21 34 % Initiate all particles … … 29 42 % at (:,x) lives the neighbor best of local_swarm 'x' 30 43 n_best = zeros(parameters,local_swarms); 31 n_fitness = ones( local_swarms);44 n_fitness = ones(parameters,local_swarms); 32 45 % at (:,p,x) leves the local best of particle 'p' in local_swarm 'x' 33 46 l_best = zeros(parameters,local_swarm_size,local_swarms); 34 47 l_fitness = ones(local_swarm_size, local_swarms); 35 48 49 idle_counter = 0; 50 start_time = time(); 36 51 37 52 % Code not optimised for performance, but for readablility 38 for i = 1: iterations53 for i = 1:max_iterations 39 54 for s = 1:local_swarms 40 55 fitness = SHGa(flock_p(:,:,s)); … … 56 71 end 57 72 73 idle_counter = idle_counter + 1; 74 58 75 % See wether we have a new global optimum 59 76 for s = 1:local_swarms 60 77 if n_fitness(s) < g_fitness 61 78 g_fitness = n_fitness(s); 62 g_best = n_best(s); 79 g_best = n_best(:,s); 80 idle_counter = 0; 63 81 end 64 82 end 83 84 % Stop conditions 85 if idle_counter == iteration_break 86 printf("Caught by idle_counter\n"); 87 break; 88 end 89 if time - start_time > max_time 90 printf("Caught by max_time used \n"); 91 break; 92 end 93 94 95 printf("%i : %.15f\n", i, g_fitness); 96 fitness_iterations = [fitness_iterations, i]; 97 fitness_history = [fitness_history, g_fitness]; 98 65 99 66 100 % Update particles to new value … … 71 105 for p = 1:local_swarm_size 72 106 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);107 flock_v(:,p,s) = flock_v(:,p,s) + (g_best - flock_p(:,p,s)) * (c_cognitive * r_cognitive); 108 flock_v(:,p,s) = flock_v(:,p,s) + (n_best(:,s) - flock_p(:,p,s)) * (c_social * r_social); 109 flock_v(:,p,s) = flock_v(:,p,s) + (l_best(:,p,s) - flock_p(:,p,s)) * (c_ego * r_ego); 110 flock_p(:,p,s) = flock_p(:,p,s) + flock_v(:,p,s); 77 111 end 78 112 end 79 113 end 80 i = 1:10;81 y = i;82 plot(i,y);83 print -deps pso-fitness.eps;84 %title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem - repetitions %i',repetitions));85 %ylabel('fitness');86 %xlabel('iterations');87 %grid on;88 %legend(sprintf('Length %i',length));89 %print("sa-fitness.eps","-depsc2");90 114 115 116 plot(fitness_iterations,fitness_history); 117 title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem')); 118 ylabel('fitness'); 119 xlabel('iterations'); 120 grid on; 121 legend(sprintf('Parameters %i',parameters)); 122 print("pso-fitness.eps","-depsc2"); 123
Note:
See TracChangeset
for help on using the changeset viewer.