[Avida-SVN] r2997 - in development: Avida.xcodeproj source/main source/tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Dec 4 22:11:20 PST 2008


Author: brysonda
Date: 2008-12-05 01:11:20 -0500 (Fri, 05 Dec 2008)
New Revision: 2997

Added:
   development/source/tools/tArrayUtils.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/main/cStateGrid.h
   development/source/main/cTaskLib.cc
   development/source/main/cTaskLib.h
Log:
Add state grid path traversal task.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2008-12-04 02:24:09 UTC (rev 2996)
+++ development/Avida.xcodeproj/project.pbxproj	2008-12-05 06:11:20 UTC (rev 2997)
@@ -505,6 +505,7 @@
 		705ACD4C0A13FED4002D5BA0 /* PrintActions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrintActions.h; sourceTree = "<group>"; };
 		705ACD4D0A13FED4002D5BA0 /* PrintActions.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrintActions.cc; sourceTree = "<group>"; };
 		70658C59085DF67D00486BED /* libncurses.5.4.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libncurses.5.4.dylib; path = /usr/lib/libncurses.5.4.dylib; sourceTree = "<absolute>"; };
+		7069470C0EE8FA2700AD67C0 /* tArrayUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tArrayUtils.h; sourceTree = "<group>"; };
 		706C6F480B83E69D003174C1 /* cSaleItem.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cSaleItem.h; sourceTree = "<group>"; };
 		706C6FFD0B83F254003174C1 /* cInstSet.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cInstSet.h; sourceTree = "<group>"; };
 		706C6FFE0B83F265003174C1 /* cInstSet.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cInstSet.cc; sourceTree = "<group>"; };
@@ -1661,6 +1662,7 @@
 				70211A5B0ECBD531004A293A /* cRCObject.h */,
 				70211A5C0ECBD531004A293A /* cRCObject.cc */,
 				70211A6C0ECBDB72004A293A /* tRCPtr.h */,
+				7069470C0EE8FA2700AD67C0 /* tArrayUtils.h */,
 			);
 			path = tools;
 			sourceTree = "<group>";

Modified: development/source/main/cStateGrid.h
===================================================================
--- development/source/main/cStateGrid.h	2008-12-04 02:24:09 UTC (rev 2996)
+++ development/source/main/cStateGrid.h	2008-12-05 06:11:20 UTC (rev 2997)
@@ -51,12 +51,14 @@
                     const tArray<int>& sense_values, const tArray<int>& grid);
   ~cStateGrid() { ; }
   
+  inline const cString& GetName() const { return m_name; }
   inline int GetWidth() const { return m_w; }
   inline int GetHeight() const { return m_h; }
   inline int GetInitialX() const { return m_init_x; }
   inline int GetInitialY() const { return m_init_y; }
   inline int GetInitialFacing() const { return m_init_facing; }
   inline int GetNumStates() const { return m_states.GetSize(); }
+  inline int GetStateID(const cString& state_name) const;
   
   inline int GetIDFor(int x, int y) const { return (x * m_w + y); }
   inline int GetStateAt(int grid_id) const { return m_grid[grid_id]; }
@@ -74,4 +76,13 @@
 }
 
 
+inline int cStateGrid::GetStateID(const cString& state_name) const
+{
+  for (int i = 0; i < m_states.GetSize(); i++) {
+    if (m_states[i] == state_name) return i;
+  }
+  
+  return -1;
+}
+
 #endif

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2008-12-04 02:24:09 UTC (rev 2996)
+++ development/source/main/cTaskLib.cc	2008-12-05 06:11:20 UTC (rev 2997)
@@ -29,12 +29,14 @@
 #include "cArgSchema.h"
 #include "cDeme.h"
 #include "cEnvReqs.h"
-#include "tHashTable.h"
 #include "cTaskState.h"
 #include "cPopulation.h"
 #include "cPopulationCell.h"
 #include "cOrgMessagePredicate.h"
 #include "cOrgMovementPredicate.h"
+#include "cStateGrid.h"
+#include "tArrayUtils.h"
+#include "tHashTable.h"
 
 #include "platform.h"
 
