[Avida-SVN] r2848 - in branches/hjg-dev/source: cpu main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Tue Oct 14 20:24:25 PDT 2008
Author: hjg
Date: 2008-10-14 23:24:25 -0400 (Tue, 14 Oct 2008)
New Revision: 2848
Modified:
branches/hjg-dev/source/cpu/cHardwareCPU.cc
branches/hjg-dev/source/cpu/cHardwareCPU.h
branches/hjg-dev/source/main/cOrganism.cc
branches/hjg-dev/source/main/cOrganism.h
Log:
changed to a probabilistic reciprocation model.
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-10-14 20:02:47 UTC (rev 2847)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-10-15 03:24:25 UTC (rev 2848)
@@ -452,13 +452,17 @@
tInstLibEntry<tMethod>("get-neighbors-opinion", &cHardwareCPU::Inst_GetNeighborsOpinion, nInstFlag::STALL),
// Reputation instructions
- tInstLibEntry<tMethod>("donate-frm", &cHardwareCPU::Inst_DonateFacingRawMaterials, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-frm", &cHardwareCPU::Inst_DonateFacingRawMaterials, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-pr", &cHardwareCPU::Inst_DonateProbReciprocate, nInstFlag::STALL),
tInstLibEntry<tMethod>("get-neighbors-reputation", &cHardwareCPU::Inst_GetNeighborsReputation, nInstFlag::STALL),
tInstLibEntry<tMethod>("get-reputation", &cHardwareCPU::Inst_GetReputation, nInstFlag::STALL),
tInstLibEntry<tMethod>("get-raw-mat-amount", &cHardwareCPU::Inst_GetAmountOfRawMaterials, nInstFlag::STALL),
tInstLibEntry<tMethod>("donation-scam", &cHardwareCPU::Inst_DonationScam, nInstFlag::STALL),
tInstLibEntry<tMethod>("punish-neighbor", &cHardwareCPU::Inst_PunishNeighbor, nInstFlag::STALL),
tInstLibEntry<tMethod>("praise-neighbor", &cHardwareCPU::Inst_PraiseNeighbor, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("inc-recip", &cHardwareCPU::Inst_IncRecipProb, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("dec-recip", &cHardwareCPU::Inst_DecRecipProb, nInstFlag::STALL),
+
// Must always be the last instruction in the array
tInstLibEntry<tMethod>("NULL", &cHardwareCPU::Inst_Nop, 0, "True no-operation instruction: does nothing"),
@@ -6703,10 +6707,6 @@
/* Donate raw materials to the facing organism. */
bool cHardwareCPU::Inst_DonateFacingRawMaterials(cAvidaContext& ctx)
{
- // hjg -- why would this be necessary?
- /*if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
- return false;
- }*/
bool isSuccessful = false;
@@ -6746,6 +6746,44 @@
+/* Donate raw materials to the facing organism that with a given
+probability (set as reputation) will reciprocate. */
+bool cHardwareCPU::Inst_DonateProbReciprocate(cAvidaContext& ctx)
+{
+ bool isSuccessful = false;
+
+ // Get faced neighbor
+ cOrganism * neighbor = organism->GetNeighbor();
+
+ // Donate only if we have found a neighbor.
+ if (neighbor != NULL) {
+ // Subtract raw materials from the organism (currently subtracts 1 resource...)
+ // fails if the organism does not have any more resources
+ if (organism->SubtractSelfRawMaterials(1)) {
+ neighbor->AddOtherRawMaterials(1, organism->GetID());
+
+ // rotate recipient to face donor
+ // by rotating until the recipient faces the donor
+ // adding a new comment.
+ while (neighbor->GetNeighbor() != organism) {
+ neighbor->Rotate(1);
+ }
+
+ // see if the neighbor reciprocates
+ unsigned int recip_prob = (unsigned int) neighbor->GetReciprocationProbability();
+ unsigned int rand_num = m_world->GetRandom().GetUInt(0, 9);
+ // neighbor donates to organism.
+ if(rand_num < recip_prob) {
+ if (neighbor->SubtractSelfRawMaterials(1)) {
+ organism->AddOtherRawMaterials(1, neighbor->GetID());
+ }
+ }
+ }
+ isSuccessful = true;
+ }
+ return isSuccessful;
+}
+
/* An organism artificially increases its reputation without donating. */
bool cHardwareCPU::Inst_DonationScam(cAvidaContext& ctx)
{
@@ -6771,18 +6809,12 @@
*/
bool cHardwareCPU::Inst_GetNeighborsReputation(cAvidaContext& ctx)
{
-
// Get faced neighbor
cOrganism * neighbor = organism->GetNeighbor();
- if (neighbor != NULL && neighbor->HasOpinion()) {
- Inst_GetNeighborsOpinion(ctx);
- } else {
- const int opinion_reg = FindModifiedRegister(REG_BX);
- const int age_reg = FindNextRegister(opinion_reg);
-
- GetRegister(opinion_reg) = 0;
- GetRegister(age_reg) = m_world->GetStats().GetUpdate();
- }
+ if (neighbor != NULL) {
+ const int raw_mat_reg = FindModifiedRegister(REG_AX);
+ GetRegister(raw_mat_reg) = neighbor->GetReputation();
+ }
return true;
}
@@ -6795,17 +6827,9 @@
*/
bool cHardwareCPU::Inst_GetReputation(cAvidaContext& ctx)
{
-
- if (organism->HasOpinion()) {
- Inst_GetOpinion(ctx);
- } else {
const int opinion_reg = FindModifiedRegister(REG_BX);
- const int age_reg = FindNextRegister(opinion_reg);
-
- GetRegister(opinion_reg) = 0;
- GetRegister(age_reg) = m_world->GetStats().GetUpdate();
- }
- return true;
+ GetRegister(opinion_reg) = organism->GetReputation();
+ return true;
}
/* Sense the number of raw materials an organism has. Store in
@@ -6850,4 +6874,19 @@
return true;
}
+/* Increase an organism's probability of reciprocating by 10% */
+bool cHardwareCPU::Inst_IncRecipProb(cAvidaContext& ctx)
+{
+ organism->IncRecipProb();
+ return true;
+}
+
+/* Decrease an organism's probability of reciprocating by 10% */
+bool cHardwareCPU::Inst_DecRecipProb(cAvidaContext& ctx)
+{
+ organism->DecRecipProb();
+ return true;
+}
+
+
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-10-14 20:02:47 UTC (rev 2847)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-10-15 03:24:25 UTC (rev 2848)
@@ -678,6 +678,8 @@
*/
// Donate a raw material to the neighbor
bool Inst_DonateFacingRawMaterials(cAvidaContext& ctx);
+ // Donation to facing neighbor with a probability that it is reciprocated
+ bool Inst_DonateProbReciprocate(cAvidaContext& ctx);
// Get a neighbor's reputation
bool Inst_GetNeighborsReputation(cAvidaContext& ctx);
// Get the organism's reputation
@@ -690,6 +692,11 @@
bool Inst_PunishNeighbor(cAvidaContext& ctx);
// Praise Neighbor - decrease reputation of neighbor
bool Inst_PraiseNeighbor(cAvidaContext& ctx);
+ // Increase the reciprocation probability
+ bool Inst_IncRecipProb(cAvidaContext& ctx);
+ // Decrease the reciprocation probability
+ bool Inst_DecRecipProb(cAvidaContext& ctx);
+
};
Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc 2008-10-14 20:02:47 UTC (rev 2847)
+++ branches/hjg-dev/source/main/cOrganism.cc 2008-10-15 03:24:25 UTC (rev 2848)
@@ -78,6 +78,7 @@
, m_opinion(0)
, m_self_raw_materials(world->GetConfig().RAW_MATERIAL_AMOUNT.Get())
, m_other_raw_materials(0)
+ , m_recip_prob(5)
{
m_hardware = m_world->GetHardwareManager().Create(this);
@@ -109,6 +110,7 @@
, m_net(NULL)
, m_msg(0)
, m_opinion(0)
+ , m_recip_prob(5)
{
m_hardware = m_world->GetHardwareManager().Create(this, inst_set);
@@ -739,7 +741,7 @@
*/
void cOrganism::SetOpinion(const Opinion& opinion)
{
-// InitOpinions();
+ InitOpinions();
m_opinion->opinion_list.push_back(std::make_pair(opinion, m_world->GetStats().GetUpdate()));
}
@@ -771,13 +773,39 @@
return isSuccessful;
}
-
+// Get an organism's reputation
int cOrganism::GetReputation() {
- int rep =0;
+ int rep =5;
if (HasOpinion()) {
rep = GetOpinion().first;
}
-
return rep;
}
+
+
+// Increase an organism's probability of donating
+// by 10%
+void cOrganism::IncRecipProb() {
+ if (m_recip_prob < 10) {
+ m_recip_prob++;
+ if (m_world->GetConfig().AUTO_REPUTATION.Get()) {
+ SetOpinion(m_recip_prob);
+ }
+ }
+}
+
+// decrease an organism's probaility of donating
+// by 10%
+void cOrganism::DecRecipProb() {
+ if (m_recip_prob > 0) {
+ m_recip_prob--;
+ if (m_world->GetConfig().AUTO_REPUTATION.Get()) {
+ SetOpinion(m_recip_prob);
+ }
+ }
+}
+
+
+
+
Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h 2008-10-14 20:02:47 UTC (rev 2847)
+++ branches/hjg-dev/source/main/cOrganism.h 2008-10-15 03:24:25 UTC (rev 2848)
@@ -436,8 +436,8 @@
// -------- Reputation support --------
- /* An organism's reputation should relate to whether or not it cooperates sucessfully with
- other organisms.
+ /* An organism has a pre-defined probability that it will cooperate
+ with another organism by reciprocating a donation.
Currently, as a first step, organisms have been given different raw materials that
they can either hoard or donate.
@@ -458,16 +458,23 @@
int GetReputation();
// get number of donors
int GetNumberOfDonors() { return donor_list.size(); }
+ // increase reciprocation probability
+ void IncRecipProb();
+ // decrease reciprocation probability
+ void DecRecipProb();
+ // get reciprocation probability
+ int GetReciprocationProbability() { return m_recip_prob; }
+ // does an organism have a reputation (init here...)
protected:
- // Initialize reputation support
-// inline void InitReputation() {if (!m_self_raw_materials) {m_self_raw_materials = 100; m_other_raw_materials =0; }}
// The organism's own raw materials
int m_self_raw_materials;
// The raw materials an oranism has collected from others
int m_other_raw_materials;
// Organisms that have donated to this organism
set<int> donor_list;
+ // the probability that an organism will reciprocate
+ int m_recip_prob;
// -------- End of reputation support --------
More information about the Avida-cvs
mailing list