[Avida-SVN] r1503 - in branches/energy/source: cpu main
beckma24 at myxo.css.msu.edu
beckma24 at myxo.css.msu.edu
Thu Apr 26 13:53:20 PDT 2007
Author: beckma24
Date: 2007-04-26 16:53:19 -0400 (Thu, 26 Apr 2007)
New Revision: 1503
Modified:
branches/energy/source/cpu/cHardwareCPU.cc
branches/energy/source/main/cOrganism.cc
branches/energy/source/main/cPhenotype.cc
branches/energy/source/main/cPhenotype.h
branches/energy/source/main/cPopulation.cc
Log:
Fixed bugs:
*caused incorrect counting of sleeping organisms
*applied energy bonus twice
Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc 2007-04-25 12:54:42 UTC (rev 1502)
+++ branches/energy/source/cpu/cHardwareCPU.cc 2007-04-26 20:53:19 UTC (rev 1503)
@@ -40,6 +40,7 @@
#include "cOrganism.h"
#include "cPhenotype.h"
#include "cPopulation.h"
+#include "cPopulationCell.h"
#include "cStringUtil.h"
#include "cTestCPU.h"
#include "cWorldDriver.h"
@@ -573,24 +574,28 @@
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);
- } else {
- // not enough energy
- return false;
- }
- }
-
- // tracking sleeping organisms
+ organism->GetPhenotype().ReduceEnergy(energy_req);
+
+
+ // tracking sleeping organisms
cString instName = m_world->GetHardwareManager().GetInstSet().GetName(cur_inst);
+ int cellID = organism->GetCellID();
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.AddBeginSleep(cellID,m_world->GetStats().GetUpdate());
}
- pop.incNumAsleep();
+ pop.GetCell(cellID).GetOrganism()->SetSleeping(true);
+ pop.incNumAsleep(); //TODO - Fix me: this functions get called repeatedly
}
-
+
+ } else {
+ // not enough energy
+ return false;
+ }
+ }
+
// 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
@@ -3661,11 +3666,15 @@
if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
pop.AddEndSleep(organism->GetCellID(), m_world->GetStats().GetUpdate());
}
+ int cellID = organism->GetCellID();
+ pop.GetCell(cellID).GetOrganism()->SetSleeping(false); //this instruction get executed at the end of a sleep cycle
pop.decNumAsleep();
if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
- organism->GetPhenotype().ApplyEnergy();
- double newMerit = organism->GetPhenotype().RefreshEnergy();
- organism->GetOrgInterface().UpdateMerit(newMerit);
+ organism->GetPhenotype().RefreshEnergy();
+ double newMerit = organism->GetPhenotype().ApplyToEnergyStore();
+ if(newMerit != -1) {
+ organism->GetOrgInterface().UpdateMerit(newMerit);
+ }
}
return true;
}
Modified: branches/energy/source/main/cOrganism.cc
===================================================================
--- branches/energy/source/main/cOrganism.cc 2007-04-25 12:54:42 UTC (rev 1502)
+++ branches/energy/source/main/cOrganism.cc 2007-04-26 20:53:19 UTC (rev 1503)
@@ -210,11 +210,10 @@
m_phenotype.TestOutput(ctx, taskctx, resource_count, res_change, insts_triggered);
if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1) {
- double newMerit = m_phenotype.RefreshEnergy();
+ m_phenotype.RefreshEnergy();
+ double newMerit = m_phenotype.ApplyToEnergyStore();
if(newMerit != -1) {
-// std::cerr<<GetCellID()<<" "<<newMerit<<std::endl;
m_interface->UpdateMerit(newMerit);
-// m_world->GetPopulation().UpdateMerit(GetCellID(), newMerit);
}
}
Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc 2007-04-25 12:54:42 UTC (rev 1502)
+++ branches/energy/source/main/cPhenotype.cc 2007-04-26 20:53:19 UTC (rev 1503)
@@ -904,7 +904,7 @@
/**
Credit organism with energy reward, but only update energy store if APPLY_ENERGY_METHOD = "no task completion" (1)
*/
-double cPhenotype::RefreshEnergy() {
+void cPhenotype::RefreshEnergy() {
if(cur_energy_bonus > 0) {
if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0 || m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
energy_tobe_applied += cur_energy_bonus;
@@ -915,12 +915,15 @@
exit(-1);
}
cur_energy_bonus = 0;
-
- return min(100 * energy_store / (m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get()), (double) m_world->GetConfig().ENERGY_CAP.Get());
}
- return -1;
}
+double cPhenotype::ApplyToEnergyStore() {
+ energy_store += energy_tobe_applied;
+ energy_tobe_applied = 0.0;
+ return min(100 * energy_store / (m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get()), (double) m_world->GetConfig().ENERGY_CAP.Get());
+}
+
void cPhenotype::DecayAllPromoterRegulation()
{
for ( int i=0; i<cur_promoter_weights.GetSize(); i++)
Modified: branches/energy/source/main/cPhenotype.h
===================================================================
--- branches/energy/source/main/cPhenotype.h 2007-04-25 12:54:42 UTC (rev 1502)
+++ branches/energy/source/main/cPhenotype.h 2007-04-26 20:53:19 UTC (rev 1503)
@@ -358,8 +358,8 @@
bool& ChildFertile() { assert(initialized == true); return child_fertile; }
bool& IsMultiThread() { assert(initialized == true); return is_multi_thread; }
- double RefreshEnergy();
- double ApplyEnergy() { energy_store += energy_tobe_applied + cur_energy_bonus; }
+ void RefreshEnergy();
+ double ApplyToEnergyStore();
};
Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc 2007-04-25 12:54:42 UTC (rev 1502)
+++ branches/energy/source/main/cPopulation.cc 2007-04-26 20:53:19 UTC (rev 1503)
@@ -345,7 +345,8 @@
//apply energy if APPLY_ENERGY_METHOD is set to "no divide" (0)
if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0) {
- parent_phenotype.ApplyEnergy();
+ parent_phenotype.RefreshEnergy();
+ parent_phenotype.ApplyToEnergyStore();
}
@@ -559,9 +560,10 @@
int cellID = in_cell.GetID();
- if(GetCell(cellID).GetOrganism()->IsSleeping())
- numAsleep--;
-
+ if(GetCell(cellID).GetOrganism()->IsSleeping()) {
+ GetCell(cellID).GetOrganism()->SetSleeping(false);
+ decNumAsleep();
+ }
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];
More information about the Avida-cvs
mailing list