[Avida-cvs] [avida-svn] r942 - in development/source: . actions cpu main third-party/trio tools utils/hist_map

goingssh at myxo.css.msu.edu goingssh at myxo.css.msu.edu
Mon Sep 11 14:27:27 PDT 2006


Author: goingssh
Date: 2006-09-11 17:27:27 -0400 (Mon, 11 Sep 2006)
New Revision: 942

Modified:
   development/source/actions/PrintActions.cc
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/cpu/cTestCPUInterface.cc
   development/source/cpu/cTestCPUInterface.h
   development/source/defs.h
   development/source/main/cAvidaConfig.h
   development/source/main/cOrgInterface.h
   development/source/main/cOrganism.cc
   development/source/main/cOrganism.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
   development/source/main/cPopulationInterface.cc
   development/source/main/cPopulationInterface.h
   development/source/main/cStats.cc
   development/source/main/cStats.h
   development/source/main/cTaskContext.h
   development/source/main/cTaskLib.cc
   development/source/main/cTaskLib.h
   development/source/third-party/trio/example.c
   development/source/tools/tList.h
   development/source/utils/hist_map/hist_map.cc
   development/source/utils/hist_map/hist_map2.cc
Log:
Market economy support added

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/actions/PrintActions.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -63,6 +63,7 @@
 STATS_OUT_FILE(PrintDominantParaData,  parasite.dat        );
 STATS_OUT_FILE(PrintInstructionData,   instruction.dat     );
 STATS_OUT_FILE(PrintGenotypeMap,       genotype_map.m      );
+STATS_OUT_FILE(PrintMarketData,		   market.dat		   );
 
 #define POP_OUT_FILE(METHOD, DEFAULT)                                                     /*  1 */ \
 class cAction ## METHOD : public cAction {                                                /*  2 */ \
@@ -1442,6 +1443,7 @@
   action_lib->Register<cActionPrintDominantParaData>("PrintDominantParaData");
   action_lib->Register<cActionPrintInstructionData>("PrintInstructionData");
   action_lib->Register<cActionPrintGenotypeMap>("PrintGenotypeMap");
+  action_lib->Register<cActionPrintMarketData>("PrintMarketData");
   
   // Population Out Files
   action_lib->Register<cActionPrintPhenotypeData>("PrintPhenotypeData");
@@ -1502,6 +1504,7 @@
   action_lib->Register<cActionPrintDominantParaData>("print_dom_parasite_data");
   action_lib->Register<cActionPrintInstructionData>("print_instruction_data");
   action_lib->Register<cActionPrintGenotypeMap>("print_genotype_map");
+  action_lib->Register<cActionPrintMarketData>("print_market_data");
   
   action_lib->Register<cActionPrintPhenotypeData>("print_number_phenotypes");
   action_lib->Register<cActionPrintPhenotypeStatus>("print_phenotype_status");

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/cpu/cHardwareCPU.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -210,6 +210,8 @@
     cInstEntryCPU("IO",        &cHardwareCPU::Inst_TaskIO, true,
                   "Output ?BX?, and input new number back into ?BX?"),
     cInstEntryCPU("match-strings", &cHardwareCPU::Inst_MatchStrings),
+	cInstEntryCPU("sell", &cHardwareCPU::Inst_Sell),
+	cInstEntryCPU("buy", &cHardwareCPU::Inst_Buy),
     cInstEntryCPU("send",      &cHardwareCPU::Inst_Send),
     cInstEntryCPU("receive",   &cHardwareCPU::Inst_Receive),
     cInstEntryCPU("sense",     &cHardwareCPU::Inst_Sense),
@@ -2525,6 +2527,23 @@
 	return true;
 }
 
