[Avida-SVN] r1436 - in branches/dkdev: Avida.xcodeproj source/cpu

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Wed Mar 28 07:24:14 PDT 2007


Author: dknoester
Date: 2007-03-28 10:24:14 -0400 (Wed, 28 Mar 2007)
New Revision: 1436

Modified:
   branches/dkdev/Avida.xcodeproj/project.pbxproj
   branches/dkdev/source/cpu/cHardwareCPU.cc
   branches/dkdev/source/cpu/cHardwareCPU.h
Log:
Ported get-facing and rotate-template instructions from coopcoom.

Modified: branches/dkdev/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/dkdev/Avida.xcodeproj/project.pbxproj	2007-03-28 03:49:08 UTC (rev 1435)
+++ branches/dkdev/Avida.xcodeproj/project.pbxproj	2007-03-28 14:24:14 UTC (rev 1436)
@@ -246,6 +246,7 @@
 		E03F28DC0B8A2840009966B8 /* cDeme.cc in Sources */ = {isa = PBXBuildFile; fileRef = E03F28DA0B8A2840009966B8 /* cDeme.cc */; };
 		E03F28DD0B8A2840009966B8 /* cDeme.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E03F28DB0B8A2840009966B8 /* cDeme.h */; };
 		E08178BA0B3DCB9600B474B6 /* cTopology.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E08178B90B3DCB9600B474B6 /* cTopology.h */; };
+		E0D141EA0BB736E800077264 /* cTopologyManager.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E0D141E90BB736E800077264 /* cTopologyManager.h */; };
 		E0D1973A0B8B2F4C005EF7EE /* cOrgMessage.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E0D197390B8B2F4C005EF7EE /* cOrgMessage.h */; };
 		E0F61AE80BA2FDBD00B53C23 /* cGermline.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E0F61AE70BA2FDBD00B53C23 /* cGermline.h */; };
 		E0F61B210BA33F5D00B53C23 /* cGermline.cc in Sources */ = {isa = PBXBuildFile; fileRef = E0F61B200BA33F5D00B53C23 /* cGermline.cc */; };
@@ -296,6 +297,7 @@
 				E03F28DD0B8A2840009966B8 /* cDeme.h in CopyFiles */,
 				E0D1973A0B8B2F4C005EF7EE /* cOrgMessage.h in CopyFiles */,
 				E0F61AE80BA2FDBD00B53C23 /* cGermline.h in CopyFiles */,
+				E0D141EA0BB736E800077264 /* cTopologyManager.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -803,6 +805,8 @@
 		E03F28DA0B8A2840009966B8 /* cDeme.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cDeme.cc; sourceTree = "<group>"; };
 		E03F28DB0B8A2840009966B8 /* cDeme.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cDeme.h; sourceTree = "<group>"; };
 		E08178B90B3DCB9600B474B6 /* cTopology.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cTopology.h; sourceTree = "<group>"; };
+		E0D141E90BB736E800077264 /* cTopologyManager.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cTopologyManager.h; sourceTree = "<group>"; };
+		E0D141F00BB7423D00077264 /* cTopologyManager.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cTopologyManager.cc; sourceTree = "<group>"; };
 		E0D197390B8B2F4C005EF7EE /* cOrgMessage.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cOrgMessage.h; sourceTree = "<group>"; };
 		E0F61AE70BA2FDBD00B53C23 /* cGermline.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cGermline.h; sourceTree = "<group>"; };
 		E0F61B200BA33F5D00B53C23 /* cGermline.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cGermline.cc; sourceTree = "<group>"; };
@@ -1301,6 +1305,8 @@
 				70B0868A08F49EA800FC65FE /* cPopulationCell.cc */,
 				70B0868608F49E9700FC65FE /* cPopulationCell.h */,
 				702D4EFD08DA5341007BA469 /* cPopulationInterface.cc */,
+				E0D141E90BB736E800077264 /* cTopologyManager.h */,
+				E0D141F00BB7423D00077264 /* cTopologyManager.cc */,
 				702D4EF608DA5328007BA469 /* cPopulationInterface.h */,
 				70B0871E08F5E82D00FC65FE /* cReaction.cc */,
 				70B0870E08F5E81000FC65FE /* cReaction.h */,

