[Avida-SVN] r3079 - in branches/hjg-dev/source: cpu main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Tue Dec 30 07:18:43 PST 2008
Author: hjg
Date: 2008-12-30 10:18:43 -0500 (Tue, 30 Dec 2008)
New Revision: 3079
Modified:
branches/hjg-dev/source/cpu/cHardwareCPU.cc
branches/hjg-dev/source/cpu/cHardwareCPU.h
branches/hjg-dev/source/main/cEnvironment.h
branches/hjg-dev/source/main/cOrganism.cc
branches/hjg-dev/source/main/cOrganism.h
branches/hjg-dev/source/main/cStats.cc
branches/hjg-dev/source/main/cTaskLib.cc
branches/hjg-dev/source/main/cTaskLib.h
Log:
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-30 15:18:43 UTC (rev 3079)
@@ -460,7 +460,10 @@
tInstLibEntry<tMethod>("donate-frm", &cHardwareCPU::Inst_DonateFacingRawMaterials, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-spec", &cHardwareCPU::Inst_DonateFacingRawMaterialsOtherSpecies, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-k", &cHardwareCPU::Inst_DonateFacingConditionalOnK, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-string", &cHardwareCPU::Inst_DonateString, nInstFlag::STALL),
tInstLibEntry<tMethod>("donate-if-donor", &cHardwareCPU::Inst_DonateIfDonor, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-string-if-donor", &cHardwareCPU::Inst_DonateStringIfDonor, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("donate-string-if-donor-rep", &cHardwareCPU::Inst_DonateStringIfDonorRep, nInstFlag::STALL),
tInstLibEntry<tMethod>("get-neighbors-reputation", &cHardwareCPU::Inst_GetNeighborsReputation, nInstFlag::STALL),
tInstLibEntry<tMethod>("get-reputation", &cHardwareCPU::Inst_GetReputation, nInstFlag::STALL),
@@ -478,6 +481,7 @@
tInstLibEntry<tMethod>("rotate-to-rep-lineage", &cHardwareCPU::Inst_RotateToGreatestReputationWithDifferentLineage, nInstFlag::STALL),
tInstLibEntry<tMethod>("rotate-to-tag", &cHardwareCPU::Inst_RotateToDifferentTag, nInstFlag::STALL),
tInstLibEntry<tMethod>("if-donor", &cHardwareCPU::Inst_IfDonor, nInstFlag::STALL),
+ tInstLibEntry<tMethod>("str-cat", &cHardwareCPU::Inst_StrCatOrg, nInstFlag::STALL),
tInstLibEntry<tMethod>("prod-string", &cHardwareCPU::Inst_ProduceString, nInstFlag::STALL),
// Must always be the last instruction in the array
@@ -7020,9 +7024,9 @@
// Donate only if we have found a neighbor.
if (neighbor != NULL) {
- // Subtract raw materials from the organism
+ // Subtract raw materials from the organism (currently subtracts 1 resource...)
// fails if the organism does not have any more resources
- if (organism->SubtractRawMaterials(organism->GetLineageLabel(), cost)) {
+ if (organism->SubtractSelfRawMaterials(cost)) {
// sometimes the donation will fail.
// get the probability of failure
@@ -7035,7 +7039,8 @@
}
- neighbor->AddRawMaterials(organism->GetLineageLabel(), cost, organism->GetID());
+ neighbor->AddOtherRawMaterials(cost, organism->GetID());
+ neighbor->AddDonatedLineage(organism->GetLineageLabel());
// rotate recipient to face donor
// by rotating until the recipient faces the donor
@@ -7046,7 +7051,7 @@
}
}
-
+
// track stats
organism->Donated();
@@ -7361,40 +7366,156 @@
}
+/* This instruction enables an organism to donate a string to its
+ facing neighbor.
+ So that we can do fun reciprocity-style experiments it also keeps
+ track of organisms that have donated strings to others.
+ */
+bool cHardwareCPU::Inst_DonateString(cAvidaContext& ctx)
+{
+ // Make sure the organism has a facing neighbor of the right sort
+ // (there is a config option that says you can only donate between
+ // species).
+
+ cOrganism * neighbor = organism->GetNeighbor();
+ if (neighbor != NULL) {
+
+ // Check if the donation is from one species to another
+ // If the config option is selected, then exit otherwise.
+ if (m_world->GetConfig().DONATION_RESTRICTIONS.Get() == 1) {
+ int spec_self = organism->GetLineageLabel();
+ int spec_neighbor = neighbor->GetLineageLabel();
+ if (spec_self == spec_neighbor) return true;
+ }
+
+
+ // Get the organism's string from its output buffer
+ tBuffer<int> org_str (organism->GetOuptput());
+
+ // This will make the string the size of the capacity.
+ int length = org_str.GetCapacity();
+ int org_str_length = org_str.GetTotal();
+
+ while (org_str_length < length) {
+ 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 (unsigned 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;
+ }
+ }
+
+
+
+ // If the organism has the same tag as the neighbor, disable the
+ // donation.
+ if (m_world->GetConfig().DONATION_RESTRICTIONS.Get() == 2) {
+ // Check if the donation is from one tag to another
+ if (organism->GetTagLabel() == neighbor->GetTagLabel())
+ return true;
+ }
+
+ // update the reputation unless the organism is a str catter
+ if (!organism->GetStringCatter()){
+
+ int len = 1;
+ if (strings_to_match.size()) len = strings_to_match[0].GetSize();
+ // organism->SetAverageReputation(max);
+ int rep = (max - floor((double)len/2));
+
+ if (rep > 0) {
+ if (m_world->GetConfig().STRING_REPUTATION.Get() == 0) {
+ organism->SetAverageReputation(rep);
+ } else if (m_world->GetConfig().STRING_REPUTATION.Get() == 1){
+ organism->AddReputation(rep);
+ }
+ }
+
+
+ }
+ // Donate the string
+ neighbor->ReceiveString(org_str, organism->GetID());
+
+ // Update all the relevant stats.
+ organism->Donated();
+ }
+
+ return true;
+}
+
+
+/* Donate if the neighbor previously donated to the organism. */
+bool cHardwareCPU::Inst_DonateStringIfDonor(cAvidaContext& ctx)
+{
+ cOrganism * neighbor = organism->GetNeighbor();
+ if (neighbor != NULL) {
+ // check if the neighbor was a donor
+ if (organism->IsDonor(neighbor->GetID())) {
+ Inst_DonateString(ctx);
+ }
+ }
+ return true;
+}
+
+
+/* Donate if the neighbor has a reputation of > 0 */
+bool cHardwareCPU::Inst_DonateStringIfDonorRep(cAvidaContext& ctx)
+{
+ cOrganism * neighbor = organism->GetNeighbor();
+ if (neighbor != NULL) {
+ // check if the neighbor was a donor
+ if (neighbor->GetReputation()) {
+ Inst_DonateString(ctx);
+ }
+ }
+ return true;
+}
+
+
+/* This instruction does not do anything, but rather is used
+ as a signal to indicate that the organism has chosen to perform
+ the string cat task. */
+bool cHardwareCPU::Inst_StrCatOrg(cAvidaContext& ctx)
+{
+ organism->SetStringCatter(true);
+ return true;
+}
+
+
/* Check if the string in the organisms buffer corresponds to the
string it is producing. If so, -1 out the buffer and increment the
number of raw materials the organism has. Otherwise, do nothing. */
bool cHardwareCPU::Inst_ProduceString(cAvidaContext& ctx)
{
- int num = -1;
- int size = 0;
+
+ int num = 0;
+
// For the time being this instruction is hardcoded for 2 strings and
// two species.
- /* if ((organism->GetLineageLabel() == 0)) {
- num = organism->MatchOutputBuffer(cString("00000000"));
- } else if ((organism->GetLineageLabel() == 1)){
- num = organism->MatchOutputBuffer(cString("11111111"));
- }*/
- if (organism->GetLineageLabel() != -1){
- map < int, cString> my_strings = m_world->GetEnvironment().GetMatchStringsFromTask();
- // iterate through the vector until you find the right tag.
- // then check if the buffer matches it.
- // record the size
- //for (unsigned int i=0; i < my_strings.size(); i++) {
- //if (organism->GetLineageLabel() == my_strings[i].first) {
- //num = organism->MatchOutputBuffer(my_strings[i].second);
- //size = my_strings[i].second.GetSize();
- //}
- //}
+ cString s = m_world->GetEnvironment().GetMatchString(organism->GetLineageLabel());
- num = organism->MatchOutputBuffer(my_strings[organism->GetLineageLabel()]);
- size = my_strings[organism->GetLineageLabel()].GetSize();
+ if (s.GetSize() > 0) {
+ /*if ((organism->GetLineageLabel() == 0)) {
+ num = organism->MatchOutputBuffer(cString("0000"));
+ } else if ((organism->GetLineageLabel() == 1)){
+ num = organism->MatchOutputBuffer(cString("1111"));
+ }*/
+ num = organism->MatchOutputBuffer(s);
- if (num == size) {
- organism->AddRawMaterials(organism->GetLineageLabel(), 1, organism->GetID());
- organism->SetOutputNegative1() ;
+ if (num == s.GetSize()) {
+ organism->AddSelfRawMaterials(1);
+ organism->SetOutputNegative1();
}
}
return true;
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h 2008-12-30 15:18:43 UTC (rev 3079)
@@ -696,9 +696,9 @@
// Donate a raw material to the neighbor, if its reputation > k
bool Inst_DonateFacingConditionalOnK(cAvidaContext& ctx);
// Donate string
- //bool Inst_DonateString(cAvidaContext& ctx);
+ bool Inst_DonateString(cAvidaContext& ctx);
// Donate a string to the neighbor if it was a donor
- //bool Inst_DonateStringIfDonor(cAvidaContext& ctx);
+ bool Inst_DonateStringIfDonor(cAvidaContext& ctx);
// Rotate to the organims with the greatest reputation
bool Inst_RotateToGreatestReputation(cAvidaContext& ctx);
@@ -719,7 +719,7 @@
// Execute the following instruction if the facing neighbor was a donor
bool Inst_IfDonor(cAvidaContext& ctx);
// Indicate that the organism is a string catter
- //bool Inst_StrCatOrg(cAvidaContext& ctx);
+ bool Inst_StrCatOrg(cAvidaContext& ctx);
// Produce string
bool Inst_ProduceString(cAvidaContext& ctx);
Modified: branches/hjg-dev/source/main/cEnvironment.h
===================================================================
--- branches/hjg-dev/source/main/cEnvironment.h 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/main/cEnvironment.h 2008-12-30 15:18:43 UTC (rev 3079)
@@ -144,8 +144,11 @@
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(); }
- map <int, cString> GetMatchStringsFromTask() { return m_tasklib.GetMatchStrings(); }
+ vector<cString> GetMatchStringsFromTask() { return m_tasklib.GetMatchStrings(); }
+ cString GetMatchString(int x) { return m_tasklib.GetMatchString(x); }
+ int GetNumberOfMatchStrings() { return m_tasklib.GetNumberOfMatchStrings(); }
+
const cResourceLib& GetResourceLib() const { return resource_lib; }
const cReactionLib& GetReactionLib() const { return reaction_lib; }
const cMutationLib& GetMutationLib() const { return mutation_lib; }
Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/main/cOrganism.cc 2008-12-30 15:18:43 UTC (rev 3079)
@@ -85,6 +85,7 @@
, m_k(0)
, m_failed_reputation_increases(0)
, m_tag(make_pair(-1, 0))
+ , m_strcatter(0)
{
m_hardware = m_world->GetHardwareManager().Create(this);
@@ -768,15 +769,10 @@
bool cOrganism::SubtractSelfRawMaterials (int amount)
{
bool isSuccessful = false;
- if (amount <= m_raw_materials[GetLineageLabel()]) {
- isSuccessful = true;
- m_raw_materials[GetLineageLabel()] -= amount;
-
- }
-/* if (amount <= m_self_raw_materials) {
+ if (amount <= m_self_raw_materials) {
isSuccessful = true;
m_self_raw_materials -= amount;
- }*/
+ }
return isSuccessful;
}
@@ -828,36 +824,6 @@
}
-/* Called to add raw materials of a given type. */
-bool cOrganism::AddRawMaterials (int mat, int amount, int donor_id) {
- bool isSuccessful = true;
-
- if (((m_raw_materials[mat] + amount) < m_world->GetConfig().RAW_MATERIAL_CAP.Get()) ||
- (m_world->GetConfig().RAW_MATERIAL_CAP.Get() == -1)){
- m_raw_materials[mat] += amount;
- donor_list.insert(donor_id);
- if (donor_id != m_id) {
- m_num_donate_received += amount;
- m_amount_donate_received++;
- }
- }
- return isSuccessful;
-}
-
-/* Called to subtract raw materials of a given type. */
-bool cOrganism::SubtractRawMaterials (int mat, int amount) {
- bool isSuccessful = true;
- int q = m_raw_materials[mat];
-
- if (amount <= m_raw_materials[mat]) {
- isSuccessful = true;
- m_raw_materials[mat] -= amount;
-
- }
-
- return isSuccessful;
-}
-
/* Get an organism's reputation, which is expressed as an
opinion. 0 is the default reputation (this should be refactored
to be cleaner). */
@@ -894,6 +860,60 @@
}
+void cOrganism::ReceiveString(tBuffer<int> str, int org_id)
+{
+// m_all_received_messages.push_back(new_donation);
+ donor_list.insert(org_id);
+ m_all_received_messages.push_back(make_pair(str, org_id));
+ m_num_donate_received++;
+}
+
+/* Return an int that represents the largest number of bits
+ matched by any of the strings received. */
+std::pair < int, int > cOrganism::MatchString(const cString& string_to_match)
+{
+ int num_matched = 0;
+ int max_num_matched = 0;
+ int org_id = 0;
+
+ if (m_all_received_messages.size()) {
+
+ 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++)
+ {
+ 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;
+ }
+ }
+
+ }
+ 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()
@@ -931,6 +951,57 @@
}
+// Return how well the organism does on catting strings.
+int cOrganism::CheckStrCat(const cString& string_to_match)
+{
+ // for each donated string try this...
+ // Get org string & make right length
+ tBuffer<int> test_str(string_to_match.GetSize());
+ tBuffer<int> org_str (GetOutputBuf());
+
+ int max_num_matched =0;
+
+ // This will make the string the size of the capacity.
+ int length = org_str.GetCapacity();
+ int org_str_length = org_str.GetTotal();
+
+ while (org_str_length < length) {
+ org_str.Add(-1);
+ org_str_length = org_str.GetTotal();
+ }
+
+ int num_matched = 0;
+
+ if (m_all_received_messages.size()) {
+ // Check with org in first position...
+ for (unsigned int k = 0; k < m_all_received_messages.size(); k++) {
+ tBuffer<int> received = m_all_received_messages[k].first;
+ num_matched = 0;
+ test_str.Cat(org_str);
+ test_str.Cat(received);
+ num_matched = MatchStringsHelper(test_str, string_to_match);
+ if (num_matched > max_num_matched){
+ max_num_matched = num_matched;
+ }
+ }
+
+ // Check with org in first position...
+ for (unsigned int k = 0; k < m_all_received_messages.size(); k++) {
+ tBuffer<int> received = m_all_received_messages[k].first;
+ num_matched = 0;
+ test_str.Cat(received);
+ test_str.Cat(org_str);
+ num_matched = MatchStringsHelper(test_str, string_to_match);
+ if (num_matched > max_num_matched){
+ max_num_matched = num_matched;
+ }
+
+ }
+
+ }
+ return max_num_matched;
+}
+
/* See if the output buffer matches the string */
int cOrganism::MatchOutputBuffer(cString string_to_match)
{
Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/main/cOrganism.h 2008-12-30 15:18:43 UTC (rev 3079)
@@ -30,7 +30,6 @@
#include <string>
#include <vector>
#include <set>
-#include <map>
#ifndef cCPUMemory_h
#include "cCPUMemory.h"
@@ -461,16 +460,6 @@
int GetSelfRawMaterials() { return m_self_raw_materials; }
// retrieve the amount of raw materials collected from others
int GetOtherRawMaterials() { return m_other_raw_materials; }
-
- // Add Raw Materials
- bool AddRawMaterials (int mat, int amount, int donor_id);
- // Subtract Raw Materials
- bool SubtractRawMaterials (int mat, int amount);
- // Get Raw Material Amount
- int GetRawMaterialAmount (int mat) { return m_raw_materials[mat]; }
-
-
-
// get the organism's reputation
int GetReputation();
// set the organism's reputation
@@ -504,14 +493,14 @@
// was the organism a donor
bool IsDonor(int neighbor_id);
// Receive a string
-// void ReceiveString(tBuffer<int> str, int org_id);
+ void ReceiveString(tBuffer<int> str, int org_id);
// Return the maximum number of characters and the organism id matched by the received strings
-// std::pair < int, int > MatchString(const cString& str);
+ 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);
+ int MatchStringsHelper(tBuffer<int> org_string, const cString& string_to_match);
// Return how well the organism does on catting strings.
-// int CheckStrCat(const cString& string_to_match);
+ int CheckStrCat(const cString& string_to_match);
// Check if buffer contains this string; return # bits correct
int MatchOutputBuffer(cString string_to_match);
@@ -532,21 +521,22 @@
// Get tag
int GetTagLabel() { return m_tag.first; }
pair < int, int > GetTag() { return m_tag; }
-// void SetStringCatter(bool val) { m_strcatter = val; }
-// bool GetStringCatter() { return m_strcatter; }
+ void SetStringCatter(bool val) { m_strcatter = val; }
+ bool GetStringCatter() { return m_strcatter; }
void SetOutputNegative1();
+ void AddDonatedLineage(int lin) { donating_lineages.insert(lin); }
+ int GetNumberOfDonatedLineages() { return donating_lineages.size(); }
protected:
- // The organism's own raw materials
+ // 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;
-
-
- map< int, int > m_raw_materials;
+ int m_other_raw_materials;
// Organisms that have donated to this organism
set<int> donor_list;
+ // Strings this organism has received.
+ set<int> donating_lineages;
// number of donations
int m_num_donate;
// number of donations received
@@ -558,11 +548,11 @@
// reputation minimum for donation/rotation
// based on Nowak89
int m_k;
-// vector < std::pair < tBuffer<int>, int > > m_all_received_messages;
+ vector < std::pair < tBuffer<int>, int > > m_all_received_messages;
// int number of reputation increase failures
int m_failed_reputation_increases;
std::pair < int, int > m_tag;
-// bool m_strcatter;
+ bool m_strcatter;
// -------- End of reputation support --------
Modified: branches/hjg-dev/source/main/cStats.cc
===================================================================
--- branches/hjg-dev/source/main/cStats.cc 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/main/cStats.cc 2008-12-30 15:18:43 UTC (rev 3079)
@@ -1833,7 +1833,9 @@
cDoubleSum num_failed_reputation_inc;
cDoubleSum own_raw_mat;
cDoubleSum other_raw_mat;
-
+ int num_all_strings = 0;
+ int num_strings = m_world->GetEnvironment().GetNumberOfMatchStrings() -1;
+
// difference between how many an organism donated & how many it received
cDoubleSum disparity;
@@ -1844,29 +1846,8 @@
// int pop_size = 0;
int num_alt =0;
int num_coop = 0;
- int perfect =0;
- // Set up the stats tracking for the strings.
- // print the tags
- /*map<int, int>::iterator iter;
- stringstream ss;
- for(iter = m_tags.begin(); iter != m_tags.end(); iter++ ) {
- ss << iter->first;
- string name = ss.str();
- df.Write(iter->second, name.c_str());
- iter->second = 0;
- }*/
- map < int, cString> my_strings = m_world->GetEnvironment().GetMatchStringsFromTask();
- map < int, cDoubleSum > tag_results;
- map < int, cDoubleSum >::iterator iter2;
-
- map< int, cString >::const_iterator iter;
- for (iter = my_strings.begin(); iter != my_strings.end(); iter++) {
- tag_results[iter->first].Add(0);
- }
-
-
df.WriteComment("Avida organism reputation information -- average donations, min donations, max donations");
df.WriteTimeStamp();
df.Write(m_update, "Update [update]");
@@ -1886,23 +1867,9 @@
donations.Add(org->GetNumberOfDonations());
num_donations_received.Add(org->GetNumberOfDonationsReceived());
amount_donations_received.Add(org->GetAmountOfDonationsReceived());
-// own_raw_mat.Add(org->GetSelfRawMaterials());
-// other_raw_mat.Add(org->GetOtherRawMaterials());
-
- // Update raw materials.
- unsigned int type_rec =0;
- for (iter2 = tag_results.begin(); iter2 != tag_results.end(); iter2++) {
- tag_results[iter2->first].Add(org->GetRawMaterialAmount(iter2->first));
- if (org->GetRawMaterialAmount(iter2->first) > 0) type_rec++;
- if (org->GetTagLabel() == iter2->first) {
- own_raw_mat.Add(org->GetRawMaterialAmount(iter2->first));
- } else {
- other_raw_mat.Add(org->GetRawMaterialAmount(iter2->first));
- }
-
- }
- if (type_rec == tag_results.size()) perfect++;
-
+ own_raw_mat.Add(org->GetSelfRawMaterials());
+ other_raw_mat.Add(org->GetOtherRawMaterials());
+
reciprocations.Add(org->GetNumberOfReciprocations());
donors.Add(org->GetNumberOfDonors());
num_failed_reputation_inc.Add(org->GetFailedReputationIncreases());
@@ -1914,6 +1881,8 @@
if (org->GetNumberOfDonations() > 0) num_alt++;
if ((org->GetNumberOfDonationsReceived() && org->GetNumberOfDonations()) > 0) num_coop++;
+ if (org->GetNumberOfDonatedLineages() == num_strings) num_all_strings++;
+
}
}
// float rep = reputations/pop_size;
@@ -1932,16 +1901,10 @@
df.Write(num_alt, "Number of altruists [altruists]");
df.Write(num_coop, "Number of cooperators [cooperators]");
-
- for (iter2 = tag_results.begin(); iter2 != tag_results.end(); iter2++) {
- //tag_results[iter->first] += organism->GetRawMaterialAmount(iter->first);
- df.Write(iter2->second.Average(), my_strings[iter2->first]);
- }
df.Write(own_raw_mat.Average(), "Avg. own raw mat [ownrawmat]");
- df.Write(other_raw_mat.Average(), "Avg. other raw mat [otherrawmat]");
- df.Write(perfect, "Number of orgs with all strings [perfect]");
+ df.Write(other_raw_mat.Average(), "Avg. other raw mat [otherrawmat]");
+ df.Write(num_all_strings, "Number of orgs with all strings [allstrings]");
-
// df.Write(k.Average(), "Avg. k of organisms [k]");
// df.Write(m_donate_to_donor, "Number of donate to donor [donatedonor]");
// df.Write(m_donate_to_facing, "Number of donate to facing [donatefacing]");
Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/main/cTaskLib.cc 2008-12-30 15:18:43 UTC (rev 3079)
@@ -44,7 +44,6 @@
#include <climits>
#include <iomanip>
#include <string>
-#include <map>
// Various workarounds for Visual Studio shortcomings
#if AVIDA_PLATFORM(WINDOWS)
@@ -357,6 +356,11 @@
Load_MatchStr(name, info, envreqs, errors);
else if (name == "match_number")
Load_MatchNumber(name, info, envreqs, errors);
+
+ if (name == "strcat")
+ Load_StrCat(name, info, envreqs, errors);
+ else if(name == "all_strings")
+ Load_AllStrings(name, info, envreqs, errors);
if (name == "sort_inputs")
Load_SortInputs(name, info, envreqs, errors);
@@ -423,8 +427,6 @@
// reputation based tasks
else if(name == "use_raw_mat")
NewTask(name, "Use raw materials from self and other", &cTaskLib::Task_UseRawMaterials);
- else if(name == "multi_raw_mat")
- NewTask(name, "Use multiple raw materials from self and other", &cTaskLib::Task_MultiRawMaterials);
else if(name == "good_reputation")
NewTask(name, "Maintain a good reputation", &cTaskLib::Task_GoodReputation);
else if(name == "use_many_raw_mat")
@@ -1915,11 +1917,6 @@
return 0.0;
}
-map < int, cString > cTaskLib::GetMatchStrings()
-{
- return m_strings;
-}
-
void cTaskLib::Load_MatchStr(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
{
cArgSchema schema;
@@ -1930,16 +1927,121 @@
schema.AddEntry("tag",2,-1);
cArgContainer* args = cArgContainer::Load(argstr, schema, errors);
envreqs.SetMinOutputs(args->GetString(0).GetSize());
- m_strings[args->GetInt(2)] = args->GetString(0);
+ m_strings.push_back(args->GetString(0));
// m_world->GetStats().AddTag(args->GetInt(2), 0);
// m_world->GetStats().AddTag(-1, 0);
if (args) NewTask(name, "MatchStr", &cTaskLib::Task_MatchStr, 0, args);
}
+/* This loads the string cat parameters.*/
+void cTaskLib::Load_StrCat(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
+{
+ cArgSchema schema;
+ schema.AddEntry("string", 0, cArgSchema::SCHEMA_STRING);
+ schema.AddEntry("partial",0, 0);
+ schema.AddEntry("binary",1,1);
+ schema.AddEntry("pow",0,2.0);
+ cArgContainer* args = cArgContainer::Load(argstr, schema, errors);
+ if (args) NewTask(name, "StrCat", &cTaskLib::Task_StrCat, 0, args);
+}
+
+vector<cString> cTaskLib::GetMatchStrings()
+{
+ return m_strings;
+}
+
+cString cTaskLib::GetMatchString(int x)
+{
+ cString s;
+ if (x >= 0 && x < m_strings.size()){
+ s = m_strings[x];
+ } else {
+ s = cString("");
+ }
+
+ return s;
+
+}
+
+
+/* This task checks to see if an organism can create a string by catting the
+ string in its buffer with each of the strings it has received as a donation. */
+double cTaskLib::Task_StrCat(cTaskContext& ctx) const
+{
+
+ //if (ctx.GetOrganism()->GetStringCatter()==0) return 0;
+
+ tBuffer<int> temp_buf(ctx.GetOutputBuffer());
+
+ const cString& string_to_match = ctx.GetTaskEntry()->GetArguments().GetString(0);
+ int partial = ctx.GetTaskEntry()->GetArguments().GetInt(0);
+// int binary = ctx.GetTaskEntry()->GetArguments().GetInt(1);
+ //int string_index;
+ //int num_matched = 0;
+ //int test_output;
+ int max_num_matched = 0;
+ int num_real=0;
+ double mypow = ctx.GetTaskEntry()->GetArguments().GetDouble(0);
+ bool used_received = false;
+
+
+ // Given the desired string, pass it to the organism which will
+ // check all possibilities. ?
+
+ for (unsigned int i=0; i<m_strings.size(); i++) {
+ max_num_matched += ctx.GetOrganism()->MatchString(m_strings[i]).first;
+ }
+
+
+ // Update tag
+ ctx.GetOrganism()->UpdateTag(1, (int)(max_num_matched/m_strings.size()));
+ // Update reputation
+ int rep = (max_num_matched/m_strings.size());
+ ctx.GetOrganism()->SetAverageReputation(rep);
+
+ cString name;
+ name = "[catted]";
+ m_world->GetStats().AddStringBitsMatchedValue(name, max_num_matched);
+
+ if (max_num_matched == string_to_match.GetSize()) m_world->GetStats().IncPerfectMatch();
+
+
+ double bonus = 0.0;
+ // return value between 0 & 1 representing the percentage of string that was matched
+
+ double base_bonus = 0;
+ //static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
+// if (m_world->GetConfig().MATCH_PERCENT.Get() == 0) {
+ // percent over 50% (Sherri)
+ base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
+// } else if (m_world->GetConfig().MATCH_PERCENT.Get() == 1) {
+ // percent (hjg)
+ // base_bonus = static_cast<double>(max_num_matched) / static_cast<double>(string_to_match.GetSize());
+ //}
+
+
+ if (partial)
+ {
+ base_bonus=double(max_num_matched)*2/double(num_real) -1;
+ }
+
+ if (base_bonus > 0.0) {
+ bonus = pow(base_bonus,mypow);
+ if (used_received)
+ m_world->GetStats().AddMarketItemUsed();
+ else
+ m_world->GetStats().AddMarketOwnItemUsed();
+ }
+ return bonus;
+}
+
+
double cTaskLib::Task_MatchStr(cTaskContext& ctx) const
{
-
+
+ if (ctx.GetOrganism()->GetStringCatter()) return 0;
+
// These even out the stats tracking.
m_world->GetStats().AddTag(ctx.GetTaskEntry()->GetArguments().GetInt(2), 0);
m_world->GetStats().AddTag(-1, 0);
@@ -2003,12 +2105,32 @@
ctx.GetOrganism()->SetReputation(0);
}
+
+ std::pair < int, int > best_string;
+ int donated_string;
+
+
+ best_string = ctx.GetOrganism()->MatchString(string_to_match);
+ num_matched = best_string.first;
+ donated_string = num_matched;
+ if (num_matched > max_num_matched) {
+ max_num_matched = num_matched;
+ // max_org_id = best_string.second;
+ }
+
+
double bonus = 0.0;
double base_bonus = 0.0;
-
- base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
+ //if (m_world->GetConfig().MATCH_PERCENT.Get() == 0) {
+ // percent over 50% (Sherri)
+ base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
+// } else if (m_world->GetConfig().MATCH_PERCENT.Get() == 1) {
+ // percent (hjg)
+ // base_bonus = static_cast<double>(max_num_matched) / static_cast<double>(string_to_match.GetSize());
+ //}
+
if (partial)
{
base_bonus=double(max_num_matched)*2/double(num_real) -1;
@@ -2021,6 +2143,74 @@
}
+/* This loads the string cat parameters.*/
+void cTaskLib::Load_AllStrings(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
+{
+ cArgSchema schema;
+ schema.AddEntry("returnval",0, 0);
+ cArgContainer* args = cArgContainer::Load(argstr, schema, errors);
+ if (args) NewTask(name, "AllStrings", &cTaskLib::Task_AllStrings, 0, args);
+}
+
+double cTaskLib::Task_AllStrings(cTaskContext& ctx) const
+{
+ // Check if the organism either created or received as a donation ;
+ // if it misses one, then fail it.
+ tBuffer<int> temp_buf(ctx.GetOutputBuffer());
+ int return_type = ctx.GetTaskEntry()->GetArguments().GetInt(0);
+
+
+ cString string_to_match; // = ctx.GetTaskEntry()->GetArguments().GetString(0);
+ // int binary = ctx.GetTaskEntry()->GetArguments().GetInt(1);
+ //int string_index;
+ int num_matched = 0;
+ //int test_output;
+ int max_num_matched = 0;
+ double sum_matched = 0;
+ int num_real=0;
+ double bonus = 0;
+ double total_size = 0;
+
+
+ // Given the desired string, pass it to the organism which will
+ // check all possibilities. ?
+ for (unsigned int i=0; i<m_strings.size(); i++) {
+ num_matched = 0;
+ string_to_match = m_strings[i];
+ // Check if the organism created the string.
+ for (int j = 0; j < string_to_match.GetSize(); j++)
+ {
+ if (string_to_match[j]!='9')
+ num_real++;
+ if (string_to_match[j]=='0' && temp_buf[j]==0 ||
+ string_to_match[j]=='1' && temp_buf[j]==1)
+ num_matched++;
+ }
+
+ max_num_matched = num_matched;
+
+ num_matched = ctx.GetOrganism()->MatchString(m_strings[i]).first;
+ if (num_matched > max_num_matched) max_num_matched = num_matched;
+
+ sum_matched += max_num_matched;
+ total_size += string_to_match.GetSize();
+ }
+
+
+ if (sum_matched == total_size) {
+ m_world->GetStats().IncPerfectMatch();
+ }
+
+ if (return_type) {
+ bonus = sum_matched/total_size;
+ } else if (sum_matched == total_size) {
+ bonus = 1;
+ }
+
+ return bonus;
+}
+
+
void cTaskLib::Load_MatchNumber(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
{
cArgSchema schema;
@@ -3188,41 +3378,6 @@
return bonus;
}
-double cTaskLib::Task_MultiRawMaterials(cTaskContext& ctx) const {
- double bonus = 0.0;
- double other_mult_val = m_world->GetConfig().ALT_BENEFIT.Get();
- double my_mult_val = m_world->GetConfig().ALT_COST.Get();
- int org_tag = ctx.GetOrganism()->GetTagLabel();
-
- // reward for own stuff
-// bonus += (my_mult_val * ctx.GetOrganism()->GetSelfRawMaterials());
-// bonus += (other_mult_val * ctx.GetOrganism()->GetOtherRawMaterials());
-/*( map<int, int>::iterator iter;
- stringstream ss;
- for(iter = m_tags.begin(); iter != m_tags.end(); iter++ ) {
- ss << iter->first;
- string name = ss.str();
- df.Write(iter->second, name.c_str());
- iter->second = 0;
- }*/
-
- map< int, cString >::const_iterator iter;
- for (iter = m_strings.begin(); iter != m_strings.end(); iter++) {
- int val = ctx.GetOrganism()->GetRawMaterialAmount(iter->first);
- if (iter->first == org_tag) {
- bonus += (my_mult_val * val);
- } else {
- bonus += (other_mult_val * val);
- }
-
- }
-
-
- return bonus;
-
-
-}
-
/* Reward organisms for having a good reputation */
double cTaskLib::Task_GoodReputation(cTaskContext& ctx) const {
// double bonus = 0.0;
Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h 2008-12-27 23:26:19 UTC (rev 3078)
+++ branches/hjg-dev/source/main/cTaskLib.h 2008-12-30 15:18:43 UTC (rev 3079)
@@ -88,11 +88,13 @@
bool UseNeighborOutput() const { return use_neighbor_output; }
// Get the strings that parameterize the MatchString tasks
- map < int, cString > GetMatchStrings();
-
+ vector<cString> GetMatchStrings();
+ cString GetMatchString(int x);
+ int GetNumberOfMatchStrings() { return m_strings.size(); }
+
private:
// Store the strings used by the MatchString tasks
- map < int, cString > m_strings;
+ vector<cString> m_strings;
private: // Direct task related methods
@@ -250,6 +252,11 @@
double Task_MatchStr(cTaskContext& ctx) const;
void Load_MatchNumber(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
double Task_MatchNumber(cTaskContext& ctx) const;
+ void Load_StrCat(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
+ double Task_StrCat(cTaskContext& ctx) const;
+ void Load_AllStrings(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
+ double Task_AllStrings(cTaskContext& ctx) const;
+
void Load_SortInputs(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
double Task_SortInputs(cTaskContext& ctx) const;
@@ -307,7 +314,8 @@
// reputation
double Task_UseRawMaterials(cTaskContext& ctx) const;
double Task_SaveRawMaterials(cTaskContext& ctx) const;
- double Task_MultiRawMaterials(cTaskContext& ctx) const;
+ double Task_SaveRawMaterials1(cTaskContext& ctx) const;
+ double Task_SaveRawMaterials2(cTaskContext& ctx) const;
double Task_GoodReputation(cTaskContext& ctx) const;
double Task_ConsumeRawMaterials(cTaskContext& ctx) const;
More information about the Avida-cvs
mailing list