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

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Wed Dec 26 14:36:39 PST 2007


Author: beckma24
Date: 2007-12-26 17:36:38 -0500 (Wed, 26 Dec 2007)
New Revision: 2238

Modified:
   branches/energy/source/actions/EnvironmentActions.cc
   branches/energy/source/actions/PopulationActions.cc
   branches/energy/source/actions/PrintActions.cc
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/cpu/cHardwareCPU.h
   branches/energy/source/cpu/cTestCPUInterface.h
   branches/energy/source/main/cAvidaConfig.h
   branches/energy/source/main/cDeme.cc
   branches/energy/source/main/cDeme.h
   branches/energy/source/main/cDemeCellEvent.cc
   branches/energy/source/main/cDemeCellEvent.h
   branches/energy/source/main/cGenome.h
   branches/energy/source/main/cOrgInterface.h
   branches/energy/source/main/cOrganism.cc
   branches/energy/source/main/cOrganism.h
   branches/energy/source/main/cPopulation.cc
   branches/energy/source/main/cPopulationCell.cc
   branches/energy/source/main/cPopulationInterface.cc
   branches/energy/source/main/cPopulationInterface.h
   branches/energy/source/main/cStats.cc
   branches/energy/source/main/cStats.h
   branches/energy/source/main/cTaskLib.cc
   branches/energy/source/main/cTaskLib.h
Log:
Added stuff for foraging

Modified: branches/energy/source/actions/EnvironmentActions.cc
===================================================================
--- branches/energy/source/actions/EnvironmentActions.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/actions/EnvironmentActions.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -603,10 +603,11 @@
   int m_duration; // length of event; subverted when deme is reset
   bool m_static_pos;
   int m_time_to_live; // update when event no longer exists
+  int m_id;
   
 public:
   cActionDelayedDemeEvent(cWorld* world, const cString& args) : cAction(world, args), m_x1(-1), m_y1(-1), m_x2(-1), m_y2(-1), m_delay(-1),
-                                                                m_duration(-1), m_static_pos(1), m_time_to_live(-1)
+                                                                m_duration(-1), m_static_pos(1), m_time_to_live(-1), m_id(-1)
   {
     cString largs(args);
     if (largs.GetSize()) m_x1 = largs.PopWord().AsInt();
@@ -617,16 +618,17 @@
     if (largs.GetSize()) m_duration = largs.PopWord().AsInt();
     if (largs.GetSize()) m_static_pos = largs.PopWord().AsInt();
     if (largs.GetSize()) m_time_to_live = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_id = largs.PopWord().AsInt();
   }
   
-  static const cString GetDescription() { return "Arguments: <int x1> <int y1> <int x2> <int y2> <int delay> <int duraion> <int static_position> <int time_to_live>"; }
+  static const cString GetDescription() { return "Arguments: <int x1> <int y1> <int x2> <int y2> <int delay> <int duraion> <int static_position> <int time_to_live> <event ID>"; }
   
   void Process(cAvidaContext& ctx)
   {
     cPopulation& pop = m_world->GetPopulation();
     int numDemes = pop.GetNumDemes();
     for(int i = 0; i < numDemes; i++) {
-      pop.GetDeme(i).SetCellEvent(m_x1, m_y1, m_x2, m_y2, m_delay, m_duration, m_static_pos, m_time_to_live);
+      pop.GetDeme(i).SetCellEvent(m_x1, m_y1, m_x2, m_y2, m_delay, m_duration, m_static_pos, m_time_to_live, m_id);
     }
   }
 };