Modified: branches/dkdev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.cc	2007-03-28 03:49:08 UTC (rev 1435)
+++ branches/dkdev/source/cpu/cHardwareCPU.cc	2007-03-28 14:24:14 UTC (rev 1436)
@@ -215,10 +215,10 @@
                   "Output ?BX?, and input new number back into ?BX?"),
     cInstEntryCPU("IO-Feedback",        &cHardwareCPU::Inst_TaskIO_Feedback, true,\
                   "Output ?BX?, and input new number back into ?BX?,  and push 1,0,\
-                  or -1 onto stack1 if merit increased, stayed the same, or decreased"),
+or -1 onto stack1 if merit increased, stayed the same, or decreased"),
     cInstEntryCPU("match-strings", &cHardwareCPU::Inst_MatchStrings),
-	cInstEntryCPU("sell", &cHardwareCPU::Inst_Sell),
-	cInstEntryCPU("buy", &cHardwareCPU::Inst_Buy),
+    cInstEntryCPU("sell", &cHardwareCPU::Inst_Sell),
+    cInstEntryCPU("buy", &cHardwareCPU::Inst_Buy),
     cInstEntryCPU("send",      &cHardwareCPU::Inst_Send),
     cInstEntryCPU("receive",   &cHardwareCPU::Inst_Receive),
     cInstEntryCPU("sense",     &cHardwareCPU::Inst_Sense),
@@ -230,6 +230,8 @@
     
     cInstEntryCPU("rotate-l",  &cHardwareCPU::Inst_RotateL),
     cInstEntryCPU("rotate-r",  &cHardwareCPU::Inst_RotateR),
+    cInstEntryCPU("rotate-tmpl", &cHardwareCPU::Inst_RotateTemplate),
+    cInstEntryCPU("get-facing", &cHardwareCPU::Inst_GetFacing),
     
     cInstEntryCPU("set-cmut",  &cHardwareCPU::Inst_SetCopyMut),
     cInstEntryCPU("mod-cmut",  &cHardwareCPU::Inst_ModCopyMut),
@@ -245,7 +247,7 @@
     cInstEntryCPU("h-divide",  &cHardwareCPU::Inst_HeadDivide, true,
                   "Divide code between read and write heads."),
     cInstEntryCPU("h-divide1RS",  &cHardwareCPU::Inst_HeadDivide1RS, true,
-		  "Divide code between read and write heads, at most one mutation on divide, resample if reverted."),
+                  "Divide code between read and write heads, at most one mutation on divide, resample if reverted."),
     cInstEntryCPU("h-divide2RS",  &cHardwareCPU::Inst_HeadDivide2RS, true,
                   "Divide code between read and write heads, at most two mutations on divide, resample if reverted."),
     cInstEntryCPU("h-divideRS",  &cHardwareCPU::Inst_HeadDivideRS, true,
@@ -344,7 +346,7 @@
     cInstEntryCPU("repro-X",    &cHardwareCPU::Inst_Repro),
     cInstEntryCPU("repro-Y",    &cHardwareCPU::Inst_Repro),
     cInstEntryCPU("repro-Z",    &cHardwareCPU::Inst_Repro),
-
+    
     cInstEntryCPU("spawn-deme", &cHardwareCPU::Inst_SpawnDeme),
     
     // Suicide
@@ -355,17 +357,17 @@
     // Placebo instructions
     // nop-x (included with nops)
     cInstEntryCPU("skip",      &cHardwareCPU::Inst_Skip),
-
+    
     // Messaging instructions
     cInstEntryCPU("send-msg", &cHardwareCPU::Inst_SendMessage),
     cInstEntryCPU("rtrv-msg", &cHardwareCPU::Inst_RetrieveMessage),
     cInstEntryCPU("cell-id", &cHardwareCPU::Inst_CellID),
-	cInstEntryCPU("get-pos", &cHardwareCPU::Inst_GetPosition),
+    cInstEntryCPU("get-pos", &cHardwareCPU::Inst_GetPosition),
     
     // Construction instructions
     cInstEntryCPU("cr-link", &cHardwareCPU::Inst_CreateLink),
-	cInstEntryCPU("rd-links", &cHardwareCPU::Inst_ReadLinks),
-	cInstEntryCPU("get-lpos", &cHardwareCPU::Inst_GetLocalPosition)
+    cInstEntryCPU("rd-links", &cHardwareCPU::Inst_ReadLinks),
+    cInstEntryCPU("get-lpos", &cHardwareCPU::Inst_GetLocalPosition)
   };
   
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntryCPU);
@@ -644,7 +646,7 @@
     << "F-Head:" << GetHead(nHardware::HEAD_FLOW).GetPosition()   << "  "
     << "RL:" << GetReadLabel().AsString() << "   "
     << endl;
