[Avida-SVN] r2693 - in development/source: cpu main

dk at myxo.css.msu.edu dk at myxo.css.msu.edu
Tue Jul 1 07:07:11 PDT 2008


Author: dk
Date: 2008-07-01 10:07:11 -0400 (Tue, 01 Jul 2008)
New Revision: 2693

Modified:
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cDeme.cc
   development/source/main/cDeme.h
   development/source/main/cOrganism.cc
   development/source/main/cOrganism.h
Log:
Added "opinion" support to organisms, where each organism in the population can express an opinion (an int) over time.  Opinions provide a generic way to address consensus-related questions across groups of individuals.



Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-06-30 20:30:47 UTC (rev 2692)
+++ development/source/cpu/cHardwareCPU.cc	2008-07-01 14:07:11 UTC (rev 2693)
@@ -444,6 +444,11 @@
     tInstLibEntry<tMethod>("if-pheromone", &cHardwareCPU::Inst_IfPheromone),
     tInstLibEntry<tMethod>("if-not-pheromone", &cHardwareCPU::Inst_IfNotPheromone),
     tInstLibEntry<tMethod>("drop-pheromone", &cHardwareCPU::Inst_DropPheromone, nInstFlag::STALL),
+    
+    // Opinion instructions.
+    // These are STALLs because opinions are only relevant with respect to time.
+    tInstLibEntry<tMethod>("set-opinion", &cHardwareCPU::Inst_SetOpinion, nInstFlag::STALL),
+    tInstLibEntry<tMethod>("get-opinion", &cHardwareCPU::Inst_GetOpinion, nInstFlag::STALL),
 
     // Must always be the last instruction in the array
     tInstLibEntry<tMethod>("NULL", &cHardwareCPU::Inst_Nop, 0, "True no-operation instruction: does nothing"),
@@ -6601,3 +6606,28 @@
 
 } //End Inst_DropPheromone()
 
+
+/*! Set this organism's current opinion to the value in ?BX?.
+ */
+bool cHardwareCPU::Inst_SetOpinion(cAvidaContext& ctx)
+{
+  organism->SetOpinion(GetRegister(FindModifiedRegister(REG_BX)));
+  return true;
+}
+
+
+/*! Sense this organism's current opinion, placing the opinion in register ?BX?
+ and the age of that opinion (in updates) in register !?BX?.  If the organism has no
+ opinion, do nothing.
+ */
+bool cHardwareCPU::Inst_GetOpinion(cAvidaContext& ctx)
+{
+  if(organism->HasOpinion()) {
+    const int opinion_reg = FindModifiedRegister(REG_BX);
+    const int age_reg = FindNextRegister(opinion_reg);
+  
+    GetRegister(opinion_reg) = organism->GetOpinion().first;
+    GetRegister(age_reg) = m_world->GetStats().GetUpdate() - organism->GetOpinion().second;
+  }
+  return true;
+}

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2008-06-30 20:30:47 UTC (rev 2692)
+++ development/source/cpu/cHardwareCPU.h	2008-07-01 14:07:11 UTC (rev 2693)
@@ -653,6 +653,18 @@
   bool Inst_IfNotPheromone(cAvidaContext& ctx);
   bool Inst_DropPheromone(cAvidaContext& ctx);
 
+  // -------- Opinion support --------
+  /* These instructions interact with the "opinion" support in cOrganism.h.  The
+   idea is that we're enabling organisms to express an opinion about *something*,
+   where that something is defined by the particular tasks and/or (deme) fitness function
+   in use.  This may have to be extended in the future to support different kinds of
+   opinions that can be expressed during the same experiment, and possibly augmented
+   with a "strength" of that opinion (but not right now).
+   */
+  //! Set this organism's current opinion.
+  bool Inst_SetOpinion(cAvidaContext& ctx);
+  //! Retrieve this organism's current opinion.
+  bool Inst_GetOpinion(cAvidaContext& ctx);
 };
 
 

Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc	2008-06-30 20:30:47 UTC (rev 2692)
+++ development/source/main/cDeme.cc	2008-07-01 14:07:11 UTC (rev 2693)
@@ -103,11 +103,18 @@
   return std::make_pair(cellid % GetWidth(), ( cellid % cell_ids.GetSize() ) / GetWidth());
 }
 
-cPopulationCell& cDeme::GetCell(int pos)
+cPopulationCell& cDeme::GetCell(int pos) const
 {
   return m_world->GetPopulation().GetCell(cell_ids[pos]);
 }
 