Modified: branches/energy/source/actions/PopulationActions.cc
===================================================================
--- branches/energy/source/actions/PopulationActions.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/actions/PopulationActions.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -495,8 +495,15 @@
     cGenome genome = cGenomeUtil::LoadGenome(m_filename, m_world->GetHardwareManager().GetInstSet());
     if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
       for(int i=1; i<m_world->GetPopulation().GetNumDemes(); ++i) {  // first org has already been injected
+        for(int j = 0 ; j < m_world->GetConfig().GERMLINE_INJECT_ORGS.Get(); j++) {
+          m_world->GetPopulation().Inject(genome,
+                                          m_world->GetPopulation().GetDeme(i).GetCellID(j),
+                                          m_merit, m_lineage_label, m_neutral_metric);
+        }
+      }
+      for(int j = 1 ; j < m_world->GetConfig().GERMLINE_INJECT_ORGS.Get(); j++) { // other orgs in first deme
         m_world->GetPopulation().Inject(genome,
-                                        m_world->GetPopulation().GetDeme(i).GetCellID(0),
+                                        m_world->GetPopulation().GetDeme(0).GetCellID(j),
                                         m_merit, m_lineage_label, m_neutral_metric);
       }
     } else {

Modified: branches/energy/source/actions/PrintActions.cc
===================================================================
--- branches/energy/source/actions/PrintActions.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/actions/PrintActions.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -95,6 +95,7 @@
 STATS_OUT_FILE(PrintSleepData,              sleep.dat           );
 STATS_OUT_FILE(PrintMessageData,            message.dat         );
 STATS_OUT_FILE(PrintPredSatFracDump,        pred_sat_frac_dump.dat);
+STATS_OUT_FILE(PrintDemeRepOrgLocation,     DemeRepOrgLocation.dat);
 
 #define POP_OUT_FILE(METHOD, DEFAULT)                                                     /*  1 */ \
 class cAction ## METHOD : public cAction {                                                /*  2 */ \
@@ -2725,6 +2726,9 @@
   // Relative or cumulative deme grid dump
   action_lib->Register<cActionPrintPredSatFracDump>("PrintPredSatFracDump");
   
+  action_lib->Register<cActionPrintDemeRepOrgLocation>("PrintDemeRepOrgLocation");
+  
+  
   // Print Settings
   action_lib->Register<cActionSetVerbose>("SetVerbose");
   

Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -377,6 +377,7 @@
     
     // Messaging
     tInstLibEntry<tMethod>("send-msg", &cHardwareCPU::Inst_SendMessage),
+    tInstLibEntry<tMethod>("send-msg-multi-hop", &cHardwareCPU::Inst_SendMessageMultiHop),
     tInstLibEntry<tMethod>("send-msg-TX", &cHardwareCPU::Inst_SendMessage_TX),
     tInstLibEntry<tMethod>("receive-msg-blocking", &cHardwareCPU::Inst_ReceiveMsg_Blocking),
     tInstLibEntry<tMethod>("retrieve-msg", &cHardwareCPU::Inst_RetrieveMessage),
@@ -4672,6 +4673,18 @@
   return organism->SendMessage(ctx, msg);
 }
 