-    
+  
   int number_of_stacks = GetNumStacks();
   for (int stack_id = 0; stack_id < number_of_stacks; stack_id++) {
     fp << "Top of stack " << stack_id << ":"
@@ -1141,7 +1143,7 @@
   }
   if (allocated_size < 1) {
     organism->Fault(FAULT_LOC_ALLOC, FAULT_TYPE_ERROR,
-          cStringUtil::Stringf("Allocate of %d too small", allocated_size));
+                    cStringUtil::Stringf("Allocate of %d too small", allocated_size));
     return false;
   }
   
@@ -1151,16 +1153,16 @@
   // Make sure that the new size is in range.
   if (new_size > MAX_CREATURE_SIZE  ||  new_size < MIN_CREATURE_SIZE) {
     organism->Fault(FAULT_LOC_ALLOC, FAULT_TYPE_ERROR,
-          cStringUtil::Stringf("Invalid post-allocate size (%d)",
-                               new_size));
+                    cStringUtil::Stringf("Invalid post-allocate size (%d)",
+                                         new_size));
     return false;
   }
   
   const int max_alloc_size = (int) (old_size * m_world->GetConfig().CHILD_SIZE_RANGE.Get());
   if (allocated_size > max_alloc_size) {
     organism->Fault(FAULT_LOC_ALLOC, FAULT_TYPE_ERROR,
-          cStringUtil::Stringf("Allocate too large (%d > %d)",
-                               allocated_size, max_alloc_size));
+                    cStringUtil::Stringf("Allocate too large (%d > %d)",
+                                         allocated_size, max_alloc_size));
     return false;
   }
   
@@ -1168,8 +1170,8 @@
     (int) (allocated_size * m_world->GetConfig().CHILD_SIZE_RANGE.Get());
   if (old_size > max_old_size) {
     organism->Fault(FAULT_LOC_ALLOC, FAULT_TYPE_ERROR,
-          cStringUtil::Stringf("Allocate too small (%d > %d)",
-                               old_size, max_old_size));
+                    cStringUtil::Stringf("Allocate too small (%d > %d)",
+                                         old_size, max_old_size));
     return false;
   }
   
