Commits

Jay Barra  committed 1da353d

more firefly structure

  • Participants
  • Parent commits 0d48c3f

Comments (0)

Files changed (1)

File Firefly/main.cpp

     float y;
 };
 
-struct Firefly {
-    int id;
+typedef struct Firefly {
+    float percieved_brightness;
+
     struct Position current_pos;
     float current_fitness;
 
     struct Position best_pos;
     float best_fitness;
-};
+} MPI_FIREFLY;
 //---------------------------------------------------------------------------
 // Algorithm Functions
 float fitness(struct Firefly& f);
   char* pcMessage = (char*)malloc(1024);
   char* pcStatus  = (char*)malloc(1024);
 
-  float vFitness[8] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
+  struct Firefly vFireflies[8]; 
   
   MPI_Comm_rank(MPI_COMM_WORLD, &id);
   MPI_Comm_size(MPI_COMM_WORLD, &iNodes);
   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);
 
-  Firefly firefly;
-  firefly.current_pos.x = ((float)rand() / (RAND_MAX)) + 1;
-  firefly.current_pos.y = ((float)rand() / (RAND_MAX)) + 1;
-  firefly.id = id;
-
-  fitness(firefly);
-  firefly.best_fitness = firefly.current_fitness;
-  firefly.best_pos.x = firefly.current_pos.x;
-  firefly.best_pos.y = firefly.current_pos.y;
-  
-  vFitness[id] = firefly.current_fitness;
+  fitness(vFireflies[id]);
+  vFireflies[id].best_fitness = vFireflies[id].current_fitness;
+  vFireflies[id].best_pos.x = vFireflies[id].current_pos.x;
+  vFireflies[id].best_pos.y = vFireflies[id].current_pos.y;
 
   if (!bRoot) {
     sprintf(pcMessage, "%i, %s", id, pcName);
     printf("%i Nodes Registered for work...\n", iNodes);
     printf("*********************************************************\n");
   }
- 
 
-  printf("%i-> fitness:  %f\n", id, firefly.current_fitness); 
   if (!bRoot) {
-    MPI_Send(&firefly.current_fitness, 1, MPI_FLOAT, ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
+    MPI_Send(&vFireflies[id].current_pos.x, 1, MPI_FLOAT , ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
+    MPI_Send(&vFireflies[id].current_pos.y, 1, MPI_FLOAT , ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
+    MPI_Send(&vFireflies[id].current_fitness, 1, MPI_FLOAT , ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
   } else {
     for (int iSource = 1; iSource < iNodes; iSource++) {
-      MPI_Recv(&vFitness[iSource], 1, MPI_FLOAT, iSource, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+      MPI_Recv(&vFireflies[iSource].current_pos.x,   1, MPI_FLOAT, iSource, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+      MPI_Recv(&vFireflies[iSource].current_pos.y,   1, MPI_FLOAT, iSource, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+      MPI_Recv(&vFireflies[iSource].current_fitness, 1, MPI_FLOAT, iSource, MESSAGE_TAG, MPI_COMM_WORLD, &status);
     }
   }
-  MPI_Barrier(MPI_COMM_WORLD);
+
+  for (int iFirefly = 0; iFirefly < iNodes; iFirefly++) {
+    MPI_Bcast(&vFireflies[iFirefly].current_pos.x,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+    MPI_Bcast(&vFireflies[iFirefly].current_pos.y,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+    MPI_Bcast(&vFireflies[iFirefly].current_fitness, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+  }
+
   if (bRoot) {
-    for (int i = 0; i < iNodes; i++) {
-      printf("%f ", vFitness[i]);
-    }
     printf("\n");
-  }
-  
-  int iBrightestNeighbor;
+    for (int iFirefly = 0; iFirefly < iNodes; iFirefly++) {
+      printf("%i: (%f, %f) -> %f\n", iFirefly, vFireflies[iFirefly].current_pos.x, vFireflies[iFirefly].current_pos.y, vFireflies[iFirefly].current_fitness);
+   }
+  } 
 
-  for (int iGeneration = 0; iGeneration < 5000; iGeneration++){
-    iBrightestNeighbor = -1;
+  MPI_Barrier(MPI_COMM_WORLD);
+  int iBrightest;
+  float fBrightness;
+
+  for (int iGeneration = 0; iGeneration < 1; iGeneration++){
+    iBrightest = -1;
    // Compare fitnesses
     for (int iFirefly = 0; iFirefly < iNodes; iFirefly++) {
       if (iFirefly == id) continue;
-      if (vFitness[iFirefly] > firefly.current_fitness) {
-        if (iBrightestNeighbor != -1) {
-          iBrightestNeighbor = iFirefly; 
-        } else {
-          if (vFitness[iBrightestNeighbor] > vFitness[iFirefly]) {
-            iBrightestNeighbor = iFirefly;
-          }
+      vFireflies[iFirefly].percieved_brightness = vFireflies[iFirefly].current_fitness;
+      if (iBrightest == -1) {
+        if (vFireflies[iFirefly].percieved_brightness > vFireflies[id].current_fitness) {
+          iBrightest  = iFirefly;
+          fBrightness = vFireflies[iFirefly].percieved_brightness;
+        }
+      } else {
+        if (fBrightness < vFireflies[iFirefly].current_fitness) {
+          iBrightest = iFirefly;
+          fBrightness = vFireflies[iFirefly].percieved_brightness;
         }
       }
-    } 
 
- 
+    if (iBrightest == -1) iBrightest = id;
+
+    printf("%i: %i %f\n", id, iBrightest, fBrightness); 
+    }
   }
 
+  // Move
 
+  // Update other nodes
 
 
-/*
-  MPI_Bcast(vFitness, 8, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+  // Find the best of the swarm
+  // MPI_Reduce()
+
 
-  for (int iSource = 0; iSource < iNodes; iSource++) {
-    if (iSource == id) continue;
-   
-  }
-*/
   MPI_Finalize();
   
   return 0;