+bool cHardwareCPU::Inst_SendMessageMultiHop(cAvidaContext& ctx)
+{
+  const int label_reg = FindModifiedRegister(REG_BX);
+  const int data_reg = FindNextRegister(label_reg);
+  
+  cOrgMessage msg = cOrgMessage(organism);
+  msg.SetLabel(GetRegister(label_reg));
+  msg.SetData(GetRegister(data_reg));
+  return organism->SendMessageMultiHop(ctx, msg);
+}
+
+
 /*!
 This method implements a transmission time for sent messages.  
  The sender and all neighbor cells are blocked from sending until the message transmission is over.

Modified: branches/energy/source/cpu/cHardwareCPU.h
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/cpu/cHardwareCPU.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -570,6 +570,7 @@
 
   //// Messaging ////
   bool Inst_SendMessage(cAvidaContext& ctx);
+  bool Inst_SendMessageMultiHop(cAvidaContext& ctx);
   bool Inst_SendMessage_TX(cAvidaContext& ctx);
   bool Inst_ReceiveMsg_Blocking(cAvidaContext& ctx);
   bool Inst_RetrieveMessage(cAvidaContext& ctx);

Modified: branches/energy/source/cpu/cTestCPUInterface.h
===================================================================
--- branches/energy/source/cpu/cTestCPUInterface.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/cpu/cTestCPUInterface.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -75,6 +75,7 @@
   bool TestOnDivide() { return false; }
   int GetFacing() { return 0; }
   bool SendMessage(cOrgMessage& msg) { return false; }
+  bool SendMessageMultiHop(cOrgMessage& msg) { return false; }
 };
 
 

Modified: branches/energy/source/main/cAvidaConfig.h
===================================================================
--- branches/energy/source/main/cAvidaConfig.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cAvidaConfig.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -299,6 +299,7 @@
   CONFIG_ADD_VAR(GERMLINE_COPY_MUT, double, 0.0075, "Prob. of copy mutations occuring during\ngermline replication.");
   CONFIG_ADD_VAR(GERMLINE_REPLACES_SOURCE, int, 0, "Whether the source germline is updated\non replication; 0=no.");
   CONFIG_ADD_VAR(GERMLINE_RANDOM_PLACEMENT, int, 0, "Defines how the seed for a germline is placed\n within the deme;\n0 = organisms is placed in center of deme, no orientation\n1 = organisms is placed in center of deme and oriented\n2 = organism is randomly placed in deme, no orientation\n3 = deme is filled with identical copies of organism, no orientation\n4 = deme is filled with identical oriented copies of organism");
+  CONFIG_ADD_VAR(GERMLINE_INJECT_ORGS, int, 1, "Defines how many organisms are injected.  GERMLINE_RANDOM_PLACEMENT must equal 2");
   CONFIG_ADD_VAR(MAX_DEME_AGE, int, 500, "The maximum age of a deme (in updates) to be\nused for age-based replication (default=500).");  
   
   CONFIG_ADD_GROUP(REPRODUCTION_GROUP, "Birth and Death");
@@ -450,7 +451,11 @@
 //  CONFIG_ADD_VAR(ENERGY_VERBOSE, bool, 0, "Print energy and merit values. 0/1 (off/on)");
   CONFIG_ADD_VAR(LOG_SLEEP_TIMES, bool, 0, "Log sleep start and end times. 0/1 (off/on)\nWARNING: may use lots of memory.");
   //  CONFIG_ADD_VAR(FIX_METABOLIC_RATE, bool, 0, "When activated the metabolic rate of all orgiansims are equal. 0/1 (off/on)"); // TODO - check for correctness
+  CONFIG_ADD_VAR(BCAST_HOPS, int, 1, "Distance a message will travel");
+  CONFIG_ADD_VAR(LOG_REP_LOCATION, bool, 0, "Log position of organisms in deme when it replicates. 0/1 (off/on)");
 
+
+
   CONFIG_ADD_GROUP(SECOND_PASS_GROUP, "Tracking metrics known after the running experiment previously");
   CONFIG_ADD_VAR(TRACK_CCLADES, int, 0, "Enable tracking of coalescence clades");
   CONFIG_ADD_VAR(TRACK_CCLADES_IDS, cString, "coalescence.ids", "File storing coalescence IDs");

Modified: branches/energy/source/main/cDeme.cc
===================================================================
--- branches/energy/source/main/cDeme.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cDeme.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -77,6 +77,7 @@
 {
   assert(cell_ids.GetSize()>0);
   assert(GetWidth() > 0);
+//  assert(cell_ids[0] <= cellid && cellid <= cell_ids[cell_ids.GetSize()-1]);  //events use this functions
   return std::make_pair(cellid % GetWidth(), ( cellid % cell_ids.GetSize() ) / GetWidth());
 }
 
@@ -109,7 +110,7 @@
       while(eventCell != -1) {
         if(event.GetEventID() == m_world->GetPopulation().GetCell(GetCellID(eventCell)).GetCellData()) { // eventID == CellData
           //set cell data to 0
-          m_world->GetPopulation().GetCell(GetCellID(eventCell)).SetCellData(0);
+          m_world->GetPopulation().GetCell(GetCellID(eventCell)).SetCellData(-1);
         }
         eventCell = event.GetNextEventCellID();
       }
@@ -143,7 +144,7 @@
   }
 
   for(int i = 0; i < GetSize(); i++) {
-    m_world->GetPopulation().GetCell(GetCellID(i)).SetCellData(0);
+    m_world->GetPopulation().GetCell(GetCellID(i)).SetCellData(-1);
   }
 }
 
@@ -219,8 +220,10 @@
   deme_resource_count.ModifyCell(cell_resources, relative_cell_id);
 }
 
-void cDeme::SetCellEvent(int x1, int y1, int x2, int y2, int delay, int duration, bool static_pos, int time_to_live) {
+void cDeme::SetCellEvent(int x1, int y1, int x2, int y2, int delay, int duration, bool static_pos, int time_to_live, int ID) {
   cDemeCellEvent demeEvent = cDemeCellEvent(x1, y1, x2, y2, delay, duration, width, GetHeight(), static_pos, time_to_live, this);
+  if(ID != -1)
+    demeEvent.SetEventID(ID);
   cell_events.Add(demeEvent);
 }
 

Modified: branches/energy/source/main/cDeme.h
===================================================================
--- branches/energy/source/main/cDeme.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cDeme.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -121,7 +121,7 @@
   void Update(double time_step) { deme_resource_count.Update(time_step); }
   int GetRelativeCellID(int absolute_cell_id) { return absolute_cell_id % GetSize(); } //!< assumes all demes are the same size
 
-  void SetCellEvent(int x1, int y1, int x2, int y2, int delay, int duration, bool static_pos, int time_to_live);
+  void SetCellEvent(int x1, int y1, int x2, int y2, int delay, int duration, bool static_pos, int time_to_live, int ID = -1);
   void SetCellEventGradient(int x1, int y1, int x2, int y2, int delay, int duration, bool static_pos, int time_to_live);
   int GetNumEvents();
   cDemeCellEvent GetEvent(int i);

Modified: branches/energy/source/main/cDemeCellEvent.cc
===================================================================
--- branches/energy/source/main/cDemeCellEvent.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cDemeCellEvent.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -13,6 +13,7 @@
 , m_active(false)
 , m_static_pos(static_pos)
 , m_dead (false)
+, m_id_set(false)
 , m_deme(deme)
 {
   assert(x1 <= x2);
@@ -53,7 +54,8 @@
     return;
   }
   
-  m_eventID = m_world->GetRandom().GetInt(0x7FFFFFFF);
+  if(!m_id_set)
+    m_eventID = m_world->GetRandom().GetInt(0x7FFFFFFF);
   if(!m_static_pos) {
     //non-static event position
     int rand_x1 = m_world->GetRandom().GetInt(m_deme_width-m_event_width);

Modified: branches/energy/source/main/cDemeCellEvent.h
===================================================================
--- branches/energy/source/main/cDemeCellEvent.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cDemeCellEvent.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -13,14 +13,14 @@
   int m_eventID;
   int m_delay, m_duration, m_iter, m_deme_width, m_deme_height;
   int m_event_width, m_event_height, m_time_to_live;
-  bool m_use_gradient, m_active, m_static_pos, m_dead;
+  bool m_use_gradient, m_active, m_static_pos, m_dead, m_id_set;
   std::pair<std::pair<int, int>, std::pair<int, int> > center;
   cDeme* m_deme;
   
   std::pair<double, double> GetCenter() const;
   
 public:
-  cDemeCellEvent() { cDemeCellEvent(-1, -1, -1, -1, 0, 0, 0, 0, true, 0, NULL); }
+  cDemeCellEvent() { cDemeCellEvent(-1, -1, -1, -1, 0, 0, 0, 0, true, 0, NULL); m_id_set = false;}
   cDemeCellEvent(int x1, int y1, int x2, int y2, int delay, int duration, int deme_width, int deme_height, bool static_pos, int time_to_live, cDeme* deme);
   int GetNextEventCellID();
   int GetDelay();
@@ -33,6 +33,8 @@
   bool IsDead() const { return m_dead; }
   bool IsDecayed() { return m_use_gradient; }
   bool InCenter(std::pair<int, int> pos) const;
+  
+  void SetEventID(int ID) { m_eventID = ID; m_id_set = true;}
 
   void DecayEventIDFromCenter() { m_use_gradient = true; }
   void ActivateEvent(cWorld* m_world);

Modified: branches/energy/source/main/cGenome.h
===================================================================
--- branches/energy/source/main/cGenome.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cGenome.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -54,6 +54,7 @@
 public:
   double GetEnergy() const { return energy; }
   void SetEnergy(const double value) const { energy = value; }
+  void UpdateEnergy(const double old_energy, const double new_energy) const { energy = energy + new_energy - old_energy; }
 
   //! Default constructor.
   cGenome() { energy = 0.0; }

Modified: branches/energy/source/main/cOrgInterface.h
===================================================================
--- branches/energy/source/main/cOrgInterface.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cOrgInterface.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -84,6 +84,7 @@
   virtual bool UpdateMerit(double new_merit) = 0;
   virtual bool TestOnDivide() = 0;
   virtual bool SendMessage(cOrgMessage& msg) = 0;
+  virtual bool SendMessageMultiHop(cOrgMessage& msg) = 0;
 };
 
 #endif

Modified: branches/energy/source/main/cOrganism.cc
===================================================================
--- branches/energy/source/main/cOrganism.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cOrganism.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -252,6 +252,7 @@
   deme_res_change = globalAndDeme_res_change.Subset(global_res_change.GetSize(), globalAndDeme_res_change.GetSize());
     
   if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1 && task_completed) {
+    double old_energy = m_phenotype.GetStoredEnergy();
     m_phenotype.RefreshEnergy();
     m_phenotype.ApplyToEnergyStore();
     double newMerit = cMerit::EnergyToMerit(GetPhenotype().GetStoredEnergy() * GetPhenotype().GetEnergyUsageRatio(), m_world);
@@ -259,7 +260,7 @@
     /******** tmp*******/
     newMerit += m_phenotype.GetCurBonus();
 //  set in germ line  
