[Avida-SVN] r3082 - in branches/hjg-dev/source: cpu main tools
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Tue Dec 30 13:37:54 PST 2008
Author: hjg
Date: 2008-12-30 16:37:54 -0500 (Tue, 30 Dec 2008)
New Revision: 3082
Modified:
branches/hjg-dev/source/cpu/cHardwareCPU.cc
branches/hjg-dev/source/main/cAvidaConfig.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/cStats.h
branches/hjg-dev/source/main/cTaskLib.cc
branches/hjg-dev/source/main/cTaskLib.h
branches/hjg-dev/source/tools/cString.h
Log:
New string tracking code
Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc 2008-12-30 21:37:54 UTC (rev 3082)
@@ -7362,16 +7362,9 @@
}
-/* 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.
- */
-
-
/* 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. */
@@ -7379,18 +7372,48 @@
{
int num = 0;
+ int max_num = 0;
+ int max_string = -1;
+ int string_size = 0;
+ bool val;
- // For the time being this instruction is hardcoded for 2 strings and
- // two species.
+ organism->InitStringMap();
+ // Figure out if it has produced any of the strings
+ std::vector < cString > temp_strings = m_world->GetEnvironment().GetMatchStringsFromTask();
+ if (temp_strings.size()) string_size = temp_strings[0].GetSize();
+ for (unsigned int i=0; i < temp_strings.size(); i++){
+ num = organism->MatchOutputBuffer(temp_strings[i]);
+ if (num > max_num) {
+ max_num = num;
+ max_string = i;
+ }
+ }
+
+ // Determine if it has to produce one in particular.
+ if (m_world->GetConfig().SPECIALISTS.Get()) {
+ if (organism->GetLineageLabel() != max_string) {
+ max_num = 0;
+ }
+ }
+
+
+ // If still ok, add the raw material and clear the buffer
+ if (max_num == string_size) {
+ // Indicate organism has produced the string
+ val = organism->ProduceString(max_string);
+
+ // temp until old code is phased out:
+ organism->AddSelfRawMaterials(1);
+
+ // Clear buffer if the organism has received credit for the string
+ if (val) organism->SetOutputNegative1();
+ }
+
+/*
cString s = m_world->GetEnvironment().GetMatchString(organism->GetLineageLabel());
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 == s.GetSize()) {
@@ -7398,6 +7421,7 @@
organism->SetOutputNegative1();
}
}
+ */
return true;
}
Modified: branches/hjg-dev/source/main/cAvidaConfig.h
===================================================================
--- branches/hjg-dev/source/main/cAvidaConfig.h 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cAvidaConfig.h 2008-12-30 21:37:54 UTC (rev 3082)
@@ -570,8 +570,8 @@
CONFIG_ADD_VAR(RANDOMIZE_RAW_MATERIAL_AMOUNT, int, 0, "Should all the organisms receive the same amount 0/1 (off/on)");
CONFIG_ADD_VAR(DONATION_RESTRICTIONS, int, 0, "0=none\n1=inter-species only\n2=different tag only");
CONFIG_ADD_VAR(INHERIT_REPUTATION, int, 0, "0=reputations are not inherited\n1=reputations are inherited\n2=tags are inherited");
- CONFIG_ADD_VAR(STRING_REPUTATION, int, 0, "0=reputation is average\n1=reputation is additive");
- CONFIG_ADD_VAR(RAW_MATERIAL_CAP, int, -1, "-1=no cap");
+ CONFIG_ADD_VAR(SPECIALISTS, int, 0, "0=generalists allowed\n1=only specialists");
+ CONFIG_ADD_VAR(STRING_AMOUNT_CAP, int, -1, "-1=no cap on string amounts\n#=CAP");
#endif
Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cOrganism.cc 2008-12-30 21:37:54 UTC (rev 3082)
@@ -920,3 +920,29 @@
m_output_buf.Clear();
}
+/* Initialize the string tracking map */
+void cOrganism::InitStringMap()
+{
+ if (!m_string_map.size()) {
+ // Get the strings from the task lib.
+ std::vector < cString > temp_strings = m_world->GetEnvironment().GetMatchStringsFromTask();
+ // Create structure for each of them.
+ for (unsigned int i=0; i < temp_strings.size(); i++){
+ m_string_map[i].m_string = temp_strings[i];
+ }
+ }
+}
+
+
+bool cOrganism::ProduceString(int i)
+{
+ bool val = false;
+ int cap = m_world->GetConfig().STRING_AMOUNT_CAP.Get();
+ if ((cap == -1) || (m_string_map[i].on_hand < cap))
+ {
+ m_string_map[i].prod_string++;
+ m_string_map[i].on_hand++;
+ val = true;
+ }
+ return val;
+}
Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cOrganism.h 2008-12-30 21:37:54 UTC (rev 3082)
@@ -30,6 +30,7 @@
#include <string>
#include <vector>
#include <set>
+#include <map>
#ifndef cCPUMemory_h
#include "cCPUMemory.h"
@@ -61,6 +62,9 @@
#ifndef cOrgMessage_h
#include "cOrgMessage.h"
#endif
+#ifndef cString_h
+#include "cString.h"
+#endif
#ifndef tArray_h
#include "tArray.h"
#endif
@@ -518,7 +522,10 @@
void SetOutputNegative1();
void AddDonatedLineage(int lin) { donating_lineages.insert(lin); }
int GetNumberOfDonatedLineages() { return donating_lineages.size(); }
-
+ void InitStringMap();
+ bool ProduceString(int i);
+ int GetNumberStringsProduced(int i) { return m_string_map[i].prod_string; }
+ int GetNumberStringsOnHand(int i) { return m_string_map[i].on_hand; }
protected:
// The organism's own raw materials
@@ -526,6 +533,7 @@
// 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;
// Strings this organism has received.
set<int> donating_lineages;
@@ -544,6 +552,24 @@
int m_failed_reputation_increases;
std::pair < int, int > m_tag;
+ /*! Contains all the different data structures needed to
+ track strings, production of strings, and donation/trade
+ of strings. It is inspired by the cMessagingSupport*/
+ struct cStringSupport
+ {
+ cStringSupport()
+ { prod_string = 0; received_string = 0; on_hand = 0; }
+ cString m_string; //!< The string being tracked
+ int prod_string; //!< The number of times this string has been produced.
+ int received_string; //!< The number of times this string has been received.
+ int on_hand; //!< The number of copies of the string this organism has on hand
+ };
+
+ /* This member variable is a map of tags to strings. It can
+ be used to track production, consumption, and donation of
+ strings. */
+ std::map < int, cStringSupport > m_string_map;
+
// -------- End of reputation support --------
};
Modified: branches/hjg-dev/source/main/cStats.cc
===================================================================
--- branches/hjg-dev/source/main/cStats.cc 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cStats.cc 2008-12-30 21:37:54 UTC (rev 3082)
@@ -1782,11 +1782,8 @@
df.Write(m_update, "Update [update]");
cOrganism* org;
- // Print number of perfect matches
- df.Write(m_perfect_match.Sum(), "PerfectMatch");
- m_perfect_match.Clear();
-
-
+
+ /*
// Interate through map of information.
map<cString,cDoubleSum>::iterator iter2;
for(iter2 = m_string_bits_matched.begin(); iter2 != m_string_bits_matched.end(); iter2++ ) {
@@ -1813,9 +1810,50 @@
string name = ss.str();
df.Write(iter->second, name.c_str());
iter->second = 0;
- }
+ }*/
+ // Print data about strings:
+ std::map <int, cDoubleSum> m_strings_stored;
+ std::map <int, cDoubleSum> m_strings_produced;
+ cDoubleSum total;
+ int perfect;
+ // Get the number of strings
+ int num = m_world->GetEnvironment().GetNumberOfMatchStrings();
+ for(int i=0; i<m_world->GetPopulation().GetSize(); ++i) {
+ cPopulationCell& cell = m_world->GetPopulation().GetCell(i);
+ org = cell.GetOrganism();
+
+ if(cell.IsOccupied()) {
+ perfect = 1;
+ for (unsigned int j = 0; j<num; j++) {
+ m_strings_stored[j].Add(org->GetNumberStringsOnHand(j));
+ perfect = perfect && org->GetNumberStringsOnHand(j);
+ total.Add(org->GetNumberStringsOnHand(j));
+ m_strings_produced[j].Add(org->GetNumberStringsProduced(j));
+ }
+ if (perfect) m_perfect_match.Add(1);
+ }
+ }
+
+ // print the string info
+ for (int k=0; k<num; k++) {
+ string name = m_world->GetEnvironment().GetMatchString(k).GetData();
+ name = "produced" + name;
+ df.Write(m_strings_produced[k].Average(), name.c_str());
+
+ name = m_world->GetEnvironment().GetMatchString(k).GetData();
+ name = "stored" + name;
+ df.Write(m_strings_stored[k].Average(), name.c_str());
+
+ }
+ df.Write(total.Average(), "totalStoredAverage");
+
+ // Print number of perfect matches
+ df.Write(m_perfect_match.Sum(), "PerfectMatch");
+ m_perfect_match.Clear();
+
+
df.Endl();
}
@@ -1836,6 +1874,7 @@
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;
@@ -1848,6 +1887,7 @@
int num_coop = 0;
+
df.WriteComment("Avida organism reputation information -- average donations, min donations, max donations");
df.WriteTimeStamp();
df.Write(m_update, "Update [update]");
Modified: branches/hjg-dev/source/main/cStats.h
===================================================================
--- branches/hjg-dev/source/main/cStats.h 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cStats.h 2008-12-30 21:37:54 UTC (rev 3082)
@@ -801,7 +801,7 @@
std::map <cString, cDoubleSum> m_string_bits_matched;
cDoubleSum m_perfect_match;
std::map <int, int> m_tags;
-
+
// -------- Deme replication support --------
public:
Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cTaskLib.cc 2008-12-30 21:37:54 UTC (rev 3082)
@@ -429,9 +429,12 @@
NewTask(name, "Consume raw materials", &cTaskLib::Task_ConsumeRawMaterials);
else if(name == "save_raw_mat")
NewTask(name, "Save raw materials", &cTaskLib::Task_SaveRawMaterials);
+ else if(name == "store_strings")
+ NewTask(name, "Produce and store strings", &cTaskLib::Task_StoreStrings);
+
// event tasks
if(name == "move_to_event")
NewTask(name, "Moved into cell containing event", &cTaskLib::Task_MoveToEvent);
@@ -3204,3 +3207,12 @@
}
+double cTaskLib::Task_StoreStrings(cTaskContext& ctx) const {
+ // The reward for your own strings should be less than the reward for others? Maybe later.
+ // Cap how many you can store of each type.
+ double count = 0;
+ for (unsigned int i = 0; i<m_strings.size(); i++) {
+ count += ctx.GetOrganism()->GetNumberStringsOnHand(i);
+ }
+ return count;
+}
Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/main/cTaskLib.h 2008-12-30 21:37:54 UTC (rev 3082)
@@ -313,6 +313,8 @@
double Task_ConsumeRawMaterials(cTaskContext& ctx) const;
double Task_UseRawMaterials(cTaskContext& ctx) const;
double Task_SaveRawMaterials(cTaskContext& ctx) const;
+ double Task_StoreStrings(cTaskContext& ctx) const;
+
};
Modified: branches/hjg-dev/source/tools/cString.h
===================================================================
--- branches/hjg-dev/source/tools/cString.h 2008-12-30 16:11:54 UTC (rev 3081)
+++ branches/hjg-dev/source/tools/cString.h 2008-12-30 21:37:54 UTC (rev 3082)
@@ -193,7 +193,9 @@
**/
int GetSize() const { return value->GetSize(); }
+ const char* GetData() const { return value->GetData(); }
+
// Comparisons
int Compare(const char * in) const; // strcmp like function
bool operator== (const char * in) const { return (Compare(in)==0); }
More information about the Avida-cvs
mailing list