[Avida-SVN] r2349 - development/source/tools

covertar at myxo.css.msu.edu covertar at myxo.css.msu.edu
Tue Feb 19 14:43:55 PST 2008


Author: covertar
Date: 2008-02-19 17:43:54 -0500 (Tue, 19 Feb 2008)
New Revision: 2349

Added:
   development/source/tools/cProbDemeProbSchedule.cc
   development/source/tools/cProbDemeProbSchedule.h
Log:
Actually add the source files for revision 2348



Added: development/source/tools/cProbDemeProbSchedule.cc
===================================================================
--- development/source/tools/cProbDemeProbSchedule.cc	                        (rev 0)
+++ development/source/tools/cProbDemeProbSchedule.cc	2008-02-19 22:43:54 UTC (rev 2349)
@@ -0,0 +1,86 @@
+/*
+ *  cDemeProbSchedule.cc
+ *  Avida
+ *  Implemented by Art Covert 2/11/2008
+ *
+ *  Copyright 1999-2007 Michigan State University. All rights reserved.
+ *  Copyright 1993-2003 California Institute of Technology.
+ *
+ *
+ *  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.
+ *
+ */
+
+#include "cProbDemeProbSchedule.h"
+
+#include "cChangeList.h"
+#include "cMerit.h"
+
+// The larger merits cause problems here; things need to be re-thought out. 
+
+/* 
+ @AWC -- the previous comment is "inherited" from cProbScheduler, this is 
+ still a within deme issue.  However, this class mitigates the problem somewhat
+ when using more than one deme!
+
+ Yes, I know we're not *suppose* to reuse code -- DEAL
+*/
+
+//get the next CPU cycle, awarded probabistically to a deme based on population with non-zero merits
+int cProbDemeProbSchedule::GetNextID()
+{
+  //choose a deme to schedule
+  curr_deme = demeChart.FindPosition(m_rng.GetDouble(demeChart.GetTotalWeight()));
+
+  //calculate the offset
+  int offset = curr_deme * deme_size;
+  
+  // get the within postion of the node whos corresponding cell will get the CPU cycle
+  const double position = m_rng.GetDouble(chart[curr_deme]->GetTotalWeight());
+  
+  // return the adjusted ID of the cell to get the CPU cycle
+  return chart[curr_deme]->FindPosition(position) + offset;
+    
+  
+}
+
+
+//adjust the weight of an org within deme -- adjust the weighting for scheduling that deme
+void cProbDemeProbSchedule::Adjust(int item_id, const cMerit& item_merit, int deme_id)
+{
+  //calculate the corrected id for the org to be adjusted
+  int offset_id = item_id - (deme_id * deme_size);
+
+  //is this cell about to be populated by a living organism?
+  if(item_merit.GetDouble() > 0.0){
+    if(chart[deme_id]->GetWeight(offset_id) == 0.0){  //...was it previously unpopulated?  -- if so, population size has increased
+      demeChart.SetWeight(deme_id,demeChart.GetWeight(deme_id) + 1.0);//increment the deme's weight to reflect the new population size
+    }
+  } 
+  else { //by definition the merit is zero -- no such thing as merits less than 0.0 in Avida
+    if(chart[deme_id]->GetWeight(offset_id) > 0.0){ //...was the cell previously populated -- is so, populatino size has decreased
+      demeChart.SetWeight(deme_id,demeChart.GetWeight(deme_id) - 1.0);//decrement the deme's weight to reflect the new population size
+    }
+  }
+       
+
+  //"inherited" from cProbScheduler
+  if (cChangeList *change_list = GetChangeList()) {
+    change_list->MarkChange(item_id);
+  }
+
+  //adjust the merit of the org in the tree
+  chart[deme_id]->SetWeight(offset_id, item_merit.GetDouble());
+}

Added: development/source/tools/cProbDemeProbSchedule.h
===================================================================
--- development/source/tools/cProbDemeProbSchedule.h	                        (rev 0)
+++ development/source/tools/cProbDemeProbSchedule.h	2008-02-19 22:43:54 UTC (rev 2349)
@@ -0,0 +1,112 @@
+/*
+ *  cProbSchedule.h
+ *  Avida
+ *
+ *  Called "prob_schedule.hh" prior to 12/7/05.
+ *  Copyright 1999-2007 Michigan State University. All rights reserved.
+ *  Copyright 1993-2003 California Institute of Technology.
+ *
+ *
+ *  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 cProbDemeProbSchedule_h
+#define cProbDemeProbSchedule_h
+
+#ifndef cRandom_h
+#include "cRandom.h"
+#endif
+#ifndef cSchedule_h
+#include "cSchedule.h"
+#endif
+#ifndef cWeightedIndex_h
+#include "cWeightedIndex.h"
+#endif
+//need a pointer to the world to figure out number of demes
+#ifndef cWorld_h
+#include "cWorld.h"
+#endif
+//need tList to construct array of trees
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
+
+/**
+ * The DemeProbiblistic Schedule has the chance for an item to
+ * be scheduled within each Deme proportional to the merit of that 
+ * item relative to the rest of the deme.
+ * 
+ * @AWC -- further implementation notes in CC file.
+ **/
+
+class cMerit;
+
+class cProbDemeProbSchedule : public cSchedule
+{
+private:
+
+  //Keep our own RNG so as to better preserve consistancy.
+  cRandom m_rng; 
+
+  //Array of WeightedIndex tree's to farm out the scheduling.
+  tArray<cWeightedIndex*> chart;
+
+  //WeightedIndex tree for scheduling demes based on population size.
+  cWeightedIndex demeChart;
+
+  //how many demes are there?
+  int num_demes;
+
+  //size of each deme... num_cells/num_demes
+  int deme_size;
+
+  //what deme should GetNextID give the next CPU cycle to?
+  int curr_deme;
+  
+  
+  cProbDemeProbSchedule(const cProbDemeProbSchedule&); // @not_implemented
+  cProbDemeProbSchedule& operator=(const cProbDemeProbSchedule&); // @not_implemented
+
+public:
+  cProbDemeProbSchedule(int num_cells, int seed, int ndemes) : cSchedule(num_cells), m_rng(seed), num_demes(ndemes), demeChart(ndemes) {     
+
+    deme_size = num_cells/num_demes;
+
+    for(int i = 0; i < num_demes; i++){
+      chart.Push(new cWeightedIndex(deme_size));
+    }
+  }
+
+  ~cProbDemeProbSchedule() { ; }
+
+  void Adjust(int item_id, const cMerit& merit, int deme_id=0);
+  int GetNextID();
+};
+
+
+#ifdef ENABLE_UNIT_TESTS
+namespace nProbSchedule {
+  /**
+   * Run unit tests
+   *
+   * @param full Run full test suite; if false, just the fast tests.
+   **/
+  void UnitTests(bool full = false);
+}
+#endif  
+
+#endif




More information about the Avida-cvs mailing list