-    m_world->GetPopulation().GetDeme(this->GetOrgInterface().GetDemeID()).GetGermline().GetLatest().SetEnergy(GetPhenotype().GetStoredEnergy());
+    m_world->GetPopulation().GetDeme(this->GetOrgInterface().GetDemeID()).GetGermline().GetLatest().UpdateEnergy(old_energy, GetPhenotype().GetStoredEnergy());
     
 //    m_phenotype.SetCurBonus(0.0);
     if(newMerit != -1) {
@@ -653,8 +654,37 @@
   return false;
 }
 
+bool cOrganism::SendMessageMultiHop(cAvidaContext& ctx, cOrgMessage& msg)
+{
+  assert(m_interface);
+  InitMessaging();
 
-bool cOrganism::ReceiveMessage(cOrgMessage& msg)
+  // If we're able to succesfully send the message...
+  if(m_interface->SendMessageMultiHop(msg)) {
+    // save it...
+    m_msg->sent.push_back(msg);
+    // stat-tracking...
+    m_world->GetStats().SentMessageMultiHop(msg);
+    // check to see if we've performed any tasks...
+    DoOutput(ctx);
+    // and set the receiver-pointer of this message to NULL.  We don't want to
+    // walk this list later thinking that the receivers are still around.
+    m_msg->sent.back().SetReceiver(0);
+    return true;
+  }
+  
+  return false;
+}
+
+bool cOrganism::ReceiveMessage_NoCollision(cOrgMessage& msg) {
+  InitMessaging();  
+  msg.SetReceiver(this);
+  m_msg->received.push_back(msg);
+  m_world->GetStats().IncMsgReceived();
+  return true;
+}
+
+bool cOrganism::ReceiveMessage_WithColision(cOrgMessage& msg)
 {
   InitMessaging();
   

Modified: branches/energy/source/main/cOrganism.h
===================================================================
--- branches/energy/source/main/cOrganism.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cOrganism.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -321,8 +321,12 @@
   
   //! Called when this organism attempts to send a message.
   bool SendMessage(cAvidaContext& ctx, cOrgMessage& msg);
+  //! Similar to above with multihop broadcast
+  bool SendMessageMultiHop(cAvidaContext& ctx, cOrgMessage& msg);
   //! Called when this organism has been sent a message.
-  bool ReceiveMessage(cOrgMessage& msg);
+//  bool ReceiveMessage(cOrgMessage& msg);
+  bool cOrganism::ReceiveMessage_NoCollision(cOrgMessage& msg);
+  bool cOrganism::ReceiveMessage_WithColision(cOrgMessage& msg);
   //! Called when this organism attempts to move a received message into its CPU.
   const cOrgMessage* RetrieveMessage();
   //! Similar to RetrieveMessage, but TX must be complete for message to be available

Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cPopulation.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -810,10 +810,10 @@
   cOrganism * org1 = cell1.RemoveOrganism();
   cOrganism * org2 = cell2.RemoveOrganism();
   // @WRE additions for movement logging
-  int x = 0;
+/*  int x = 0;
   int y = 0;
   int x1 = 0;
-  int y1 = 0;
+  int y1 = 0;*/
   int cid = 0;
 
   //cout << "SwapCells: organism 2 is non-null, fix up source cell" << endl;
@@ -828,8 +828,9 @@
     cell2.InsertOrganism(org1);
     schedule->Adjust(cell2.GetID(), org1->GetPhenotype().GetMerit());
     // @WRE additions for movement logging
-    cell1.GetPosition(x1,y1);
-    cell2.GetPosition(x,y);
+    pair<int,int> cell1Pos = GetDeme(cell1.GetDemeID()).GetRelativeCellPosition(cell1.GetID());
+    pair<int,int> cell2Pos = GetDeme(cell2.GetDemeID()).GetRelativeCellPosition(cell2.GetID());
+//    cell2.GetPosition(x,y);
     cid = org1->GetID();
 /*
     // Output some information
@@ -837,10 +838,10 @@
     movelog.open("data/movelog.txt",ios::app);
     // By columns: org ID, from cell xy, to cell xy
     movelog << cid << " " <<
-      x1 << " " <<
-      y1 << " " <<
-      x << " " <<
-      y << " " << endl;
+      cell1Pos.first << " " <<
+      cell1Pos.second << " " <<
+      cell2Pos.first << " " <<
+      cell2Pos.second << " " << endl;
     movelog.close();
 */
   } else {
@@ -1170,9 +1171,19 @@
       target_id = random.GetUInt(num_demes);
     }
     cDeme& target_deme = deme_array[target_id];
