[Avida-SVN] r2586 - in branches/energy/source: cpu main

connel42 at myxo.css.msu.edu connel42 at myxo.css.msu.edu
Mon May 19 11:31:11 PDT 2008


Author: connel42
Date: 2008-05-19 14:31:11 -0400 (Mon, 19 May 2008)
New Revision: 2586

Modified:
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/cpu/cHardwareCPU.h
   branches/energy/source/main/cDeme.cc
   branches/energy/source/main/cOrgMovementPredicate.h
Log:
added new commands sense-target-faced, movetarget-forward, exploit-forward



Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2008-05-19 18:11:40 UTC (rev 2585)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2008-05-19 18:31:11 UTC (rev 2586)
@@ -247,14 +247,17 @@
     tInstLibEntry<tMethod>("phero-off", &cHardwareCPU::Inst_PheroOff),
     tInstLibEntry<tMethod>("pherotoggle", &cHardwareCPU::Inst_PheroToggle),
     tInstLibEntry<tMethod>("sense-target", &cHardwareCPU::Inst_SenseTarget),
+    tInstLibEntry<tMethod>("sense-target-faced", &cHardwareCPU::Inst_SenseTargetFaced),
     tInstLibEntry<tMethod>("sensef", &cHardwareCPU::Inst_SenseLog2Facing),
     tInstLibEntry<tMethod>("sensef-unit", &cHardwareCPU::Inst_SenseUnitFacing),
     tInstLibEntry<tMethod>("sensef-m100", &cHardwareCPU::Inst_SenseMult100Facing),
     tInstLibEntry<tMethod>("sense-pheromone", &cHardwareCPU::Inst_SensePheromone),
     tInstLibEntry<tMethod>("sense-pheromone-faced", &cHardwareCPU::Inst_SensePheromoneFaced),
     tInstLibEntry<tMethod>("exploit", &cHardwareCPU::Inst_Exploit),
+    tInstLibEntry<tMethod>("exploit-forward", &cHardwareCPU::Inst_ExploitForward),
     tInstLibEntry<tMethod>("explore", &cHardwareCPU::Inst_Explore),
     tInstLibEntry<tMethod>("movetarget", &cHardwareCPU::Inst_MoveTarget),
+    tInstLibEntry<tMethod>("movetarget-forward", &cHardwareCPU::Inst_MoveTargetForward),
     tInstLibEntry<tMethod>("supermove", &cHardwareCPU::Inst_SuperMove),
     tInstLibEntry<tMethod>("if-target", &cHardwareCPU::Inst_IfTarget),
     tInstLibEntry<tMethod>("if-not-target", &cHardwareCPU::Inst_IfNotTarget),
@@ -3251,10 +3254,6 @@
           deme.AddPheromone(destcellID, pher_amount);
 	}
 
-       // Old CellData-based version
-       //const int newval = pop.GetCell(destcellID).GetCellData() + 1;
-       //pop.GetCell(destcellID).SetCellData(newval);
-
     } //End laying pheromone
 
 
@@ -3285,6 +3284,133 @@
 
 
 
