[34] | 1 | % Particle Swarm optimalisation
|
---|
[28] | 2 | % BSDLicence
|
---|
| 3 | % Rick van der Zwet - 0433373 - <hvdzwet@liacs.nl>
|
---|
[34] | 4 | % Modeled after http://en.wikipedia.org/wiki/Particle_swarm_optimization
|
---|
[28] | 5 |
|
---|
[36] | 6 | % Dimention settings
|
---|
| 7 | parameters = 80;
|
---|
[32] | 8 |
|
---|
[36] | 9 | % If global optimum does not change this many steps bail out
|
---|
| 10 | iteration_break = 5;
|
---|
| 11 | max_iterations = 1000;
|
---|
[47] | 12 | max_time = 10 * 60; % in sec
|
---|
[36] | 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;
|
---|
| 21 |
|
---|
[34] | 22 | % 'Influence' of the envirionment with regards to solutions
|
---|
| 23 | % Trust the group global solution to be feasible
|
---|
| 24 | c_social = 0.4;
|
---|
| 25 | % Trust the neighbor solution to be feasible
|
---|
| 26 | c_cognitive = 0.4;
|
---|
| 27 | % Trust the own best solution to be feasible
|
---|
| 28 | c_ego = 0.2;
|
---|
[32] | 29 |
|
---|
[36] | 30 | % Variables used for plotting
|
---|
| 31 | fitness_history = [];
|
---|
| 32 | fitness_iterations = [];
|
---|
| 33 |
|
---|
[34] | 34 | % Initiate all particles
|
---|
| 35 | flock_p = rand(parameters,local_swarm_size,local_swarms) .* (2 * pi);
|
---|
| 36 | flock_v = zeros(size(flock_p));
|
---|
[30] | 37 |
|
---|
[35] | 38 |
|
---|
[34] | 39 | % Global best placeholder
|
---|
[47] | 40 | g_best = ones(parameters,1) .* 9;
|
---|
| 41 | g_fitness = 0;
|
---|
[34] | 42 | % at (:,x) lives the neighbor best of local_swarm 'x'
|
---|
[47] | 43 | n_best = ones(parameters,local_swarms) .* 9;
|
---|
| 44 | n_fitness = zeros(parameters,local_swarms);
|
---|
[34] | 45 | % at (:,p,x) leves the local best of particle 'p' in local_swarm 'x'
|
---|
[47] | 46 | l_best = ones(parameters,local_swarm_size,local_swarms) .* 9;
|
---|
| 47 | l_fitness = zeros(local_swarm_size, local_swarms);
|
---|
[28] | 48 |
|
---|
[36] | 49 | idle_counter = 0;
|
---|
[37] | 50 | tic();
|
---|
[31] | 51 |
|
---|
[34] | 52 | % Code not optimised for performance, but for readablility
|
---|
[36] | 53 | for i = 1:max_iterations
|
---|
[34] | 54 | for s = 1:local_swarms
|
---|
| 55 | fitness = SHGa(flock_p(:,:,s));
|
---|
| 56 | % See if we got any better local optimum
|
---|
| 57 | for p = 1:local_swarm_size
|
---|
[47] | 58 | if fitness(p) > l_fitness(p,s)
|
---|
[34] | 59 | l_fitness(p,s) = fitness(p);
|
---|
| 60 | l_best(:,p,s) = flock_p(:,p,s);
|
---|
[35] | 61 | end
|
---|
| 62 | end
|
---|
[31] | 63 |
|
---|
[34] | 64 | % See if we got any better neighbor optimum
|
---|
| 65 | for p = 1:local_swarm_size
|
---|
[47] | 66 | if l_fitness(p,s) > n_fitness(s)
|
---|
[34] | 67 | n_fitness(s) = l_fitness(p,s);
|
---|
| 68 | n_best(:,s) = l_best(:,p,s);
|
---|
[35] | 69 | end
|
---|
| 70 | end
|
---|
| 71 | end
|
---|
[34] | 72 |
|
---|
[36] | 73 | idle_counter = idle_counter + 1;
|
---|
| 74 |
|
---|
[34] | 75 | % See wether we have a new global optimum
|
---|
| 76 | for s = 1:local_swarms
|
---|
[47] | 77 | if n_fitness(s) > g_fitness
|
---|
[34] | 78 | g_fitness = n_fitness(s);
|
---|
[36] | 79 | g_best = n_best(:,s);
|
---|
| 80 | idle_counter = 0;
|
---|
[35] | 81 | end
|
---|
| 82 | end
|
---|
[34] | 83 |
|
---|
[36] | 84 | % Stop conditions
|
---|
| 85 | if idle_counter == iteration_break
|
---|
[37] | 86 | fprintf('Caught by idle_counter\n');
|
---|
[47] | 87 | break;
|
---|
[36] | 88 | end
|
---|
[37] | 89 | if toc > max_time
|
---|
| 90 | fprintf('Caught by max_time used \n');
|
---|
[47] | 91 | break;
|
---|
[36] | 92 | end
|
---|
| 93 |
|
---|
| 94 |
|
---|
[47] | 95 | fprintf('%04i : %.15f\n', i, g_fitness);
|
---|
[36] | 96 | fitness_iterations = [fitness_iterations, i];
|
---|
| 97 | fitness_history = [fitness_history, g_fitness];
|
---|
| 98 |
|
---|
| 99 |
|
---|
[34] | 100 | % Update particles to new value
|
---|
| 101 | r_cognitive = rand();
|
---|
| 102 | r_social = rand();
|
---|
| 103 | r_ego = rand();
|
---|
| 104 | for s = 1:local_swarms
|
---|
| 105 | for p = 1:local_swarm_size
|
---|
[37] | 106 | flock_v(:,p,s) = flock_v(:,p,s) * wander + ...
|
---|
| 107 | (g_best - flock_p(:,p,s)) * (c_cognitive * r_cognitive) + ...
|
---|
| 108 | (n_best(:,s) - flock_p(:,p,s)) * (c_social * r_social) + ...
|
---|
| 109 | (l_best(:,p,s) - flock_p(:,p,s)) * (c_ego * r_ego);
|
---|
[36] | 110 | flock_p(:,p,s) = flock_p(:,p,s) + flock_v(:,p,s);
|
---|
[35] | 111 | end
|
---|
| 112 | end
|
---|
| 113 | end
|
---|
[34] | 114 |
|
---|
[47] | 115 | g_best
|
---|
[36] | 116 |
|
---|
| 117 | plot(fitness_iterations,fitness_history);
|
---|
| 118 | title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem'));
|
---|
| 119 | ylabel('fitness');
|
---|
| 120 | xlabel('iterations');
|
---|
| 121 | grid on;
|
---|
| 122 | legend(sprintf('Parameters %i',parameters));
|
---|
[47] | 123 | print(sprintf('pso-fitness-%f.eps', max(fitness_history)),'-depsc2');
|
---|