+    vector<pair<int, int> > locations;
+    
     for (int i=0; i<target_deme.GetSize(); i++) {
+      if(m_world->GetConfig().LOG_REP_LOCATION.Get() == true && cell_array[target_deme.GetCellID(i)].IsOccupied()) {
+        locations.push_back(target_deme.GetRelativeCellPosition(target_deme.GetCellID(i)));
+      }
       KillOrganism(cell_array[target_deme.GetCellID(i)]);
     }
+
+    if(m_world->GetConfig().LOG_REP_LOCATION.Get() == true) {
+      m_world->GetStats().addOrgLocations(locations);
+    }
+
         
     // Ok, there are two potential places where the seed for the target deme can
     // come from.  First, it could be a random organism in the source deme.
@@ -1278,22 +1289,35 @@
             cell_array[target_deme_inject_cell].Rotate(cell_array[newFacing]);
           }
         }
-      } else { // 1 organism
+      } else { // GERMLINE_INJECT_ORGS organisms (all identical)
         int source_deme_inject_cell;
         int target_deme_inject_cell;
         
         if(m_world->GetConfig().GERMLINE_RANDOM_PLACEMENT.Get() == 2) {
-          // organism is randomly placed in deme
-          source_deme_inject_cell = source_deme.GetCellID(m_world->GetRandom().GetInt(0, source_deme.GetSize()-1));
-          target_deme_inject_cell = target_deme.GetCellID(m_world->GetRandom().GetInt(0, target_deme.GetSize()-1));
+          // inject GERMLINE_INJECT_ORGS organisms in source deme
+          while(source_deme.GetOrgCount() < m_world->GetConfig().GERMLINE_INJECT_ORGS.Get()) {
+            // organism is randomly placed in deme
+            source_deme_inject_cell = source_deme.GetCellID(m_world->GetRandom().GetInt(0, source_deme.GetSize()-1));
+            // Lineage label is wrong here; fix.
+            InjectGenome(source_deme_inject_cell, source_germline.GetLatest(), 0); // source deme
+          }
+
+          // inject GERMLINE_INJECT_ORGS organisms in target deme          
+          while(target_deme.GetOrgCount() < m_world->GetConfig().GERMLINE_INJECT_ORGS.Get()) {
+            // organism is randomly placed in deme
+            target_deme_inject_cell = target_deme.GetCellID(m_world->GetRandom().GetInt(0, target_deme.GetSize()-1));
+            // Lineage label is wrong here; fix.
+            InjectGenome(target_deme_inject_cell, target_germline.GetLatest(), 0); // target deme
+          }
         } else {
           // organisms is placed in center of deme
-          source_deme_inject_cell = source_deme.GetCellID(source_deme.GetSize()/2);
+          source_deme_inject_cell = source_deme.GetCellID(source_deme.GetSize()/2);  // does not work for even size worlds
           target_deme_inject_cell = target_deme.GetCellID(target_deme.GetSize()/2);
+          // inject 1 organisms
+          // Lineage label is wrong here; fix.
+          InjectGenome(source_deme_inject_cell, source_germline.GetLatest(), 0); // source deme
+          InjectGenome(target_deme_inject_cell, target_germline.GetLatest(), 0); // target deme
         }
-        // Lineage label is wrong here; fix.
-        InjectGenome(source_deme_inject_cell, source_germline.GetLatest(), 0); // source deme
-        InjectGenome(target_deme_inject_cell, target_germline.GetLatest(), 0); // target deme
         
         if(m_world->GetConfig().GERMLINE_RANDOM_PLACEMENT.Get() == 1) {
           // Rotate both injected cells to face northwest.

Modified: branches/energy/source/main/cPopulationCell.cc
===================================================================
--- branches/energy/source/main/cPopulationCell.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cPopulationCell.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -91,7 +91,7 @@
   m_x = x;
   m_y = y;
   m_deme_id = -1;
-  m_cell_data = 0;
+  m_cell_data = -1;
   
   if (m_mut_rates == NULL)
     m_mut_rates = new cMutationRates(in_rates);

Modified: branches/energy/source/main/cPopulationInterface.cc
===================================================================
--- branches/energy/source/main/cPopulationInterface.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cPopulationInterface.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -253,5 +253,35 @@
   }
   cOrganism* recvr = rcell->GetOrganism();
   assert(recvr != NULL);
-  return recvr->ReceiveMessage(msg);
+  return recvr->ReceiveMessage_NoCollision(msg);
 }