@@ -1249,16 +1251,16 @@
 }
 
 /*
-  Almost the same as Divide_Main, but resamples reverted offspring.
-
-  RESAMPLING ONLY WORKS CORRECTLY WHEN ALL MUTIONS OCCUR ON DIVIDE!!
-
-  AWC - 06/29/06
-*/
+ Almost the same as Divide_Main, but resamples reverted offspring.
+ 
+ RESAMPLING ONLY WORKS CORRECTLY WHEN ALL MUTIONS OCCUR ON DIVIDE!!
+ 
+ AWC - 06/29/06
+ */
 bool cHardwareCPU::Divide_MainRS(cAvidaContext& ctx, const int div_point,
-                               const int extra_lines, double mut_multiplier)
+                                 const int extra_lines, double mut_multiplier)
 {
-
+  
   //cStats stats = m_world->GetStats();
   const int child_size = GetMemory().GetSize() - div_point - extra_lines;
   
@@ -1277,20 +1279,20 @@
   unsigned 
     totalMutations = 0,
     mutations = 0;
-    //RScount = 0;
-
-
+  //RScount = 0;
+  
+  
   bool
     fitTest = false;
-
+  
   // Handle Divide Mutations...
   /*
-    Do mutations until one of these conditions are satisified:
-     we have resampled X times
-     we have an offspring with the same number of muations as the first offspring
-      that is not reverted
-     the parent is steralized (usually means an implicit mutation)
-  */
+   Do mutations until one of these conditions are satisified:
+   we have resampled X times
+   we have an offspring with the same number of muations as the first offspring
+   that is not reverted
+   the parent is steralized (usually means an implicit mutation)
+   */
   for(unsigned i = 0; i <= 100; i++){
     if(i == 0){
       mutations = totalMutations = Divide_DoMutations(ctx, mut_multiplier);
@@ -1299,23 +1301,23 @@
       mutations = Divide_DoMutations(ctx, mut_multiplier);
       m_world->GetStats().IncResamplings();
     }
-
+    
     fitTest = Divide_TestFitnessMeasures(ctx);
     
     if(!fitTest && mutations >= totalMutations) break;
-
+    
   } 
   // think about making this mutations == totalMuations - though this may be too hard...
   /*
-  if(RScount > 2)
-    cerr << "Resampled " << RScount << endl;
-  */
+   if(RScount > 2)
+   cerr << "Resampled " << RScount << endl;
+   */
   //org could not be resampled beneath the hard cap -- it is then steraalized
   if(fitTest/*RScount == 11*/) {
     organism->GetPhenotype().ChildFertile() = false;
     m_world->GetStats().IncFailedResamplings();
   }
-
+  
 #if INSTRUCTION_COSTS
   // reset first time instruction costs
   for (int i = 0; i < inst_ft_cost.GetSize(); i++) {
@@ -1339,17 +1341,17 @@
 }
 
 /*
-  Almost the same as Divide_Main, but only allows for one mutation 
-    on divde and resamples reverted offspring.
-
-  RESAMPLING ONLY WORKS CORRECTLY WHEN ALL MUTIONS OCCUR ON DIVIDE!!
-
-  AWC - 07/28/06
-*/
+ Almost the same as Divide_Main, but only allows for one mutation 
+ on divde and resamples reverted offspring.
+ 
+ RESAMPLING ONLY WORKS CORRECTLY WHEN ALL MUTIONS OCCUR ON DIVIDE!!
+ 
+ AWC - 07/28/06
+ */
 bool cHardwareCPU::Divide_Main1RS(cAvidaContext& ctx, const int div_point,
-                               const int extra_lines, double mut_multiplier)
+                                  const int extra_lines, double mut_multiplier)
 {
-
+  
   //cStats stats = m_world->GetStats();
   const int child_size = GetMemory().GetSize() - div_point - extra_lines;
   
@@ -1369,19 +1371,19 @@
     totalMutations = 0,
     mutations = 0;
   //    RScount = 0;
-
+  
   bool
     fitTest = false;
-
-
+  
+  
   // Handle Divide Mutations...
   /*
-    Do mutations until one of these conditions are satisified:
-     we have resampled X times
-     we have an offspring with the same number of muations as the first offspring
-      that is not reverted
-     the parent is steralized (usually means an implicit mutation)
-  */
+   Do mutations until one of these conditions are satisified:
+   we have resampled X times
+   we have an offspring with the same number of muations as the first offspring
+   that is not reverted
+   the parent is steralized (usually means an implicit mutation)
+   */
   for(unsigned i = 0; i < 100; i++){
     if(!i){
       mutations = totalMutations = Divide_DoMutations(ctx, mut_multiplier,1);
@@ -1390,23 +1392,23 @@
       mutations = Divide_DoExactMutations(ctx, mut_multiplier,1);
       m_world->GetStats().IncResamplings();
     }
-
+    
     fitTest = Divide_TestFitnessMeasures(ctx);
     //if(mutations > 1 ) cerr << "Too Many mutations!!!!!!!!!!!!!!!" << endl;
     if(!fitTest && mutations >= totalMutations) break;
-
+    
   } 
   // think about making this mutations == totalMuations - though this may be too hard...
   /*
-  if(RScount > 2)
-    cerr << "Resampled " << RScount << endl;
-  */
+   if(RScount > 2)
+   cerr << "Resampled " << RScount << endl;
+   */
   //org could not be resampled beneath the hard cap -- it is then steraalized
   if(fitTest/*RScount == 11*/) {
     organism->GetPhenotype().ChildFertile() = false;
     m_world->GetStats().IncFailedResamplings();
   }
-
+  
 #if INSTRUCTION_COSTS
   // reset first time instruction costs
   for (int i = 0; i < inst_ft_cost.GetSize(); i++) {
@@ -1430,17 +1432,17 @@
 }
 
 /*
-  Almost the same as Divide_Main, but only allows for one mutation 
-    on divde and resamples reverted offspring.
-
-  RESAMPLING ONLY WORKS CORRECTLY WHEN ALL MUTIONS OCCUR ON DIVIDE!!
-
-  AWC - 07/28/06
-*/
+ Almost the same as Divide_Main, but only allows for one mutation 
+ on divde and resamples reverted offspring.
+ 
+ RESAMPLING ONLY WORKS CORRECTLY WHEN ALL MUTIONS OCCUR ON DIVIDE!!
+ 
+ AWC - 07/28/06
+ */
 bool cHardwareCPU::Divide_Main2RS(cAvidaContext& ctx, const int div_point,
-                               const int extra_lines, double mut_multiplier)
+                                  const int extra_lines, double mut_multiplier)
 {
-
+  
   //cStats stats = m_world->GetStats();
   const int child_size = GetMemory().GetSize() - div_point - extra_lines;
   
@@ -1460,19 +1462,19 @@
     totalMutations = 0,
     mutations = 0;
   //    RScount = 0;
-
+  
   bool
     fitTest = false;
-
-
+  
+  
   // Handle Divide Mutations...
   /*
-    Do mutations until one of these conditions are satisified:
-     we have resampled X times
-     we have an offspring with the same number of muations as the first offspring
-      that is not reverted
-     the parent is steralized (usually means an implicit mutation)
-  */
+   Do mutations until one of these conditions are satisified:
+   we have resampled X times
+   we have an offspring with the same number of muations as the first offspring
+   that is not reverted
+   the parent is steralized (usually means an implicit mutation)
+   */
   for(unsigned i = 0; i < 100; i++){
     if(!i){
       mutations = totalMutations = Divide_DoMutations(ctx, mut_multiplier,2);
@@ -1481,23 +1483,23 @@
       Divide_DoExactMutations(ctx, mut_multiplier,mutations);
       m_world->GetStats().IncResamplings();
     }
-
+    
     fitTest = Divide_TestFitnessMeasures(ctx);
     //if(mutations > 1 ) cerr << "Too Many mutations!!!!!!!!!!!!!!!" << endl;
     if(!fitTest && mutations >= totalMutations) break;
-
+    
   } 
   // think about making this mutations == totalMuations - though this may be too hard...
   /*
-  if(RScount > 2)
-    cerr << "Resampled " << RScount << endl;
-  */
+   if(RScount > 2)
+   cerr << "Resampled " << RScount << endl;
+   */
   //org could not be resampled beneath the hard cap -- it is then steraalized
   if(fitTest/*RScount == 11*/) {
     organism->GetPhenotype().ChildFertile() = false;
     m_world->GetStats().IncFailedResamplings();
   }
-
+  
 #if INSTRUCTION_COSTS
   // reset first time instruction costs
   for (int i = 0; i < inst_ft_cost.GetSize(); i++) {
@@ -2191,7 +2193,7 @@
   const int op1 = REG_BX;
   const int op2 = REG_AX;
 #endif
-
+  
   const cHeadCPU from(this, GetRegister(op1));
   cHeadCPU to(this, GetRegister(op2) + GetRegister(op1));
   sCPUStats& cpu_stats = organism->CPUStats();
@@ -2200,7 +2202,7 @@
     to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();  // Mark this instruction as mutated...
     to.SetFlagCopyMut();  // Mark this instruction as copy mut...
-                              //organism->GetPhenotype().IsMutated() = true;
+    //organism->GetPhenotype().IsMutated() = true;
     cpu_stats.mut_stats.copy_mut_count++;
   } else {
     to.SetInst(from.GetInst());
@@ -2221,7 +2223,7 @@
 #else
   const int src = REG_BX;
 #endif
-
+  
   const cHeadCPU from(this, GetRegister(src));
   
   // Dis-allowing mutations on read, for the moment (write only...)
@@ -2240,24 +2242,24 @@
   const int op1 = REG_BX;
   const int op2 = REG_AX;
 #endif
-
+  
   cHeadCPU to(this, GetRegister(op2) + GetRegister(op1));
   const int value = Mod(GetRegister(src), m_inst_set->GetSize());
   sCPUStats& cpu_stats = organism->CPUStats();
-
+  
   // Change value on a mutation...
   if (organism->TestCopyMut(ctx)) {
     to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();      // Mark this instruction as mutated...
     to.SetFlagCopyMut();      // Mark this instruction as copy mut...
-                                  //organism->GetPhenotype().IsMutated() = true;
+    //organism->GetPhenotype().IsMutated() = true;
     cpu_stats.mut_stats.copy_mut_count++;
   } else {
     to.SetInst(cInstruction(value));
     to.ClearFlagMutated();     // UnMark
     to.ClearFlagCopyMut();     // UnMark
   }
-
+  
   to.SetFlagCopied();  // Set the copied flag.
   cpu_stats.mut_stats.copies_exec++;
   return true;
@@ -2288,7 +2290,7 @@
     to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();      // Mark this instruction as mutated...
     to.SetFlagCopyMut();      // Mark this instruction as copy mut...
-                                  //organism->GetPhenotype().IsMutated() = true;
+    //organism->GetPhenotype().IsMutated() = true;
     cpu_stats.mut_stats.copy_mut_count++;
   } else {
     to.SetInst(cInstruction(value));
@@ -2311,7 +2313,7 @@
   const int op1 = REG_BX;
   const int op2 = REG_AX;
 #endif
-
+  
   cHeadCPU from(this, GetRegister(op1));
   cHeadCPU to(this, GetRegister(op2) + GetRegister(op1));
   
@@ -2320,7 +2322,7 @@
     to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();      // Mark this instruction as mutated...
     to.SetFlagCopyMut();      // Mark this instruction as copy mut...
-                                  //organism->GetPhenotype().IsMutated() = true;
+    //organism->GetPhenotype().IsMutated() = true;
   }
   
   GetRegister(dst) = from.GetInst().GetOp() - to.GetInst().GetOp();
@@ -2337,7 +2339,7 @@
   const int op1 = REG_BX;
   const int op2 = REG_AX;
 #endif
-
+  
   const cHeadCPU from(this, GetRegister(op1));
   const cHeadCPU to(this, GetRegister(op2) + GetRegister(op1));
   
@@ -2377,11 +2379,11 @@
 }
 
 /*
-  Divide with resampling -- Same as regular divide but on reversions will be 
-  resampled after they are reverted.
-
-  AWC 06/29/06
-
+ Divide with resampling -- Same as regular divide but on reversions will be 
+ resampled after they are reverted.
+ 
+ AWC 06/29/06
+ 
  */
 
 bool cHardwareCPU::Inst_DivideRS(cAvidaContext& ctx)  
