[Avida-SVN] r1388 - in branches/dkdev/source: actions cpu main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Mon Mar 5 06:03:48 PST 2007


Author: dknoester
Date: 2007-03-05 09:03:47 -0500 (Mon, 05 Mar 2007)
New Revision: 1388

Modified:
   branches/dkdev/source/actions/PopulationActions.cc
   branches/dkdev/source/cpu/cHardwareCPU.cc
   branches/dkdev/source/cpu/cHardwareCPU.h
   branches/dkdev/source/main/cDeme.h
   branches/dkdev/source/main/cStats.cc
Log:
Added the following new instructions:
rd-links: Retrieves  the number and ids of the cells that the caller is 
  connected to via the constructed topology.
get-lpos: Retrieves the local position of the caller within the deme (x,y).

Added the following population action:
InjectDemes: Inject an organism into the 0th position of all demes.

Further improvements to outputting the constructed topology.



Modified: branches/dkdev/source/actions/PopulationActions.cc
===================================================================
--- branches/dkdev/source/actions/PopulationActions.cc	2007-03-05 13:24:46 UTC (rev 1387)
+++ branches/dkdev/source/actions/PopulationActions.cc	2007-03-05 14:03:47 UTC (rev 1388)
@@ -395,6 +395,56 @@
 };
 
 