+// This command should move the organism to the neighbor cell that is a
+// 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.  This version ignores the neighbors beind the organism, i.e.
+// the cells directly behind and behind and to the left and right.
+bool cHardwareCPU::Inst_MoveTargetForward(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();
+
+  // Pheromone drop stuff
+  double pher_amount = 0;
+  int drop_mode = -1;
+
+  cPopulationCell faced = mycell.GetCellFaced();
+
+  // Find if any neighbor is a target
+  for(int i = 0; i < mycell.ConnectionList().GetSize(); i++) {
+    
+    // Skip the cells behind
+    if(i == 3 || i == 4 || i == 5) {
+      mycell.ConnectionList().CircNext();
+      continue;
+    }
+
+    cell_data = mycell.GetCellFaced().GetCellData();
+
+    if(cell_data > 0) {
+      num_rotations = i;
+    }
+
+    mycell.ConnectionList().CircNext();
+  }
+
+  assert(faced == pop.GetCell(fromcellID).GetCellFaced());
+
+  // Rotate until we face the neighbor with a target.
+  // If there was no winner, just move forward.
+  for(int i = 0; i < num_rotations; i++) {
+    mycell.ConnectionList().CircNext();
+  }
+
+  // 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(s)
+    if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
+        (organism->GetPheromoneStatus() == true) ) {
+
+        pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+	drop_mode = m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+	
+	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().MOVETARGET_LOG_START.Get()) ) {
+      cString tmpfilename = cStringUtil::Stringf("movelog.dat");
+      cDataFile& df = m_world->GetDataFile(tmpfilename);
+
+      int rel_srcid = deme.GetRelativeCellID(fromcellID);
+      int rel_destid = deme.GetRelativeCellID(destcellID);
+
+      cString UpdateStr = cStringUtil::Stringf("%d,%d,%d,%d,%d,%f,%d,6",  m_world->GetStats().GetUpdate(), organism->GetID(), deme.GetDemeID(), rel_srcid, rel_destid, pher_amount, drop_mode);
+      df.WriteRaw(UpdateStr);
+    }
+
+    organism->Move(ctx);
+
+    return true;
+  } else {
+    return false;
+  }
+
+  return true;
+
+} // End Inst_MoveTargetForward()
+
+
 //TODO: implement and comment
 bool cHardwareCPU::Inst_SuperMove(cAvidaContext& ctx)
 {
@@ -3397,10 +3523,6 @@
           deme.AddPheromone(destcellID, pher_amount);
 	}
 
-       // Old CellData-based version
-       //const int newval = pop.GetCell(destcellID).GetCellData() + 1;
-       //pop.GetCell(destcellID).SetCellData(newval);
-
     } //End laying pheromone
 
 
@@ -3502,10 +3624,6 @@
           deme.AddPheromone(destcellID, pher_amount);
 	}
 
-       // Old CellData-based version
-       //const int newval = pop.GetCell(destcellID).GetCellData() + 1;
-       //pop.GetCell(destcellID).SetCellData(newval);
-
     } //End laying pheromone
 
 
@@ -3630,10 +3748,6 @@
           deme.AddPheromone(destcellID, pher_amount);
 	}
 
-       // Old CellData-based version
-       //const int newval = pop.GetCell(destcellID).GetCellData() + 1;
-       //pop.GetCell(destcellID).SetCellData(newval);
-
     } //End laying pheromone
 
 
@@ -3664,6 +3778,141 @@
 
 
 
+// Sense neighboring cells, and rotate and move to the neighbor with the
+// strongest pheromone.  If there is no winner, just move forward.
+// This instruction doesn't sense the three cells behind the organism,
+// i.e. the one directly behind, and behind and to the left and right.
+bool cHardwareCPU::Inst_ExploitForward(cAvidaContext& ctx)
+{
+  int num_rotations = 0;
+  float phero_amount = 0;
+  float max_pheromone = 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 relative_cell_id = deme.GetRelativeCellID(cellid);
+  //tArray<double> cell_resources = deme_resource_count.GetCellResources(relative_cell_id);
+  tArray<double> cell_resources;
+
+  int fromcellID, destcellID;
+  
+  // 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();
+
+  // Pheromone drop stuff
+  double pher_amount = 0;
+  int drop_mode = -1;
+
+  // Find which neighbor has the strongest pheromone
+  for(int i = 0; i < mycell.ConnectionList().GetSize(); i++) {
+
+    // Skip the cells in the back
+    if(i == 3 || i == 4 || i == 5) {
+      mycell.ConnectionList().CircNext();
+      continue;
+    }
+
+    phero_amount = 0;
+    cell_resources = deme_resource_count.GetCellResources(deme.GetRelativeCellID(mycell.GetCellFaced().GetID()));
+
+    for (int j = 0; j < deme_resource_count.GetSize(); j++) {
+      if(strcmp(deme_resource_count.GetResName(j), "pheromone") == 0) {
+        phero_amount = cell_resources[j];
+      }
+    }
+
+    if(phero_amount > max_pheromone) {
+      num_rotations = i;
+      max_pheromone = phero_amount;
+    }
+
+    mycell.ConnectionList().CircNext();
+  }
+
+  // Rotate until we face the neighbor with the strongest pheromone.
+  // If there was no winner, just move forward.
+  for(int i = 0; i < num_rotations; i++) {
+    mycell.ConnectionList().CircNext();
+  }
+
+  // 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(s)
+    if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
+        (organism->GetPheromoneStatus() == true) ) {
+
+        pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+	drop_mode = m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+	
+	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().EXPLOIT_LOG_START.Get()) ) {
+      cString tmpfilename = cStringUtil::Stringf("movelog.dat");
+      cDataFile& df = m_world->GetDataFile(tmpfilename);
+
+      int rel_srcid = deme.GetRelativeCellID(fromcellID);
+      int rel_destid = deme.GetRelativeCellID(destcellID);
+
+      cString UpdateStr = cStringUtil::Stringf("%d,%d,%d,%d,%d,%f,%d,7",  m_world->GetStats().GetUpdate(), organism->GetID(), deme.GetDemeID(), rel_srcid, rel_destid, pher_amount, drop_mode);
+      df.WriteRaw(UpdateStr);
+    }
+
+    organism->Move(ctx);
+
+    return true;
+  } else {
+    return false;
+  }
+
+  return true;
+
+} //End Inst_ExploitForward()
+
+
+
+
 bool cHardwareCPU::Inst_IfTarget(cAvidaContext& ctx)
 {
   int cell_data = m_world->GetPopulation().GetCell(organism->GetCellID()).GetCellData();
@@ -3972,6 +4221,26 @@
 } //End Inst_SenseTarget()
 
 