@@ -2667,39 +2669,39 @@
 bool cHardwareCPU::Inst_TaskIO_Feedback(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_BX);
-
+  
   //check cur_bonus before the output
   double preOutputBonus = organism->GetPhenotype().GetCurBonus();
   
   // Do the "put" component
   const int value_out = GetRegister(reg_used);
   organism->DoOutput(ctx, value_out);  // Check for tasks completed.
-
+  
   //check cur_merit after the output
   double postOutputBonus = organism->GetPhenotype().GetCurBonus(); 
   
   
   //push the effect of the IO on merit (+,0,-) to the active stack
-
+  
   if (preOutputBonus > postOutputBonus){
     StackPush(-1);
-    }
+  }
   else if (preOutputBonus == postOutputBonus){
     StackPush(0);
-    }
+  }
   else if (preOutputBonus < postOutputBonus){
     StackPush(1);
-    }
+  }
   else {
     assert(0);
     //Bollocks. There was an error.
-    }
-
-
+  }
   
-
-
   
+  
+  
+  
+  
   // Do the "get" component
   const int value_in = organism->GetNextInput();
   GetRegister(reg_used) = value_in;
@@ -2999,6 +3001,88 @@
   return true;
 }
 
+
+/*! This instruction rotates the cell to the direction given by the following template.
+The template is interpreted in the same way as in Inst_GetFacing; see comments there for
+details.  If this organism is not capable of facing in the specified direction, no
+change is made to its orientation.
+*/
+bool cHardwareCPU::Inst_RotateTemplate(cAvidaContext& ctx) {
+  if(organism->GetCellID()==-1) return false;
+  // Get the label, interpret it as an integer, and decipher the vector.
+  ReadLabel();
+  int label = GetLabel().AsInt(NUM_NOPS);
+  std::pair<int,int> v = std::make_pair((label & 0x0C) >> 2, label & 0x03);
+
+  // Make sure the vector's sane.
+  if(v.first > 2 || v.second > 2) return false;
+
+  // Find cell id of what we're supposed to be facing.
+  int new_facing = GridNeighbor(organism->GetCellID(), 
+                                m_world->GetPopulation().GetWorldX(),
+                                m_world->GetPopulation().GetWorldY(),
+                                --v.first, --v.second);
+  
+  // But make sure that cell actually exists!
+  if(new_facing >= m_world->GetPopulation().GetSize()) return false;
+  
+  cPopulationCell& self_cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+  cPopulationCell& faced_cell = m_world->GetPopulation().GetCell(new_facing);
+
+  // If this cell actually has a connection to where we think it should, rotate.
+  if(self_cell.ConnectionList().FindPtr(&faced_cell) != 0) {
+    self_cell.Rotate(faced_cell);
+  }
+
+  return true;
+}
+
+
+/*! This instruction returns the current facing of the cell as an integer.  Note that the
+current facing is given as an absolute direction given the current environment -- It is
+not dependent upon the number of connections of the current cell.  Also, the facing of
+an organism in a 2x2 TORUS is indeterminant; Inst_GetFacing will still work, but it
+will return facings as if the organism is in a 2x2 GRID.
+
+Finally, a word on encodings: The organism's facing is encoded in a single integer.
+4 bits are used to indicate the organism's facing, 2 bits for each direction, according
+to the following (shown in 32-bit hex):
+
+facing == 0x00 00 00 0[XY]; X-bits==0x0C, Y-bits==0x03.
+
+Directions of each of X and Y are given by: LEFT,DOWN=0x0; NEITHER=0x1; RIGHT,UP=0x2.
+*/
+bool cHardwareCPU::Inst_GetFacing(cAvidaContext& ctx) {
+  if(organism->GetCellID()==-1) return false;
+  // This function only works under certain conditions.
+  assert(m_world->GetConfig().WORLD_GEOMETRY.Get()==nGeometry::GRID
+         || m_world->GetConfig().WORLD_GEOMETRY.Get()==nGeometry::TORUS);
+  assert(m_world->GetConfig().NUM_DEMES.Get()>1); // We only work with demes right now.
+  
+  // Get our own position and the position of the cell we're currently facing.
+  cPopulationCell& self_cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+  const cDeme& self_deme = m_world->GetPopulation().GetDeme(self_cell.GetDemeID());
+  std::pair<int,int> self_pos = self_deme.GetCellPosition(self_cell.GetID());
+  
+  const cPopulationCell& faced_cell = *self_cell.ConnectionList().GetFirst();
+  std::pair<int,int> faced_pos = self_deme.GetCellPosition(faced_cell.GetID());
+
+  // What is the vector from self->faced?
+  std::pair<int,int> v = std::make_pair(faced_pos.first - self_pos.first,
+                                        faced_pos.second - self_pos.second);
+  
+  // If the magnitude of either element of v is > 1, flip the sign and set it to 1.
+  // This indicates a wrap-around.
+  if(abs(v.first) > 1) { v.first /= -1 * v.first; }
+  if(abs(v.second) > 1) { v.second /= -1 * v.second; }
+  
+  // v now contains this organism's orientation, in vector form.  All that remains
+  // is to map this vector to an integer for the CPU.
+  GetRegister(FindModifiedRegister(REG_BX)) = (v.first+1) << 2 | (v.second+1);
+  return true;
+}
+
+
 bool cHardwareCPU::Inst_SetCopyMut(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_BX);
