[Avida-SVN] r2700 - in branches/uml-merge: . source/cpu source/main
dk at myxo.css.msu.edu
dk at myxo.css.msu.edu
Wed Jul 2 07:43:52 PDT 2008
Author: dk
Date: 2008-07-02 10:43:52 -0400 (Wed, 02 Jul 2008)
New Revision: 2700
Modified:
branches/uml-merge/Avida.vcproj
branches/uml-merge/source/cpu/cHardwareCPU.cc
branches/uml-merge/source/cpu/cHardwareCPU.h
branches/uml-merge/source/main/cDeme.cc
branches/uml-merge/source/main/cDeme.h
branches/uml-merge/source/main/cOrgMovementPredicate.h
branches/uml-merge/source/main/cOrganism.cc
branches/uml-merge/source/main/cOrganism.h
Log:
Merged 2686:2698 from development.
Modified: branches/uml-merge/Avida.vcproj
===================================================================
--- branches/uml-merge/Avida.vcproj 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/Avida.vcproj 2008-07-02 14:43:52 UTC (rev 2700)
@@ -653,6 +653,14 @@
>
</File>
<File
+ RelativePath=".\source\main\cOrgMessagePredicate.h"
+ >
+ </File>
+ <File
+ RelativePath=".\source\main\cOrgMovementPredicate.h"
+ >
+ </File>
+ <File
RelativePath=".\source\main\cOrgSeqMessage.h"
>
</File>
Modified: branches/uml-merge/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/uml-merge/source/cpu/cHardwareCPU.cc 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/cpu/cHardwareCPU.cc 2008-07-02 14:43:52 UTC (rev 2700)
@@ -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"),
@@ -5358,8 +5363,11 @@
}
}
- GetRegister(reg_to_set) = (int)round(pher_amount);
+ // In Visual Studio 2005 round function does not exist use floor instead
+ // GetRegister(reg_to_set) = (int)round(pher_amount);
+ GetRegister(reg_to_set) = (int)floor(pher_amount + 0.5);
+
return true;
} //End DoSensePheromone()
@@ -6598,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: branches/uml-merge/source/cpu/cHardwareCPU.h
===================================================================
--- branches/uml-merge/source/cpu/cHardwareCPU.h 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/cpu/cHardwareCPU.h 2008-07-02 14:43:52 UTC (rev 2700)
@@ -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: branches/uml-merge/source/main/cDeme.cc
===================================================================
--- branches/uml-merge/source/main/cDeme.cc 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/main/cDeme.cc 2008-07-02 14:43:52 UTC (rev 2700)
@@ -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: branches/uml-merge/source/main/cDeme.h
===================================================================
--- branches/uml-merge/source/main/cDeme.h 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/main/cDeme.h 2008-07-02 14:43:52 UTC (rev 2700)
@@ -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: branches/uml-merge/source/main/cOrgMovementPredicate.h
===================================================================
--- branches/uml-merge/source/main/cOrgMovementPredicate.h 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/main/cOrgMovementPredicate.h 2008-07-02 14:43:52 UTC (rev 2700)
@@ -194,7 +194,7 @@
{
// See how many times this org has gone back and forth. If it has
// done it enough times, it is considered successful
- unsigned int num_backforth = (int) floor(org.GetNumTaskCellsReached()/2);
+ unsigned int num_backforth = org.GetNumTaskCellsReached()/2;
if(num_backforth >= m_total_times) {
m_successful_orgs.insert(org_id);
Modified: branches/uml-merge/source/main/cOrganism.cc
===================================================================
--- branches/uml-merge/source/main/cOrganism.cc 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/main/cOrganism.cc 2008-07-02 14:43:52 UTC (rev 2700)
@@ -47,6 +47,7 @@
#include "cStats.h"
#include <iomanip>
+#include <utility>
using namespace std;
@@ -76,6 +77,7 @@
, m_net(NULL)
, m_msg(0)
, m_orchid(0)
+ , m_opinion(0)
{
// Initialization of structures...
m_hardware = m_world->GetHardwareManager().Create(this);
@@ -104,9 +106,10 @@
assert(m_is_running == false);
delete m_hardware;
delete m_interface;
- if(m_net != NULL) delete m_net;
if(m_msg) delete m_msg;
+ if(m_net) delete m_net;
if(m_orchid) delete m_orchid;
+ if(m_opinion) delete m_opinion;
}
cOrganism::cNetSupport::~cNetSupport()
@@ -694,3 +697,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: branches/uml-merge/source/main/cOrganism.h
===================================================================
--- branches/uml-merge/source/main/cOrganism.h 2008-07-02 14:31:32 UTC (rev 2699)
+++ branches/uml-merge/source/main/cOrganism.h 2008-07-02 14:43:52 UTC (rev 2700)
@@ -404,6 +404,43 @@
protected:
cOrchidOrganism* m_orchid; //!< This organism's Orchid facet; null if not using Orchid.
//-------- End of Orchid support --------
+
+ // -------- 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