+bool cHardwareCPU::Inst_Sell(cAvidaContext& ctx)
+{
+	int search_label = GetLabel().AsInt(3) % MARKET_SIZE;
+	int send_value = GetRegister(REG_BX);
+	int sell_price = m_world->GetConfig().SELL_PRICE.Get();
+	organism->SellValue(send_value, search_label, sell_price);
+	return true;
+}
+
+bool cHardwareCPU::Inst_Buy(cAvidaContext& ctx)
+{
+	int search_label = GetLabel().AsInt(3) % MARKET_SIZE;
+	int buy_price = m_world->GetConfig().BUY_PRICE.Get();
+	GetRegister(REG_BX) = organism->BuyValue(search_label, buy_price);
+	return true;
+}
+
 bool cHardwareCPU::Inst_Send(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_BX);

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/cpu/cHardwareCPU.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -377,6 +377,8 @@
   bool Inst_TaskPut(cAvidaContext& ctx);
   bool Inst_TaskIO(cAvidaContext& ctx);
   bool Inst_MatchStrings(cAvidaContext& ctx);
+  bool Inst_Sell(cAvidaContext& ctx);
+  bool Inst_Buy(cAvidaContext& ctx);
   bool Inst_Send(cAvidaContext& ctx);
   bool Inst_Receive(cAvidaContext& ctx);
   bool Inst_Sense(cAvidaContext& ctx);

Modified: development/source/cpu/cTestCPUInterface.cc
===================================================================
--- development/source/cpu/cTestCPUInterface.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/cpu/cTestCPUInterface.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -81,6 +81,16 @@
   return m_testcpu->GetReceiveValue();
 }
 
+void cTestCPUInterface::SellValue(const int data, const int label, const int sell_price, const int org_id)
+{
+
+}
+
+int cTestCPUInterface::BuyValue(const int label, const int buy_price)
+{
+	return m_testcpu->GetReceiveValue();
+}
+
 bool cTestCPUInterface::InjectParasite(cOrganism* parent, const cGenome& injected_code)
 {
   return false;

Modified: development/source/cpu/cTestCPUInterface.h
===================================================================
--- development/source/cpu/cTestCPUInterface.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/cpu/cTestCPUInterface.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -48,6 +48,8 @@
   cOrgSinkMessage* NetReceive() { return NULL; } // @DMB - todo: receive message
   bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg) { return false; } // @DMB - todo: validate message
   int ReceiveValue();
+  void SellValue(const int data, const int label, const int sell_price, const int org_id);
+  int BuyValue(const int label, const int buy_price);
   bool InjectParasite(cOrganism* parent, const cGenome& injected_code);
   bool UpdateMerit(double new_merit);
   bool TestOnDivide() { return false; }

Modified: development/source/defs.h
===================================================================
--- development/source/defs.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/defs.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -97,6 +97,8 @@
 const int OUTPUT_BUF_SIZE = 3;
 const int SEND_BUF_SIZE = 3;
 const int RECEIVE_BUF_SIZE = 3;
+#define RECEIVED_MESSAGES_SIZE 10
+const int MARKET_SIZE = 10000;
 
 
 enum tFileType

Modified: development/source/main/cAvidaConfig.h
===================================================================
--- development/source/main/cAvidaConfig.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cAvidaConfig.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -273,6 +273,12 @@
   CONFIG_ADD_VAR(NET_MUT_PROB, double, 0.0, "Message corruption probability");
   CONFIG_ADD_VAR(NET_MUT_TYPE, int, 0, "Type of message corruption.  0 = Random Single Bit, 1 = Always Flip Last");
   CONFIG_ADD_VAR(NET_STYLE, int, 0, "Communication Style.  0 = Random Next, 1 = Receiver Facing");
+
+  CONFIG_ADD_GROUP(BUY_SELL_GROUP, "Buying and Selling Parameters");
+  CONFIG_ADD_VAR(SAVE_RECEIVED, bool, 0, "Enable storage of all inputs bought from other orgs");
+  CONFIG_ADD_VAR(BUY_PRICE, int, 0, "price offered by organisms attempting to buy");
+  CONFIG_ADD_VAR(SELL_PRICE, int, 0, "price offered by organisms attempting to sell");
+
 #endif
   
   void Load(const cString& filename);

Modified: development/source/main/cOrgInterface.h
===================================================================
--- development/source/main/cOrgInterface.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cOrgInterface.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -48,6 +48,8 @@
   virtual cOrgSinkMessage* NetReceive() = 0;
   virtual bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg) = 0;
   virtual int ReceiveValue() = 0;
