Changeset 36 for liacs/nc


Ignore:
Timestamp:
Dec 16, 2009, 4:21:58 PM (15 years ago)
Author:
Rick van der Zwet
Message:

Working version for octave, ajusted to match MatLab code

File:
1 edited

Legend:

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

    r35 r36  
    44% Modeled after http://en.wikipedia.org/wiki/Particle_swarm_optimization
    55
    6 % Parameters
    7 iterations = 1;
    8 parameters = 3;
    9 local_swarm_size = 2;
    10 local_swarms = 5;
    11 wander = 0.5;
     6% Dimention settings
     7parameters = 80;
     8
     9% If global optimum does not change this many steps bail out
     10iteration_break = 5;
     11max_iterations = 1000;
     12max_time = 5 * 60; % in sec
     13
     14% Flock properties
     15local_swarm_size = 10;
     16local_swarms = 50;
     17
     18%% Particle properties
     19% Speed of walking around to a certain direction
     20wander = 0.4;
    1221
    1322% 'Influence' of the envirionment with regards to solutions
     
    1827% Trust the own best solution to be feasible
    1928c_ego = 0.2;
     29
     30% Variables used for plotting
     31fitness_history = [];
     32fitness_iterations = [];
    2033
    2134% Initiate all particles
     
    2942% at (:,x) lives the neighbor best of local_swarm 'x'
    3043n_best = zeros(parameters,local_swarms);
    31 n_fitness = ones(local_swarms);
     44n_fitness = ones(parameters,local_swarms);
    3245% at (:,p,x) leves the local best of particle 'p' in local_swarm 'x'
    3346l_best = zeros(parameters,local_swarm_size,local_swarms);
    3447l_fitness = ones(local_swarm_size, local_swarms);
    3548
     49idle_counter = 0;
     50start_time = time();
    3651
    3752% Code not optimised for performance, but for readablility
    38 for i = 1:iterations
     53for i = 1:max_iterations
    3954    for s = 1:local_swarms
    4055        fitness = SHGa(flock_p(:,:,s));
     
    5671    end
    5772
     73    idle_counter = idle_counter + 1;
     74
    5875    % See wether we have a new global optimum
    5976    for s = 1:local_swarms
    6077        if n_fitness(s) < g_fitness
    6178          g_fitness = n_fitness(s);
    62           g_best = n_best(s);
     79          g_best = n_best(:,s);
     80          idle_counter = 0;
    6381        end
    6482    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   
    6599
    66100    % Update particles to new value
     
    71105        for p = 1:local_swarm_size
    72106            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);
    77111        end
    78112    end
    79113end
    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");
    90114
     115
     116plot(fitness_iterations,fitness_history);
     117title(sprintf('Particle Swarm Optimalisation on Laser-Pulse shaping problem'));
     118ylabel('fitness');
     119xlabel('iterations');
     120grid on;
     121legend(sprintf('Parameters %i',parameters));
     122print("pso-fitness.eps","-depsc2");
     123
Note: See TracChangeset for help on using the changeset viewer.