[Avida-SVN] r2987 - in branches/hjg-dev/source: cpu main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Sat Nov 29 11:17:37 PST 2008
Author: hjg
Date: 2008-11-29 14:17:36 -0500 (Sat, 29 Nov 2008)
New Revision: 2987
Modified:
branches/hjg-dev/source/cpu/cHardwareCPU.cc
branches/hjg-dev/source/cpu/cHardwareCPU.h
branches/hjg-dev/source/main/cOrganism.h
Log:
Implemented Inst_DonateDirectReciprocity which is similar to the other donate instructions, but uses a simplified form of direct reciprocity.
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-11-29 16:08:18 UTC (rev 2986)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-11-29 19:17:36 UTC (rev 2987)
@@ -223,6 +223,7 @@
tInstLibEntry<tMethod>("donate-shadedgb", &cHardwareCPU::Inst_DonateShadedGreenBeard, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-threshgb", &cHardwareCPU::Inst_DonateThreshGreenBeard, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-quantagb", &cHardwareCPU::Inst_DonateQuantaThreshGreenBeard, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-directra", &cHardwareCPU::Inst_DonateDirectReciprocity, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-NUL", &cHardwareCPU::Inst_DonateNULL, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-facing", &cHardwareCPU::Inst_DonateFacing, nInstFlag::STALL),
@@ -3392,6 +3393,9 @@
double other_merit = to_org->GetPhenotype().GetMerit().GetDouble();
other_merit += merit_received;
to_org->UpdateMerit(other_merit);
+
+ // Update the list of donors of the donation recipient
+ to_org->AddDonor(organism->GetID());
}
void cHardwareCPU::DoEnergyDonate(cOrganism* to_org)
@@ -3469,6 +3473,52 @@
}
+
+/* A simple implementation of direct reciprocity. Essentially, the
+ organism will donate to the first neigbhor it encounters that has
+ previously donated to it for whatever reason (e.g., could be kin,
+ greenbearding, random, etc. The amount the neighbor has donated
+ does not matter.*/
+bool cHardwareCPU::Inst_DonateDirectReciprocity(cAvidaContext& ctx)
+{
+ if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
+ return false;
+ }
+
+ // Find the target as the first organism that has donated to this organims
+ // within the neighborhood.
+ const int num_neighbors = organism->GetNeighborhoodSize();
+
+ // Turn to face a random neighbor
+ int neighbor_id = ctx.GetRandom().GetInt(num_neighbors);
+ for (int i = 0; i < neighbor_id; i++) organism->Rotate(1);
+ cOrganism * neighbor = organism->GetNeighbor();
+
+ int max_id = neighbor_id + num_neighbors;
+ bool found = false;
+ while (neighbor_id < max_id) {
+ neighbor = organism->GetNeighbor();
+ if ((neighbor != NULL) &&
+ (organism->IsDonor(neighbor->GetID()))) {
+ found = true;
+ break;
+ }
+ organism->Rotate(1);
+ neighbor_id++;
+ }
+
+ if (found == false) neighbor = NULL;
+
+ // Put the facing back where it was.
+ for (int i = 0; i < neighbor_id; i++) organism->Rotate(-1);
+
+ // Donate only if we have found a close enough relative...
+ if (neighbor != NULL){
+ DoDonate(neighbor);
+ }
+ return true;
+}
+
bool cHardwareCPU::Inst_DonateKin(cAvidaContext& ctx)
{
if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-11-29 16:08:18 UTC (rev 2986)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-11-29 19:17:36 UTC (rev 2987)
@@ -485,6 +485,7 @@
bool Inst_DonateShadedGreenBeard(cAvidaContext& ctx);
bool Inst_DonateThreshGreenBeard(cAvidaContext& ctx);
bool Inst_DonateQuantaThreshGreenBeard(cAvidaContext& ctx);
+ bool Inst_DonateDirectReciprocity(cAvidaContext& ctx);
bool Inst_DonateNULL(cAvidaContext& ctx);
bool Inst_DonateFacing(cAvidaContext& ctx);
Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h 2008-11-29 16:08:18 UTC (rev 2986)
+++ branches/hjg-dev/source/main/cOrganism.h 2008-11-29 19:17:36 UTC (rev 2987)
@@ -494,9 +494,10 @@
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); }
-
protected:
// The organism's own raw materials
int m_self_raw_materials;
More information about the Avida-cvs
mailing list