+  virtual void SellValue(const int data, const int label, const int sell_price, const int org_id) = 0;
+  virtual int BuyValue(const int label, const int buy_price) = 0;
   virtual bool InjectParasite(cOrganism* parent, const cGenome& injected_code) = 0;
   virtual bool UpdateMerit(double new_merit) = 0;
   virtual bool TestOnDivide() = 0;

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cOrganism.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -28,6 +28,7 @@
 #include "cTaskContext.h"
 #include "cTools.h"
 #include "cWorld.h"
+#include "cStats.h"
 
 #include <iomanip>
 
@@ -55,6 +56,7 @@
   , inbox(0)
   , sent(0)
   , m_net(NULL)
+  , received_messages(RECEIVED_MESSAGES_SIZE)
   , is_running(false)
 {
   // Initialization of structures...
@@ -75,6 +77,7 @@
   }
   
   if (m_world->GetConfig().NET_ENABLED.Get()) m_net = new cNetSupport();
+  m_id = m_world->GetStats().GetTotCreatures();
 }
 
 
@@ -113,7 +116,39 @@
   return out_value;
 }
 
+void cOrganism::SellValue(const int data, const int label, const int sell_price)
+{
+	if (sold_items.GetSize() < 10)
+	{
+		assert (m_interface);
+		m_interface->SellValue(data, label, sell_price, m_id);
+		m_world->GetStats().AddMarketItemSold();
+	}
+}
 
