Commits

Jay Barra committed 3c33037

moved over a more reliable rand generator from the PSO code

  • Participants
  • Parent commits 7fc38cf

Comments (0)

Files changed (1)

File Firefly/main.cpp

 //---------------------------------------------------------------------------
 // Algorithm Functions
 float fitness(struct Firefly& f);
+float randFloat(float a, float b);
 //---------------------------------------------------------------------------
 // MPI Functions
 void getStatus(MPI_Status status, char* pcStatus);
 //---------------------------------------------------------------------------
 main (int argc, char** argv)
 {
-  srand(time(NULL));
 
   MPI_Init(&argc, &argv);
+  srand(time(NULL));
 	
   int iNameLen;
   int iNodes;
   MPI_Get_processor_name(pcName, &iNameLen);
   
   bool bRoot = (id == ROOT);
-  vFireflies[id].current_pos.x = ((float)(rand() % 1000) / 1000.0);
-  vFireflies[id].current_pos.y = ((float)(rand() % 1000) / 1000.0);
-
+  vFireflies[id].current_pos.x = randFloat(0.0, 4.0);
+  vFireflies[id].current_pos.y = randFloat(0.0, 4.0);
+ 
   fitness(vFireflies[id]);
   vFireflies[id].best_fitness = vFireflies[id].current_fitness;
   vFireflies[id].best_pos.x = vFireflies[id].current_pos.x;
         beta = 0;
       }
       
-      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);
+      vFireflies[id].current_pos.x += beta * exp(-1 * GAMMA * dx * dx) + 0.1 * randFloat(0.0, 1.0);
+      vFireflies[id].current_pos.y += beta * exp(-1 * GAMMA * dy * dy) + 0.1 * randFloat(0.0, 1.0);
       if (bRoot) {
         printf("calc:%f dx: %f  dy:%f  dist:%f  newx:%f  newy:%f\n", beta * exp(-1 * GAMMA * dx * dx), dx, dy, dist, vFireflies[id].current_pos.x, vFireflies[id].current_pos.y);
       }
         MPI_Bcast(&vFireflies[i].current_pos.x,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
         MPI_Bcast(&vFireflies[i].current_pos.y,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
         MPI_Bcast(&vFireflies[i].current_fitness, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
-        if(bRoot) {
-          //printf("%i (%f, %f) -> %f\n", i, vFireflies[i].current_pos.x, vFireflies[i].current_pos.y, vFireflies[i].current_fitness);
-        }
       }
       if (bRoot) { printf("\n"); }
       
 //-------------------------------------------------
 float fitness(struct Firefly& f)
 {
-/*
- *(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 = sin(sqrt(pow(f.current_pos.x, 2) + pow(f.current_pos.y, 2)));
+    f.current_fitness =  -1 * pow(f.current_pos.x - .5, 2) + 4 * f.current_pos.x;
     if (f.current_fitness > f.best_fitness) {
         f.best_fitness = f.current_fitness;
-        f.best_pos     = f.current_pos;
+        f.best_pos.x   = f.current_pos.x;
+        f.best_pos.y   = f.current_pos.y; 
     }
 
     return f.current_fitness;
 }
 //-------------------------------------------------
+float randFloat(float a, float b)
+{
+  float random = ((float) rand()) / (float) RAND_MAX;
+  float diff = b - a;
+  float r = random * diff;
+  return a + r;
+}
+//-------------------------------------------------
 void getStatus(MPI_Status status, char* pcStatus)
 {
 switch (status.MPI_ERROR) {