+
+/*! Send a message to all organisms within BCAST_HOPS*/
+bool cPopulationInterface::SendMessageMultiHop(cOrgMessage& msg) {
+  cPopulationCell& cell = m_world->GetPopulation().GetCell(GetCellID());
+  assert(cell.IsOccupied()); // This organism; sanity.
+  cDeme& deme = m_world->GetPopulation().GetDeme(GetDemeID());
+  bool successfully_sent = false;
+  m_world->GetStats().IncMsgSent();
+  
+  for(int i = 0; i < deme.GetSize(); i++) {
+    int possible_receiver_id = deme.GetCellID(i);
+    
+    if(m_world->GetPopulation().GetCell(possible_receiver_id).IsOccupied() && possible_receiver_id != GetCellID()) {
+      //check distance
+      pair<int, int> sender_pos = deme.GetRelativeCellPosition(GetCellID());
+      pair<int, int> possible_receiver_pos = deme.GetRelativeCellPosition(possible_receiver_id);
+      int hop_distance = max( abs(sender_pos.first  - possible_receiver_pos.first),
+                              abs(sender_pos.second - possible_receiver_pos.second));
+      if(hop_distance <= m_world->GetConfig().BCAST_HOPS.Get()) {
+        // send message to organisms
+        cPopulationCell& rcell = m_world->GetPopulation().GetCell(possible_receiver_id);
+        cOrganism* recvr = rcell.GetOrganism();
+        assert(recvr != NULL);
+        recvr->ReceiveMessage_NoCollision(msg);
+        successfully_sent = true;
+      }
+    }
+  }
+  return successfully_sent;
+}

