[Avida-SVN] r2525 - branches/energy/source/cpu

connel42 at myxo.css.msu.edu connel42 at myxo.css.msu.edu
Tue Apr 8 07:23:46 PDT 2008


Author: connel42
Date: 2008-04-08 10:23:45 -0400 (Tue, 08 Apr 2008)
New Revision: 2525

Modified:
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/cpu/cHardwareCPU.h
Log:
-renamed explore instruction to movetarget
-created explore instruction which rotates randomly then moves one cell
-created if-target and if-pheromone instructions
-created drop-pheromone instruction




Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2008-04-07 21:15:09 UTC (rev 2524)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2008-04-08 14:23:45 UTC (rev 2525)
@@ -254,6 +254,10 @@
     tInstLibEntry<tMethod>("sense-pheromone-faced", &cHardwareCPU::Inst_SensePheromoneFaced),
     tInstLibEntry<tMethod>("exploit", &cHardwareCPU::Inst_Exploit),
     tInstLibEntry<tMethod>("explore", &cHardwareCPU::Inst_Explore),
+    tInstLibEntry<tMethod>("movetarget", &cHardwareCPU::Inst_MoveTarget),
+    tInstLibEntry<tMethod>("if-target", &cHardwareCPU::Inst_IfTarget),
+    tInstLibEntry<tMethod>("if-pheromone", &cHardwareCPU::Inst_IfPheromone),
+    tInstLibEntry<tMethod>("drop-pheromone", &cHardwareCPU::Inst_DropPheromone),
 
     
     // Threading instructions
@@ -3158,7 +3162,7 @@
 // target.  If more than one target exists, it moves towards the last-seen
 // one.  If no target exists, the organism moves forward in its original
 // facing.