+
+
+/*! Injects an organism into all demes in the population. 
+
+Parameters:
+filename (string):
+  The filename of the genotype to load. If this is left empty, or the keyword
+  "START_CREATURE" is given, than the genotype specified in the genesis
+  file under "START_CREATURE" is used.
+cell ID (integer) default: 0
+  The grid-point into which the organism should be placed.
+merit (double) default: -1
+  The initial merit of the organism. If set to -1, this is ignored.
+lineage label (integer) default: 0
+  An integer that marks all descendants of this organism.
+neutral metric (double) default: 0
+  A double value that randomly drifts over time.
+*/
+class cActionInjectDemes : public cAction
+{
+private:
+	cString m_filename;
+	double m_merit;
+	int m_lineage_label;
+	double m_neutral_metric;
+public:
+		cActionInjectDemes(cWorld* world, const cString& args) : cAction(world, args), m_merit(-1), m_lineage_label(0), m_neutral_metric(0)
+	{
+			cString largs(args);
+			if (!largs.GetSize()) m_filename = "START_CREATURE"; else m_filename = largs.PopWord();
+			if (largs.GetSize()) m_merit = largs.PopWord().AsDouble();
+			if (largs.GetSize()) m_lineage_label = largs.PopWord().AsInt();
+			if (largs.GetSize()) m_neutral_metric = largs.PopWord().AsDouble();
+			if (m_filename == "START_CREATURE") m_filename = m_world->GetConfig().START_CREATURE.Get();
+	}
+	
+	static const cString GetDescription() { return "Arguments: [string fname=\"START_CREATURE\"] [double merit=-1] [int lineage_label=0] [double neutral_metric=0]"; }
+	
+	void Process(cAvidaContext& ctx)
+	{
+		cGenome genome = cInstUtil::LoadGenome(m_filename, m_world->GetHardwareManager().GetInstSet());
+		for(int i=0; i<m_world->GetPopulation().GetNumDemes(); ++i) {
+			m_world->GetPopulation().Inject(genome,
+											m_world->GetPopulation().GetDeme(i).GetCellID(0),
+											m_merit, m_lineage_label, m_neutral_metric);
+		}
+	}
+};
+
+
 /*
  Randomly removes a certain proportion of the population.
  
@@ -1239,6 +1289,7 @@
   action_lib->Register<cActionInjectAll>("InjectAll");
   action_lib->Register<cActionInjectRange>("InjectRange");
   action_lib->Register<cActionInjectSequence>("InjectSequence");
+  action_lib->Register<cActionInjectDemes>("InjectDemes");
 
   action_lib->Register<cActionInjectParasite>("InjectParasite");
   action_lib->Register<cActionInjectParasitePair>("InjectParasitePair");

Modified: branches/dkdev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.cc	2007-03-05 13:24:46 UTC (rev 1387)
+++ branches/dkdev/source/cpu/cHardwareCPU.cc	2007-03-05 14:03:47 UTC (rev 1388)
@@ -360,9 +360,12 @@
     cInstEntryCPU("send-msg", &cHardwareCPU::Inst_SendMessage),
     cInstEntryCPU("rtrv-msg", &cHardwareCPU::Inst_RetrieveMessage),
     cInstEntryCPU("cell-id", &cHardwareCPU::Inst_CellID),
+	cInstEntryCPU("get-pos", &cHardwareCPU::Inst_GetPosition),
     
     // Construction instructions
-    cInstEntryCPU("cr-link", &cHardwareCPU::Inst_CreateLink)
+    cInstEntryCPU("cr-link", &cHardwareCPU::Inst_CreateLink),
+	cInstEntryCPU("rd-links", &cHardwareCPU::Inst_ReadLinks),
+	cInstEntryCPU("get-lpos", &cHardwareCPU::Inst_GetLocalPosition)
   };
   
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntryCPU);
@@ -3428,6 +3431,15 @@
 }
 
 
+/*! Places this organism's position in registers ?BX? and !?BX?. */
+bool cHardwareCPU::Inst_GetPosition(cAvidaContext& ctx) {
+	const int x = FindModifiedRegister(REG_BX);
+	const int y = FindNextRegister(x);
+	assert(false);
+	return true;
+}
+
+
 /*! Creates a link between cells, a form of distributed construction. 
 
 Going to try something a little different here, and put all the logic for the link
@@ -3457,20 +3469,71 @@
   // add them; if yes, then get a ref to the vertex descriptors.
   cDeme::CellVertexMap::iterator u = cvMap.find(cell.GetID());
   if(u == cvMap.end()) {
-    // Add this cell to the network.
     u = cvMap.insert(std::make_pair(cell.GetID(), 
-									boost::add_vertex(deme.GetCellPosition(cell.GetID()),
-													  network))).first;
+		boost::add_vertex(
+		cDeme::vertex_properties(deme.GetCellPosition(cell.GetID()), cell.GetID()),
+						  network))).first;
   }
   cDeme::CellVertexMap::iterator v = cvMap.find(connectTo);
   if(v == cvMap.end()) {
     // Add the other cell to the network.
     v = cvMap.insert(std::make_pair(connectTo, 
-									boost::add_vertex(deme.GetCellPosition(connectTo),
-													  network))).first;
+		boost::add_vertex(
+		cDeme::vertex_properties(deme.GetCellPosition(connectTo), connectTo),
+						  network))).first;
   }  
 
   // Finally, add the edge.  We're done.
   boost::add_edge(u->second, v->second, network);
   return true;
 }
+
+
+/*! Retrieves the cell ids of adjacent vertices, stores then in the stack, and 
+then stores the number of adjacent vertices in a register.
+*/
+bool cHardwareCPU::Inst_ReadLinks(cAvidaContext& ctx) {
+	if(organism->GetCellID()==-1) return false;
+	cPopulationCell& cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+	cDeme& deme = m_world->GetPopulation().GetDeme(cell.GetDemeID());
+	cDeme::Network& network = deme.GetNetwork();
+	cDeme::CellVertexMap& cvMap = deme.GetCellVertexMap();
+	const int reg_used = FindModifiedRegister(REG_BX);
+	
+	// See if this cell is connected to any others.
+	cDeme::CellVertexMap::iterator u = cvMap.find(cell.GetID());
+	int edge_count=0;
+	if(u == cvMap.end()) {
+		// Nope; set the number of edges to 0.
+		edge_count = 0;
+	} else {
+		// Ok, get the adjacent vertices, grab their cell ids, and push each one
+		// on the stack.
+		cDeme::Network::adjacency_iterator vi, vi_end;
+		for(boost::tie(vi,vi_end)=boost::adjacent_vertices(u->second, network)
+			; vi!=vi_end
+			; ++vi, ++edge_count) {
+			// Push this on the stack.
+			StackPush(boost::get(boost::vertex_bundle, network)[*vi]._cell_id);
+		}
+	}
+
+	GetRegister(reg_used) = edge_count;
+	return true;
+}
+
+
+/*! Retrieves the local position of this organism (its position within its deme). */
+bool cHardwareCPU::Inst_GetLocalPosition(cAvidaContext& ctx) {
+	if(organism->GetCellID()==-1) return false;
+	cPopulationCell& cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+	cDeme& deme = m_world->GetPopulation().GetDeme(cell.GetDemeID());
+	const int x = FindModifiedRegister(REG_BX);
+	const int y = FindNextRegister(x);
+
+	// Get this cell's position, and set our registers.
+	std::pair<int,int> pos = deme.GetCellPosition(cell.GetID());
+	GetRegister(x) = pos.first;
+	GetRegister(y) = pos.second;	
+	return true;
+}