@@ -355,15 +357,13 @@
   else if (name == "match_number")
     Load_MatchNumber(name, info, envreqs, errors);
   
+  // Sequence Tasks
   if (name == "sort_inputs")
     Load_SortInputs(name, info, envreqs, errors);
   else if (name == "fibonacci_seq")
     Load_FibonacciSequence(name, info, envreqs, errors);
   
-  // Optimization Tasks
-  if (name == "optimize")
-    Load_Optimize(name, info, envreqs, errors);
-  
+  // Math Tasks
   if (name == "mult")
     Load_Mult(name, info, envreqs, errors);
   else if (name == "div")
@@ -382,6 +382,10 @@
     Load_Cosine(name, info, envreqs, errors);
   
   
+  // Optimization Tasks
+  if (name == "optimize")
+    Load_Optimize(name, info, envreqs, errors);
+  
   // Communication Tasks
   if (name == "comm_echo")
     NewTask(name, "Echo of Neighbor's Input", &cTaskLib::Task_CommEcho, REQ_NEIGHBOR_INPUT);
@@ -423,6 +427,11 @@
   else if(name == "event_killed")
     NewTask(name, "Killed event", &cTaskLib::Task_EventKilled);
   
+  // Optimization Tasks
+  if (name == "sg_path_traversal")
+    Load_SGPathTraversal(name, info, envreqs, errors);  
+  
+  
   // Make sure we have actually found a task  
   if (task_array.GetSize() == start_size) {
     if (errors != NULL && errors->GetSize() == 0) {
@@ -3051,3 +3060,52 @@
   if (ctx.GetOrganism()->GetEventKilled()) return 1.0;
   return 0.0;
 }
+
+
+
+void cTaskLib::Load_SGPathTraversal(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
+{
+  cArgSchema schema;
+
+  // Integer Arguments
+  schema.AddEntry("pathlen", 0, cArgSchema::SCHEMA_INT);
+  
+  // String Arguments
+  schema.AddEntry("sgname", 0, cArgSchema::SCHEMA_STRING);
+  schema.AddEntry("poison", 1, cArgSchema::SCHEMA_STRING);
+  
+  cArgContainer* args = cArgContainer::Load(argstr, schema, errors);
+  if (args) NewTask(name, "State Grid Path Traversal", &cTaskLib::Task_SGPathTraversal, 0, args);
+}
+
+double cTaskLib::Task_SGPathTraversal(cTaskContext& ctx) const
+{
+  const cArgContainer& args = ctx.GetTaskEntry()->GetArguments();
+  const cStateGrid& sg = ctx.GetOrganism()->GetStateGrid();
+
+  if (sg.GetName() != args.GetString(0)) return 0.0;
+
+  int state = sg.GetStateID(args.GetString(1));
+  if (state < 0) return 0.0;
+  
+  const tSmartArray<int>& ext_mem = ctx.GetExtendedMemory();
+  
+  // Build and sort history
+  const int history_offset = 3 + sg.GetNumStates();
+  tArray<int> history(ext_mem.GetSize() - history_offset);
+  for (int i = 0; i < history.GetSize(); i++) history[i] = ext_mem[i + history_offset];
+  tArrayUtils::QSort(history);
+  
+  // Calculate how many unique non-poison cells have been touched
+  int traversed = 0;
+  int last = -1;
+  for (int i = 0; i < history.GetSize(); i++) {
+    if (history[i] == last) continue;
+    last = history[i];
+    if (sg.GetStateAt(last) != state) traversed++;
+  }
+  
+  traversed -= ext_mem[3 + state];
+  
+  return ((double)((traversed >= 0) ? traversed : 0) / (double)args.GetInt(0));
+}  

Modified: development/source/main/cTaskLib.h
===================================================================
--- development/source/main/cTaskLib.h	2008-12-04 02:24:09 UTC (rev 2996)
+++ development/source/main/cTaskLib.h	2008-12-05 06:11:20 UTC (rev 2997)
@@ -244,15 +244,13 @@
   void Load_MatchNumber(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_MatchNumber(cTaskContext& ctx) const;
 
+  // Sequence Tasks
   void Load_SortInputs(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_SortInputs(cTaskContext& ctx) const;
   void Load_FibonacciSequence(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_FibonacciSequence(cTaskContext& ctx) const;
-  
-   // Optimization Tasks
-  void Load_Optimize(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
-  double Task_Optimize(cTaskContext& ctx) const;
 
+  // Math Tasks
   void Load_Mult(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_Mult(cTaskContext& ctx) const;
   void Load_Div(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
@@ -270,7 +268,11 @@
   void Load_Cosine(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_Cosine(cTaskContext& ctx) const;
 
-
+  // Optimization Tasks
+  void Load_Optimize(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
+  double Task_Optimize(cTaskContext& ctx) const;
+  
+  
   // Communication Tasks
   double Task_CommEcho(cTaskContext& ctx) const;
   double Task_CommNot(cTaskContext& ctx) const;
@@ -296,6 +298,10 @@
   // movement
   double Task_MoveToEvent(cTaskContext& ctx) const;
   double Task_EventKilled(cTaskContext& ctx) const;
+
+  // State Grid Tasks
+  void Load_SGPathTraversal(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
+  double Task_SGPathTraversal(cTaskContext& ctx) const;  
 };
 
 

Added: development/source/tools/tArrayUtils.h
===================================================================
--- development/source/tools/tArrayUtils.h	                        (rev 0)
+++ development/source/tools/tArrayUtils.h	2008-12-05 06:11:20 UTC (rev 2997)
@@ -0,0 +1,110 @@
+/*
+ *  tArrayUtils.h
+ *  Avida
+ *   from ThinkTank
+ *
+ *  Created by David on 11/19/06.
+ *  Copyright 2006,2008 David Michael Bryson. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef tArrayUtils_h
+#define tArrayUtils_h
+
+#include "tArray.h"
+
+
+class tArrayUtils
+{
+private:
+  static const int QUICKSORT_THRESHOLD = 10;
+  
+  tArrayUtils(); // @not_implemented
+  tArrayUtils(const tArrayUtils&); // @not_implemented
+  tArrayUtils& operator=(const tArrayUtils&); // @not_implemented
+
+
+public:
+    
+  template<typename T> inline static void QSort(tArray<T>& array) { QSort(array, 0, array.GetSize() - 1); }
+  template<typename T> static void QSort(tArray<T>& array, int begin, int end)
+  {
+    if (end < begin) return;
+    
+    if (begin - end <= QUICKSORT_THRESHOLD) {
+      ISort(array, begin, end);
+      return;
+    }
+    
+    T pivot = array[begin];
+    int l = begin + 1;
+    int r = end;
+    
+    while (l != r - 1) {
+      if (array[l] > pivot)
+        l++;
+      else
+        swap(array, l, r--);
+    }
+    
+    if (array[l] > pivot && array[r] > pivot) {
+      l = r + 1;
+    } else if (array[l] > pivot && array[r] <= pivot) {
+      l++; r--;
+    } else if (array[l] <= pivot && array[r] > pivot) {
+      swap(array, l++, r--);
+    } else {
+      r = l - 1;
+    }
+    
+    swap(array, r--, begin);
+    QSort(array, begin, r);
+    QSort(array, l, end);
+  }
+    
+    
+  template<typename T> inline static void ISort(tArray<T>& array) { isort(array, 0, array.GetSize() - 1); }
+  template<typename T> static void ISort(tArray<T>& array, int begin, int end)
+  {
+    T value;
+    int j;
+    
+    // for each entry
+    for (int i = begin + 1; i <= end; i++) {
+      // insert into array starting from the end of our sub-array
+      value = array[i];
+      j = i - 1;
+      while (j >= begin && array[j] < value) {
+        array[j + 1] = array[j];
+        j--;
+      }
+      array[j + 1] = value;
+    }
+  }
+  
+  
+private:
+  
+  template<typename T> inline static void swap(tArray<T>& array, int i, int j)
+  {
+    T v = array[i];
+    array[i] = array[j];
+    array[j] = v;
+  }
+};
+
+#endif




More information about the Avida-cvs mailing list