Commits

Jay Barra  committed 4e8773f

a fully functional, finally correctly reimplemented from C++ firefly algorithm

  • Participants
  • Parent commits e8bd20f

Comments (1)

Files changed (1)

File Firefly/main.cpp

 #define MESSAGE_TAG 1000
 #define WORK_TAG    1001
 //---------------------------------------------------------------------------
-#define LAMBDA 0.2
+#define GAMMA 0.2
+#define GENERATIONS 1000
 //---------------------------------------------------------------------------
 struct Position {
     float x;
   MPI_Get_processor_name(pcName, &iNameLen);
   
   bool bRoot = (id == ROOT);
-  vFireflies[id].current_pos.x = ((float)(rand() % 10000) / 10000.0);
-  vFireflies[id].current_pos.y = ((float)(rand() % 10000) / 10000.0);
+  vFireflies[id].current_pos.x = ((float)(rand() % 1000) / 1000.0);
+  vFireflies[id].current_pos.y = ((float)(rand() % 1000) / 1000.0);
 
   fitness(vFireflies[id]);
   vFireflies[id].best_fitness = vFireflies[id].current_fitness;
   } 
 
   MPI_Barrier(MPI_COMM_WORLD);
-  int iBrightest;
-  for (int iGeneration = 0; iGeneration < 1; iGeneration++){
-    iBrightest = id;
-    vFireflies[id].percieved_brightness = vFireflies[id].current_fitness;
+  float beta;
+
+  for (int iGeneration = 0; iGeneration < GENERATIONS; iGeneration++) {
+  if (bRoot) {
+    printf("Generation %i\n", iGeneration);
+  }
 
    // Compare fitnesses
     for (int iFirefly = 0; iFirefly < iNodes; iFirefly++) {
       if (iFirefly == id) continue;
-      
-      vFireflies[iFirefly].percieved_brightness = vFireflies[iFirefly].current_fitness;
-
-      if (vFireflies[iFirefly].percieved_brightness > vFireflies[iBrightest].percieved_brightness) {
-        iBrightest = iFirefly;
+      float dx = vFireflies[id].current_pos.x - vFireflies[iFirefly].current_pos.x;
+      float dy = vFireflies[id].current_pos.y - vFireflies[iFirefly].current_pos.y;
+      float dist = sqrt((dx * dx) + (dy * dy));
+      vFireflies[iFirefly].percieved_brightness = vFireflies[iFirefly].current_fitness * exp(-GAMMA * dist);
+
+      if (vFireflies[iFirefly].percieved_brightness > vFireflies[id].current_fitness) {
+        beta = 1;
+      } else {
+        beta = 0;
       }
-    }
-    //printf("%i: %i %f\n", id, iBrightest, vFireflies[iBrightest].percieved_brightness); 
-  
-    if (iBrightest == id) { 
-      // Move randomly
-      vFireflies[id].current_pos.x += (rand() % 1000) / 1000.0;
-      vFireflies[id].current_pos.y += (rand() % 1000) / 1000.0;
-    } else {
-      // Move towards the brighter firefly
-    }
+       vFireflies[id].current_pos.x += beta * exp(-1 * GAMMA * dx * dx) + ((float)(rand() % 1000)/ 1000.0);
+       vFireflies[id].current_pos.y += beta * exp(-1 * GAMMA * dy * dy) + ((float)(rand() % 1000)/ 1000.0);
 
-    // Recalculate fitness
-    fitness(vFireflies[id]);
-   
-    MPI_Bcast(&vFireflies[id].current_pos.x,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
-    MPI_Bcast(&vFireflies[id].current_pos.y,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
-    MPI_Bcast(&vFireflies[id].current_fitness, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+      fitness(vFireflies[id]);
+      //printf("[%i (%f, %f) -> %f\n", id, vFireflies[id].current_pos.x, vFireflies[id].current_pos.y, vFireflies[id].current_fitness);
+    
+    }
+      MPI_Bcast(&vFireflies[id].current_pos.x,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+      MPI_Bcast(&vFireflies[id].current_pos.y,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+      MPI_Bcast(&vFireflies[id].current_fitness, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+  //MPI_Barrier(MPI_COMM_WORLD); 
   }
-  MPI_Barrier(MPI_COMM_WORLD); 
 
   // Find the best of the swarm
   int iBest = 0;
 /*
  *(x^3-3*x + y^3-3*y)
  */
-    //f.current_fitness = pow(f.current_pos.x, 3) - (3 * f.current_pos.x) +
-     //                   pow(f.current_pos.y, 3) - (3 * f.current_pos.y);
+    //f.current_fitness = pow(f.current_pos.x, 3) - (3 * f.current_pos.x) + //                   pow(f.current_pos.y, 3) - (3 * f.current_pos.y);
     f.current_fitness = sin(sqrt(pow(f.current_pos.x, 2) + pow(f.current_pos.y, 2)));
     if (f.current_fitness > f.best_fitness) {
         f.best_fitness = f.current_fitness;