[Avida-SVN] r1931 - in branches/energy/source: cpu main
beckma24 at myxo.css.msu.edu
beckma24 at myxo.css.msu.edu
Mon Aug 13 11:59:01 PDT 2007
Author: beckma24
Date: 2007-08-13 14:59:00 -0400 (Mon, 13 Aug 2007)
New Revision: 1931
Modified:
branches/energy/source/cpu/cHardwareBase.cc
branches/energy/source/cpu/cHardwareCPU.cc
branches/energy/source/cpu/cHardwareCPU.h
branches/energy/source/main/cPhenotype.cc
branches/energy/source/main/cPopulation.cc
branches/energy/source/main/cPopulation.h
branches/energy/source/main/cPopulationCell.cc
Log:
Added energy donate instruction and more energy related outptu
Modified: branches/energy/source/cpu/cHardwareBase.cc
===================================================================
--- branches/energy/source/cpu/cHardwareBase.cc 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/cpu/cHardwareBase.cc 2007-08-13 18:59:00 UTC (rev 1931)
@@ -778,7 +778,7 @@
if(m_world->GetConfig().ENERGY_ENABLED.Get() > 0) {
// TODO: Get rid of magic number. check avaliable energy first
double energy_req = inst_energy_cost[cur_inst.GetOp()] * (organism->GetPhenotype().GetMerit().GetDouble() / 100.0); //compensate by factor of 100
-
+
if (energy_req > 0.0) {
if (organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
inst_energy_cost[cur_inst.GetOp()] = 0;
Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/cpu/cHardwareCPU.cc 2007-08-13 18:59:00 UTC (rev 1931)
@@ -211,12 +211,16 @@
tInstLibEntry<tMethod>("donate-threshgb", &cHardwareCPU::Inst_DonateThreshGreenBeard),
tInstLibEntry<tMethod>("donate-quantagb", &cHardwareCPU::Inst_DonateQuantaThreshGreenBeard),
tInstLibEntry<tMethod>("donate-NUL", &cHardwareCPU::Inst_DonateNULL),
-
+ tInstLibEntry<tMethod>("donate-facing", &cHardwareCPU::Inst_DonateFacing),
+
tInstLibEntry<tMethod>("IObuf-add1", &cHardwareCPU::Inst_IOBufAdd1),
tInstLibEntry<tMethod>("IObuf-add0", &cHardwareCPU::Inst_IOBufAdd0),
tInstLibEntry<tMethod>("rotate-l", &cHardwareCPU::Inst_RotateL),
tInstLibEntry<tMethod>("rotate-r", &cHardwareCPU::Inst_RotateR),
+ tInstLibEntry<tMethod>("rotate-oneL", &cHardwareCPU::Inst_RotateOneL),
+ tInstLibEntry<tMethod>("rotate-oneR", &cHardwareCPU::Inst_RotateOneR),
+
tInstLibEntry<tMethod>("rotate-label", &cHardwareCPU::Inst_RotateLabel),
tInstLibEntry<tMethod>("set-cmut", &cHardwareCPU::Inst_SetCopyMut),
@@ -3052,6 +3056,45 @@
to_org->UpdateMerit(other_merit);
}
+void cHardwareCPU::DoEnergyDonate(cOrganism* to_org)
+{
+ assert(to_org != NULL);
+
+ const double frac_energy_given = m_world->GetConfig().MERIT_GIVEN.Get();
+
+ double cur_energy = organism->GetPhenotype().GetStoredEnergy();
+ double energy_given = cur_energy * frac_energy_given;
+
+ //update energy store and merit of donor
+ organism->GetPhenotype().ReduceEnergy(energy_given);
+ double senderMerit = cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy(), m_world) * organism->GetPhenotype().GetExecutionRatio();
+ organism->UpdateMerit(senderMerit);
+
+ // update energy store and merit of donee
+ to_org->GetPhenotype().ReduceEnergy(-1.0*energy_given);
+ double receiverMerit = cMerit::EnergyToMerit(to_org->GetPhenotype().GetStoredEnergy(), m_world) * to_org->GetPhenotype().GetExecutionRatio();
+ to_org->UpdateMerit(receiverMerit);
+}
+
+bool cHardwareCPU::Inst_DonateFacing(cAvidaContext& ctx) {
+ if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
+ return false;
+ }
+ organism->GetPhenotype().IncDonates();
+ organism->GetPhenotype().SetIsDonorRand();
+
+ // Get faced neighbor
+ cOrganism * neighbor = organism->GetNeighbor();
+
+ // Donate only if we have found a neighbor.
+ if (neighbor != NULL) {
+ DoEnergyDonate(neighbor);
+
+ neighbor->GetPhenotype().SetIsReceiver();
+ }
+ return true;
+}
+
bool cHardwareCPU::Inst_DonateRandom(cAvidaContext& ctx)
{
@@ -3626,6 +3669,22 @@
return true;
}
+bool cHardwareCPU::Inst_RotateOneL(cAvidaContext& ctx)
+{
+ // If this organism has no neighbors, ignore rotate.
+ if (organism->GetNeighborhoodSize() == 0) return false;
+ organism->Rotate(-1);
+ return true;
+}
+
+bool cHardwareCPU::Inst_RotateOneR(cAvidaContext& ctx)
+{
+ // If this organism has no neighbors, ignore rotate.
+ if (organism->GetNeighborhoodSize() == 0) return false;
+ organism->Rotate(1);
+ return true;
+}
+
/**
Rotate to facing specified by following label
*/
Modified: branches/energy/source/cpu/cHardwareCPU.h
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.h 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/cpu/cHardwareCPU.h 2007-08-13 18:59:00 UTC (rev 1931)
@@ -421,6 +421,7 @@
bool DoSense(cAvidaContext& ctx, int conversion_method, double base);
void DoDonate(cOrganism * to_org);
+ void DoEnergyDonate(cOrganism* to_org);
bool Inst_DonateRandom(cAvidaContext& ctx);
bool Inst_DonateKin(cAvidaContext& ctx);
bool Inst_DonateEditDist(cAvidaContext& ctx);
@@ -429,6 +430,7 @@
bool Inst_DonateThreshGreenBeard(cAvidaContext& ctx);
bool Inst_DonateQuantaThreshGreenBeard(cAvidaContext& ctx);
bool Inst_DonateNULL(cAvidaContext& ctx);
+ bool Inst_DonateFacing(cAvidaContext& ctx);
bool Inst_SearchF(cAvidaContext& ctx);
bool Inst_SearchB(cAvidaContext& ctx);
@@ -441,6 +443,8 @@
bool Inst_RotateL(cAvidaContext& ctx);
bool Inst_RotateR(cAvidaContext& ctx);
+ bool Inst_RotateOneL(cAvidaContext& ctx);
+ bool Inst_RotateOneR(cAvidaContext& ctx);
bool Inst_RotateLabel(cAvidaContext& ctx);
bool Inst_SetCopyMut(cAvidaContext& ctx);
bool Inst_ModCopyMut(cAvidaContext& ctx);
Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/main/cPhenotype.cc 2007-08-13 18:59:00 UTC (rev 1931)
@@ -1382,10 +1382,11 @@
*/
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) {
+ if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0 || // on divide
+ m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) { // on sleep
energy_tobe_applied += cur_energy_bonus;
} else if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1) {
- SetEnergy(energy_store + cur_energy_bonus); //TODO: use SetEnergy
+ SetEnergy(energy_store + cur_energy_bonus);
} else {
cerr<< "Unknown APPLY_ENERGY_METHOD value " << m_world->GetConfig().APPLY_ENERGY_METHOD.Get();
exit(-1);
Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/main/cPopulation.cc 2007-08-13 18:59:00 UTC (rev 1931)
@@ -274,7 +274,7 @@
tArray<cOrganism*> child_array;
tArray<cMerit> merit_array;
-
+
// Update the parent's phenotype.
// This needs to be done before the parent goes into the birth chamber
// or the merit doesn't get passed onto the child correctly
@@ -320,32 +320,32 @@
// Do lineage tracking for the new organisms.
LineageSetupOrganism(child_array[i], parent_organism.GetLineage(),
parent_organism.GetLineageLabel(), parent_genotype);
-
- //By default, store the parent cclade, this may get modified in ActivateOrgansim (@MRR)
- child_array[i]->SetCCladeLabel(parent_organism.GetCCladeLabel());
+
+ //By default, store the parent cclade, this may get modified in ActivateOrgansim (@MRR)
+ child_array[i]->SetCCladeLabel(parent_organism.GetCCladeLabel());
}
-
+
// If we're not about to kill the parent, do some extra work on it.
if (parent_alive == true) {
-
- // Reset inputs and re-calculate merit if required
+
+ // Reset inputs and re-calculate merit if required
if (m_world->GetConfig().RESET_INPUTS_ON_DIVIDE.Get() > 0){
- environment.SetupInputs(ctx, parent_cell.input_array);
+ environment.SetupInputs(ctx, parent_cell.input_array);
int pc_phenotype = m_world->GetConfig().PRECALC_PHENOTYPE.Get();
- if (pc_phenotype){
- cCPUTestInfo test_info;
- cTestCPU* test_cpu = m_world->GetHardwareManager().CreateTestCPU();
- test_info.UseManualInputs(parent_cell.input_array); // Test using what the environment will be
- test_cpu->TestGenome(ctx, test_info, parent_organism.GetHardware().GetMemory()); // Use the true genome
- if (pc_phenotype & 1) // If we must update the merit
+ if (pc_phenotype){
+ cCPUTestInfo test_info;
+ cTestCPU* test_cpu = m_world->GetHardwareManager().CreateTestCPU();
+ test_info.UseManualInputs(parent_cell.input_array); // Test using what the environment will be
+ test_cpu->TestGenome(ctx, test_info, parent_organism.GetHardware().GetMemory()); // Use the true genome
+ if (pc_phenotype & 1) // If we must update the merit
parent_phenotype.SetMerit(test_info.GetTestPhenotype().GetMerit());
if (pc_phenotype & 2) // If we must update the gestation time
parent_phenotype.SetGestationTime(test_info.GetTestPhenotype().GetGestationTime());
parent_phenotype.SetFitness(parent_phenotype.GetMerit().CalcFitness(parent_phenotype.GetGestationTime())); //Update fitness
- delete test_cpu;
- }
- }
- schedule->Adjust(parent_cell.GetID(), parent_phenotype.GetMerit());
+ delete test_cpu;
+ }
+ }
+ schedule->Adjust(parent_cell.GetID(), parent_phenotype.GetMerit());
// In a local run, face the child toward the parent.
const int birth_method = m_world->GetConfig().BIRTH_METHOD.Get();
@@ -516,7 +516,6 @@
}
in_organism->GetPhenotype().SetCurBonusInstCount(num_rewarded_instructions);
}
-
}
// @WRE 2007/07/05 Helper function to take care of side effects of Avidian
@@ -1595,6 +1594,11 @@
df_mut_rates.Write(total_mut_rate.Ave(), "Average deme mutation rate averaged across Demes.");
+ if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+ PrintDemeSpatialEnergyData();
+ PrintDemeSpatialSleepData();
+ }
+
df_fit.Endl();
df_life_fit.Endl();
df_merit.Endl();
@@ -1629,8 +1633,62 @@
df.WriteRaw("];");
}
+// Write spatial energy data to a file that can easily be read into Matlab
+void cPopulation::PrintDemeSpatialEnergyData() const {
+ int cellID = 0;
+ for(int i = 0; i < m_world->GetPopulation().GetNumDemes(); i++) {
+ cString tmpfilename = cStringUtil::Stringf( "deme_spacial_energy_%i.m", i );
+ cDataFile& df = m_world->GetDataFile(tmpfilename);
+ cString UpdateStr = cStringUtil::Stringf( "deme_energy_%07i_", i ) +
+ cStringUtil::Stringf( "_%07i", m_world->GetStats().GetUpdate() ) + " = [ ...";
+ df.WriteRaw(UpdateStr);
+ int gridsize = m_world->GetPopulation().GetDeme(i).GetSize();
+ int xsize = m_world->GetConfig().WORLD_X.Get();
+
+ // write grid to file
+ for (int j = 0; j < gridsize; j++) {
+ cPopulationCell cell = m_world->GetPopulation().GetCell(cellID);
+ if(cell.IsOccupied()) {
+ df.WriteBlockElement(cell.GetOrganism()->GetPhenotype().GetStoredEnergy(), j, xsize);
+ } else {
+ df.WriteBlockElement(0.0, j, xsize);
+ }
+ cellID++;
+ }
+ df.WriteRaw("];");
+ }
+}
+
+// Write spatial energy data to a file that can easily be read into Matlab
+void cPopulation::PrintDemeSpatialSleepData() const {
+ int cellID = 0;
+ for(int i = 0; i < m_world->GetPopulation().GetNumDemes(); i++) {
+ cString tmpfilename = cStringUtil::Stringf( "deme_spacial_sleep_%i.m", i );
+ cDataFile& df = m_world->GetDataFile(tmpfilename);
+ cString UpdateStr = cStringUtil::Stringf( "deme_sleep_%07i_", i ) +
+ cStringUtil::Stringf( "_%07i", m_world->GetStats().GetUpdate() ) + " = [ ...";
+
+ df.WriteRaw(UpdateStr);
+
+ int gridsize = m_world->GetPopulation().GetDeme(i).GetSize();
+ int xsize = m_world->GetConfig().WORLD_X.Get();
+
+ // write grid to file
+ for (int j = 0; j < gridsize; j++) {
+ cPopulationCell cell = m_world->GetPopulation().GetCell(cellID);
+ if(cell.IsOccupied()) {
+ df.WriteBlockElement(cell.GetOrganism()->IsSleeping(), j, xsize);
+ } else {
+ df.WriteBlockElement(0.0, j, xsize);
+ }
+ cellID++;
+ }
+ df.WriteRaw("];");
+ }
+}
+
/**
* This function is responsible for adding an organism to a given lineage,
* and setting the organism's lineage label and the lineage pointer.
@@ -2653,8 +2711,8 @@
cOrganism* new_organism = new cOrganism(m_world, ctx, new_genotype->GetGenome());
- //Coalescense Clade Setup
- new_organism->SetCCladeLabel(-1);
+ //Coalescense Clade Setup
+ new_organism->SetCCladeLabel(-1);
// Set the genotype...
new_organism->SetGenotype(new_genotype);
@@ -2663,14 +2721,14 @@
cPhenotype & phenotype = new_organism->GetPhenotype();
phenotype.SetupInject(new_genotype->GetGenome()); //TODO sets merit to lenght of genotype
- if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
+ if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
double initial_energy = min(m_world->GetConfig().ENERGY_GIVEN_ON_INJECT.Get(), m_world->GetConfig().ENERGY_CAP.Get());
phenotype.SetEnergy(initial_energy);
+ phenotype.SetMerit(cMerit(cMerit::EnergyToMerit(phenotype.GetStoredEnergy(), m_world)));
+ } else {
+ phenotype.SetMerit( cMerit(new_genotype->GetTestMerit(ctx)) );
}
- // BB - Don't need to fix metabolic rate here, only on birth
-
- phenotype.SetMerit( cMerit(new_genotype->GetTestMerit(ctx)) );
-
+
// @CAO are these really needed?
phenotype.SetLinesCopied( new_genotype->GetTestCopiedSize(ctx) );
phenotype.SetLinesExecuted( new_genotype->GetTestExecutedSize(ctx) );
Modified: branches/energy/source/main/cPopulation.h
===================================================================
--- branches/energy/source/main/cPopulation.h 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/main/cPopulation.h 2007-08-13 18:59:00 UTC (rev 1931)
@@ -184,6 +184,8 @@
void SpawnDeme(int deme1_id, int deme2_id=-1);
void PrintDemeStats();
void PrintDemeSpatialResData( cResourceCount res, const int i, const int deme_id);
+ void PrintDemeSpatialEnergyData() const;
+ void PrintDemeSpatialSleepData() const;
// Print donation stats
void PrintDonationStats();
Modified: branches/energy/source/main/cPopulationCell.cc
===================================================================
--- branches/energy/source/main/cPopulationCell.cc 2007-08-13 18:05:55 UTC (rev 1930)
+++ branches/energy/source/main/cPopulationCell.cc 2007-08-13 18:59:00 UTC (rev 1931)
@@ -192,9 +192,8 @@
double uptake_energy = UptakeCellEnergy(1.0);
if(uptake_energy != 0.0) {
// update energy and merit
- cPhenotype& phen = organism->GetPhenotype();
- phen.ReduceEnergy(-1.0 * uptake_energy);
- phen.SetMerit(cMerit(cMerit::EnergyToMerit(phen.GetStoredEnergy(), m_world)));
+ organism->GetPhenotype().ReduceEnergy(-1.0 * uptake_energy);
+ organism->GetPhenotype().SetMerit(cMerit(cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy(), m_world) * organism->GetPhenotype().GetExecutionRatio()));
}
}
}
More information about the Avida-cvs
mailing list