@@ -3124,8 +3208,8 @@
 }
 
 /*
-  Resample Divide -- AWC 06/29/06
-*/
+ Resample Divide -- AWC 06/29/06
+ */
 
 bool cHardwareCPU::Inst_HeadDivideRS(cAvidaContext& ctx)
 {
@@ -3141,8 +3225,8 @@
 }
 
 /*
-  Resample Divide -- single mut on divide-- AWC 07/28/06
-*/
+ Resample Divide -- single mut on divide-- AWC 07/28/06
+ */
 
 bool cHardwareCPU::Inst_HeadDivide1RS(cAvidaContext& ctx)
 {
@@ -3158,8 +3242,8 @@
 }
 
 /*
-  Resample Divide -- double mut on divide-- AWC 08/29/06
-*/
+ Resample Divide -- double mut on divide-- AWC 08/29/06
+ */
 
 bool cHardwareCPU::Inst_HeadDivide2RS(cAvidaContext& ctx)
 {
@@ -3373,7 +3457,7 @@
 {
   const int reg_used = FindModifiedRegister(REG_CX);
   GetHead(nHardware::HEAD_FLOW).Set(GetRegister(reg_used));
-return true; 
+  return true; 
 }
 
 
@@ -3451,7 +3535,7 @@
   cDeme& deme = m_world->GetPopulation().GetDeme(cell.GetDemeID());
   cDeme::Network& network = deme.GetNetwork();
   cDeme::CellVertexMap& cvMap = deme.GetCellVertexMap();
-
+  
   // What is the cell that we're trying to connect to?
   int connectTo=0;
   // First, do we have a label?
@@ -3464,25 +3548,25 @@
     // No; get the cell to connect to from our facing.
     connectTo = cell.ConnectionList().GetFirst()->GetID();
   }