Modified: branches/dkdev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.h	2007-03-05 13:24:46 UTC (rev 1387)
+++ branches/dkdev/source/cpu/cHardwareCPU.h	2007-03-05 14:03:47 UTC (rev 1388)
@@ -476,9 +476,12 @@
   bool Inst_SendMessage(cAvidaContext& ctx);
   bool Inst_RetrieveMessage(cAvidaContext& ctx);
   bool Inst_CellID(cAvidaContext& ctx);
+  bool Inst_GetPosition(cAvidaContext& ctx);
   
   // Construction
   bool Inst_CreateLink(cAvidaContext& ctx);
+  bool Inst_ReadLinks(cAvidaContext& ctx);
+  bool Inst_GetLocalPosition(cAvidaContext& ctx);
 };
 
 

Modified: branches/dkdev/source/main/cDeme.h
===================================================================
--- branches/dkdev/source/main/cDeme.h	2007-03-05 13:24:46 UTC (rev 1387)
+++ branches/dkdev/source/main/cDeme.h	2007-03-05 14:03:47 UTC (rev 1388)
@@ -22,9 +22,18 @@
 
 class cDeme {
 public:
+	//! The internal vertex properties.
+	struct vertex_properties {
+		vertex_properties() { }
+		vertex_properties(std::pair<int,int> pos, int cell_id) 
+		: _x(pos.first), _y(pos.second), _cell_id(cell_id) { }
+		int _x, _y, _cell_id;
+	};
+	
   //! An ease-of-use typedef to support the distributed construction of a network.
-  typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS,
-	std::pair<int, int> > Network;
+  typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, 
+	  vertex_properties> Network;
+  
   //! A map of cell IDs to vertex descriptors.
   typedef std::map<int, Network::vertex_descriptor> CellVertexMap;
   

Modified: branches/dkdev/source/main/cStats.cc
===================================================================
--- branches/dkdev/source/main/cStats.cc	2007-03-05 13:24:46 UTC (rev 1387)
+++ branches/dkdev/source/main/cStats.cc	2007-03-05 14:03:47 UTC (rev 1388)
@@ -868,8 +868,8 @@
 	
 	template<typename VertexOrEdge>
 	void operator()(std::ostream& out, const VertexOrEdge& v) {
-		out << "[pos=\"" << boost::get(boost::vertex_bundle, _network)[v].first 
-		<< "," << boost::get(boost::vertex_bundle, _network)[v].second << "!\"]";
+		out << "[pos=\"" << boost::get(boost::vertex_bundle, _network)[v]._x 
+		<< "," << boost::get(boost::vertex_bundle, _network)[v]._y << "!\"]";
 	}
 	cDeme::Network& _network;
 };




More information about the Avida-cvs mailing list