[Avida-SVN] r2992 - branches/hjg-dev/source/cpu
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Tue Dec 2 12:45:56 PST 2008
Author: hjg
Date: 2008-12-02 15:45:56 -0500 (Tue, 02 Dec 2008)
New Revision: 2992
Modified:
branches/hjg-dev/source/cpu/cHardwareCPU.cc
branches/hjg-dev/source/cpu/cHardwareCPU.h
Log:
added a donate-indirectra instruction
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-02 15:50:03 UTC (rev 2991)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-02 20:45:56 UTC (rev 2992)
@@ -224,6 +224,7 @@
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-indirectra", &cHardwareCPU::Inst_DonateIndirectReciprocity, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-NUL", &cHardwareCPU::Inst_DonateNULL, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-facing", &cHardwareCPU::Inst_DonateFacing, nInstFlag::STALL),
@@ -3484,7 +3485,9 @@
if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
return false;
}
-
+
+ organism->GetPhenotype().IncDonates();
+
// Find the target as the first organism that has donated to this organims
// within the neighborhood.
// const int num_neighbors = organism->GetNeighborhoodSize();
@@ -3519,6 +3522,59 @@
return true;
}
+/* A simple implementation of indirect reciprocity. Essentially, the
+ organism will donate to the neigbhor that has donated the most. */
+bool cHardwareCPU::Inst_DonateIndirectReciprocity(cAvidaContext& ctx)
+{
+ if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
+ return false;
+ }
+
+ organism->GetPhenotype().IncDonates();
+
+ // Turn to face a random neighbor
+ const int num_neighbors = organism->GetNeighborhoodSize();
+ int neighbor_id = ctx.GetRandom().GetInt(num_neighbors);
+ for (int i = 0; i < neighbor_id; i++) organism->Rotate(1);
+
+
+
+ // Turn to face the neighbor that has donated the most
+ cOrganism * neighbor = organism->GetNeighbor();
+ cOrganism * max_neighbor = NULL;
+ int max_donation = 0;
+ int max_id = neighbor_id + num_neighbors;
+ bool found = false;
+
+
+ while (neighbor_id < max_id) {
+ neighbor = organism->GetNeighbor();
+ if ((neighbor != NULL) &&
+ (neighbor->GetPhenotype().GetCurNumDonates() > max_donation)) {
+ found = true;
+ max_donation = neighbor->GetPhenotype().GetCurNumDonates();
+ max_neighbor = organism->GetNeighbor();
+ }
+ organism->Rotate(1);
+ neighbor_id++;
+ }
+
+ if (found == false) {
+ max_neighbor = NULL;
+ }
+
+ // Put the facing back where it was.
+ for (int i = 0; i < neighbor_id; i++) organism->Rotate(-1);
+
+
+ // Donate
+ if (max_neighbor != NULL){
+ DoDonate(max_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-12-02 15:50:03 UTC (rev 2991)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-12-02 20:45:56 UTC (rev 2992)
@@ -486,6 +486,7 @@
bool Inst_DonateThreshGreenBeard(cAvidaContext& ctx);
bool Inst_DonateQuantaThreshGreenBeard(cAvidaContext& ctx);
bool Inst_DonateDirectReciprocity(cAvidaContext& ctx);
+ bool Inst_DonateIndirectReciprocity(cAvidaContext& ctx);
bool Inst_DonateNULL(cAvidaContext& ctx);
bool Inst_DonateFacing(cAvidaContext& ctx);
More information about the Avida-cvs
mailing list