[Avida-SVN] r1236 - in extras: . source/testsuites

avidaedward at myxo.css.msu.edu avidaedward at myxo.css.msu.edu
Sat Jan 27 22:18:16 PST 2007


Author: avidaedward
Date: 2007-01-28 01:18:16 -0500 (Sun, 28 Jan 2007)
New Revision: 1236

Modified:
   extras/
   extras/source/testsuites/nAnalyze.cpp
Log:
 r1260 at clearly:  kaben | 2007-01-27 23:13:56 -0500
 Coalesced brainstorm of COMPETE analyze command into a testable
 function in the namespace "Brainstorm_SynchronousVsContinuous".



Property changes on: extras
___________________________________________________________________
Name: svk:merge
   - 079b078a-dbed-46b9-b3da-37668d4295ca:/avida/local/extras:1259
   + 079b078a-dbed-46b9-b3da-37668d4295ca:/avida/local/extras:1260

Modified: extras/source/testsuites/nAnalyze.cpp
===================================================================
--- extras/source/testsuites/nAnalyze.cpp	2007-01-28 06:18:13 UTC (rev 1235)
+++ extras/source/testsuites/nAnalyze.cpp	2007-01-28 06:18:16 UTC (rev 1236)
@@ -896,25 +896,159 @@
 
   /* cAnalyze_Brainstorm_SynchronousVsContinuous {{{2 */
   namespace Brainstorm_SynchronousVsContinuous {
-  void Compete(cAnalyze &a, cString cur_string)
-  {
-    if (cur_string.GetSize() == 0) {
-      cerr << "Duplicate Error: Must include from ID!" << endl;
-      if (a.exit_on_error) exit(1);
-    }
-    int batch_from = cur_string.PopWord().AsInt();
+    /* COMPETE : brainstorm of new Analyze command. */
+    void Compete(cAnalyze &a, cString cur_string)
+    {
+      if (cur_string.GetSize() == 0) {
+        cerr << "Compete Error: Must include target batch size!" << endl;
+        if (a.exit_on_error) exit(1);
+      }
+      int batch_size = cur_string.PopWord().AsInt();
+      
+      if (cur_string.GetSize() == 0) {
+        cerr << "Compete Error: Must include from ID!" << endl;
+        if (a.exit_on_error) exit(1);
+      }
+      int batch_from = cur_string.PopWord().AsInt();
+      
+      int batch_to = a.cur_batch;
+      if (cur_string.GetSize() > 0) batch_to = cur_string.PopWord().AsInt();
+      
+      if (a.m_world->GetVerbosity() >= VERBOSE_ON) {
+        cout << "Compete from batch " << batch_from << " to batch " << batch_to << "." << endl;
+      }
     
-    int batch_to = a.cur_batch;
-    if (cur_string.GetSize() > 0) batch_to = cur_string.PopWord().AsInt();
+      if(true){
+        /* Arguments to COMPETE: */
+        /*
+          batch_size
+          from_id
+          to_id=current
+        */
+        /* Get iterator into "from" batch. */ 
+        tListIterator<cAnalyzeGenotype> batch_it(a.batch[batch_from].List());
+        /* Get size of "from" batch. */
+        const int parent_batch_size = a.batch[batch_from].List().GetSize();
+
+        /* Create scheduler. */
+        cSchedule* schedule = new cProbSchedule(
+          parent_batch_size,
+          a.m_world->GetRandom().GetInt(0x7FFFFFFF)
+        );
+
+        /* Initialize scheduler with fitness values per-organism. */
+        tArray<cAnalyzeGenotype *> genotype_array(parent_batch_size);
+        tArray<cCPUMemory> offspring_genome_array(parent_batch_size);
+        tArray<cMerit> fitness_array(parent_batch_size);
+        cAnalyzeGenotype * genotype = NULL;
+
+        cTestCPU *testcpu = a.m_world->GetHardwareManager().CreateTestCPU();
+        TEST(0 != testcpu);
+
+        cCPUTestInfo *test_info = new cCPUTestInfo();
+        TEST(0 != test_info);
+
+        test_info->UseRandomInputs(false); 
     
-    if (a.m_world->GetVerbosity() >= VERBOSE_ON) {
-      cout << "Duplicating from batch " << batch_from << " to batch " << batch_to << "." << endl;
+        int array_pos = 0;
+        while ((genotype = batch_it.Next()) != NULL) {
+          //cAnalyzeGenotype * new_genotype = new cAnalyzeGenotype(*genotype);
+          //batch[batch_to].List().PushRear(new_genotype);
+          genotype_array[array_pos] = genotype;
+          genotype->Recalculate(a.m_world->GetDefaultContext(), testcpu, NULL, test_info);
+          if(genotype->GetViable()){
+            /*
+            kgn at XXX FIXME HACK
+            multiplication by 1000 because merits less than 1 are truncated to zero.
+            */
+            fitness_array[array_pos] = genotype->GetFitness() * 1000.;
+            offspring_genome_array[array_pos] = test_info->GetTestOrganism(0)->ChildGenome();
+          } else {
+            fitness_array[array_pos] = 0.0;
+            //offspring_genome_array[array_pos].Reset(0);
+          }
+          schedule->Adjust(array_pos, fitness_array[array_pos]);
+          array_pos++;
+        }
+
+
+        /* Use scheduler to sample organisms in "from" batch. */
+        for(int i=0; i<batch_size; /* don't increment i yet */){
+          /* Sample an organism. */
+          array_pos = schedule->GetNextID();
+          if(array_pos < 0){
+            cout << "Warning: No organisms in origin batch have positive fitness, cannot sample to destination batch." << endl; 
+            break;
+          }
+          genotype = genotype_array[array_pos];
+          /* If not viable, discard and continue. */
+          if(!genotype->GetViable()){
+            ERROR("scheduler sampled a non-viable organism!");
+            continue;
+          }
+
+          /*
+          Replication brainstorm :
+          - Try exact replicationm, without mutation or crossover.
+          - Now split-out a genome, such that it can be altered, and use
+            that genome (unaltered) for replication.
+          - Alter said genome in a controlled way.
+          - Mutations: Copy Mutation, Divide Insertion, Divide Deletion
+          */
+          if(true){
+            double copy_mut_prob = a.m_world->GetConfig().COPY_MUT_PROB.Get();
+            double ins_mut_prob = a.m_world->GetConfig().DIVIDE_INS_PROB.Get();
+            double del_mut_prob = a.m_world->GetConfig().DIVIDE_DEL_PROB.Get();
+            int ins_line = -1;
+            int del_line = -1;
+
+            cCPUMemory child_genome = offspring_genome_array[array_pos];
+
+            if (copy_mut_prob > 0.0) {
+              for (int n = 0; n < child_genome.GetSize(); n++) {
+                if (a.m_world->GetRandom().P(copy_mut_prob)) {
+                  child_genome[n] = a.inst_set.GetRandomInst(a.m_ctx);
+                }
+              }
+            }
+            
+            /* Perform an Insertion if it has one. */
+            if (a.m_world->GetRandom().P(ins_mut_prob)) {
+              ins_line = a.m_world->GetRandom().GetInt(child_genome.GetSize() + 1);
+              child_genome.Insert(ins_line, a.inst_set.GetRandomInst(a.m_ctx));
+            }
+            
+            /* Perform a Deletion if it has one. */
+            if (a.m_world->GetRandom().P(del_mut_prob)) {
+              del_line = a.m_world->GetRandom().GetInt(child_genome.GetSize());
+              child_genome.Remove(del_line);
+            }
+
+            /* Create (possibly mutated) offspring. */
+            cAnalyzeGenotype * new_genotype = new cAnalyzeGenotype(
+              a.m_world,
+              child_genome,
+              a.inst_set
+            );
+            /* Place offspring in "to" batch. */
+            a.batch[batch_to].List().PushRear(new_genotype);
+          }
+
+          a.batch[batch_to].SetLineage(false);
+          a.batch[batch_to].SetAligned(false);
+
+          /* Increment and continue. */
+          i++;
+        }
+
+        if(test_info){ delete test_info; test_info = 0; }
+        if(testcpu){ delete testcpu; testcpu = 0; }
+        if(schedule){ delete schedule; schedule = 0; }
+      }
+
+      return;
     }
-  
 
-    return;
-  }
-
     void test(cTestSettings &settings){
       /* Check settings. {{{3 */
       if(
@@ -1030,11 +1164,10 @@
         }
         a.Send(s);
         a.Send("RECALCULATE");
-        a.Send("RECALCULATE");
         a.Send("DETAIL archive_0/detail.dat id parent_id num_cpus fitness viable length sequence");
 
         /* 'num_gens' synchronous generations. {{{4 */
-        int num_gens = 3;
+        int num_gens = 60;
         for(int g=0; g<num_gens; g++){
           /* Mock-up of arguments to COMPETE command. {{{5 */
           int world_x = world->GetConfig().WORLD_X.Get();
@@ -1045,18 +1178,6 @@
           int batch_to = g + 1;
 
           {
-            s = "SET f ";
-            s += cStringUtil::Convert(batch_from);
-          }
-          a.Send(s);
-
-          a.Send("SET_BATCH $f");
-          //a.Send("RECALCULATE");
-          //a.Send("DETAIL archive_$f/detail.dat fitness merit gest_time length viable sequence");
-          //a.Send("TRACE archive_$f");
-          //a.Send("PRINT archive_$f");
-
-          {
             s = "SET t ";
             s += cStringUtil::Convert(batch_to);
           }
@@ -1065,138 +1186,21 @@
           a.Send("SET_BATCH $t");
           a.Send("PURGE_BATCH");
 
-          /* COMPETE : brainstorm of new Analyze command. {{{5 */
-          if(true){
-            /* Arguments to COMPETE: */
-            /*
-              batch_size
-              from_id
-              to_id=current
-            */
-            /* Get iterator into "from" batch. */ 
-            tListIterator<cAnalyzeGenotype> batch_it(a.batch[batch_from].List());
-            /* Get size of "from" batch. */
-            const int parent_batch_size = a.batch[batch_from].List().GetSize();
-            if(parent_batch_size <= 0){
-              cout << "Warning: origin batch is empty, cannot sample to destination batch." << endl; 
-              break;
-            }
-            /* Create scheduler. */
-            cSchedule* schedule = new cProbSchedule(
-              parent_batch_size,
-              world->GetRandom().GetInt(0x7FFFFFFF)
-            );
+          /* Arguments to COMPETE: */
+          /*
+            batch_size
+            from_id
+            to_id=current
+          */
+          cString arguments("");
+          arguments += cStringUtil::Convert(batch_size);
+          arguments += " ";
+          arguments += cStringUtil::Convert(batch_from);
+          arguments += " ";
+          arguments += cStringUtil::Convert(batch_to);
 
-            /* Initialize scheduler with fitness values per-organism. */
-            tArray<cAnalyzeGenotype *> genotype_array(parent_batch_size);
-            tArray<cCPUMemory> offspring_genome_array(parent_batch_size);
-            tArray<cMerit> fitness_array(parent_batch_size);
-            cAnalyzeGenotype * genotype = NULL;
+          Compete(a, arguments);
 
-            cTestCPU *testcpu = world->GetHardwareManager().CreateTestCPU();
-            TEST(0 != testcpu);
-
-            cCPUTestInfo *test_info = new cCPUTestInfo();
-            TEST(0 != test_info);
-
-            test_info->UseRandomInputs(false); 
-  
-            int array_pos = 0;
-            while ((genotype = batch_it.Next()) != NULL) {
-              //cAnalyzeGenotype * new_genotype = new cAnalyzeGenotype(*genotype);
-              //batch[batch_to].List().PushRear(new_genotype);
-              genotype_array[array_pos] = genotype;
-              genotype->Recalculate(world->GetDefaultContext(), testcpu, NULL, test_info);
-              if(genotype->GetViable()){
-                /*
-                kgn at XXX FIXME HACK
-                multiplication by 1000 because merits less than 1 are truncated to zero.
-                */
-                fitness_array[array_pos] = genotype->GetFitness() * 1000.;
-                offspring_genome_array[array_pos] = test_info->GetTestOrganism(0)->ChildGenome();
-              } else {
-                fitness_array[array_pos] = 0.0;
-                //offspring_genome_array[array_pos].Reset(0);
-              }
-              schedule->Adjust(array_pos, fitness_array[array_pos]);
-              array_pos++;
-            }
-
-
-            /* Use scheduler to sample organisms in "from" batch. */
-            for(int i=0; i<batch_size; /* don't increment i yet */){
-              cout << "i : " << i << endl;
-              /* Sample an organism. */
-              array_pos = schedule->GetNextID();
-              if(array_pos < 0){
-                cout << "Warning: No organisms in origin batch have positive fitness, cannot sample to destination batch." << endl; 
-                break;
-              }
-              genotype = genotype_array[array_pos];
-              /* If not viable, discard and continue. */
-              if(!genotype->GetViable()){
-                ERROR("scheduler sampled a non-viable organism!");
-                continue;
-              }
-
-              /*
-              Replication brainstorm :
-              - Try exact replicationm, without mutation or crossover.
-              - Now split-out a genome, such that it can be altered, and use
-                that genome (unaltered) for replication.
-              - Alter said genome in a controlled way.
-              - Mutations: Copy Mutation, Divide Insertion, Divide Deletion
-              */
-              if(true){
-                double copy_mut_prob = world->GetConfig().COPY_MUT_PROB.Get();
-                double ins_mut_prob = world->GetConfig().DIVIDE_INS_PROB.Get();
-                double del_mut_prob = world->GetConfig().DIVIDE_DEL_PROB.Get();
-                int ins_line = -1;
-                int del_line = -1;
-
-                cCPUMemory child_genome = offspring_genome_array[array_pos];
-
-                if (copy_mut_prob > 0.0) {
-                  for (int n = 0; n < child_genome.GetSize(); n++) {
-                    if (world->GetRandom().P(copy_mut_prob)) {
-                      child_genome[n] = a.inst_set.GetRandomInst(world->GetDefaultContext());
-                    }
-                  }
-                }
-                
-                /* Perform an Insertion if it has one. */
-                if (world->GetRandom().P(ins_mut_prob)) {
-                  ins_line = world->GetRandom().GetInt(child_genome.GetSize() + 1);
-                  child_genome.Insert(ins_line, a.inst_set.GetRandomInst(world->GetDefaultContext()));
-                }
-                
-                /* Perform a Deletion if it has one. */
-                if (world->GetRandom().P(del_mut_prob)) {
-                  del_line = world->GetRandom().GetInt(child_genome.GetSize());
-                  child_genome.Remove(del_line);
-                }
-
-                /* Create (possibly mutated) offspring. */
-                cAnalyzeGenotype * new_genotype = new cAnalyzeGenotype(
-                  world,
-                  child_genome,
-                  a.inst_set
-                );
-                /* Place offspring in "to" batch. */
-                a.batch[batch_to].List().PushRear(new_genotype);
-              }
-
-              a.batch[batch_to].SetLineage(false);
-              a.batch[batch_to].SetAligned(false);
-
-              /* Increment and continue. */
-              i++;
-            }
-
-            if(test_info){ delete test_info; test_info = 0; }
-            if(testcpu){ delete testcpu; testcpu = 0; }
-            if(schedule){ delete schedule; schedule = 0; }
-          }
           a.Send("SET_BATCH $t");
           a.Send("RECALCULATE");
           a.Send("DETAIL archive_$t/detail.dat id parent_id num_cpus fitness viable length sequence");




More information about the Avida-cvs mailing list