Modified: branches/energy/source/main/cPopulationInterface.h
===================================================================
--- branches/energy/source/main/cPopulationInterface.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cPopulationInterface.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -85,8 +85,10 @@
   bool InjectParasite(cOrganism* parent, const cCodeLabel& label, const cGenome& injected_code);
   bool UpdateMerit(double new_merit);
   bool TestOnDivide();
+  
   //! Send a message to the faced organism.
   bool SendMessage(cOrgMessage& msg);
+  bool SendMessageMultiHop(cOrgMessage& msg);
 };
 
 

Modified: branches/energy/source/main/cStats.cc
===================================================================
--- branches/energy/source/main/cStats.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cStats.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -231,6 +231,10 @@
   relative_pos_pred_sat.SetAll(0);
 
   SetupPrintDatabase();
+  for(int i = 0; i < m_world->GetConfig().GERMLINE_INJECT_ORGS.Get(); i++) {
+    std::vector<std::pair<int, int> > tmp;
+    org_location.push_back(tmp);
+  }
 }
 
 void cStats::SetupPrintDatabase()
@@ -1164,6 +1168,17 @@
   from_to_message_list[msg.GetSender()->GetCellID()] = msg.GetReceiver()->GetCellID();
 }
 
+void cStats::SentMessageMultiHop(const cOrgMessage& msg)
+{
+  // Check to see if this message matches any of our predicates.
+  for(message_pred_ptr_list::iterator i=m_message_predicates.begin(); 
+      i!=m_message_predicates.end(); ++i) {
+    (**i)(msg); // Predicate is responsible for tracking info about messages.
+  }
+  
+  from_to_message_list[msg.GetSender()->GetCellID()] = msg.GetReceiver()->GetCellID();
+}
+
 /*! This method is called whenever an organism moves.*/
 void cStats::Move(const cOrganism& org) {
   // Check to see if this message matches any of our predicates.
@@ -1259,6 +1274,35 @@
   relative_pos_event_count.SetAll(0);
 }
 