+
+cOrganism* cDeme::GetOrganism(int pos) const
+{
+  return GetCell(pos).GetOrganism();
+}
+
+
 void cDeme::ProcessUpdate() {
   energyUsage.Clear();
 

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2008-06-30 20:30:47 UTC (rev 2692)
+++ development/source/main/cDeme.h	2008-07-01 14:07:11 UTC (rev 2693)
@@ -137,7 +137,8 @@
   int GetDemeID() const { return _id; }
   //! Returns an (x,y) pair for the position of the passed-in cell ID.
   std::pair<int, int> GetCellPosition(int cellid) const;
-  cPopulationCell& GetCell(int pos);
+  cPopulationCell& GetCell(int pos) const;
+  cOrganism* GetOrganism(int pos) const;
 
   int GetWidth() const { return width; }
   int GetHeight() const { return cell_ids.GetSize() / width; }

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2008-06-30 20:30:47 UTC (rev 2692)
+++ development/source/main/cOrganism.cc	2008-07-01 14:07:11 UTC (rev 2693)
@@ -46,6 +46,7 @@
 #include "cStats.h"
 
 #include <iomanip>
+#include <utility>
 
 using namespace std;
 
@@ -74,6 +75,7 @@
   , killed_event(false)
   , m_net(NULL)
   , m_msg(0)
+  , m_opinion(0)
 {
   // Initialization of structures...
   m_hardware = m_world->GetHardwareManager().Create(this);
@@ -104,6 +106,7 @@
   delete m_interface;
   if(m_net) delete m_net;
   if(m_msg) delete m_msg;
+  if(m_opinion) delete m_opinion;
 }
 
 cOrganism::cNetSupport::~cNetSupport()
@@ -690,3 +693,13 @@
   // move IP to alarm_label
   m_hardware->Jump_To_Alarm_Label(jump_label);
 }
+
+
+/*! Called to set this organism's opinion, which remains valid until a new opinion
+ is expressed.
+ */
+void cOrganism::SetOpinion(const Opinion& opinion)
+{
+  InitOpinions();
+  m_opinion->opinion_list.push_back(std::make_pair(opinion, m_world->GetStats().GetUpdate()));
+}

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2008-06-30 20:30:47 UTC (rev 2692)
+++ development/source/main/cOrganism.h	2008-07-01 14:07:11 UTC (rev 2693)
@@ -392,6 +392,43 @@
   
   void SetEventKilled() { killed_event = true; }
   bool GetEventKilled() { return killed_event; }
+  
+  // -------- Opinion support --------
+  /*  Organisms express an opinion at a given point in time.  We can assume that they
+   hold this opinion until they express a new one.  The semantics of opinions are
+   left to the particular tasks or fitness functions in use; opinions are merely a generic
+   approach that we as developers can use to evaluate when an organism has made a decision.
+   
+   If we ever have a need for organisms to express different kinds of opinions, this code
+   can easily be adapted for that purpose (e.g., change the OpinionList typedef to a
+   std::map<int, DatedOpinion>, where the key represents the kind of opinion being expressed).
+   
+   As with other such types of "extended" functionality, opinion support is encapsulated in
+   a lazily-initialized struct.
+   */  
+public:
+  typedef int Opinion; //!< Typedef for an opinion.
+  typedef std::pair<Opinion, int> DatedOpinion; //!< Typedef for an opinion held at a given update.
+  typedef std::vector<DatedOpinion> DatedOpinionList; //!< Typedef for a list of dated opinions.
+  //! Called to set this organism's opinion.
+  void SetOpinion(const Opinion& opinion);
+  //! Retrieve this organism's current opinion.
+  const DatedOpinion& GetOpinion() { InitOpinions(); return m_opinion->opinion_list.back(); }
+  //! Retrieve all opinions expressed during this organism's lifetime.
+  const DatedOpinionList& GetOpinions() { InitOpinions(); return m_opinion->opinion_list; }
+  //! Return whether this organism has an opinion.
+  bool HasOpinion() { InitOpinions(); return m_opinion->opinion_list.size(); }
+  
+protected:
+  //! Initialize opinion support.
+  inline void InitOpinions() { if(!m_opinion) { m_opinion = new cOpinionSupport(); } }
+  //! Container for the data used to support opinions.
+  struct cOpinionSupport
+  {
+    DatedOpinionList opinion_list; //!< All opinions expressed by this organism during its lifetime.
+  };
+  cOpinionSupport* m_opinion; //!< Lazily-initialized pointer to the opinion data.
+  // -------- End of opinion support --------
 };
 
 




More information about the Avida-cvs mailing list