+
+bool cHardwareCPU::Inst_SenseTargetFaced(cAvidaContext& ctx) {
+  int reg_to_set = FindModifiedRegister(REG_CX);
+
+  cPopulation& pop = m_world->GetPopulation();
+  cPopulationCell& mycell = pop.GetCell(organism->GetCellID());
+
+  int cell_data = mycell.GetCellFaced().GetCellData(); //absolute id of faced cell
+  int val = 0;
+
+  if(cell_data > 0) {
+    val = 1;
+  }
+
+  GetRegister(reg_to_set) = val;
+  return true;
+} //End Inst_SenseTargetFaced()
+
+
+
 bool cHardwareCPU::Inst_CollectCellData(cAvidaContext& ctx) {
   int cellID = organism->GetCellID();
   const int out_reg = FindModifiedRegister(REG_BX);
@@ -4810,7 +5079,6 @@
     /*********************/    
     
     // Actually perform the move using SwapCells
-    //cout << "SwapCells: " << fromcellID << " to " << destcellID << endl;
     pop.SwapCells(pop.GetCell(fromcellID),pop.GetCell(destcellID));
     // Swap inputs and facings between cells using helper function
     pop.MoveOrganisms(ctx, pop.GetCell(fromcellID), pop.GetCell(destcellID));

Modified: branches/energy/source/cpu/cHardwareCPU.h
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.h	2008-05-19 18:11:40 UTC (rev 2585)
+++ branches/energy/source/cpu/cHardwareCPU.h	2008-05-19 18:31:11 UTC (rev 2586)
@@ -495,12 +495,15 @@
   bool Inst_SenseUnitFacing(cAvidaContext& ctx);
   bool Inst_SenseMult100Facing(cAvidaContext& ctx);
   bool Inst_SenseTarget(cAvidaContext& ctx);
+  bool Inst_SenseTargetFaced(cAvidaContext& ctx);
   bool DoSensePheromone(cAvidaContext& ctx, int cellid);
   bool Inst_SensePheromone(cAvidaContext& ctx);
   bool Inst_SensePheromoneFaced(cAvidaContext& ctx);
   bool Inst_Exploit(cAvidaContext& ctx);
+  bool Inst_ExploitForward(cAvidaContext& ctx);
   bool Inst_Explore(cAvidaContext& ctx);
   bool Inst_MoveTarget(cAvidaContext& ctx);
+  bool Inst_MoveTargetForward(cAvidaContext& ctx);
   bool Inst_SuperMove(cAvidaContext& ctx);
   bool Inst_IfTarget(cAvidaContext& ctx);
   bool Inst_IfNotTarget(cAvidaContext& ctx);

Modified: branches/energy/source/main/cDeme.cc
===================================================================
--- branches/energy/source/main/cDeme.cc	2008-05-19 18:11:40 UTC (rev 2585)
+++ branches/energy/source/main/cDeme.cc	2008-05-19 18:31:11 UTC (rev 2586)
@@ -229,6 +229,8 @@
   assert(cell_ids[0] <= absolute_cell_id);
   assert(absolute_cell_id <= cell_ids[cell_ids.GetSize()-1]);
 
+  cPopulation& pop = m_world->GetPopulation();
+
   int relative_cell_id = GetRelativeCellID(absolute_cell_id);
   tArray<double> cell_resources = deme_resource_count.GetCellResources(relative_cell_id);
 
@@ -236,11 +238,9 @@
     if(strcmp(deme_resource_count.GetResName(i), "pheromone") == 0) {
       // There should only be one "pheromone" resource, so no need to divvy value up
       cell_resources[i] = value;
-      //cout << "BDC-DEBUG: Adding " << value << " to pheromone resource " << deme_resource_count.GetResName(i) << "\n";
     }
     else {
       cell_resources[i] = 0;
-      //cout << "BDC-DEBUG: Looking at non-pheromone resource " << deme_resource_count.GetResName(i) << "\n";
     }
   }
 
