[Avida-SVN] r2977 - development/source/main

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Wed Nov 26 12:47:23 PST 2008


Author: brysonda
Date: 2008-11-26 15:47:23 -0500 (Wed, 26 Nov 2008)
New Revision: 2977

Modified:
   development/source/main/cOrgInterface.h
   development/source/main/cOrganism.cc
   development/source/main/cOrganism.h
   development/source/main/cPhenotype.cc
   development/source/main/cTaskContext.h
   development/source/main/cTaskLib.cc
Log:
Cleanup and correct organism interface abstraction in some recently added tasks in cTaskLib so that they can eventually be used outside of population based experiments.

Modified: development/source/main/cOrgInterface.h
===================================================================
--- development/source/main/cOrgInterface.h	2008-11-26 20:13:48 UTC (rev 2976)
+++ development/source/main/cOrgInterface.h	2008-11-26 20:47:23 UTC (rev 2977)
@@ -71,13 +71,16 @@
   virtual void SetPrevTaskCellID(int in_id) = 0;
 
   virtual bool Divide(cAvidaContext& ctx, cOrganism* parent, cGenome& child_genome) = 0;
+  
   virtual cOrganism* GetNeighbor() = 0;
   virtual bool IsNeighborCellOccupied() = 0;
   virtual int GetNumNeighbors() = 0;
   virtual int GetFacing() = 0; //!< Returns the facing of this organism.
   virtual int GetNeighborCellContents() = 0;
   virtual void Rotate(int direction = 1) = 0;
+  
   virtual void Breakpoint() = 0;
+  
   virtual int GetInputAt(int& input_pointer) = 0;
   virtual void ResetInputs(cAvidaContext& ctx) = 0;
   virtual const tArray<int>& GetInputs() const = 0;

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2008-11-26 20:13:48 UTC (rev 2976)
+++ development/source/main/cOrganism.cc	2008-11-26 20:47:23 UTC (rev 2977)
@@ -324,8 +324,8 @@
   tBuffer<int>* received_messages_point = &m_received_messages;
   if (!m_world->GetConfig().SAVE_RECEIVED.Get()) received_messages_point = NULL;
   
-  cTaskContext taskctx(m_interface, input_buffer, output_buffer, other_input_list, other_output_list,
-                       m_hardware->GetExtendedMemory(), net_valid, 0, on_divide, received_messages_point, this);
+  cTaskContext taskctx(this, input_buffer, output_buffer, other_input_list, other_output_list,
+                       m_hardware->GetExtendedMemory(), net_valid, 0, on_divide, received_messages_point);
                        
   //combine global and deme resource counts
   const tArray<double> globalAndDeme_resource_count = global_resource_count + deme_resource_count;
@@ -511,7 +511,7 @@
     tArray<double> res_change(resource_count.GetSize());
     tArray<int> insts_triggered;
 
