Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/15/10 19:58:42 (14 years ago)
Author:
gkronber
Message:

Fixed bugs in ADF operators and added test classes for ADF operators. #290 (Implement ADFs)

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests
Files:
4 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests

    • Property svn:ignore
      •  

        old new  
        11bin
        22obj
         3*.user
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    3455      var trees = new List<SymbolicExpressionTree>();
    3556      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    36       var random = new MersenneTwister();
     57      var random = new MersenneTwister(31415);
     58      int failedEvents = 0;
    3759      for (int i = 0; i < POPULATION_SIZE; i++) {
    3860        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    39         ArgumentCreater.CreateNewArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    40         Assert.IsTrue(tree.IsValidExpression());
     61        if (!ArgumentCreater.CreateNewArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
     62          failedEvents++;
     63        Util.IsValid(tree);
    4164        trees.Add(tree);
    4265      }
    4366      Assert.Inconclusive("ArgumentCreator: " + Environment.NewLine +
    44         Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
     67        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
     68        Util.GetSizeDistributionString(trees, 200, 20) + Environment.NewLine +
    4569        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
    4670        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDeleterTest.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    3455      var trees = new List<SymbolicExpressionTree>();
    3556      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    36       var random = new MersenneTwister();
     57      var random = new MersenneTwister(31415);
     58      int failedEvents = 0;
    3759      for (int i = 0; i < POPULATION_SIZE; i++) {
    3860        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    39         ArgumentDeleter.DeleteArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    40         Assert.IsTrue(tree.IsValidExpression());
     61        if (!ArgumentDeleter.DeleteArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
     62          failedEvents++;
     63        Util.IsValid(tree);
    4164        trees.Add(tree);
    4265      }
    4366      Assert.Inconclusive("ArgumentDeleter: " + Environment.NewLine +
     67        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
    4468        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
    4569        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    7495      return g;
    7596    }
     97
     98    public static void HasValidAdfGrammars(SymbolicExpressionTree tree) {
     99      Assert.AreEqual(tree.Root.Grammar.Symbols.Count(), 8);
     100      Assert.AreEqual(tree.Root.GetAllowedSymbols(0).Count(), 1); // only the start symbol is allowed
     101      // we allow 3 ADF branches
     102      Assert.AreEqual(tree.Root.GetAllowedSymbols(1).Count(), 1); // only the defun branch is allowed
     103      Assert.AreEqual(tree.Root.GetAllowedSymbols(2).Count(), 1); // only the defun symbol is allowed
     104      Assert.AreEqual(tree.Root.GetAllowedSymbols(3).Count(), 1); // only the defun symbol is allowed
     105      foreach (var subtree in tree.Root.SubTrees) {
     106        // check consistency of each sub-tree grammar independently
     107        var allowedSymbols = subtree.GetAllowedSymbols(0);
     108        int numberOfAllowedSymbols = allowedSymbols.Count();
     109        foreach (var parent in allowedSymbols) {
     110          for (int argIndex = 0; argIndex < subtree.Grammar.GetMaxSubtreeCount(parent); argIndex++) {
     111            var allowedChildren = from child in subtree.Grammar.Symbols
     112                                  where subtree.Grammar.IsAllowedChild(parent, child, argIndex)
     113                                  select child;
     114            Assert.AreEqual(numberOfAllowedSymbols, allowedChildren.Count());
     115          }
     116        }
     117      }
     118    }
     119
    76120  }
    77121}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj

    r3338 r3360  
    4141    <Compile Include="ArgumentCreaterTest.cs" />
    4242    <Compile Include="ArgumentDeleterTest.cs" />
     43    <Compile Include="ArgumentDuplicaterTest.cs" />
     44    <Compile Include="AllArchitectureAlteringOperatorsTest.cs" />
     45    <Compile Include="SubroutineDeleterTest.cs" />
     46    <Compile Include="SubroutineDuplicaterTest.cs" />
    4347    <Compile Include="Grammars.cs" />
     48    <Compile Include="SubroutineCreaterTest.cs">
     49      <SubType>Code</SubType>
     50    </Compile>
    4451    <Compile Include="SubtreeCrossoverTest.cs">
    4552      <SubType>Code</SubType>
     
    5057  </ItemGroup>
    5158  <ItemGroup>
     59    <ProjectReference Include="..\..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
     60      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
     61      <Name>HeuristicLab.Collections-3.3</Name>
     62    </ProjectReference>
    5263    <ProjectReference Include="..\..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
    5364      <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
    5465      <Name>HeuristicLab.Core-3.3</Name>
     66    </ProjectReference>
     67    <ProjectReference Include="..\..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj">
     68      <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>
     69      <Name>HeuristicLab.Data-3.3</Name>
    5570    </ProjectReference>
    5671    <ProjectReference Include="..\..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    3859      }
    3960
    40       foreach (var tree in randomTrees)
    41         Assert.IsTrue(tree.IsValidExpression());
     61      foreach (var tree in randomTrees) {
     62        Util.IsValid(tree);
     63      }
    4264      Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine +
    4365        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
     
    5577      var random = new MersenneTwister();
    5678      for (int i = 0; i < POPULATION_SIZE; i++) {
    57         randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3));
     79        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     80        Grammars.HasValidAdfGrammars(tree);
     81        Util.IsValid(tree);
     82        randomTrees.Add(tree);
    5883      }
    59       foreach (var tree in randomTrees)
    60         Assert.IsTrue(tree.IsValidExpression());
    6184      Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine +
    6285        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    829using System.Diagnostics;
    930using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
    10 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
    1131
    1232namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
    1333  [TestClass]
    1434  public class SubroutineCreaterTest {
    15     private static ISymbolicExpressionGrammar grammar;
    16     private static List<SymbolicExpressionTree> subroutineTrees;
    17     private static int failedEvents;
    18 
     35    private const int POPULATION_SIZE = 1000;
     36    private const int MAX_TREE_SIZE = 100;
     37    private const int MAX_TREE_HEIGHT = 10;
    1938    private TestContext testContextInstance;
    2039
     
    3251    }
    3352
    34     [ClassInitialize()]
    35     public static void SubroutineCreaterTestInitialize(TestContext testContext) {
    36       var randomTrees = new List<SymbolicExpressionTree>();
    37       subroutineTrees = new List<SymbolicExpressionTree>();
    38       int populationSize = 1000;
    39       failedEvents = 0;
    40       grammar = Grammars.CreateArithmeticAndAdfGrammar();
    41       var random = new MersenneTwister();
    42       for (int i = 0; i < populationSize; i++) {
    43         var randTree = ProbabilisticTreeCreator.Create(random, grammar, 100, 10);
    44         // PTC create is tested separately
    45         randomTrees.Add(randTree);
     53    [TestMethod()]
     54    public void SubroutineCreaterDistributionsTest() {
     55      var trees = new List<SymbolicExpressionTree>();
     56      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
     57      var random = new MersenneTwister(31415);
     58      int failedEvents = 0;
     59      for (int i = 0; i < POPULATION_SIZE; i++) {
     60        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     61        if (!SubroutineCreater.CreateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
     62          failedEvents++;
     63        Util.IsValid(tree);
     64        trees.Add(tree);
    4665      }
    47       var newPopulation = new List<SymbolicExpressionTree>();
    48       for (int i = 0; i < populationSize; i++) {
    49         var par0 = (SymbolicExpressionTree)randomTrees[random.Next(populationSize)].Clone();
    50         bool success = SubroutineCreater.CreateSubroutine(random, par0, grammar, 100, 10, 3, 3);
    51         if (!success) failedEvents++;
    52         subroutineTrees.Add(par0);
    53       }
    54     }
    55 
    56 
    57     [TestMethod()]
    58     public void SubroutineCreaterCreateTest() {
    59       foreach (var tree in subroutineTrees)
    60         Assert.IsTrue(grammar.IsValidExpression(tree));
    61     }
    62 
    63     [TestMethod()]
    64     public void SubroutineCreaterSizeDistributionTest() {
    65       Assert.Inconclusive("SubroutineCreater: " + Util.GetSizeDistributionString(subroutineTrees, 105, 5));
    66     }
    67 
    68     [TestMethod()]
    69     public void SubroutineCreaterFunctionDistributionTest() {
    70       Assert.Inconclusive("SubroutineCreater: " + Util.GetFunctionDistributionString(subroutineTrees));
    71     }
    72 
    73     [TestMethod()]
    74     public void SubroutineCreaterNumberOfSubTreesDistributionTest() {
    75       Assert.Inconclusive("SubroutineCreater: " + Util.GetNumberOfSubTreesDistributionString(subroutineTrees));
    76     }
    77 
    78 
    79     [TestMethod()]
    80     public void SubroutineCreaterTerminalDistributionTest() {
    81       Assert.Inconclusive("SubroutineCreater: " + Util.GetTerminalDistributionString(subroutineTrees));
     66      Assert.Inconclusive("SubroutineCreator: " + Environment.NewLine +
     67        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
     68        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
     69        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
     70        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
     71        Util.GetTerminalDistributionString(trees) + Environment.NewLine
     72        );
    8273    }
    8374  }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    1132  [TestClass]
    1233  public class SubtreeCrossoverTest {
    13     private static ISymbolicExpressionGrammar grammar;
    14     private static List<SymbolicExpressionTree> crossoverTrees;
    15     private static double msPerCrossoverEvent;
    16 
     34    private const int POPULATION_SIZE = 1000;
    1735    private TestContext testContextInstance;
    1836
     
    3048    }
    3149
    32     [ClassInitialize()]
    33     public static void SubtreeCrossoverTestInitialize(TestContext testContext) {
    34       crossoverTrees = new List<SymbolicExpressionTree>();
    35       int populationSize = 1000;
     50    [TestMethod()]
     51    public void SubtreeCrossoverDistributionsTest() {
    3652      int generations = 5;
     53      var trees = new List<SymbolicExpressionTree>();
     54      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
     55      var random = new MersenneTwister();
    3756      int failedEvents = 0;
    38       grammar = Grammars.CreateArithmeticAndAdfGrammar();
    39       var random = new MersenneTwister();
    40       for (int i = 0; i < populationSize; i++) {
    41         crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
     57      List<SymbolicExpressionTree> crossoverTrees;
     58      double msPerCrossoverEvent;
     59
     60      for (int i = 0; i < POPULATION_SIZE; i++) {
     61        trees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
    4262      }
    4363      Stopwatch stopwatch = new Stopwatch();
     
    4565      for (int gCount = 0; gCount < generations; gCount++) {
    4666        var newPopulation = new List<SymbolicExpressionTree>();
    47         for (int i = 0; i < populationSize; i++) {
    48           var par0 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
    49           var par1 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
     67        for (int i = 0; i < POPULATION_SIZE; i++) {
     68          var par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
     69          var par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
    5070          bool success;
    5171          newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success));
     
    5575      }
    5676      stopwatch.Stop();
    57       foreach (var tree in crossoverTrees)
    58         Assert.IsTrue(tree.IsValidExpression());
    59       msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;     
    60     }
     77      foreach (var tree in trees)
     78        Util.IsValid(tree);
    6179
     80      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)generations;
    6281
    63 
    64     [TestMethod()]
    65     public void SubtreeCrossoverSpeed() {
    66 
    67       Assert.Inconclusive(msPerCrossoverEvent + " ms per crossover event (~" +
    68         Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)");
    69     }
    70 
    71     [TestMethod()]
    72     public void SubtreeCrossoverSizeDistributions() {
    73       Assert.Inconclusive("SubtreeCrossover: " + Util.GetSizeDistributionString(crossoverTrees, 105, 5));
    74     }
    75 
    76     [TestMethod()]
    77     public void SubtreeCrossoverFunctionDistributionTest() {
    78       Assert.Inconclusive("SubtreeCrossover: " + Util.GetFunctionDistributionString(crossoverTrees));
    79     }
    80 
    81     [TestMethod()]
    82     public void SubtreeCrossoverNumberOfSubTreesDistributionTest() {
    83       Assert.Inconclusive("SubtreeCrossover: " + Util.GetNumberOfSubTreesDistributionString(crossoverTrees));
    84     }
    85 
    86 
    87     [TestMethod()]
    88     public void SubtreeCrossoverTerminalDistributionTest() {
    89       Assert.Inconclusive("SubtreeCrossover: " + Util.GetTerminalDistributionString(crossoverTrees));
     82      Assert.Inconclusive("SubtreeCrossover: " + Environment.NewLine +
     83        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
     84        msPerCrossoverEvent + " ms per crossover event (~" + Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)" + Environment.NewLine +
     85        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
     86        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
     87        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
     88        Util.GetTerminalDistributionString(trees) + Environment.NewLine
     89        );
    9090    }
    9191  }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs

    r3338 r3360  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Text;
    324using System.Collections.Generic;
     
    1334      int[] histogram = new int[maxTreeSize / binSize];
    1435      for (int i = 0; i < trees.Count; i++) {
    15         histogram[trees[i].Size / binSize]++;
     36        int binIndex = Math.Min(histogram.Length - 1, trees[i].Size / binSize);
     37        histogram[binIndex]++;
    1638      }
    1739      StringBuilder strBuilder = new StringBuilder();
    18       for (int i = 0; i < histogram.Length; i++) {
     40      for (int i = 0; i < histogram.Length - 1; i++) {
    1941        strBuilder.Append(Environment.NewLine);
    2042        strBuilder.Append("< "); strBuilder.Append((i + 1) * binSize);
    2143        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)trees.Count);
    2244      }
     45      strBuilder.Append(Environment.NewLine);
     46      strBuilder.Append(">= "); strBuilder.Append(histogram.Length * binSize);
     47      strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[histogram.Length - 1] / (double)trees.Count);
     48
    2349      return "Size distribution: " + strBuilder;
    2450    }
     
    88114      return "Terminal distribution: " + strBuilder;
    89115    }
     116
     117    public static void IsValid(SymbolicExpressionTree tree) {
     118      Grammars.HasValidAdfGrammars(tree);
     119      Assert.AreEqual(tree.Root.Symbol, tree.Root.Grammar.StartSymbol);
     120      foreach (var subtree in tree.Root.SubTrees)
     121        Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);
     122      IsValid(tree.Root);
     123    }
     124
     125    public static void IsValid(SymbolicExpressionTreeNode treeNode) {
     126      var matchingSymbol = (from symb in treeNode.Grammar.Symbols
     127                            where symb.Name == treeNode.Symbol.Name
     128                            select symb).SingleOrDefault();
     129      Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));
     130      Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));
     131      for (int i = 0; i < treeNode.SubTrees.Count; i++) {
     132        Assert.IsTrue(treeNode.GetAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));
     133        IsValid(treeNode.SubTrees[i]);
     134      }
     135    }
    90136  }
    91137}
Note: See TracChangeset for help on using the changeset viewer.