@@ -250,6 +250,10 @@
 
   deme_resource_count.ModifyCell(cell_resources, relative_cell_id);
 
+  // CellData-based version
+  //const int newval = pop.GetCell(absolute_cell_id).GetCellData() + (int) round(value);
+  //pop.GetCell(absolute_cell_id).SetCellData(newval);
+
 } //End AddPheromone()
 
 

Modified: branches/energy/source/main/cOrgMovementPredicate.h
===================================================================
--- branches/energy/source/main/cOrgMovementPredicate.h	2008-05-19 18:11:40 UTC (rev 2585)
+++ branches/energy/source/main/cOrgMovementPredicate.h	2008-05-19 18:31:11 UTC (rev 2586)
@@ -144,7 +144,7 @@
  *  Modified from cOrgMovementPred_EventMovedIntoCenter
 */
 struct cOrgMovementPred_EventMovedBetweenTargets : public cOrgMovementPredicate {
-  cOrgMovementPred_EventMovedBetweenTargets(tVector<cDemeCellEvent *> events, cPopulation& population, int times) :
+  cOrgMovementPred_EventMovedBetweenTargets(tVector<cDemeCellEvent *> events, cPopulation& population, unsigned int times) :
   pop(population)
   , m_event_received(false)
   , m_stats_updated(false)
@@ -202,12 +202,12 @@
                //---- do some logging ----------------------------------------------------------------------
                ofstream predlog;
                predlog.open("data/predlog.dat", ios::app);
-               predlog << "BDCDEBUG organism " << org_id << " in deme " << deme_id ;
+               predlog << "Organism " << org_id << " in deme " << deme_id ;
                predlog << " has touched both targets " << num_backforth << " times. " << m_successful_orgs.size();
                predlog << " orgs have done this out of " << m_total_orgs << endl;
 
                set<int>::iterator it;
-               for(int q = 0; q < m_event_success.size(); q++) {
+               for(unsigned int q = 0; q < m_event_success.size(); q++) {
                  predlog << "ORGS FOR EVENT " << q;
                    for ( it=m_event_success[q].begin() ; it != m_event_success[q].end(); it++ )
                      predlog << " " << *it;
@@ -285,7 +285,7 @@
   bool m_event_received;
   bool m_stats_updated;
   tVector<cDemeCellEvent *> m_events;
-  int m_total_times;
+  unsigned int m_total_times;
   int m_total_orgs;
 
   vector< set<int> > m_event_success;
@@ -300,7 +300,7 @@
 */
 
 struct cOrgMovementPred_EventNUniqueIndividualsMovedIntoTarget : public cOrgMovementPredicate {
-  cOrgMovementPred_EventNUniqueIndividualsMovedIntoTarget(cDemeCellEvent* event, cPopulation& population, int unique_individuals) :
+  cOrgMovementPred_EventNUniqueIndividualsMovedIntoTarget(cDemeCellEvent* event, cPopulation& population, unsigned int unique_individuals) :
   pop(population)
   , m_satisfied(false)
   , m_stats_updated(false)
@@ -371,7 +371,7 @@
   bool m_satisfied;
   bool m_stats_updated;
   cDemeCellEvent* m_event;
-  int m_total_individuals;
+  unsigned int m_total_individuals;
   set<int> unique_org_ids;
 };
 




More information about the Avida-cvs mailing list