+int cOrganism::BuyValue(const int label, const int buy_price)
+{
+	assert (m_interface);
+	const int receive_value = m_interface->BuyValue(label, buy_price);
+	if (receive_value != 0)
+	{
+		// put this value in storage place for recieved values
+		received_messages.Add(receive_value);
+		// update loss of buy_price to merit
+		double cur_merit = GetPhenotype().GetMerit().GetDouble();
+		cur_merit -= buy_price;
+		UpdateMerit(cur_merit);
+		m_world->GetStats().AddMarketItemBought();
+	}
+	return receive_value;
+}
+
+tListNode<tListNode<cSaleItem> >* cOrganism::AddSoldItem(tListNode<cSaleItem>* sold_node)
+{
+	tListNode<tListNode<cSaleItem> >* node_pt = sold_items.PushRear(sold_node);
+	return node_pt;
+}
+
 void cOrganism::DoInput(const int value)
 {
   input_buf.Add(value);
@@ -159,7 +194,11 @@
   output_buf.Add(value);
   tArray<double> res_change(resource_count.GetSize());
   tArray<int> insts_triggered;
-  cTaskContext taskctx(input_buf, output_buf, other_input_list, other_output_list, net_valid, 0);
+
+  tBuffer<int>* received_messages_point = &received_messages;
+  if (!m_world->GetConfig().SAVE_RECEIVED.Get())
+	  received_messages_point = NULL;
+  cTaskContext taskctx(input_buf, output_buf, other_input_list, other_output_list, net_valid, 0, received_messages_point);
   phenotype.TestOutput(ctx, taskctx, send_buf, receive_buf, resource_count, res_change, insts_triggered);
   m_interface->UpdateResources(res_change);
 

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cOrganism.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -52,6 +52,9 @@
 #ifndef tSmartArray_h
 #include "tSmartArray.h"
 #endif
+#ifndef cSaleItem_h
+#include "cSaleItem.h"
+#endif
 
 /**
  * The cOrganism class controls the running and manages all the statistics
@@ -81,6 +84,7 @@
   cMutationRates mut_rates;             // Rate of all possible mutations.
   cLocalMutations mut_info;             // Info about possible mutations;
   cOrgInterface* m_interface;           // Interface back to the population.
+  int m_id;								// unique id for each org, is just the number it was born
 
   // Input and Output with the environment
   int input_pointer;
@@ -88,6 +92,8 @@
   tBuffer<int> output_buf;
   tBuffer<int> send_buf;
   tBuffer<int> receive_buf;
+  tBuffer<int> received_messages;
+  tList<tListNode<cSaleItem> > sold_items;
 
   // Communication
   int sent_value;         // What number is this org sending?
@@ -144,12 +150,15 @@
   void Kaboom(int dist) { assert(m_interface); m_interface->Kaboom(dist);}
   int GetCellID() { assert(m_interface); return m_interface->GetCellID(); }
   int GetDebugInfo() { assert(m_interface); return m_interface->Debug(); }
-
+  int GetID() { return m_id; }
   bool GetSentActive() { return sent_active; }
   void SendValue(int value) { sent_active = true; sent_value = value; }
   int RetrieveSentValue() { sent_active = false; return sent_value; }
   int ReceiveValue();
-
+  void SellValue(const int data, const int label, const int sell_price);
+  int BuyValue(const int label, const int buy_price);
+  tListNode<tListNode<cSaleItem> >* AddSoldItem(tListNode<cSaleItem>* );
+  tList<tListNode<cSaleItem> >* GetSoldItems() { return &sold_items; }
   void UpdateMerit(double new_merit) { assert(m_interface); m_interface->UpdateMerit(new_merit); }
   
   // Input & Output Testing

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cPopulation.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -78,6 +78,7 @@
   
   cell_array.Resize(num_cells);
   resource_count.ResizeSpatialGrids(world_x, world_y);
+  market.Resize(MARKET_SIZE);
   
   bool bottom_flag, top_flag, right_flag, left_flag;
   for (int cell_id = 0; cell_id < num_cells; cell_id++) {
@@ -451,7 +452,20 @@
   cOrganism* organism = in_cell.GetOrganism();
   cGenotype* genotype = organism->GetGenotype();
   m_world->GetStats().RecordDeath();
-  
+
+  tList<tListNode<cSaleItem> >* sold_items = organism->GetSoldItems();
+  if (sold_items)
+  {
+	  tListIterator<tListNode<cSaleItem> > sold_it(*sold_items);
+	  tListNode<cSaleItem> * test_node;
+
+	  while ( (test_node = sold_it.Next()) != NULL)
+	  {
+		tListIterator<cSaleItem> market_it(market[test_node->data->GetLabel()]);
+		market_it.Set(test_node);
+		delete market_it.Remove();
+	  }
+  }
   // Do the lineage handling
   if (m_world->GetConfig().LOG_LINEAGES.Get()) { m_world->GetClassificationManager().RemoveLineageOrganism(organism); }
   
@@ -519,7 +533,82 @@
   // @SLG my prediction = 92% and, 28 get equals
 }
 
+void cPopulation::AddSellValue(const int data, const int label, const int sell_price, const int org_id, const int cell_id)
+{
+	// find list under appropriate label, labels more than 8 nops long are simply the same
+	// as a smaller label modded by the market size
+	//int pos = label % market.GetSize();
 
+	//// id of genotype currently residing in cell that seller live(d) in compared to 
+	//// id of genotype of actual seller, if different than seller is dead, remove item from list
+	//while ( market[pos].GetSize() > 0 && 
+	//	(!GetCell(market[pos].GetFirst()->GetCellID()).IsOccupied() ||
+	//	GetCell(market[pos].GetFirst()->GetCellID()).GetOrganism()->GetID()
+	//	!= 	market[pos].GetFirst()->GetOrgID()) )
+	//{
+	//	market[pos].Pop();
+	//}
+
+	// create sale item
+	cSaleItem *new_item = new cSaleItem(data, label, sell_price, org_id, cell_id);
+
+	// place into array by label, array is big enough for labels up to 8 nops long
+	tListNode<cSaleItem>* sell_node = market[label].PushRear(new_item);
+	tListNode<tListNode<cSaleItem> >* org_node = GetCell(cell_id).GetOrganism()->AddSoldItem(sell_node);
+	sell_node->data->SetNodePtr(org_node);
+
+	//:7 for Kolby
+}
+
+int cPopulation::BuyValue(const int label, const int buy_price, const int cell_id)
+{
+	// find list under appropriate label, labels more than 8 nops long are simply the same
+	// as a smaller label modded by the market size
+	//int pos = label % market.GetSize();
+
+	//// id of genotype currently residing in cell that seller live(d) in compared to 
+	//// id of genotype of actual seller, if different than seller is dead, remove item from list
+	//while ( market[pos].GetSize() > 0 && 
+	//	(!GetCell(market[pos].GetFirst()->GetCellID()).IsOccupied() ||
+	//	GetCell(market[pos].GetFirst()->GetCellID()).GetOrganism()->GetID()
+	//	!= 	market[pos].GetFirst()->GetOrgID()) )
+	//{
+	//	market[pos].Pop();
+	//}
+
+	// if there's nothing in the list don't bother with rest
+	if (market[label].GetSize() <= 0)
+		return 0;
+
+	// if the sell price is higher than we're willing to pay no purchase made
+	if (market[label].GetFirst()->GetPrice() > buy_price)
+		return 0;
+
+	// if the buy price is higher than buying org's current merit no purchase made
+	if (GetCell(cell_id).GetOrganism()->GetPhenotype().GetMerit().GetDouble() < buy_price)
+		return 0;
+
+	// otherwise transaction should be completed!
+	cSaleItem* chosen = market[label].Pop();
+	tListIterator<tListNode<cSaleItem> > sold_it(*GetCell(chosen->GetCellID()).GetOrganism()->GetSoldItems());
+	sold_it.Set(chosen->GetNodePtr());
+	sold_it.Remove();
+
+	// first update sellers merit
+	double cur_merit = GetCell(chosen->GetCellID()).GetOrganism()->GetPhenotype().GetMerit().GetDouble();
+	cur_merit += buy_price;
+	
+	GetCell(chosen->GetCellID()).GetOrganism()->UpdateMerit(cur_merit);
+	
+	// next remove sold item from list in market 
+	//market[pos].Remove(chosen);
+
+
+	// finally return recieve value, buyer merit will be updated if return a valid value here
+	int receive_value = chosen->GetData();
+	return receive_value;
+}
+
 // CompeteDemes  probabilistically copies demes into the next generation
 // based on their fitness. How deme fitness is estimated is specified by 
 // competition_type input argument as by the birth count (1), average
@@ -999,6 +1088,12 @@
 
 void cPopulation::UpdateOrganismStats()
 {
+	if (m_world->GetStats().GetUpdate() % 100 == 0)
+	{
+		int market_tot = 0;
+		for (int i=0; i<10000; i++)
+			market_tot += market[i].GetSize();
+	}
   // Loop through all the cells getting stats and doing calculations
   // which must be done on a creature by creature basis.
   

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cPopulation.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -48,6 +48,7 @@
 class cOrganism;
 class cPopulationCell;
 class cSchedule;
+class cSaleItem;
 
 
 class cPopulation
@@ -59,6 +60,7 @@
   tArray<cPopulationCell> cell_array;  // Local cells composing the population
   cResourceCount resource_count;       // Global resources available
   cBirthChamber birth_chamber;         // Global birth chamber.
+  tArray<tList<cSaleItem> > market;   // list of lists of items for sale, each list goes with 1 label
 
   // Data Tracking...
   tList<cPopulationCell> reaper_queue; // Death order in some mass-action runs
@@ -129,7 +131,8 @@
   // Deactivate an organism in the population (required for deactivations)
   void KillOrganism(cPopulationCell& in_cell);
   void Kaboom(cPopulationCell& in_cell, int distance=0);
-
+  void AddSellValue(const int data, const int label, const int sell_price, const int org_id, const int cell_id);
+  int BuyValue(const int label, const int buy_price, const int cell_id);
   // Deme-related methods
   void CompeteDemes(int competition_type);
   void ResetDemes();

Modified: development/source/main/cPopulationInterface.cc
===================================================================
--- development/source/main/cPopulationInterface.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cPopulationInterface.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -172,6 +172,17 @@
   return 0;
 }
 
+void cPopulationInterface::SellValue(const int data, const int label, const int sell_price, const int org_id)
+{
+	m_world->GetPopulation().AddSellValue(data, label, sell_price, org_id, m_cell_id);
+}
+
+int cPopulationInterface::BuyValue(const int label, const int buy_price)
+{
+	int value = m_world->GetPopulation().BuyValue(label, buy_price, m_cell_id);
+	return value;
+}
+
 bool cPopulationInterface::InjectParasite(cOrganism* parent, const cGenome& injected_code)
 {
   assert(parent != NULL);

Modified: development/source/main/cPopulationInterface.h
===================================================================
--- development/source/main/cPopulationInterface.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cPopulationInterface.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -57,6 +57,8 @@
   cOrgSinkMessage* NetReceive();
   bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg);
   int ReceiveValue();
+  void SellValue(const int data, const int label, const int sell_price, const int org_id);
+  int BuyValue(const int label, const int buy_price);
   bool InjectParasite(cOrganism* parent, const cGenome& injected_code);
   bool UpdateMerit(double new_merit);
   bool TestOnDivide();

Modified: development/source/main/cStats.cc
===================================================================
--- development/source/main/cStats.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cStats.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -83,6 +83,10 @@
   , tot_lineages(0)
   , tot_executed(0)
   , last_update(0)
+  , num_bought(0)
+  , num_sold(0)
+  , num_used(0)
+  , num_own_used(0)
 {
   task_cur_count.Resize( m_world->GetNumTasks() );
   task_last_count.Resize( m_world->GetNumTasks() );
@@ -828,3 +832,19 @@
   }
   df.WriteRaw("];");
 }
+
+void cStats::PrintMarketData(const cString& filename)
+{
+	cDataFile& df = m_world->GetDataFile(filename);
+
+	df.WriteComment( "Avida market data\n" );
+	df.WriteComment("cumulative totals since the last update data was printed" );
+	df.WriteTimeStamp();
+	df.Write( GetUpdate(), "update" );
+	df.Write( num_bought, "num bought" );
+	df.Write( num_sold, "num sold" );
+	df.Write(num_used, "num used" );
+	df.Write(num_own_used, "num own used" );
+	num_bought = num_sold = num_used = num_own_used = 0;
+df.Endl();
+}

Modified: development/source/main/cStats.h
===================================================================
--- development/source/main/cStats.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cStats.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -210,6 +210,12 @@
   // State variables
   int last_update;
 
+  // Stats for market econ
+  int num_bought;
+  int num_sold;
+  int num_used;
+  int num_own_used;
+
   cStats(); // @not_implemented
   cStats(const cStats&); // @not_implemented
   cStats& operator=(const cStats&); // @not_implemented
@@ -427,6 +433,12 @@
   void SetReactionName(int id, const cString & name) { reaction_names[id] = name; }
   void SetResourceName(int id, const cString & name) { resource_names[id] = name; }
 
+  //market info
+  void AddMarketItemBought() { num_bought++;}
+  void AddMarketItemSold() { num_sold++; }
+  void AddMarketItemUsed() { num_used++; }
+  void AddMarketOwnItemUsed() { num_own_used++; }
+
   // Information retrieval section...
 
   int GetNumBirths() const          { return num_births; }
@@ -464,6 +476,12 @@
   const tArray<double> & GetReactions() const { return reaction_count; }
   const tArray<double> & GetResources() const { return resource_count; }
 
+  // market info
+  int GetMarketNumBought() const { return num_bought; }
+  int GetMarketNumSold() const { return num_sold; }
+  int GetMarketNumUsed() const { return num_used; }
+  int GetMarketNumOwnUsed() const { return num_own_used; }
+
   double GetAveReproRate() const  { return sum_repro_rate.Average(); }
 
   double GetAveMerit() const      { return sum_merit.Average(); }
@@ -539,6 +557,7 @@
   void PrintMutationRateData(const cString& filename);
   void PrintInstructionData(const cString& filename);
   void PrintGenotypeMap(const cString& filename);
+  void PrintMarketData(const cString& filename);
 };
 
 

Modified: development/source/main/cTaskContext.h
===================================================================
--- development/source/main/cTaskContext.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cTaskContext.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -28,13 +28,16 @@
   const tList<tBuffer<int> >& other_output_buffers;
   bool net_valid;
   int net_completed;
+  tBuffer<int>* received_messages;
   int logic_id;
 
 public:
   cTaskContext(const tBuffer<int>& inputs, const tBuffer<int>& outputs, const tList<tBuffer<int> >& other_inputs,
-               const tList<tBuffer<int> >& other_outputs, bool in_net_valid, int in_net_completed)
+               const tList<tBuffer<int> >& other_outputs, bool in_net_valid, int in_net_completed, 
+			   tBuffer<int>* in_received_messages=NULL)
     : input_buffer(inputs), output_buffer(outputs), other_input_buffers(other_inputs),
-    other_output_buffers(other_outputs), net_valid(in_net_valid), net_completed(in_net_completed), logic_id(0)
+    other_output_buffers(other_outputs), net_valid(in_net_valid), net_completed(in_net_completed), 
+	received_messages(in_received_messages), logic_id(0)
   {
   }
 };

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cTaskLib.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -1739,69 +1739,96 @@
 		return 0;
 
 	temp_buf.Pop(); // pop the signal value off of the buffer
-	if (temp_buf.GetNumStored() == 0)
-		return 0;
 
 	const cString & string_to_match = task_array[cur_task]->GetInfo();
-	int max_num_matched = 0;
-	//for (int i=0; i<temp_buf.GetNumStored(); i++)
-	//{
-		const int test_output = temp_buf[0];
-		int num_matched = 0;
-		int string_index;
+	int string_index;
+	int num_matched = 0;
+	int test_output, max_num_matched = 0;
 
+	if (temp_buf.GetNumStored() > 0)
+	{
+		test_output = temp_buf[0];
+	
 		for (int j=0; j<string_to_match.GetSize(); j++)
 		{	
 			string_index=string_to_match.GetSize()-j-1;		// start with last char in string
 			int k = 1 << j;
 			if ((string_to_match[string_index]=='0' && !(test_output & k)) || (string_to_match[string_index]=='1' && (test_output & k))) 
 				num_matched++;
-		}	
-		if (num_matched > max_num_matched)
-			max_num_matched = num_matched;
-	//}
+		}
+		max_num_matched = num_matched;
+	}
 
+	bool used_received = false;
+	if (ctx->received_messages)
+	{
+		tBuffer<int> received(*(ctx->received_messages));
+		for (int i=0; i<received.GetNumStored(); i++)
+		{
+			test_output = received[i];
+			num_matched = 0;
+			for (int j=0; j<string_to_match.GetSize(); j++)
+			{	
+				string_index=string_to_match.GetSize()-j-1;		// start with last char in string
+				int k = 1 << j;
+				if ((string_to_match[string_index]=='0' && !(test_output & k)) || (string_to_match[string_index]=='1' && (test_output & k))) 
+					num_matched++;
+			}
+			if (num_matched > max_num_matched)
+			{
+				max_num_matched = num_matched;
+				used_received = true;
+			}
+		}
+	}
+
 	double bonus = 0;
 	// return value between 0 & 1 representing the percentage of string that was matched
 	double base_bonus = double(max_num_matched)*2/(double)string_to_match.GetSize() - 1;
+	
 	if (base_bonus > 0)
+	{
 		bonus = pow(base_bonus,2);
-	
+		if (used_received)
+			m_world->GetStats().AddMarketItemUsed();
+		else
+			m_world->GetStats().AddMarketOwnItemUsed();
+	}	
 	return bonus;
 }
 
 double cTaskLib::Task_CommEcho(cTaskContext* ctx) const
 {
-  const int test_output = ctx->output_buffer[0];
-  
-  tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
-  
-  while (buff_it.Next() != NULL) {
-    const tBuffer<int>& cur_buff = *(buff_it.Get());
-    const int buff_size = cur_buff.GetNumStored();
-    for (int i = 0; i < buff_size; i++) {
-      if (test_output == cur_buff[i]) return 1.0;
-    }
-  }
-  
-  return 0.0;
+	const int test_output = ctx->output_buffer[0];
+
+	tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
+
+	while (buff_it.Next() != NULL) {
+		const tBuffer<int>& cur_buff = *(buff_it.Get());
+		const int buff_size = cur_buff.GetNumStored();
+		for (int i = 0; i < buff_size; i++) {
+			if (test_output == cur_buff[i]) return 1.0;
+		}
+	}
+
+	return 0.0;
 }
 
 double cTaskLib::Task_CommNot(cTaskContext* ctx) const
 {
-  const int test_output = ctx->output_buffer[0];
-  
-  tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
-  
-  while (buff_it.Next() != NULL) {
-    const tBuffer<int>& cur_buff = *(buff_it.Get());
-    const int buff_size = cur_buff.GetNumStored();
-    for (int i = 0; i < buff_size; i++) {
-      if (test_output == (0-(cur_buff[i]+1))) return 1.0;
-    }
-  }
-  
-  return 0.0;
+	const int test_output = ctx->output_buffer[0];
+
+	tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
+
+	while (buff_it.Next() != NULL) {
+		const tBuffer<int>& cur_buff = *(buff_it.Get());
+		const int buff_size = cur_buff.GetNumStored();
+		for (int i = 0; i < buff_size; i++) {
+			if (test_output == (0-(cur_buff[i]+1))) return 1.0;
+		}
+	}
+
+	return 0.0;
 }
 
 double cTaskLib::Task_NetSend(cTaskContext* ctx) const

Modified: development/source/main/cTaskLib.h
===================================================================
--- development/source/main/cTaskLib.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/main/cTaskLib.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -20,6 +20,12 @@
 #ifndef tArray_h
 #include "tArray.h"
 #endif
+#ifndef cWorld_h
+#include "cWorld.h"
+#endif
+#ifndef cStats_h
+#include "cStats.h"
+#endif
 
 
 class cString;

Modified: development/source/third-party/trio/example.c
===================================================================
--- development/source/third-party/trio/example.c	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/third-party/trio/example.c	2006-09-11 21:27:27 UTC (rev 942)
@@ -5,7 +5,7 @@
 #include <stdarg.h>
 #include <limits.h>
 #include <math.h>
-#include <unistd.h>
+//#include <unistd.h>
 #include <wchar.h>
 #include "triodef.h"
 #include "strio.h"

Modified: development/source/tools/tList.h
===================================================================
--- development/source/tools/tList.h	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/tools/tList.h	2006-09-11 21:27:27 UTC (rev 942)
@@ -254,7 +254,7 @@
     size++;
   }
   
-  void PushRear(T * _in) {
+  tListNode<T>* PushRear(T * _in) {
     tListNode<T> * new_node = new tListNode<T>;
     new_node->data = _in;
     new_node->next = &root;
@@ -262,6 +262,7 @@
     root.prev->next = new_node;
     root.prev = new_node;
     size++;
+	return new_node;
   }
   
   const T * GetFirst() const { return root.next->data; }

Modified: development/source/utils/hist_map/hist_map.cc
===================================================================
--- development/source/utils/hist_map/hist_map.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/utils/hist_map/hist_map.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -2,12 +2,13 @@
 // sends to standard out a matrix where each row is a histogram of the
 // relevant column.
 
-#include <iostream.h>
+#include <iostream>
 
-#include "../../tools/tMatrix.h"
+#include "tools/tMatrix.h"
 
-#include "../../tools/cString.h"
-#include "../../tools/cFile.h"
+#include "tools/cString.h"
+#include "tools/cFile.h"
+#include "tools/cInitFile.h"
 
 
 using namespace std;

Modified: development/source/utils/hist_map/hist_map2.cc
===================================================================
--- development/source/utils/hist_map/hist_map2.cc	2006-09-11 00:24:00 UTC (rev 941)
+++ development/source/utils/hist_map/hist_map2.cc	2006-09-11 21:27:27 UTC (rev 942)
@@ -2,7 +2,7 @@
 // sends to standard out a matrix where each row is a histogram of the
 // relevant column.
 
-#include <iostream.h>
+#include <iostream>
 
 #include "../../tools/tMatrix.h"
 




More information about the Avida-cvs mailing list