-    cTaskContext taskctx(m_interface, m_input_buf, m_output_buf, other_input_list, other_output_list,
+    cTaskContext taskctx(this, m_input_buf, m_output_buf, other_input_list, other_output_list,
                          m_hardware->GetExtendedMemory(), false, completed);
     m_phenotype.TestOutput(ctx, taskctx, resource_count, m_rbins, res_change, insts_triggered);
     m_interface->UpdateResources(res_change);

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2008-11-26 20:13:48 UTC (rev 2976)
+++ development/source/main/cOrganism.h	2008-11-26 20:47:23 UTC (rev 2977)
@@ -227,23 +227,33 @@
   
   // --------  cOrgInterface Methods  --------
   cHardwareBase& GetHardware() { return *m_hardware; }
+  int GetID() { return m_id; }
+
+  int GetCellID() { return m_interface->GetCellID(); }
+  int GetDemeID() { return m_interface->GetDemeID(); }
+  cDeme* GetDeme() { return m_interface->GetDeme(); }
+  
+  int GetCellData() { return m_interface->GetCellData(); }
+  void SetCellData(const int data) { m_interface->SetCellData(data); }  
+  
   cOrganism* GetNeighbor() { return m_interface->GetNeighbor(); }
   bool IsNeighborCellOccupied() { return m_interface->IsNeighborCellOccupied(); }
   int GetNeighborhoodSize() { return m_interface->GetNumNeighbors(); }
   int GetFacing() { assert(m_interface); return m_interface->GetFacing(); }  // Returns the facing of this organism.
   int GetNeighborCellContents() const { return m_interface->GetNeighborCellContents(); }
   void Rotate(int direction) { m_interface->Rotate(direction); }
+  
   void DoBreakpoint() { m_interface->Breakpoint(); }
+  
+  int GetInputAt(int i) { return m_interface->GetInputAt(i); }
   int GetNextInput() { return m_interface->GetInputAt(m_input_pointer); }
-  int GetNextInput(int& in_input_pointer) { return m_interface->GetInputAt(in_input_pointer); } //@JEB alternate for GX
+  int GetNextInput(int& in_input_pointer) { return m_interface->GetInputAt(in_input_pointer); }
   tBuffer<int>& GetInputBuf() { return m_input_buf; }
   tBuffer<int>& GetOutputBuf() { return m_output_buf; }
   void Die() { m_interface->Die(); m_is_dead = true; }
   void Kaboom(int dist) { m_interface->Kaboom(dist);}
   void SpawnDeme() { m_interface->SpawnDeme(); }
-  int GetCellID() { return m_interface->GetCellID(); }
   int GetDebugInfo() { return m_interface->Debug(); }
-  int GetID() { return m_id; }
   bool GetSentActive() { return m_sent_active; }
   void SendValue(int value) { m_sent_active = true; m_sent_value = value; }
   int RetrieveSentValue() { m_sent_active = false; return m_sent_value; }
@@ -261,8 +271,6 @@
   int GetNumTaskCellsReached() const { return m_interface->GetNumTaskCellsReached(); }
   void AddReachedTaskCell() { m_interface->AddReachedTaskCell(); }
 
-  int GetCellData() { return m_interface->GetCellData(); }
-  void SetCellData(const int data) { m_interface->SetCellData(data); }  
 
   
   // --------  Input and Output Methods  --------

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2008-11-26 20:13:48 UTC (rev 2976)
+++ development/source/main/cPhenotype.cc	2008-11-26 20:47:23 UTC (rev 2977)
@@ -28,6 +28,7 @@
 #include "cEnvironment.h"
 #include "cDeme.h"
 #include "cHardwareManager.h"
+#include "cOrganism.h"
 #include "cReactionResult.h"
 #include "cTaskState.h"
 #include "cTools.h"
@@ -1138,8 +1139,8 @@
   
   // Update deme merit (guard against running in the test CPU, where there is
   // no deme object.  Don't touch deme merit if there is no deme frac component.
-  cDeme* deme = taskctx.GetOrgInterface()->GetDeme();
-  if(deme) {
+  cDeme* deme = taskctx.GetOrganism()->GetDeme();
+  if (deme) {
   
     if (result.GetActiveDeme()) {
       double deme_bonus = deme->GetHeritableDemeMerit().GetDouble();

Modified: development/source/main/cTaskContext.h
===================================================================
--- development/source/main/cTaskContext.h	2008-11-26 20:13:48 UTC (rev 2976)
+++ development/source/main/cTaskContext.h	2008-11-26 20:47:23 UTC (rev 2977)
@@ -45,7 +45,7 @@
 class cTaskContext
 {
 private:
-  cOrgInterface* m_interface;
+  cOrganism* m_organism;
   const tBuffer<int>& m_input_buffer;
   const tBuffer<int>& m_output_buffer;
   const tList<tBuffer<int> >& m_other_input_buffers;
@@ -64,15 +64,13 @@
   cTaskEntry* m_task_entry;
   tHashTable<void*, cTaskState*>* m_task_states;
   
-  cOrganism* m_org;
   
-  
 public:
-  cTaskContext(cOrgInterface* interface, const tBuffer<int>& inputs, const tBuffer<int>& outputs,
+  cTaskContext(cOrganism* organism, const tBuffer<int>& inputs, const tBuffer<int>& outputs,
                const tList<tBuffer<int> >& other_inputs, const tList<tBuffer<int> >& other_outputs,
                const tArray<int>& ext_mem, bool in_net_valid, int in_net_completed, bool in_on_divide = false,
-               tBuffer<int>* in_received_messages = NULL, cOrganism* org = NULL)
-    : m_interface(interface)
+               tBuffer<int>* in_received_messages = NULL)
+    : m_organism(organism)
     , m_input_buffer(inputs)
     , m_output_buffer(outputs)
     , m_other_input_buffers(other_inputs)
@@ -85,13 +83,11 @@
     , m_on_divide(in_on_divide)
     , m_task_entry(NULL)
     , m_task_states(NULL)
-    , m_org(org)
   {
 	  m_task_value = 0;
   }
   
-  inline cOrgInterface* GetOrgInterface() { return m_interface; }
-  inline int GetInputAt(int index) { return m_interface->GetInputAt(index); }
+  inline cOrganism* GetOrganism() { return m_organism; }
   inline const tBuffer<int>& GetInputBuffer() { return m_input_buffer; }
   inline const tBuffer<int>& GetOutputBuffer() { return m_output_buffer; }
   inline const tList<tBuffer<int> >& GetNeighborhoodInputBuffers() { return m_other_input_buffers; }
@@ -108,9 +104,7 @@
   
   inline void SetTaskEntry(cTaskEntry* in_entry) { m_task_entry = in_entry; }
   inline cTaskEntry* GetTaskEntry() { return m_task_entry; }
-  
-  inline cOrganism* GetOrganism() { return m_org; }
-  
+    
   inline void SetTaskStates(tHashTable<void*, cTaskState*>* states) { m_task_states = states; }
   
   inline cTaskState* GetTaskState()

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2008-11-26 20:13:48 UTC (rev 2976)
+++ development/source/main/cTaskLib.cc	2008-11-26 20:47:23 UTC (rev 2977)
@@ -2031,7 +2031,7 @@
   int maxscore = 0;
   
   // add all valid inputs into the value map
-  for (int i = 0; i < size; i++) valmap.Add(ctx.GetInputAt(i), -1);
+  for (int i = 0; i < size; i++) valmap.Add(ctx.GetOrganism()->GetInputAt(i), -1);
   
   int span_start = -1;
   int span_end = stored;
@@ -2103,7 +2103,7 @@
     for (int i = 0; i < size; i++) {
       int idx;
       // if input was not observed
-      if (valmap.Find(ctx.GetInputAt(i), idx) && idx == -1) {
+      if (valmap.Find(ctx.GetOrganism()->GetInputAt(i), idx) && idx == -1) {
         maxscore += count; // add to the maximum move count
         score += count; // missing values, scored as maximally out of order
         count++; // increment observed count
@@ -2899,20 +2899,18 @@
 }
 
 double cTaskLib::Task_MoveToRightSide(cTaskContext& ctx) const {	
-  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
-  std::pair<int, int> location = deme.GetCellPosition(ctx.GetOrgInterface()->GetCellID());
+  cDeme* deme = ctx.GetOrganism()->GetDeme();
+  std::pair<int, int> location = deme->GetCellPosition(ctx.GetOrganism()->GetCellID());
   
-  if(location.first == m_world->GetConfig().WORLD_X.Get()-1)
-    return 1.0;
+  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.GetCellPosition(ctx.GetOrgInterface()->GetCellID());
+  cDeme* deme = ctx.GetOrganism()->GetDeme();
+  std::pair<int, int> location = deme->GetCellPosition(ctx.GetOrganism()->GetCellID());
   
-  if(location.first == 0)
-    return 1.0;
+  if (location.first == 0) return 1.0;
   return 0.0;
 }
 
@@ -2930,30 +2928,31 @@
 double cTaskLib::Task_MoveToTarget(cTaskContext& ctx) const
 //Note - a generic version of this is now at - Task_MoveToMovementEvent
 {
-  cOrgInterface* iface = ctx.GetOrgInterface();
+  cOrganism* org = ctx.GetOrganism();
+  
+  if (org->GetCellID() == -1) return 0.0;		
 	
-  if(ctx.GetOrganism()->GetCellID() == -1) {
-    return 0.0;		
-  }
-	
-  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
-  int celldata = m_world->GetPopulation().GetCell(iface->GetCellID()).GetCellData();
-
-  int current_cell = deme.GetRelativeCellID(iface->GetCellID());
-  int prev_target = deme.GetRelativeCellID(iface->GetPrevTaskCellID());
+  cDeme* deme = org->GetDeme();
+  assert(deme);
   
+  int cell_data = org->GetCellData();
+  if (cell_data <= 0) return 0.0;
+  
+  int current_cell = deme->GetRelativeCellID(org->GetCellID());
+  int prev_target = deme->GetRelativeCellID(org->GetPrevTaskCellID());
+  
   // If the organism is currently on a target cell, see which target cell it previously
   // visited.  Since we want them to move back and forth, only reward if we are on
   // a different target cell.
 
-  if(celldata > 1) 
+  if (cell_data > 1) 
   {
     if (current_cell == prev_target) {
       // At some point, we may want to return a fraction
       return 0;
     } else {
-      iface->AddReachedTaskCell();
-      iface->SetPrevTaskCellID(current_cell);
+      org->AddReachedTaskCell();
+      org->SetPrevTaskCellID(current_cell);
       return 1.0;
     }
   }
@@ -2962,97 +2961,93 @@
 
 } //End cTaskLib::TaskMoveToTarget()
 
-double cTaskLib::Task_MoveToMovementEvent(cTaskContext& ctx) const {
+double cTaskLib::Task_MoveToMovementEvent(cTaskContext& ctx) const
+{
+  cOrganism* org = ctx.GetOrganism();
+  
+  if (org->GetCellID() == -1) return 0.0;		
 	
-  if(ctx.GetOrganism()->GetCellID() == -1) {
-    return 0.0;		
-  }	
-	
-  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
-  int cell_data = m_world->GetPopulation().GetCell(ctx.GetOrgInterface()->GetCellID()).GetCellData();
-  cOrgInterface* iface = ctx.GetOrgInterface();
-
-  if(cell_data <= 0)
-    return 0.0;
+  cDeme* deme = org->GetDeme();
+  assert(deme);
+  
+  int cell_data = org->GetCellData();
+  if (cell_data <= 0) return 0.0;
     
-  for(int i = 0; i < deme.GetNumMovementPredicates(); i++) {
-      if(deme.GetMovPredicate(i)->GetEvent(0)->GetEventID() == cell_data) {
-        iface->AddReachedTaskCell();
-        iface->SetPrevTaskCellID(cell_data);
-        return 1.0;
-      }
+  for (int i = 0; i < deme->GetNumMovementPredicates(); i++) {
+    if (deme->GetMovPredicate(i)->GetEvent(0)->GetEventID() == cell_data) {
+      org->AddReachedTaskCell();
+      org->SetPrevTaskCellID(cell_data);
+      return 1.0;
+    }
   }
   return 0.0;
 }
 
 
-double cTaskLib::Task_MoveBetweenMovementEvent(cTaskContext& ctx) const {
+double cTaskLib::Task_MoveBetweenMovementEvent(cTaskContext& ctx) const
+{	
+  cOrganism* org = ctx.GetOrganism();
+
+  if (org->GetCellID() == -1) return 0.0;
 	
-  if(ctx.GetOrganism()->GetCellID() == -1) {
-    return 0.0;		
-  }	
-	
-  cDeme& deme = m_world->GetPopulation().GetDeme(ctx.GetOrgInterface()->GetDemeID());
-  int cell_data = m_world->GetPopulation().GetCell(ctx.GetOrgInterface()->GetCellID()).GetCellData();
-  cOrgInterface* iface = ctx.GetOrgInterface();
-  int prev_target = deme.GetRelativeCellID(iface->GetPrevTaskCellID());
+  cDeme* deme = org->GetDeme();
+  assert(deme);
 
-//  int cellid = ctx.GetOrgInterface()->GetCellID();
+  int cell_data = org->GetCellData();
+  
+  int prev_target = deme->GetRelativeCellID(org->GetPrevTaskCellID());
 
   // NOTE: as of now, orgs aren't rewarded if they touch a target more than
   //   once in a row.  Could be useful in the future to have fractional reward
   //   or something.
-  if( (cell_data <= 0) || (cell_data == prev_target) )
-    return 0.0;
+  if ( (cell_data <= 0) || (cell_data == prev_target) ) return 0.0;
     
-  for(int i = 0; i < deme.GetNumMovementPredicates(); i++) {
-      // NOTE: having problems with calling the GetNumEvents function for some reason.  FIXME
-      //int num_events = deme.GetMovPredicate(i)->GetNumEvents;
-      int num_events = 2;
+  for (int i = 0; i < deme->GetNumMovementPredicates(); i++) {
+    // NOTE: having problems with calling the GetNumEvents function for some reason.  FIXME
+    //int num_events = deme.GetMovPredicate(i)->GetNumEvents;
+    int num_events = 2;
 
-      if(num_events == 1) {
-        if( (deme.GetMovPredicate(i)->GetEvent(0)->IsActive()) &&
-            (deme.GetMovPredicate(i)->GetEvent(0)->GetEventID() == cell_data) ) {
-          iface->AddReachedTaskCell();
-          iface->SetPrevTaskCellID(cell_data);
+    if (num_events == 1) {
+      if ( (deme->GetMovPredicate(i)->GetEvent(0)->IsActive()) &&
+          (deme->GetMovPredicate(i)->GetEvent(0)->GetEventID() == cell_data) ) {
+        org->AddReachedTaskCell();
+        org->SetPrevTaskCellID(cell_data);
+        return 1.0;
+      }
+    } else {
+      for (int j = 0; j < num_events; j++) {
+        cDemeCellEvent* event = deme->GetMovPredicate(i)->GetEvent(j);
+        if( (event != NULL) && (event->IsActive()) && (event->GetEventID() == cell_data) ) {
+          org->AddReachedTaskCell();
+          org->SetPrevTaskCellID(cell_data);
           return 1.0;
         }
-      } else {
-        for(int j = 0; j < num_events; j++) {
-          cDemeCellEvent *event = deme.GetMovPredicate(i)->GetEvent(j);
-          if( (event != NULL) && (event->IsActive()) &&
-              (event->GetEventID() == cell_data) ) {
-            iface->AddReachedTaskCell();
-            iface->SetPrevTaskCellID(cell_data);
-            return 1.0;
-          }
-        }
       }
-
+    }
   }
   return 0.0;
 }
 
-double cTaskLib::Task_MoveToEvent(cTaskContext& ctx) const {
+double cTaskLib::Task_MoveToEvent(cTaskContext& ctx) const
+{
+  cOrganism* org = ctx.GetOrganism();
+  
+  if (org->GetCellID() == -1) return 0.0;
 	
-  if(ctx.GetOrganism()->GetCellID() == -1) {
-    return 0.0;		
-  }	
-	
-  cDeme* deme = ctx.GetOrganism()->GetOrgInterface().GetDeme();
-  int cell_data = ctx.GetOrganism()->GetOrgInterface().GetCellData();
-  if(cell_data <= 0)
-    return 0.0;
+  cDeme* deme = org->GetDeme();
+  assert(deme);
+  
+  int cell_data = org->GetCellData();
+  if (cell_data <= 0) return 0.0;
     
-  for(int i = 0; i < deme->GetNumEvents(); i++) {
-    if(deme->GetCellEvent(i)->GetEventID() == cell_data)
-      return 1.0;
+  for (int i = 0; i < deme->GetNumEvents(); i++) {
+    if (deme->GetCellEvent(i)->GetEventID() == cell_data) return 1.0;
   }
   return 0.0;
 }
 
-double cTaskLib::Task_EventKilled(cTaskContext& ctx) const {
-  if(ctx.GetOrganism()->GetEventKilled())
-    return 1.0;
+double cTaskLib::Task_EventKilled(cTaskContext& ctx) const
+{
+  if (ctx.GetOrganism()->GetEventKilled()) return 1.0;
   return 0.0;
 }




More information about the Avida-cvs mailing list