[Avida-SVN] r2186 - in branches/dkdev/source: cpu main
dknoester at myxo.css.msu.edu
dknoester at myxo.css.msu.edu
Thu Nov 8 08:32:52 PST 2007
Author: dknoester
Date: 2007-11-08 11:32:51 -0500 (Thu, 08 Nov 2007)
New Revision: 2186
Modified:
branches/dkdev/source/cpu/cHardwareCPU.cc
branches/dkdev/source/cpu/cHardwareCPU.h
branches/dkdev/source/main/cGermline.h
branches/dkdev/source/main/cOrganism.cc
branches/dkdev/source/main/cOrganism.h
branches/dkdev/source/main/cPopulation.cc
branches/dkdev/source/main/cPopulationCell.cc
branches/dkdev/source/main/cPopulationCell.h
branches/dkdev/source/main/cStats.cc
Log:
Bug fix to outside bounded region.
Modified: branches/dkdev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.cc 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/cpu/cHardwareCPU.cc 2007-11-08 16:32:51 UTC (rev 2186)
@@ -36,6 +36,7 @@
#include <limits.h>
#include <fstream>
#include <utility>
+#include <set>
using namespace std;
@@ -393,10 +394,14 @@
cInstEntryCPU("if-less.end", &cHardwareCPU::Inst_IfLessEnd),
cInstEntryCPU("if-n-equ.end", &cHardwareCPU::Inst_IfNotEqualEnd),
cInstEntryCPU("if->=.end", &cHardwareCPU::Inst_IfGrtEquEnd),
-// cInstEntryCPU("if-less.else", &cHardwareCPU::Inst_IfLessElse),
cInstEntryCPU("else", &cHardwareCPU::Inst_Else),
cInstEntryCPU("end-if", &cHardwareCPU::Inst_EndIf),
+ cInstEntryCPU("bcast1", &cHardwareCPU::Inst_Broadcast1),
+ cInstEntryCPU("bcast2", &cHardwareCPU::Inst_Broadcast2),
+ cInstEntryCPU("bcast4", &cHardwareCPU::Inst_Broadcast4),
+ cInstEntryCPU("bcast8", &cHardwareCPU::Inst_Broadcast8),
// cInstEntryCPU("", &cHardwareCPU::Inst_),
+ // cInstEntryCPU("", &cHardwareCPU::Inst_),
};
const int n_size = sizeof(s_n_array)/sizeof(cNOPEntryCPU);
@@ -4023,3 +4028,60 @@
bool cHardwareCPU::Inst_EndIf(cAvidaContext& ctx) {
return true;
}
+
+
+/*! A generic broadcast method, to simplify the development of different range
+broadcasts.
+*/
+bool cHardwareCPU::BroadcastX(cAvidaContext& ctx, unsigned int depth) {
+ if(organism->GetCellID()==-1) return false;
+ cPopulationCell& cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+
+ // Build the message we're going to send.
+ 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));
+
+ // Get the set of organisms that we're going to send the message to.
+ std::set<cOrganism*> org_set;
+ cell.GetNeighbors(org_set, depth);
+
+ // Now, send the message to each organism in that set.
+ for(std::set<cOrganism*>::iterator i=org_set.begin(); i!=org_set.end(); ++i) {
+ organism->SendMessage(ctx, msg, *i);
+ }
+}
+
+
+/*! A single-hop broadcast instruction - send a message to all 1-hop neighbors
+of this organism.
+*/
+bool cHardwareCPU::Inst_Broadcast1(cAvidaContext& ctx) {
+ return BroadcastX(ctx, 1);
+}
+
+
+/*! A double-hop broadcast instruction - send a message to all 2-hop neighbors
+of this organism.
+*/
+bool cHardwareCPU::Inst_Broadcast2(cAvidaContext& ctx) {
+ return BroadcastX(ctx, 2);
+}
+
+
+/*! Another broadcast instruction variant - send a message to all 4-hop neighbors
+of this organism.
+*/
+bool cHardwareCPU::Inst_Broadcast4(cAvidaContext& ctx) {
+ return BroadcastX(ctx, 4);
+}
+
+
+/*! Another broadcast instruction variant - send a message to all 8-hop neighbors
+of this organism.
+*/
+bool cHardwareCPU::Inst_Broadcast8(cAvidaContext& ctx) {
+ return BroadcastX(ctx, 8);
+}
Modified: branches/dkdev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.h 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/cpu/cHardwareCPU.h 2007-11-08 16:32:51 UTC (rev 2186)
@@ -517,6 +517,13 @@
bool Inst_IfGrtEquEnd(cAvidaContext& ctx);
bool Inst_Else(cAvidaContext& ctx);
bool Inst_EndIf(cAvidaContext& ctx);
+
+ // Different range broadcast instructions.
+ bool BroadcastX(cAvidaContext& ctx, unsigned int depth);
+ bool Inst_Broadcast1(cAvidaContext& ctx);
+ bool Inst_Broadcast2(cAvidaContext& ctx);
+ bool Inst_Broadcast4(cAvidaContext& ctx);
+ bool Inst_Broadcast8(cAvidaContext& ctx);
};
Modified: branches/dkdev/source/main/cGermline.h
===================================================================
--- branches/dkdev/source/main/cGermline.h 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cGermline.h 2007-11-08 16:32:51 UTC (rev 2186)
@@ -11,6 +11,7 @@
class cGermline {
public:
cGenome& GetLatest() { return _germline.back(); }
+ const std::deque<double>& GetMeritWindow() { return _merit_window; }
void Add(const cGenome& genome) { _germline.push_back(genome); }
unsigned int Size() const { return _germline.size(); }
@@ -21,7 +22,7 @@
void UpdateMerit(double v) { UpdateWindowedMerit(v, 1); }
void UpdateWindowedMerit(double v, int window_size) {
assert(window_size >= 1);
- if(_merit_window.size() >= window_size) {
+ if(_merit_window.size() >= (unsigned int)window_size) {
_merit_window.pop_front();
}
_merit_window.push_back(v);
Modified: branches/dkdev/source/main/cOrganism.cc
===================================================================
--- branches/dkdev/source/main/cOrganism.cc 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cOrganism.cc 2007-11-08 16:32:51 UTC (rev 2186)
@@ -558,6 +558,71 @@
}
+/*! This is a point-to-point send message. Somehow, this organism has managed to
+send a message to a specific individual in the population. This makes things...
+funky.
+*/
+bool cOrganism::SendMessage(cAvidaContext& ctx, cOrgMessage& msg, cOrganism* receiver) {
+ assert(receiver != 0); // sanity
+
+ // Did the targetted organism receive it?
+ if(receiver->ReceiveMessage(msg)) {
+ // Store the message.
+ m_msg_sent.push_back(msg);
+
+ const tArray<double> & resource_count = m_interface->GetResources();
+
+ tList<tBuffer<int> > other_input_list;
+ tList<tBuffer<int> > other_output_list;
+
+ // If tasks require us to consider neighbor inputs, collect them...
+ if (m_world->GetEnvironment().GetTaskLib().UseNeighborInput() == true) {
+ const int num_neighbors = m_interface->GetNumNeighbors();
+ for (int i = 0; i < num_neighbors; i++) {
+ m_interface->Rotate();
+ cOrganism * cur_neighbor = m_interface->GetNeighbor();
+ if (cur_neighbor == NULL) continue;
+
+ other_input_list.Push( &(cur_neighbor->m_input_buf) );
+ }
+ }
+
+ // If tasks require us to consider neighbor outputs, collect them...
+ if (m_world->GetEnvironment().GetTaskLib().UseNeighborOutput() == true) {
+ const int num_neighbors = m_interface->GetNumNeighbors();
+ for (int i = 0; i < num_neighbors; i++) {
+ m_interface->Rotate();
+ cOrganism * cur_neighbor = m_interface->GetNeighbor();
+ if (cur_neighbor == NULL) continue;
+
+ other_output_list.Push( &(cur_neighbor->m_output_buf) );
+ }
+ }
+
+ bool net_valid = false;
+ tArray<double> res_change(resource_count.GetSize());
+ tArray<int> insts_triggered;
+
+ tBuffer<int>* received_messages_point = &m_received_messages;
+ if (!m_world->GetConfig().SAVE_RECEIVED.Get())
+ received_messages_point = NULL;
+ cTaskContext taskctx(m_input_buf, m_output_buf, other_input_list, other_output_list, net_valid, 0, received_messages_point, this);
+ m_phenotype.TestOutput(ctx, taskctx, m_send_buf, m_receive_buf, resource_count, res_change, insts_triggered);
+ m_interface->UpdateResources(res_change);
+
+ for (int i = 0; i < insts_triggered.GetSize(); i++) {
+ const int cur_inst = insts_triggered[i];
+ m_hardware->ProcessBonusInst(ctx, cInstruction(cur_inst) );
+ }
+
+ m_msg_sent.back().SetReceiver(NULL);
+ return true;
+ }
+
+ return false;
+}
+
+
/*! Add this message to the list of messages that this organism has received.
*/
bool cOrganism::ReceiveMessage(cOrgMessage& msg)
Modified: branches/dkdev/source/main/cOrganism.h
===================================================================
--- branches/dkdev/source/main/cOrganism.h 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cOrganism.h 2007-11-08 16:32:51 UTC (rev 2186)
@@ -268,6 +268,8 @@
// -------- Messaging Methods --------
//! Called when this organism attempts to send a message.
bool SendMessage(cAvidaContext& ctx, cOrgMessage& msg);
+ //! Called when this organism attempts to send a message to a specific organism.
+ bool SendMessage(cAvidaContext& ctx, cOrgMessage& msg, cOrganism* receiver);
//! Called when this organism has been sent a message.
bool ReceiveMessage(cOrgMessage& msg);
//! Called when this organism attempts to move a received message into its CPU.
Modified: branches/dkdev/source/main/cPopulation.cc
===================================================================
--- branches/dkdev/source/main/cPopulation.cc 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cPopulation.cc 2007-11-08 16:32:51 UTC (rev 2186)
@@ -141,9 +141,9 @@
std::pair<int,int> two_xy = _deme.GetCellPosition(_deme.GetCellsToLink().second);
std::pair<int,int> self_xy = _deme.GetCellPosition(organism->GetCellID());
-// std::cout << "Organism " << organism->GetCellID() << " @ (" << self_xy.first << "," << self_xy.second << ") declares "
-// << organism->GetRegion() << " for cell1 @ (" << one_xy.first << "," << one_xy.second << ") and cell2 @ ("
-// << two_xy.first << "," << two_xy.second << "); ";
+ std::cout << "Organism " << organism->GetCellID() << " @ (" << self_xy.first << "," << self_xy.second << ") declares "
+ << organism->GetRegion() << " for cell1 @ (" << one_xy.first << "," << one_xy.second << ") and cell2 @ ("
+ << two_xy.first << "," << two_xy.second << "); ";
// create the region
std::pair<int,int> x_range = std::make_pair(std::min(one_xy.first, two_xy.first), std::max(one_xy.first, two_xy.first));
@@ -152,21 +152,22 @@
if((self_xy.first >= x_range.first)
&& (self_xy.first <= x_range.second)
&& (self_xy.second >= y_range.first)
- && (self_xy.second <= y_range.second)
- && (organism->GetRegion()==1)) {
- // Is this organism correctly declaring itself to be in the region?
-// std::cout << " inside.";
- result += 1.0;
+ && (self_xy.second <= y_range.second)) {
+ // In the region.
+ if(organism->GetRegion()==1) {
+ std::cout << " inside.";
+ result += 1.0;
+ }
} else if(organism->GetRegion()==-1) {
// if(((self_xy.first < x_range.first) || (self_xy.first > x_range.second))
// && ((self_xy.second < y_range.first) || (self_xy.second > y_range.second))
// && (organism->GetRegion()==-1)) {
// Is this organism correctly declaring itself to be outside the region?
-// std::cout << " outside.";
+ std::cout << " outside.";
result += 1.0;
}
-// std::cout << std::endl;
+ std::cout << std::endl;
return result;
}
Modified: branches/dkdev/source/main/cPopulationCell.cc
===================================================================
--- branches/dkdev/source/main/cPopulationCell.cc 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cPopulationCell.cc 2007-11-08 16:32:51 UTC (rev 2186)
@@ -88,6 +88,32 @@
}
+/*! This method recursively builds a set of organisms that neighbor this cell. The
+set must be passed in by-reference, as calls to this method must share a common set
+of already-visited cells. In general, this method visits all cells within the given
+depth, and adds occupants to the set.
+*/
+void cPopulationCell::GetNeighbors(std::set<cOrganism*>& org_set, unsigned int depth) const {
+ typedef std::set<cOrganism*> org_set_t;
+
+ // For each cell in our connection list,
+ tConstListIterator<cPopulationCell> i(connection_list);
+ while(!i.AtEnd()) {
+ const cPopulationCell* cell = i.Next();
+ // Get the occupying organism, if any
+ if(cell->IsOccupied()) {
+ // Attempt to add the organism from that cell to the set.
+ std::pair<org_set_t::iterator, bool> ins = org_set.insert(cell->GetOrganism());
+ // If we just added a new organism to the set, and it wouldn't exceed the depth,
+ // recurse to that cell.
+ if(ins.second && (depth > 1)) {
+ cell->GetNeighbors(org_set, depth-1);
+ }
+ }
+ }
+}
+
+
int cPopulationCell::GetInput()
{
if (cur_input >= nHardware::IO_SIZE) cur_input = 0;
Modified: branches/dkdev/source/main/cPopulationCell.h
===================================================================
--- branches/dkdev/source/main/cPopulationCell.h 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cPopulationCell.h 2007-11-08 16:32:51 UTC (rev 2186)
@@ -12,17 +12,13 @@
#define cPopulationCell_h
#include <fstream>
+#include <set>
-#ifndef cMutationRates_h
#include "cMutationRates.h"
-#endif
-#ifndef tArray_h
#include "tArray.h"
-#endif
-#ifndef tList_h
#include "tList.h"
-#endif
+
class cPopulation;
class cOrganism;
class cPopulationCell;
@@ -61,6 +57,8 @@
cOrganism* GetOrganism() const { return organism; }
tList<cPopulationCell> & ConnectionList() { return connection_list; }
+ //! Recursively build a set of organisms that neighbor this cell, out to the given depth.
+ void GetNeighbors(std::set<cOrganism*>& org_set, unsigned int depth=1) const;
const cMutationRates & MutationRates() const { return *mutation_rates; }
cMutationRates & MutationRates() { return *mutation_rates; }
int GetInput();
Modified: branches/dkdev/source/main/cStats.cc
===================================================================
--- branches/dkdev/source/main/cStats.cc 2007-11-07 21:28:02 UTC (rev 2185)
+++ branches/dkdev/source/main/cStats.cc 2007-11-08 16:32:51 UTC (rev 2186)
@@ -969,7 +969,7 @@
for(int i=0; i<m_world->GetPopulation().GetNumDemes(); ++i) {
cDeme& deme = m_world->GetPopulation().GetDeme(i);
cDeme::Network& network = deme.GetNetwork();
- if((boost::num_vertices(network) == deme.GetSize()) && network_is_connected(network)) {
+ if((boost::num_vertices(network) == (unsigned int)deme.GetSize()) && network_is_connected(network)) {
std::stringstream filename;
filename << "./data/connected-topo-" << i << "-" << GetUpdate() << "u.dot";
std::ofstream outfile(filename.str().c_str());
More information about the Avida-cvs
mailing list