-bool cHardwareCPU::Inst_Explore(cAvidaContext& ctx)
+bool cHardwareCPU::Inst_MoveTarget(cAvidaContext& ctx)
 {
   int num_rotations = 0;
 
@@ -3176,7 +3180,7 @@
   const int stepsize = m_world->GetConfig().BIOMIMETIC_MOVEMENT_STEP.Get();
 
   // Find if any neighbor is a target
-  for(int i = 0; i < 8; i++) {
+  for(int i = 0; i < mycell.ConnectionList().GetSize(); i++) {
     cell_data = m_world->GetPopulation().GetCell(organism->GetCellID()).GetCellData();
 
     if(cell_data > 0) {
@@ -3218,6 +3222,7 @@
     
     m_world->GetStats().Move(*organism);
 
+/*
     // If organism is dropping pheromones, mark the appropriate cell
     if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
         (organism->GetPheromoneStatus() == true) ) {
@@ -3238,6 +3243,7 @@
 	}
 
     } //End laying pheromone
+*/
 
 
     // Write some logging information if LOG_PHEROMONE is set.  This is done
@@ -3273,6 +3279,115 @@
 
   return true;
 
+} // End Inst_MoveTarget()
+
+
+
+bool cHardwareCPU::Inst_Explore(cAvidaContext& ctx)
+{
+  int num_rotations = 0;
+
+  cPopulation& pop = m_world->GetPopulation();
+  int cellid = organism->GetCellID();
+  cPopulationCell& mycell = pop.GetCell(cellid);
+  cDeme &deme = pop.GetDeme(pop.GetCell(cellid).GetDemeID());
+  cResourceCount deme_resource_count = deme.GetDemeResourceCount();
+
+  int fromcellID, destcellID;
+  int cell_data;
+  
+  // Get stepsize. Currently, all moves are one cell regardless of stepsize.
+  // This could be changed in the future.
+  const int stepsize = m_world->GetConfig().BIOMIMETIC_MOVEMENT_STEP.Get();
+
+  // Rotate randomly.  Code taken from tumble.
+  const int num_neighbors = organism->GetNeighborhoodSize();
+  organism->Rotate(ctx.GetRandom().GetUInt(num_neighbors));
+
+
+  // Move to the faced cell
+  if(stepsize > 0) {
+    fromcellID = organism->GetCellID();
+
+    if(fromcellID == -1) {
+      return false;
+    }
+
+    destcellID = pop.GetCell(fromcellID).GetCellFaced().GetID();
+
+    /*********************/
+    // TEMP.  Remove once movement tasks are implemented.
+    if(pop.GetCell(fromcellID).GetCellData() < pop.GetCell(destcellID).GetCellData()) { // move up gradient
+      organism->SetGradientMovement(1.0);
+    } else if(pop.GetCell(fromcellID).GetCellData() == pop.GetCell(destcellID).GetCellData()) {
+      organism->SetGradientMovement(0.0);
+    } else { // move down gradient
+      organism->SetGradientMovement(-1.0);    
+    }
+    /*********************/ 
+
+    pop.SwapCells(pop.GetCell(fromcellID),pop.GetCell(destcellID));
+    pop.MoveOrganisms(ctx, pop.GetCell(fromcellID), pop.GetCell(destcellID));
+    
+    m_world->GetStats().Move(*organism);
+
+/*
+    // If organism is dropping pheromones, mark the appropriate cell
+    if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
+        (organism->GetPheromoneStatus() == true) ) {
+	
+	const double pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+	const int drop_mode =  m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+
+        cDeme &deme = pop.GetDeme(pop.GetCell(organism->GetCellID()).GetDemeID());
+
+	if(drop_mode == 0) {
+          deme.AddPheromone(fromcellID, pher_amount/2);
+          deme.AddPheromone(destcellID, pher_amount/2);
+	} else if(drop_mode == 1) {
+          deme.AddPheromone(fromcellID, pher_amount);
+	}
+	else if(drop_mode == 2) {
+          deme.AddPheromone(destcellID, pher_amount);
+	}
+
+    } //End laying pheromone
+*/
+
+
+    // Write some logging information if LOG_PHEROMONE is set.  This is done
+    // out here so that non-pheromone moves are recorded.
+    if( (m_world->GetConfig().LOG_PHEROMONE.Get() == 1) &&
+        (m_world->GetStats().GetUpdate() >= m_world->GetConfig().EXPLORE_LOG_START.Get()) ) {
+      cString tmpfilename = cStringUtil::Stringf("explore_movelog.dat");
+      cDataFile& df = m_world->GetDataFile(tmpfilename);
+
+      int rel_srcid = deme.GetRelativeCellID(fromcellID);
+      int rel_destid = deme.GetRelativeCellID(destcellID);
+      double pher_amount;
+      const int drop_mode =  m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+
+      // By columns: update ID, org ID, source cell (relative), destination cell (relative), amount dropped, drop mode
+      if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
+          (organism->GetPheromoneStatus() == true) ) {
+        pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+      } else {
+        pher_amount = 0;
+      }
+
+      cString UpdateStr = cStringUtil::Stringf("%d %d %d %d %f %d",  m_world->GetStats().GetUpdate(), organism->GetID(), rel_srcid, rel_destid, pher_amount, drop_mode);
+      df.WriteRaw(UpdateStr);
+    }
+
+    organism->Move(ctx);
+
+    return true;
+  } else {
+    return false;
+  }
+
+  return true;
+
 } // End Inst_Explore()
 
 
@@ -3349,6 +3464,7 @@
     
     m_world->GetStats().Move(*organism);
 
+/*
     // If organism is dropping pheromones, mark the appropriate cell
     if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
         (organism->GetPheromoneStatus() == true) ) {
@@ -3369,8 +3485,8 @@
 	}
 
     } //End laying pheromone
+*/
 
-
     // Write some logging information if LOG_PHEROMONE is set.  This is done
     // out here so that non-pheromone moves are recorded.
     if( (m_world->GetConfig().LOG_PHEROMONE.Get() == 1) &&
@@ -3408,6 +3524,80 @@
 
 
 
+bool cHardwareCPU::Inst_IfTarget(cAvidaContext& ctx)
+{
+  int cell_data = m_world->GetPopulation().GetCell(organism->GetCellID()).GetCellData();
+
+  if(cell_data > 0) {
+    IP().Advance();
+  }
+
+  return true;
+} //End Inst_IfTarget()
+
+
+bool cHardwareCPU::Inst_IfPheromone(cAvidaContext& ctx)
+{
+  int cellid = organism->GetCellID(); //absolute id of current cell
+  cPopulation& pop = m_world->GetPopulation();
+  cDeme &deme = pop.GetDeme(pop.GetCell(cellid).GetDemeID());
+  int relative_cell_id = deme.GetRelativeCellID(cellid);
+
+  cResourceCount deme_resource_count = deme.GetDemeResourceCount();
+  tArray<double> cell_resources = deme_resource_count.GetCellResources(relative_cell_id);
+
+  if(deme_resource_count.GetSize() == 0) return false;
+  
+  for (int i = 0; i < deme_resource_count.GetSize(); i++) {
+    if(strcmp(deme_resource_count.GetResName(i), "pheromone") == 0) {
+      if(cell_resources[i] > 0) {
+        IP().Advance();
+      }
+    }
+  }
+
+  return true;
+
+} //End Inst_IfPheromone()
+
+
+
+bool cHardwareCPU::Inst_DropPheromone(cAvidaContext& ctx)
+{
+  cPopulation& pop = m_world->GetPopulation();
+  int cellid = organism->GetCellID();
+  cDeme &deme = pop.GetDeme(pop.GetCell(cellid).GetDemeID());
+
+  // If organism is dropping pheromones, mark the appropriate cell
+  // Note: right now, we're ignoring the organism's pheromone status and always
+  //   dropping if pheromones are enabled
+  if(m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) {
+	
+    const double pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+    //const int drop_mode =  m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+
+    // We can't use the different drop modes, because we only know the cell
+    // that the organism is currently in.
+    /*
+    if(drop_mode == 0) {
+      deme.AddPheromone(fromcellID, pher_amount/2);
+      deme.AddPheromone(destcellID, pher_amount/2);
+    } else if(drop_mode == 1) {
+      deme.AddPheromone(fromcellID, pher_amount);
+    } else if(drop_mode == 2) {
+      deme.AddPheromone(destcellID, pher_amount);
+    }
+    */
+    deme.AddPheromone(cellid, pher_amount);
+//GEEZ
+
+  } //End laying pheromone
+
+  return true;
+
+} //End Inst_IfPheromone()
+
+
 // DoSensePheromone -- modified version of DoSense to only sense from
 // pheromone resource in given cell
 bool cHardwareCPU::DoSensePheromone(cAvidaContext& ctx, int cellid)
@@ -4255,7 +4445,7 @@
     exit(-1);
   }
   newFacing = GetLabel().AsIntGreyCode(NUM_NOPS) % standardNeighborhoodSize;
-  
+
   for(int i = 0; i < actualNeighborhoodSize; i++) {
     currentFacing = organism->GetFacing();
     if(newFacing == currentFacing)
@@ -4425,6 +4615,7 @@
     // updates movement predicates
     m_world->GetStats().Move(*organism);
 
+/*
     // BDC
     // If organism is dropping pheromones, mark the destination cell
     if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
@@ -4450,6 +4641,7 @@
        //pop.GetCell(destcellID).SetCellData(newval);
 
     } //End laying pheromone
+*/
 
     // Write some logging information if LOG_PHEROMONE is set.  This is done
     // out here so that non-pheromone moves are recorded.

Modified: branches/energy/source/cpu/cHardwareCPU.h
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.h	2008-04-07 21:15:09 UTC (rev 2524)
+++ branches/energy/source/cpu/cHardwareCPU.h	2008-04-08 14:23:45 UTC (rev 2525)
@@ -500,6 +500,10 @@
   bool Inst_SensePheromoneFaced(cAvidaContext& ctx);
   bool Inst_Exploit(cAvidaContext& ctx);
   bool Inst_Explore(cAvidaContext& ctx);
+  bool Inst_MoveTarget(cAvidaContext& ctx);
+  bool Inst_IfTarget(cAvidaContext& ctx);
+  bool Inst_IfPheromone(cAvidaContext& ctx);
+  bool Inst_DropPheromone(cAvidaContext& ctx);
 
 
   // Multi-threading...




More information about the Avida-cvs mailing list