-
+  
   // Now, are this cell and the cell we're connecting to in the network?  If no,
   // add them; if yes, then get a ref to the vertex descriptors.
   cDeme::CellVertexMap::iterator u = cvMap.find(cell.GetID());
   if(u == cvMap.end()) {
     u = cvMap.insert(std::make_pair(cell.GetID(), 
-		boost::add_vertex(
-		cDeme::vertex_properties(deme.GetCellPosition(cell.GetID()), cell.GetID()),
-						  network))).first;
+                                    boost::add_vertex(
+                                                      cDeme::vertex_properties(deme.GetCellPosition(cell.GetID()), cell.GetID()),
+                                                      network))).first;
   }
   cDeme::CellVertexMap::iterator v = cvMap.find(connectTo);
   if(v == cvMap.end()) {
     // Add the other cell to the network.
     v = cvMap.insert(std::make_pair(connectTo, 
-		boost::add_vertex(
-		cDeme::vertex_properties(deme.GetCellPosition(connectTo), connectTo),
-						  network))).first;
+                                    boost::add_vertex(
+                                                      cDeme::vertex_properties(deme.GetCellPosition(connectTo), connectTo),
+                                                      network))).first;
   }  
-
+  
   // Finally, add the edge.  We're done.
   boost::add_edge(u->second, v->second, network);
   return true;
