[Avida-SVN] r1501 - in branches/energy/source: actions cpu main
beckma24 at myxo.css.msu.edu
beckma24 at myxo.css.msu.edu
Tue Apr 24 22:49:25 PDT 2007
Author: beckma24
Date: 2007-04-25 01:49:25 -0400 (Wed, 25 Apr 2007)
New Revision: 1501
Modified:
branches/energy/source/actions/PrintActions.cc
branches/energy/source/cpu/cHardwareCPU.cc
branches/energy/source/main/cAvidaConfig.h
branches/energy/source/main/cOrganism.h
branches/energy/source/main/cPopulation.cc
branches/energy/source/main/cPopulation.h
Log:
Added config option to turn off logging of sleep intervals.
Modified: branches/energy/source/actions/PrintActions.cc
===================================================================
--- branches/energy/source/actions/PrintActions.cc 2007-04-24 21:18:44 UTC (rev 1500)
+++ branches/energy/source/actions/PrintActions.cc 2007-04-25 05:49:25 UTC (rev 1501)
@@ -299,15 +299,20 @@
df.WriteComment("Cell sleep log");
df.WriteTimeStamp();
- for(int i = 0; i < x*y; i++) {
- tVector<pair<int,int> > cell_log = m_world->GetPopulation().getCellLog(i);
+ if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
+ for(int i = 0; i < x*y; i++) {
+ tVector<pair<int,int> > cell_log = m_world->GetPopulation().getCellSleepLog(i);
- for(int j = 0; j < cell_log.Size(); j++) {
- df.Write(i, "Cell");
- df.Write(cell_log[j].first, "Sleep start time");
- df.Write(cell_log[j].second, "Sleep stop time");
- df.Endl();
+ for(int j = 0; j < cell_log.Size(); j++) {
+ df.Write(i, "Cell");
+ df.Write(cell_log[j].first, "Sleep start time");
+ df.Write(cell_log[j].second, "Sleep stop time");
+ df.Endl();
+ }
}
+ } else {
+ df.WriteComment("Config option LOG_SLEEP_TIMES is turned off.");
+ df.Endl();
}
df.Flush();
}
Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc 2007-04-24 21:18:44 UTC (rev 1500)
+++ branches/energy/source/cpu/cHardwareCPU.cc 2007-04-25 05:49:25 UTC (rev 1501)
@@ -568,25 +568,29 @@
// check avaliable energy first
double energy_req = inst_energy_cost[cur_inst.GetOp()] * (organism->GetPhenotype().GetMerit().GetDouble() / 100.0); //compensate by factor of 100
- //cerr << energy_req <<endl;
+
if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1 && energy_req > 0.0) {
-
if(organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
-
inst_energy_cost[cur_inst.GetOp()] = 0;
//subtract energy used from current org energy.
- organism->GetPhenotype().ReduceEnergy(energy_req);
- cString instName = m_world->GetHardwareManager().GetInstSet().GetName(cur_inst);
- if( instName == cString("sleep") || instName == cString("sleep1") || instName == cString("sleep2") ||
- instName == cString("sleep3") || instName == cString("sleep4"))
- m_world->GetPopulation().AddBeginSleep(organism->GetCellID(),m_world->GetStats().GetUpdate());
-
+ organism->GetPhenotype().ReduceEnergy(energy_req);
} else {
// not enough energy
return false;
}
}
+ // tracking sleeping organisms
+ cString instName = m_world->GetHardwareManager().GetInstSet().GetName(cur_inst);
+ if( instName == cString("sleep") || instName == cString("sleep1") || instName == cString("sleep2") ||
+ instName == cString("sleep3") || instName == cString("sleep4")) {
+ cPopulation& pop = m_world->GetPopulation();
+ if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
+ pop.AddBeginSleep(organism->GetCellID(),m_world->GetStats().GetUpdate());
+ }
+ pop.incNumAsleep();
+ }
+
// If first time cost hasn't been paid off...
if (m_has_ft_costs && inst_ft_cost[cur_inst.GetOp()] > 0) {
inst_ft_cost[cur_inst.GetOp()]--; // dec cost
@@ -619,7 +623,7 @@
// If there is an execution error, execute a random instruction.
if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst(ctx);
#endif /* EXECUTION_ERRORS */
-
+
// Get a pointer to the corresponding method...
int inst_idx = m_inst_set->GetLibFunctionIndex(actual_inst);
@@ -3653,7 +3657,11 @@
}
bool cHardwareCPU::Inst_Sleep(cAvidaContext& ctx) {
- m_world->GetPopulation().AddEndSleep(organism->GetCellID(), m_world->GetStats().GetUpdate());
+ cPopulation& pop = m_world->GetPopulation();
+ if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
+ pop.AddEndSleep(organism->GetCellID(), m_world->GetStats().GetUpdate());
+ }
+ pop.decNumAsleep();
if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
organism->GetPhenotype().ApplyEnergy();
double newMerit = organism->GetPhenotype().RefreshEnergy();
Modified: branches/energy/source/main/cAvidaConfig.h
===================================================================
--- branches/energy/source/main/cAvidaConfig.h 2007-04-24 21:18:44 UTC (rev 1500)
+++ branches/energy/source/main/cAvidaConfig.h 2007-04-25 05:49:25 UTC (rev 1501)
@@ -339,8 +339,9 @@
CONFIG_ADD_VAR(NUM_INST_EXC_BEFORE_0_ENERGY, int, 0, "Number of instructions executed before energy is exhausted.");
CONFIG_ADD_VAR(ENERGY_CAP, int, -1, "Maximun amount of energy that can be stored in an organism. -1 means the cap is set to Max Int"); // TODO - is this done?
CONFIG_ADD_VAR(APPLY_ENERGY_METHOD, int, 0, "When should rewarded energy be applied to current energy.\n0 = on divide\n1 = on completion of task\n2 = on sleep");
+ CONFIG_ADD_VAR(ENERGY_VERBOSE, bool, 0, "Print energy and merit values. 0/1 (off/on)");
+ CONFIG_ADD_VAR(LOG_SLEEP_TIMES, bool, 0, "Log sleep start and end times. 0/1 (off/on)\nWARNING: may use lots of memory.");
// CONFIG_ADD_VAR(FIX_METABOLIC_RATE, bool, 0, "When activated the metabolic rate of all orgiansims are equal. 0/1 (off/on)"); // TODO - check for correctness
- CONFIG_ADD_VAR(ENERGY_VERBOSE, bool, 0, "Print energy and merit values. 0/1 (off/on)");
#endif
Modified: branches/energy/source/main/cOrganism.h
===================================================================
--- branches/energy/source/main/cOrganism.h 2007-04-24 21:18:44 UTC (rev 1500)
+++ branches/energy/source/main/cOrganism.h 2007-04-25 05:49:25 UTC (rev 1501)
@@ -114,6 +114,7 @@
int m_max_executed; // Max number of instruction executed before death.
bool m_is_running; // Does this organism have the CPU?
+ bool m_is_sleeping; // Is this organisms sleeping?
class cNetSupport
{
@@ -167,6 +168,9 @@
void SetRunning(bool in_running) { m_is_running = in_running; }
bool IsRunning() { return m_is_running; }
+
+ void SetSleeping(bool in_sleeping) { m_is_sleeping = in_sleeping; }
+ bool IsSleeping() { return m_is_sleeping; }
// -------- cOrgInterface Methods --------
Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc 2007-04-24 21:18:44 UTC (rev 1500)
+++ branches/energy/source/main/cPopulation.cc 2007-04-25 05:49:25 UTC (rev 1501)
@@ -84,9 +84,8 @@
if(m_world->GetConfig().ENERGY_CAP.Get() == -1)
m_world->GetConfig().ENERGY_CAP.Set(INT_MAX);
- if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+ if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
sleep_log = new tVector<pair<int,int> >[world_x*world_y];
- cout<<"IN\n";
}
// Print out world details
if (world->GetVerbosity() > VERBOSE_NORMAL) {
@@ -104,16 +103,12 @@
cell_array.Resize(num_cells);
resource_count.ResizeSpatialGrids(world_x, world_y);
market.Resize(MARKET_SIZE);
-
- //sleep_log.Resize(num_cells);
-
+
for (int cell_id = 0; cell_id < num_cells; cell_id++) {
int x = cell_id % world_x;
int y = cell_id / world_x;
cell_array[cell_id].Setup(world, cell_id, environment.GetMutRates());
-// sleep_log[cell_id] = tList<pair<int,int> >();
-
// If we're working with a bounded grid, we need to take care of it.
bool bottom_flag = true;
bool top_flag = true;
@@ -562,8 +557,12 @@
cGenotype* genotype = organism->GetGenotype();
m_world->GetStats().RecordDeath();
- if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
- int cellID = in_cell.GetID();
+ int cellID = in_cell.GetID();
+
+ if(GetCell(cellID).GetOrganism()->IsSleeping())
+ numAsleep--;
+
+ if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
if(sleep_log[cellID].Size() > 0) {
pair<int,int> p = sleep_log[cellID][sleep_log[cellID].Size()-1];
if(p.second == -1) {
@@ -572,6 +571,7 @@
}
}
+
tList<tListNode<cSaleItem> >* sold_items = organism->GetSoldItems();
if (sold_items)
{
@@ -2517,16 +2517,10 @@
void cPopulation::AddBeginSleep(int cellID, int start_time) {
sleep_log[cellID].Add(make_pair(start_time,-1));
- numAsleep++;
}
void cPopulation::AddEndSleep(int cellID, int end_time) {
pair<int,int> p = sleep_log[cellID][sleep_log[cellID].Size()-1];
sleep_log[cellID].RemoveAt(sleep_log[cellID].Size()-1);
sleep_log[cellID].Add(make_pair(p.first, end_time));
- numAsleep--;
}
-
-int cPopulation::getNumAsleep() {
- return numAsleep;
-}
Modified: branches/energy/source/main/cPopulation.h
===================================================================
--- branches/energy/source/main/cPopulation.h 2007-04-24 21:18:44 UTC (rev 1500)
+++ branches/energy/source/main/cPopulation.h 2007-04-25 05:49:25 UTC (rev 1501)
@@ -229,10 +229,11 @@
void AddBeginSleep(int cellID, int start_time);
void AddEndSleep(int cellID, int end_time);
- tVector<pair<int,int> > getCellLog(int i) { return sleep_log[i]; }
+ tVector<pair<int,int> > getCellSleepLog(int i) { return sleep_log[i]; }
- int getNumAsleep();
-
+ int getNumAsleep() { return numAsleep; }
+ void incNumAsleep() { numAsleep++; }
+ void decNumAsleep() { numAsleep--; }
};
More information about the Avida-cvs
mailing list