[Avida-SVN] r2968 - in development/source: actions main
mmcgill at myxo.css.msu.edu
mmcgill at myxo.css.msu.edu
Mon Nov 24 11:43:19 PST 2008
Author: mmcgill
Date: 2008-11-24 14:43:19 -0500 (Mon, 24 Nov 2008)
New Revision: 2968
Modified:
development/source/actions/PopulationActions.cc
development/source/main/cDeme.cc
development/source/main/cDeme.h
Log:
added new cCompeteDemes subclass, and fixed a bug in cDeme::CalculateTotalEnergy() that incorrectly caused energy resource in a cell to be cleared
Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc 2008-11-24 19:35:55 UTC (rev 2967)
+++ development/source/actions/PopulationActions.cc 2008-11-24 19:43:19 UTC (rev 2968)
@@ -1562,7 +1562,50 @@
}
};
+class cActionCompeteDemesByTaskCountAndEfficiency : public cAbstractCompeteDemes {
+private:
+ double _initial_deme_energy;
+ int _task_num; // the task num to use when calculating fitness,
+ // defaults to 0 (the first task)
+public:
+ cActionCompeteDemesByTaskCountAndEfficiency(cWorld* world, const cString& args)
+ : cAbstractCompeteDemes(world, args) {
+ cString largs(args);
+ if (largs.GetSize() == 0) {
+ cerr << "CompeteDemesByTaskCountAndEfficiency must be given an initial deme energy amount" << endl;
+ exit(1);
+ }
+ _initial_deme_energy = largs.PopWord().AsDouble();
+ cout << "initial deme energy = " << _initial_deme_energy << endl;
+ assert(_initial_deme_energy > 0);
+
+ if (largs.GetSize() > 1) {
+ _task_num = largs.PopWord().AsInt();
+ assert(_task_num >= 0);
+ assert(_task_num < m_world->GetEnvironment().GetNumTasks());
+ } else {
+ _task_num = 0;
+ }
+ }
+ ~cActionCompeteDemesByTaskCountAndEfficiency() {}
+
+ static const cString GetDescription() {
+ return "Competes demes according to the number of times a given task has been completed within that deme and the efficiency with which it was done";
+ }
+
+ virtual double Fitness(const cDeme& deme) {
+ double energy_used = _initial_deme_energy - deme.CalculateTotalEnergy();
+ double fitness =
+ pow(deme.GetCurTaskExeCount()[_task_num] * (_initial_deme_energy/energy_used),2);
+ if (fitness == 0.0) fitness = 0.1;
+ cout << "Deme " << deme.GetID() << ": used " << energy_used << " energy"
+ << " fitness=" << fitness << endl;
+ return fitness;
+ }
+};
+
+
/*! Send an artificial flash to a single organism in each deme in the population
at a specified period.
@@ -2445,6 +2488,7 @@
action_lib->Register<cActionSwapCells>("SwapCells");
action_lib->Register<cActionCompeteDemesByTaskCount>("CompeteDemesByTaskCount");
+ action_lib->Register<cActionCompeteDemesByTaskCountAndEfficiency>("CompeteDemesByTaskCountAndEfficiency");
// @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
action_lib->Register<cActionInject>("inject");
Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc 2008-11-24 19:35:55 UTC (rev 2967)
+++ development/source/main/cDeme.cc 2008-11-24 19:43:19 UTC (rev 2968)
@@ -446,6 +446,25 @@
}
}
+double cDeme::GetCellEnergy(int absolute_cell_id) const {
+ assert(cell_ids[0] <= absolute_cell_id);
+ assert(absolute_cell_id <= cell_ids[cell_ids.GetSize()-1]);
+
+ double total_energy = 0.0;
+ int relative_cell_id = GetRelativeCellID(absolute_cell_id);
+ tArray<double> cell_resources = deme_resource_count.GetCellResources(relative_cell_id);
+
+ // sum all energy resources
+ for(int i = 0; i < energy_res_ids.GetSize(); i++) {
+ if(cell_resources[energy_res_ids[i]] > 0.0) {
+ total_energy += cell_resources[energy_res_ids[i]];
+ cell_resources[energy_res_ids[i]] *= -1.0;
+ }
+ }
+
+ return total_energy;
+}
+
double cDeme::GetAndClearCellEnergy(int absolute_cell_id) {
assert(cell_ids[0] <= absolute_cell_id);
assert(absolute_cell_id <= cell_ids[cell_ids.GetSize()-1]);
@@ -577,7 +596,7 @@
cPhenotype& phenotype = organism->GetPhenotype();
energy_sum += phenotype.GetStoredEnergy();
} else {
- double energy_in_cell = cell.UptakeCellEnergy(1.0);
+ double energy_in_cell = GetCellEnergy(cellid);
energy_sum += energy_in_cell * m_world->GetConfig().FRAC_ENERGY_TRANSFER.Get();
}
}
Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h 2008-11-24 19:35:55 UTC (rev 2967)
+++ development/source/main/cDeme.h 2008-11-24 19:43:19 UTC (rev 2968)
@@ -234,6 +234,7 @@
void SetDemeResourceCount(const cResourceCount in_res) { deme_resource_count = in_res; }
void ResizeSpatialGrids(const int in_x, const int in_y) { deme_resource_count.ResizeSpatialGrids(in_x, in_y); }
void ModifyDemeResCount(const tArray<double> & res_change, const int absolute_cell_id);
+ double GetCellEnergy(int absolute_cell_id) const;
double GetAndClearCellEnergy(int absolute_cell_id);
void GiveBackCellEnergy(int absolute_cell_id, double value);
void SetupDemeRes(int id, cResource * res, int verbosity);
More information about the Avida-cvs
mailing list