[Avida-SVN] r3007 - in branches/hjg-dev/source: cpu main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Sat Dec 6 13:35:20 PST 2008
Author: hjg
Date: 2008-12-06 16:35:20 -0500 (Sat, 06 Dec 2008)
New Revision: 3007
Modified:
branches/hjg-dev/source/cpu/cHardwareCPU.cc
branches/hjg-dev/source/cpu/cHardwareCPU.h
branches/hjg-dev/source/cpu/cTestCPUInterface.h
branches/hjg-dev/source/main/cEnvironment.h
branches/hjg-dev/source/main/cOrgInterface.h
branches/hjg-dev/source/main/cOrganism.cc
branches/hjg-dev/source/main/cOrganism.h
branches/hjg-dev/source/main/cPopulationInterface.cc
branches/hjg-dev/source/main/cPopulationInterface.h
branches/hjg-dev/source/main/cTaskLib.cc
branches/hjg-dev/source/main/cTaskLib.h
Log:
added support for instantaneous reputation based on string donation, tagging of organisms based on their string, and rotate to an oganism that creates a different string and has a high reputation.
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-06 21:35:20 UTC (rev 3007)
@@ -477,6 +477,7 @@
tInstLibEntry<tMethod>("rotate-to-rep", &cHardwareCPU::Inst_RotateToGreatestReputation, nInstFlag::STALL),
tInstLibEntry<tMethod>("rotate-to-rep-k", &cHardwareCPU::Inst_RotateToReputationK, nInstFlag::STALL),
tInstLibEntry<tMethod>("rotate-to-rep-and-donate", &cHardwareCPU::Inst_RotateToGreatestReputationAndDonate, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("rotate-to-rep-tag", &cHardwareCPU::Inst_RotateToGreatestReputationWithDifferentTag, nInstFlag::STALL),
tInstLibEntry<tMethod>("if-donor", &cHardwareCPU::Inst_IfDonor, nInstFlag::STALL),
@@ -7224,6 +7225,15 @@
return true;
}
+/* Rotate to face the organism with the highest reputation that has
+ a different tag. */
+bool cHardwareCPU::Inst_RotateToGreatestReputationWithDifferentTag(cAvidaContext& ctx)
+{
+ organism->GetOrgInterface().RotateToGreatestReputationWithDifferentTag(organism->GetTag());
+ return true;
+}
+
+
/* Rotate to face the organism with the highest reputation and then
immediately donate */
bool cHardwareCPU::Inst_RotateToGreatestReputationAndDonate(cAvidaContext& ctx)
@@ -7343,6 +7353,23 @@
org_str.Add(-1);
org_str_length = org_str.GetTotal();
}
+
+ // Update the organism's reputation on the basis of the string
+ int max = 0;
+ int cur = 0;
+ int best = 0;
+ vector<cString> strings_to_match = m_world->GetEnvironment().GetMatchStringsFromTask();
+ for (int i=0; i < strings_to_match.size(); i++) {
+ cur = organism->MatchStringsHelper(org_str, strings_to_match[i]);
+ if (cur > max) {
+ max = cur;
+ best = i;
+ }
+ }
+
+ // tag the organism & update the reputation
+ organism->SetTag(best);
+ organism->SetReputation(max);
// Donate the string
neighbor->ReceiveString(org_str, organism->GetID());
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -701,6 +701,8 @@
// Rotate to the organims with the greatest reputation
bool Inst_RotateToGreatestReputation(cAvidaContext& ctx);
+ // Rotate to the organims with the greatest reputation that has a different tag
+ bool Inst_RotateToGreatestReputationWithDifferentTag(cAvidaContext& ctx);
// Rotate to the organims with the greatest reputation and donate
bool Inst_RotateToGreatestReputationAndDonate(cAvidaContext& ctx);
// Roate to Reputation k
Modified: branches/hjg-dev/source/cpu/cTestCPUInterface.h
===================================================================
--- branches/hjg-dev/source/cpu/cTestCPUInterface.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/cpu/cTestCPUInterface.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -94,6 +94,7 @@
void DivideOrgTestamentAmongDeme(double value) {;}
void RotateToGreatestReputation(){ }
+ void RotateToGreatestReputationWithDifferentTag(int tag){ }
void RotateToReputationK(int k){ }
};
Modified: branches/hjg-dev/source/main/cEnvironment.h
===================================================================
--- branches/hjg-dev/source/main/cEnvironment.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cEnvironment.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -144,6 +144,7 @@
const cTaskEntry& GetTask(int id) const { return m_tasklib.GetTask(id); }
bool UseNeighborInput() const { return m_tasklib.UseNeighborInput(); }
bool UseNeighborOutput() const { return m_tasklib.UseNeighborOutput(); }
+ vector<cString> GetMatchStringsFromTask() { return m_tasklib.GetMatchStrings(); }
const cResourceLib& GetResourceLib() const { return resource_lib; }
const cReactionLib& GetReactionLib() const { return reaction_lib; }
Modified: branches/hjg-dev/source/main/cOrgInterface.h
===================================================================
--- branches/hjg-dev/source/main/cOrgInterface.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cOrgInterface.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -103,6 +103,7 @@
virtual void DivideOrgTestamentAmongDeme(double value) = 0;
virtual void RotateToGreatestReputation() =0;
+ virtual void RotateToGreatestReputationWithDifferentTag(int tag) =0;
virtual void RotateToReputationK(int k) =0;
};
Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cOrganism.cc 2008-12-06 21:35:20 UTC (rev 3007)
@@ -84,6 +84,7 @@
, m_amount_donate_received(0)
, m_k(0)
, m_failed_reputation_increases(0)
+ , m_tag(0)
{
m_hardware = m_world->GetHardwareManager().Create(this);
@@ -872,12 +873,13 @@
for (unsigned int k = 0; k < m_all_received_messages.size(); k++) {
tBuffer<int> received = m_all_received_messages[k].first;
num_matched = 0;
- for (int j = 0; j < string_to_match.GetSize(); j++)
+ /*for (int j = 0; j < string_to_match.GetSize(); j++)
{
if (string_to_match[j]=='0' && received[j]==0 ||
string_to_match[j]=='1' && received[j]==1)
num_matched++;
- }
+ }*/
+ num_matched = MatchStringsHelper(received, string_to_match);
if (num_matched > max_num_matched){
max_num_matched = num_matched;
org_id = m_all_received_messages[k].second;
@@ -888,6 +890,22 @@
return make_pair(max_num_matched, org_id);
}
+/* Return the number of bits matched in the two strings. This is
+ generalized so that it can be used not just by the task, but also
+ by the donate-string instruction. */
+int cOrganism::MatchStringsHelper(tBuffer<int> org_string, const cString& string_to_match)
+{
+ int num_matched = 0;
+ for (int j = 0; j < string_to_match.GetSize(); j++)
+ {
+ if (string_to_match[j]=='0' && org_string[j]==0 ||
+ string_to_match[j]=='1' && org_string[j]==1)
+ num_matched++;
+ }
+ return num_matched;
+}
+
+
/* Increase the reputation of all of the organisms that donated to this
organism. Then, clear the donors list.*/
void cOrganism::PraiseDonors()
Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cOrganism.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -488,14 +488,20 @@
bool IsDonor(int neighbor_id);
// Receive a string
void ReceiveString(tBuffer<int> str, int org_id);
- // Return the maximum number of characters matched by the received strings
+ // Return the maximum number of characters and the organism id matched by the received strings
std::pair < int, int > MatchString(const cString& str);
+ // Return the maximum number of characters matched by the strings
+ int MatchStringsHelper(tBuffer<int> org_string, const cString& string_to_match);
// Praise all donors of this organism and then clear the list
void PraiseDonors();
// Get number of failed reputation increases
int GetFailedReputationIncreases() { return m_failed_reputation_increases; }
// Add a donor
void AddDonor(int org_id) { donor_list.insert(org_id); }
+ // Set tag
+ void SetTag(int new_tag) { m_tag = new_tag; }
+ // Get tag
+ int GetTag() { return m_tag; }
protected:
@@ -519,8 +525,8 @@
vector < std::pair < tBuffer<int>, int > > m_all_received_messages;
// int number of reputation increase failures
int m_failed_reputation_increases;
+ int m_tag;
-
// -------- End of reputation support --------
};
Modified: branches/hjg-dev/source/main/cPopulationInterface.cc
===================================================================
--- branches/hjg-dev/source/main/cPopulationInterface.cc 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cPopulationInterface.cc 2008-12-06 21:35:20 UTC (rev 3007)
@@ -396,6 +396,63 @@
}
+/* Rotate an organism to face the neighbor with the highest reputation
+ where the neighbor has a different tag than the organism*/
+void cPopulationInterface::RotateToGreatestReputationWithDifferentTag(int tag)
+{
+
+ cPopulationCell& cell = m_world->GetPopulation().GetCell(GetCellID());
+ int high_rep=-1;
+ vector <int> high_rep_orgs;
+
+ // loop to find the max reputation
+ for(int i=0; i<cell.ConnectionList().GetSize(); ++i) {
+ const cPopulationCell* faced_cell = cell.ConnectionList().GetFirst();
+ // cell->organism, if occupied, check reputation, etc.
+ if (IsNeighborCellOccupied()) {
+ cOrganism* cur_neighbor = faced_cell->GetOrganism();
+
+ // if it has high reputation
+ if ((cur_neighbor->GetTag() != tag) && (cur_neighbor->GetReputation() >= high_rep)) {
+ if (cur_neighbor->GetReputation() > high_rep) {
+ high_rep = cur_neighbor->GetReputation();
+ high_rep_orgs.clear();
+ }
+ high_rep_orgs.push_back(cur_neighbor->GetID());
+ }
+ }
+
+ // check the next neighbor
+ cell.ConnectionList().CircNext();
+ }
+
+ // Pick an organism to donate to
+
+ if (high_rep_orgs.size() > 0) {
+ unsigned int rand_num = m_world->GetRandom().GetUInt(0, high_rep_orgs.size());
+ int high_org_id = high_rep_orgs[rand_num];
+
+ for(int i=0; i<cell.ConnectionList().GetSize(); ++i) {
+ const cPopulationCell* faced_cell = cell.ConnectionList().GetFirst();
+
+ if (IsNeighborCellOccupied()) {
+
+ cOrganism* cur_neighbor = faced_cell->GetOrganism();
+ // int test_rep = cell.ConnectionList().GetFirst()->GetOrganism()->GetReputation();
+
+ // if it has high reputation
+ if (cur_neighbor->GetID() == high_org_id) {
+ int test_id = cur_neighbor->GetTag();
+ break;
+ }
+ }
+
+ cell.ConnectionList().CircNext();
+
+ }
+ }
+
+}
Modified: branches/hjg-dev/source/main/cPopulationInterface.h
===================================================================
--- branches/hjg-dev/source/main/cPopulationInterface.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cPopulationInterface.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -109,6 +109,7 @@
// Reputation
void RotateToGreatestReputation();
+ void RotateToGreatestReputationWithDifferentTag(int tag);
void RotateToReputationK(int k);
void DivideOrgTestamentAmongDeme(double value);
Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cTaskLib.cc 2008-12-06 21:35:20 UTC (rev 3007)
@@ -1919,6 +1919,23 @@
if (args) NewTask(name, "MatchStr", &cTaskLib::Task_MatchStr, 0, args);
}
+
+vector<cString> cTaskLib::GetMatchStrings()
+{
+ if (m_strings.size() == 0) {
+
+ for (int i = 0; i < task_array.GetSize(); i++) {
+ assert(task_array[i] != NULL);
+ if (task_array[i]->GetDesc() == "MatchStr") {
+ cString string_to_match(task_array[i]->GetArguments().GetString(0));
+ m_strings.push_back(string_to_match);
+ }
+ }
+ }
+
+ return m_strings;
+}
+
double cTaskLib::Task_MatchStr(cTaskContext& ctx) const
{
tBuffer<int> temp_buf(ctx.GetOutputBuffer());
@@ -1995,7 +2012,7 @@
// Update the reputation of the organism whose string we used!
// Check if it the organisms string:
- cOrganism * neighbor = ctx.GetOrganism()->GetNeighbor();
+/* cOrganism * neighbor = ctx.GetOrganism()->GetNeighbor();
if (max_org_id == ctx.GetOrganism()->GetID()) {
ctx.GetOrganism()->IncReputation();
} else {
@@ -2014,6 +2031,7 @@
}
}
+ */
double bonus = 0.0;
// return value between 0 & 1 representing the percentage of string that was matched
Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h 2008-12-06 05:50:24 UTC (rev 3006)
+++ branches/hjg-dev/source/main/cTaskLib.h 2008-12-06 21:35:20 UTC (rev 3007)
@@ -86,6 +86,13 @@
bool UseNeighborInput() const { return use_neighbor_input; }
bool UseNeighborOutput() const { return use_neighbor_output; }
+
+ // Get the strings that parameterize the MatchString tasks
+ vector<cString> GetMatchStrings();
+
+private:
+ // Store the strings used by the MatchString tasks
+ vector<cString> m_strings;
private: // Direct task related methods
More information about the Avida-cvs
mailing list