@@ -3511,13 +3595,13 @@
 		// on the stack.
 		cDeme::Network::adjacency_iterator vi, vi_end;
 		for(boost::tie(vi,vi_end)=boost::adjacent_vertices(u->second, network)
-			; vi!=vi_end
-			; ++vi, ++edge_count) {
+        ; vi!=vi_end
+        ; ++vi, ++edge_count) {
 			// Push this on the stack.
 			StackPush(boost::get(boost::vertex_bundle, network)[*vi]._cell_id);
 		}
 	}
-
+  
 	GetRegister(reg_used) = edge_count;
 	return true;
 }
@@ -3530,7 +3614,7 @@
 	cDeme& deme = m_world->GetPopulation().GetDeme(cell.GetDemeID());
 	const int x = FindModifiedRegister(REG_BX);
 	const int y = FindNextRegister(x);
-
+  
 	// Get this cell's position, and set our registers.
 	std::pair<int,int> pos = deme.GetCellPosition(cell.GetID());
 	GetRegister(x) = pos.first;

Modified: branches/dkdev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.h	2007-03-28 03:49:08 UTC (rev 1435)
+++ branches/dkdev/source/cpu/cHardwareCPU.h	2007-03-28 14:24:14 UTC (rev 1436)
@@ -401,6 +401,9 @@
 
   bool Inst_RotateL(cAvidaContext& ctx);
   bool Inst_RotateR(cAvidaContext& ctx);
+  bool Inst_RotateTemplate(cAvidaContext& ctx); //!< Rotate the cell to face the given direction.
+  bool Inst_GetFacing(cAvidaContext& ctx); //!< Retrieve the current facing of the cell.
+ 
   bool Inst_SetCopyMut(cAvidaContext& ctx);
   bool Inst_ModCopyMut(cAvidaContext& ctx);
 




More information about the Avida-cvs mailing list