top of page

Parçacık Sürü Optimizasyonu Örneği

Merhaba, yaklaşık bir sene önce youtube kanalımda Parçacık Sürü Optimizasyonunu (PSO'yu) tanıtan bir video çekmiştim. Aşağıdan videoya erişebilir. PSO'nun temel mantığını anlayabilirsiniz.


Bunun haricinde hem sizlere hem de bana öğreticilik açısından faydalı bir PSO kodu bırakıyorum.


Temel PSO Formülleri


Hız Güncelleme Formülü:


w : ağırlık faktörü

c1, c2 : öğrenme katsayısı

r1, r2 : [0,1] aralığında rastgele sayılar


Konum Güncelleme Formülü:

En İyi Konum Güncellemesi (Birey):



En İyi Konum Güncellemesi (Global):





PSO Örneği

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> typedef struct{ double position; double velocity; double best_position; double fitness; }Particle; #define NUM_PARTICLES 10 #define MAX_ITERATIONS 100 #define C1 2.0 // learning coeff 1 #define C2 2.0 // learning coeff 2 #define W 0.5 // weight factor double objectiveFunction(double x){ return pow(x-8, 2); //return pow(x-3,2); // amac fonksiyonu (objective func) -> (x-3)^2 } void printParticles(Particle particles[]){ int i; for(i=0; i<NUM_PARTICLES; i++){ printf("Particle: %d Position: %lf Velocity: %lf Best_pos: %lf Fitness: %lf\n",i,particles[i].position,particles[i].velocity,particles[i].best_position,particles[i].fitness); } printf("\n"); } void particleSwarmOptimization(Particle particles[], double *global_best_position, double *global_best_fitness){ // ------------- STEP 1: Particles are placed in random positions ------------- int i,iteration; srand(time(NULL)); for(i=0; i<NUM_PARTICLES; i++){ particles[i].position = ((double)rand()/RAND_MAX) * 10.0; // initial position range [0,10] particles[i].velocity = 0.0; // initial velocity particles[i].best_position = particles[i].position; // initial best position equal position particles[i].fitness = objectiveFunction(particles[i].position); // initial fitness value } // ---------------------------------------------------------------------------- // ----------------------------------------------------- STEP 2: PSO iterations ----------------------------------------------------- for(iteration=0; iteration<MAX_ITERATIONS; iteration++){ printf("iteration %d:\n",iteration); // print iteration and values printParticles(particles); // check the best position and update global best position for(i=0; i<NUM_PARTICLES; i++){ if(objectiveFunction(particles[i].best_position) > particles[i].fitness){ particles[i].best_position = particles[i].position; } if(objectiveFunction(*global_best_fitness)>particles[i].fitness){ *global_best_position = particles[i].position; *global_best_fitness = particles[i].fitness; } } // particles position and velocity update for(i=0; i<NUM_PARTICLES; i++){ // velocity update particles[i].velocity = W * particles[i].velocity + ( C1 * ((double)rand() / RAND_MAX) * (particles[i].best_position - particles[i].position) ) + ( C2 * ((double)rand()/RAND_MAX) * (*global_best_position - particles[i].position) ); // position update particles[i].position = particles[i].position + particles[i].velocity; particles[i].fitness = objectiveFunction(particles[i].position); } } // ---------------------------------------------------------------------------------------------------------------------------------- } int main(){ Particle particles[NUM_PARTICLES]; // creates 10 particles double global_best_position; double global_best_fitness; particleSwarmOptimization(particles,&global_best_position,&global_best_fitness); printf("Results:\n\n"); printf("Best location: %lf\n",global_best_position); printf("Best fitness value: %lf\n",global_best_fitness); return 0; }












19 görüntüleme0 yorum

Son Yazılar

Hepsini Gör

Comments


bottom of page