[Avida-SVN] r2551 - branches/energy/source/cpu
connel42 at myxo.css.msu.edu
connel42 at myxo.css.msu.edu
Thu Apr 24 07:47:27 PDT 2008
Author: connel42
Date: 2008-04-24 10:47:26 -0400 (Thu, 24 Apr 2008)
New Revision: 2551
Modified:
branches/energy/source/cpu/cHardwareCPU.cc
branches/energy/source/cpu/cHardwareCPU.h
Log:
* added new supermove instruction
Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc 2008-04-24 01:59:14 UTC (rev 2550)
+++ branches/energy/source/cpu/cHardwareCPU.cc 2008-04-24 14:47:26 UTC (rev 2551)
@@ -255,6 +255,7 @@
tInstLibEntry<tMethod>("exploit", &cHardwareCPU::Inst_Exploit),
tInstLibEntry<tMethod>("explore", &cHardwareCPU::Inst_Explore),
tInstLibEntry<tMethod>("movetarget", &cHardwareCPU::Inst_MoveTarget),
+ tInstLibEntry<tMethod>("supermove", &cHardwareCPU::Inst_SuperMove),
tInstLibEntry<tMethod>("if-target", &cHardwareCPU::Inst_IfTarget),
tInstLibEntry<tMethod>("if-pheromone", &cHardwareCPU::Inst_IfPheromone),
tInstLibEntry<tMethod>("drop-pheromone", &cHardwareCPU::Inst_DropPheromone),
@@ -3283,6 +3284,158 @@
+//TODO: implement and comment
+bool cHardwareCPU::Inst_SuperMove(cAvidaContext& ctx)
+{
+ int num_rotations = 0;
+ float phero_amount = 0;
+ float max_pheromone = 0;
+//MEGA
+
+ cPopulation& pop = m_world->GetPopulation();
+ int cellid = organism->GetCellID();
+ cPopulationCell& mycell = pop.GetCell(cellid);
+ cDeme &deme = pop.GetDeme(pop.GetCell(cellid).GetDemeID());
+ cResourceCount deme_resource_count = deme.GetDemeResourceCount();
+ int relative_cell_id = deme.GetRelativeCellID(cellid);
+ tArray<double> cell_resources = deme_resource_count.GetCellResources(relative_cell_id);
+
+ int fromcellID, destcellID;
+ int cell_data;
+
+ // Get stepsize. Currently, all moves are one cell regardless of stepsize.
+ // This could be changed in the future.
+ const int stepsize = m_world->GetConfig().BIOMIMETIC_MOVEMENT_STEP.Get();
+
+
+ // Set num_rotations to a random number for explore -- lowest priority
+ const int num_neighbors = organism->GetNeighborhoodSize();
+ num_rotations = ctx.GetRandom().GetUInt(num_neighbors);
+
+
+ // Find the neighbor with highest pheromone -- medium priority
+ for(int i = 0; i < mycell.ConnectionList().GetSize(); i++) {
+
+ phero_amount = 0;
+
+ for (int j = 0; j < deme_resource_count.GetSize(); j++) {
+ if(strcmp(deme_resource_count.GetResName(j), "pheromone") == 0) {
+ phero_amount = cell_resources[j];
+ }
+ }
+
+ if(phero_amount > max_pheromone) {
+ num_rotations = i;
+ max_pheromone = phero_amount;
+ }
+
+ mycell.ConnectionList().CircNext();
+ }
+
+ // Find if any neighbor is a target -- highest priority
+ for(int i = 0; i < mycell.ConnectionList().GetSize(); i++) {
+ cell_data = m_world->GetPopulation().GetCell(organism->GetCellID()).GetCellData();
+
+ if(cell_data > 0) {
+ num_rotations = i;
+ }
+
+ mycell.ConnectionList().CircNext();
+ }
+
+ // Rotate until we face the neighbor with a target.
+ // If there was no winner, just move forward.
+ for(int i = 0; i < num_rotations; i++) {
+ mycell.ConnectionList().CircNext();
+ }
+
+ // Move to the faced cell
+ if(stepsize > 0) {
+ fromcellID = organism->GetCellID();
+
+ if(fromcellID == -1) {
+ return false;
+ }
+
+ destcellID = pop.GetCell(fromcellID).GetCellFaced().GetID();
+
+ /*********************/
+ // TEMP. Remove once movement tasks are implemented.
+ if(pop.GetCell(fromcellID).GetCellData() < pop.GetCell(destcellID).GetCellData()) { // move up gradient
+ organism->SetGradientMovement(1.0);
+ } else if(pop.GetCell(fromcellID).GetCellData() == pop.GetCell(destcellID).GetCellData()) {
+ organism->SetGradientMovement(0.0);
+ } else { // move down gradient
+ organism->SetGradientMovement(-1.0);
+ }
+ /*********************/
+
+ pop.SwapCells(pop.GetCell(fromcellID),pop.GetCell(destcellID));
+ pop.MoveOrganisms(ctx, pop.GetCell(fromcellID), pop.GetCell(destcellID));
+
+ m_world->GetStats().Move(*organism);
+
+/*
+ // If organism is dropping pheromones, mark the appropriate cell
+ if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
+ (organism->GetPheromoneStatus() == true) ) {
+
+ const double pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+ const int drop_mode = m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+
+ cDeme &deme = pop.GetDeme(pop.GetCell(organism->GetCellID()).GetDemeID());
+
+ if(drop_mode == 0) {
+ deme.AddPheromone(fromcellID, pher_amount/2);
+ deme.AddPheromone(destcellID, pher_amount/2);
+ } else if(drop_mode == 1) {
+ deme.AddPheromone(fromcellID, pher_amount);
+ }
+ else if(drop_mode == 2) {
+ deme.AddPheromone(destcellID, pher_amount);
+ }
+
+ } //End laying pheromone
+*/
+
+
+ // Write some logging information if LOG_PHEROMONE is set. This is done
+ // out here so that non-pheromone moves are recorded.
+ if( (m_world->GetConfig().LOG_PHEROMONE.Get() == 1) &&
+ (m_world->GetStats().GetUpdate() >= m_world->GetConfig().MOVETARGET_LOG_START.Get()) ) {
+ cString tmpfilename = cStringUtil::Stringf("movelog.dat");
+ cDataFile& df = m_world->GetDataFile(tmpfilename);
+
+ int rel_srcid = deme.GetRelativeCellID(fromcellID);
+ int rel_destid = deme.GetRelativeCellID(destcellID);
+ double pher_amount;
+ const int drop_mode = m_world->GetConfig().PHEROMONE_DROP_MODE.Get();
+
+ // By columns: update ID, org ID, source cell (relative), destination cell (relative), amount dropped, drop mode
+ if( (m_world->GetConfig().PHEROMONE_ENABLED.Get() == 1) &&
+ (organism->GetPheromoneStatus() == true) ) {
+ pher_amount = m_world->GetConfig().PHEROMONE_AMOUNT.Get();
+ } else {
+ pher_amount = 0;
+ }
+
+ cString UpdateStr = cStringUtil::Stringf("%d,%d,%d,%d,%d,%f,%d,4", m_world->GetStats().GetUpdate(), organism->GetID(), deme.GetDemeID(), rel_srcid, rel_destid, pher_amount, drop_mode);
+ df.WriteRaw(UpdateStr);
+ }
+
+ organism->Move(ctx);
+
+ return true;
+ } else {
+ return false;
+ }
+
+ return true;
+
+} // End Inst_SuperMove()
+
+
+
bool cHardwareCPU::Inst_Explore(cAvidaContext& ctx)
{
int num_rotations = 0;
@@ -3416,7 +3569,7 @@
// Find which neighbor has the strongest pheromone
- for(int i = 0; i < 8; i++) {
+ for(int i = 0; i < mycell.ConnectionList().GetSize(); i++) {
phero_amount = 0;
@@ -4669,12 +4822,12 @@
// Write some logging information if LOG_PHEROMONE is set. This is done
// out here so that non-pheromone moves are recorded.
- if(m_world->GetConfig().LOG_PHEROMONE.Get() == 1) {
- ofstream pherolog;
+ if( (m_world->GetConfig().LOG_PHEROMONE.Get() == 1) &&
+ (m_world->GetStats().GetUpdate() >= m_world->GetConfig().MOVETARGET_LOG_START.Get()) ) {
+ cString tmpfilename = cStringUtil::Stringf("movelog.dat");
+ cDataFile& df = m_world->GetDataFile(tmpfilename);
+
cDeme &deme = pop.GetDeme(pop.GetCell(organism->GetCellID()).GetDemeID());
-
-//TODO: would be nice to use the DATA_DIR from the config file
- pherolog.open("data/pheromones.dat", ios::app);
int rel_srcid = deme.GetRelativeCellID(fromcellID);
int rel_destid = deme.GetRelativeCellID(destcellID);
double pher_amount;
@@ -4688,11 +4841,11 @@
pher_amount = 0;
}
- pherolog << m_world->GetStats().GetUpdate() << " " << organism->GetID() << " " << rel_srcid << " " << rel_destid << " " << pher_amount << " " << drop_mode << endl;
-
- pherolog.close();
+ cString UpdateStr = cStringUtil::Stringf("%d,%d,%d,%d,%d,%f,%d,5", m_world->GetStats().GetUpdate(), organism->GetID(), deme.GetDemeID(), rel_srcid, rel_destid, pher_amount, drop_mode);
+ df.WriteRaw(UpdateStr);
}
+
// check tasks. general movement tasks are not yet supported.
//organism->DoOutput(ctx);
Modified: branches/energy/source/cpu/cHardwareCPU.h
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.h 2008-04-24 01:59:14 UTC (rev 2550)
+++ branches/energy/source/cpu/cHardwareCPU.h 2008-04-24 14:47:26 UTC (rev 2551)
@@ -501,6 +501,7 @@
bool Inst_Exploit(cAvidaContext& ctx);
bool Inst_Explore(cAvidaContext& ctx);
bool Inst_MoveTarget(cAvidaContext& ctx);
+ bool Inst_SuperMove(cAvidaContext& ctx);
bool Inst_IfTarget(cAvidaContext& ctx);
bool Inst_IfPheromone(cAvidaContext& ctx);
bool Inst_DropPheromone(cAvidaContext& ctx);
More information about the Avida-cvs
mailing list