Commits

Jay Barra committed e8bd20f

just need to swap out parts now

  • Participants
  • Parent commits 92eff82

Comments (0)

Files changed (1)

File Firefly/main.cpp

     }
   }
 
-  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);
-  }
+  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);
 
   if (bRoot) {
     printf("\n");
       vFireflies[iFirefly].percieved_brightness = vFireflies[iFirefly].current_fitness;
 
       if (vFireflies[iFirefly].percieved_brightness > vFireflies[iBrightest].percieved_brightness) {
-        iBrightest  = iFirefly;
+        iBrightest = iFirefly;
       }
     }
-    printf("%i: %i %f\n", id, iBrightest, vFireflies[iBrightest].percieved_brightness); 
-  }
-
-  // Move
-
-  // Update other nodes
+    //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
+    }
 
+    // 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);
+  }
+  MPI_Barrier(MPI_COMM_WORLD); 
 
   // Find the best of the swarm
-  // MPI_Reduce()
-
-
+  int iBest = 0;
+  for (int i = 1; i < iNodes; i++) {
+    if (vFireflies[i].best_fitness > vFireflies[iBest].best_fitness) {
+      iBest = i;
+    }
+  }
+  if (bRoot) {
+    printf("And the winner is: %i (%f, %f) -> %f\n", iBest, vFireflies[iBest].best_pos.x, vFireflies[iBest].best_pos.y, vFireflies[iBest].best_fitness);
+  }
+ 
   MPI_Finalize();
   
   return 0; 
 }
 //-------------------------------------------------
-/*
-void updateFirefly()
-{
-  float fFitness;
-  int iNeighborX;
-  int iNeighborY;
-
-  struct Firefly nearestNeighbor;
-
-  foreach (SwarmParticle* p, m_swarmParticles) {
-    int iNeighborDist = 32768;
-    foreach (SwarmParticle* op, m_swarmParticles) {
-      if (p == op) // ourself
-        continue;
-
-          iNeighborX = abs(p->pos().x() - op->pos().x());
-          iNeighborY = abs(p->pos().y() - op->pos().y());
-
-          iNeighborX *= iNeighborX;
-          iNeighborY *= iNeighborY;
-
-          if (iNeighborDist > sqrt((double)iNeighborX + (double)iNeighborY)) {
-            iNeighborDist = sqrt((double)iNeighborX + (double)iNeighborY);
-            nearestNeighbor = op;
-          }
-        }
-
-        float c1 = 2.0; // cognitive
-        float c2 = 2.0; // social
-
-        float rx1 = (rand() % 1000) / 1000.0;
-        float rx2 = (rand() % 1000) / 1000.0;
-
-        float fVXNext;
-        if (abs(nearestNeighbor->pos().x() - p->pos().x() + 50) < abs(bestSwarmPos().x() - p->pos().x())) {
-          float xLocal = c1 * rx1 * (p->bestPos().x() - p->pos().x());
-          float xGroup = c2 * rx2 * (bestSwarmPos().x() - p->pos().x());
-
-          fVXNext = p->velocity().fVX + xLocal + xGroup;
-        } else {
-          fVXNext = p->velocity().fVX - (nearestNeighbor->pos().x() - p->pos().x());
-        }
-
-        float ry1 = (rand() % 1000) / 1000.0;
-        float ry2 = (rand() % 1000) / 1000.0;
-
-        float fVYNext;
-        if (abs(nearestNeighbor->pos().y() - p->pos().y() + 50) < abs(bestSwarmPos().y() - p->pos().y())) {
-          float yLocal = c1 * rx1 * (p->bestPos().y() - p->pos().y());
-          float yGroup = c2 * rx2 * (bestSwarmPos().y() - p->pos().y());
-
-          fVYNext = p->velocity().fVY + yLocal + yGroup;
-        } else {
-          fVYNext = p->velocity().fVY - (nearestNeighbor->pos().y() - p->pos().y());
-        }
-
-        // Velocity Limiter
-        if (fVXNext > 10)
-          fVXNext = 10;
-        if (fVXNext < -10)
-          fVXNext = -10;
-
-        if (fVYNext > 10)
-          fVYNext = 10;
-        if (fVYNext < -10)
-          fVYNext = -10;
-
-        // Update the velocity of the particle
-        p->setVelocity(fVXNext, fVYNext);
-
-        // Update the end position of the particle base ond the new velocity
-        QPoint endPos(p->pos().x() + fVXNext, p->pos().y() + fVYNext);
-
-        fFitness = fitness(endPos);
-
-        if (fFitness < p->fitness()) {
-          p->setBestPos(endPos, fFitness);
-        }
-
-        if (fFitness < m_fBest) {
-          setBestSwarmPos(endPos, fFitness);
-        }
-
-        QPropertyAnimation* an = new QPropertyAnimation(p, "pos");
-        an->setStartValue(p->pos());
-        an->setEndValue(endPos);
-        an->setDuration(300);
-        aniGroup->addAnimation(an);
-      }
-
-      aniGroup->start(QAbstractAnimation::DeleteWhenStopped);
-}
-*/
-//-------------------------------------------------
 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 = 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;
         f.best_pos     = f.current_pos;