Commits

Jay Barra committed 0d48c3f

refactoring and wrking out bugs, ready to implement cross node communications

  • Participants
  • Parent commits 746561f

Comments (0)

Files changed (1)

File Firefly/main.cpp

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
+#include <time.h>
 #include <mpi.h>
 //---------------------------------------------------------------------------
 #define ROOT 0
 };
 
 struct Firefly {
+    int id;
     struct Position current_pos;
     float current_fitness;
 
 };
 //---------------------------------------------------------------------------
 // Algorithm Functions
-void  updateFirefly(struct Firefly f);
-
-float percievedBrightnes(struct Firefly f1, struct Firefly f2);
 float fitness(struct Firefly& f);
-
-float distance(struct Position p1, struct Position p2);
 //---------------------------------------------------------------------------
 // MPI Functions
 void getStatus(MPI_Status status, char* pcStatus);
 //---------------------------------------------------------------------------
 main (int argc, char** argv)
 {
+  srand(time(NULL));
+
   MPI_Init(&argc, &argv);
 	
   int iNameLen;
   char* pcName    = (char*)malloc(1024);
   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};
   
   MPI_Comm_rank(MPI_COMM_WORLD, &id);
   MPI_Comm_size(MPI_COMM_WORLD, &iNodes);
   bool bRoot = (id == ROOT);
 
   Firefly firefly;
-  firefly.pos_x = rand();
-  firefly.pos_y = rand();
+  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;
 
   if (!bRoot) {
-    sprintf(pcMessage, "%i", id, pcName);
+    sprintf(pcMessage, "%i, %s", id, pcName);
     MPI_Send(pcMessage, strlen(pcMessage)+1, MPI_CHAR, ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
   } else {
     printf("Initializing Nodes\n");
     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);
+  } else {
+    for (int iSource = 1; iSource < iNodes; iSource++) {
+      MPI_Recv(&vFitness[iSource], 1, MPI_FLOAT, iSource, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+    }
+  }
+  MPI_Barrier(MPI_COMM_WORLD);
+  if (bRoot) {
+    for (int i = 0; i < iNodes; i++) {
+      printf("%f ", vFitness[i]);
+    }
+    printf("\n");
+  }
+  
+  int iBrightestNeighbor;
+
+  for (int iGeneration = 0; iGeneration < 5000; iGeneration++){
+    iBrightestNeighbor = -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;
+          }
+        }
+      }
+    } 
+
+ 
+  }
 
 
 
 
+/*
+  MPI_Bcast(vFitness, 8, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
 
+  for (int iSource = 0; iSource < iNodes; iSource++) {
+    if (iSource == id) continue;
+   
+  }
+*/
   MPI_Finalize();
   
   return 0; 
 }
 //-------------------------------------------------
+/*
 void updateFirefly()
 {
   float fFitness;
   int iNeighborX;
   int iNeighborY;
 
-  SwarmParticle* nearestNeighbor;
+  struct Firefly nearestNeighbor;
 
   foreach (SwarmParticle* p, m_swarmParticles) {
     int iNeighborDist = 32768;
 
       aniGroup->start(QAbstractAnimation::DeleteWhenStopped);
 }
-//-------------------------------------------------
-float percievedBrightnes(struct Firefly f1, struct Firefly f2)
-{
-    float dist = distance(f.current_pos, f2.current_pos)
-    return pow(-LAMBDA * delta);
-}
+*/
 //-------------------------------------------------
 float fitness(struct Firefly& f)
 {
     return f.current_fitness;
 }
 //-------------------------------------------------
-float distance(struct Position p1, struct Position p2)
-{
-    return sqrt((pow(p1.x + p2.x, 2) + pow(p1.y + p2.y, 2)));
-}
-//-------------------------------------------------
 void getStatus(MPI_Status status, char* pcStatus)
 {
 switch (status.MPI_ERROR) {