+
+void cStats::addOrgLocations(std::vector<std::pair<int, int> > record) {
+//  cerr<<record.size() <<" "<< m_world->GetConfig().GERMLINE_INJECT_ORGS.Get() << endl;
+  assert(record.size() <= m_world->GetConfig().GERMLINE_INJECT_ORGS.Get());
+  int i = 0;
+  for(std::vector<std::pair<int, int> >::iterator iter = record.begin(); iter != record.end(); iter++) {  
+    org_location.at(i).push_back(*iter);
+    i++;
+  }
+}
+
+
+void cStats::PrintDemeRepOrgLocation(const cString& filename) {
+  cDataFile& df = m_world->GetDataFile(filename);
+  df.WriteComment( "Displays relative locations of organisms when deme replicates.\n" );
+  df.FlushComments();
+  df.Write( GetUpdate(), "update" );
+  for(unsigned int i = 0; i < org_location.at(0).size(); i++) {
+    for(unsigned int j = 0; j < org_location.size(); j++) {
+      df.Write(org_location.at(j).at(i).first, "org x");
+      df.Write(org_location.at(j).at(i).second, "org y");
+    }
+  }
+  df.Endl();
+  for(unsigned int k = 0; k < org_location.size(); k++) {
+    org_location.at(k).erase(org_location.at(k).begin(), org_location.at(k).end());
+  }
+}
+
 bool cStats::TimeHasPassed(int in_update, int in_subupdate) {
   int currentUpdate = m_world->GetStats().GetUpdate();
   int currentSubupdate = m_world->GetStats().GetSubUpdate();

Modified: branches/energy/source/main/cStats.h
===================================================================
--- branches/energy/source/main/cStats.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cStats.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -269,6 +269,9 @@
   tMatrix<int> relative_pos_event_count;
   tMatrix<int> relative_pos_pred_sat;
 
+  // organism(s) location when deme replicates
+  std::vector<std::vector<std::pair<int, int> > > org_location;
+
   // message stats
   int num_msg_sent_no_one;
   int num_msg_sent;
@@ -677,6 +680,9 @@
   void PrintSleepData(const cString& filename);
   void PrintPredSatFracDump(const cString& filename);
   
+  void addOrgLocations(std::vector<std::pair<int, int> >);
+  void PrintDemeRepOrgLocation(const cString& filename);
+  
   // -------- Messaging support --------
 public:
   //! Type for a list of pointers to message predicates.
@@ -684,6 +690,7 @@
   
   //! Called for every message successfully sent anywhere in the population.
   void SentMessage(const cOrgMessage& msg);
+  void SentMessageMultiHop(const cOrgMessage& msg);
   //! Adds a predicate that will be evaluated for each message.
   void AddMessagePredicate(cOrgMessagePredicate* predicate);
   //! Removes a predicate.

Modified: branches/energy/source/main/cTaskLib.cc
===================================================================
--- branches/energy/source/main/cTaskLib.cc	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cTaskLib.cc	2007-12-26 22:36:38 UTC (rev 2238)
@@ -30,6 +30,7 @@
 #include "cDeme.h"
 #include "cEnvReqs.h"
 #include "cPopulation.h"
+#include "cPopulationCell.h"
 #include "tHashTable.h"
 #include "cTaskState.h"
 
@@ -394,6 +395,12 @@
     NewTask(name, "Move down gradient", &cTaskLib::Task_MoveDownGradient);
   else if (name == "move_not_up_gradient")
     NewTask(name, "Move not up gradient", &cTaskLib::Task_MoveNotUpGradient);
+  else if (name == "move_to_right_side")
+    NewTask(name, "Move to right side", &cTaskLib::Task_MoveToRightSide);
+  else if (name == "move_to_left_side")
+    NewTask(name, "Move to left side", &cTaskLib::Task_MoveToLeftSide);
+  else if (name == "move_to_event")
+    NewTask(name, "Move to event", &cTaskLib::Task_MoveToEvent);
 
   
   
@@ -2752,6 +2759,10 @@
 
 
 double cTaskLib::Task_SentEventID(cTaskContext& ctx) const {
+  if(ctx.GetOrganism()->GetSentMessages().empty()) {
+    return 0.0;
+  }
+  
   const cOrgMessage& last_sent_msg = ctx.GetOrganism()->GetSentMessages().back();
   cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
     
@@ -2786,3 +2797,34 @@
     return 0.0;
   return 1.0;
 }
+
+double cTaskLib::Task_MoveToRightSide(cTaskContext& ctx) const {
+  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
+  std::pair<int, int> location = deme.GetRelativeCellPosition(ctx.GetOrgInterface()->GetCellID());
+  
+  if(location.first == m_world->GetConfig().WORLD_X.Get()-1)
+    return 1.0;
+  return 0.0;
+}
+
+double cTaskLib::Task_MoveToLeftSide(cTaskContext& ctx) const {
+  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
+  std::pair<int, int> location = deme.GetRelativeCellPosition(ctx.GetOrgInterface()->GetCellID());
+  
+  if(location.first == 0)
+    return 1.0;
+  return 0.0;
+}
+
+double cTaskLib::Task_MoveToEvent(cTaskContext& ctx) const {
+  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
+  int cell_data = m_world->GetPopulation().GetCell(ctx.GetOrgInterface()->GetCellID()).GetCellData();
+  if(cell_data <= 0)
+    return 0.0;
+    
+  for(int i = 0; i < deme.GetNumMessagePredicates(); i++) {
+    if(deme.GetEvent(i).GetEventID() == cell_data)
+      return 1.0;
+  }
+  return 0.0;
+}

Modified: branches/energy/source/main/cTaskLib.h
===================================================================
--- branches/energy/source/main/cTaskLib.h	2007-12-19 21:55:34 UTC (rev 2237)
+++ branches/energy/source/main/cTaskLib.h	2007-12-26 22:36:38 UTC (rev 2238)
@@ -282,6 +282,9 @@
   double Task_MoveNeutralGradient(cTaskContext& ctx) const;
   double Task_MoveDownGradient(cTaskContext& ctx) const;
   double Task_MoveNotUpGradient(cTaskContext& ctx) const;
+  double Task_MoveToRightSide(cTaskContext& ctx) const;
+  double Task_MoveToLeftSide(cTaskContext& ctx) const;
+  double Task_MoveToEvent(cTaskContext& ctx) const;
 };
 
 




More information about the Avida-cvs mailing list