Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/22/11 19:04:54 (14 years ago)
Author:
gkronber
Message:

#1418 unified size/height vs. length/depth terminology and adapted unit tests for symbolic expression tree encoding version 3.4

Location:
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/AllArchitectureAlteringOperatorsTest.cs

    r5445 r5549  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
    3027using HeuristicLab.Random;
    3128using Microsoft.VisualStudio.TestTools.UnitTesting;
    3229
    33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3431  [TestClass]
    3532  public class AllArchitectureAlteringOperatorsTest {
    3633    private const int POPULATION_SIZE = 1000;
    3734    private const int N_ITERATIONS = 20;
    38     private const int MAX_TREE_SIZE = 100;
    39     private const int MAX_TREE_HEIGHT = 10;
     35    private const int MAX_TREE_LENGTH = 100;
     36    private const int MAX_TREE_DEPTH = 10;
    4037    private TestContext testContextInstance;
    4138
     
    5552    [TestMethod()]
    5653    public void AllArchitectureAlteringOperatorsDistributionTest() {
    57       var trees = new List<SymbolicExpressionTree>();
    58       var newTrees = new List<SymbolicExpressionTree>();
     54      var trees = new List<ISymbolicExpressionTree>();
     55      var newTrees = new List<ISymbolicExpressionTree>();
    5956      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    6057      var random = new MersenneTwister(31415);
    61       IntValue maxTreeSize = new IntValue(MAX_TREE_SIZE);
    62       IntValue maxTreeHeigth = new IntValue(MAX_TREE_HEIGHT);
     58      IntValue maxTreeSize = new IntValue(MAX_TREE_LENGTH);
     59      IntValue maxTreeHeigth = new IntValue(MAX_TREE_DEPTH);
    6360      IntValue maxDefuns = new IntValue(3);
    6461      IntValue maxArgs = new IntValue(3);
    6562      for (int i = 0; i < POPULATION_SIZE; i++) {
    66         var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     63        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6764        Util.IsValid(tree);
    6865        trees.Add(tree);
     
    7067      Stopwatch stopwatch = new Stopwatch();
    7168      stopwatch.Start();
    72       var combinedAAOperator = new MultiSymbolicExpressionTreeArchitectureManipulator();
    7369      int failedEvents = 0;
    7470      for (int g = 0; g < N_ITERATIONS; g++) {
     
    7672          if (random.NextDouble() < 0.5) {
    7773            // manipulate
    78             var selectedTree = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
    79             var op = combinedAAOperator.Operators.SelectRandom(random);
     74            var selectedTree = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone();
    8075            bool success = false;
    81             op.ModifyArchitecture(random, selectedTree, grammar, maxTreeSize, maxTreeHeigth, maxDefuns, maxArgs, out success);
    82             if (!success) failedEvents++; // architecture manipulation might fail
     76            switch (random.Next(6)) {
     77              case 0: success = ArgumentCreater.CreateNewArgument(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;
     78              case 1: success = ArgumentDeleter.DeleteArgument(random, selectedTree, 3, 3); break;
     79              case 2: success = ArgumentDuplicater.DuplicateArgument(random, selectedTree, 3, 3); break;
     80              case 3: success = SubroutineCreater.CreateSubroutine(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;
     81              case 4: success = SubroutineDuplicater.DuplicateSubroutine(random, selectedTree, 3, 3); break;
     82              case 5: success = SubroutineDeleter.DeleteSubroutine(random, selectedTree, 3, 3); break;
     83            }
     84            if (!success) failedEvents++;
    8385            Util.IsValid(selectedTree);
    8486            newTrees.Add(selectedTree);
     
    9092              par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
    9193              par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
    92             } while (par0.Size > MAX_TREE_SIZE || par1.Size > MAX_TREE_SIZE);
    93             bool success;
    94             newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success));
    95             Assert.IsTrue(success); // crossover must succeed
     94            } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH);
     95            newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH));
    9696          }
    9797        }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentCreaterTest.cs

    r5445 r5549  
    2424using System.Collections.Generic;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2826using HeuristicLab.Random;
    2927using Microsoft.VisualStudio.TestTools.UnitTesting;
    30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3128
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3330  [TestClass]
    3431  public class ArgumentCreaterTest {
    3532    private const int POPULATION_SIZE = 1000;
    36     private const int MAX_TREE_SIZE = 100;
    37     private const int MAX_TREE_HEIGHT = 10;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3835    private TestContext testContextInstance;
    3936
     
    5350    [TestMethod()]
    5451    public void ArgumentCreaterDistributionsTest() {
    55       var trees = new List<SymbolicExpressionTree>();
     52      var trees = new List<ISymbolicExpressionTree>();
    5653      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5754      var random = new MersenneTwister(31415);
    5855      int failedOps = 0;
    5956      for (int i = 0; i < POPULATION_SIZE; i++) {
    60         SymbolicExpressionTree tree;
     57        ISymbolicExpressionTree tree;
    6158        do {
    62           tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     59          tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6360        } while (!TreeHasAdfWithParameter(tree, 3));
    64         var success = ArgumentCreater.CreateNewArgument(random, tree, grammar, 60000, 100, 3, 3);
     61        var success = ArgumentCreater.CreateNewArgument(random, tree, 60000, 100, 3, 3);
    6562        if (!success) failedOps++;
    6663        Util.IsValid(tree);
     
    7976    }
    8077
    81     private bool TreeHasAdfWithParameter(SymbolicExpressionTree tree, int maxParameters) {
    82       return tree.Root.SubTrees.Count == 2 &&
    83       tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() < maxParameters;
     78    private bool TreeHasAdfWithParameter(ISymbolicExpressionTree tree, int maxParameters) {
     79      if(tree.Root.SubTrees.Count() != 2 ) return false;
     80      var firstAdf = tree.Root.GetSubTree(1);
     81      return firstAdf.Grammar.GetAllowedSymbols(firstAdf.Symbol, 0).Where(x => x is Argument).Count() < maxParameters;
    8482    }
    8583  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentDeleterTest.cs

    r5445 r5549  
    2424using System.Collections.Generic;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2826using HeuristicLab.Random;
    2927using Microsoft.VisualStudio.TestTools.UnitTesting;
    30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3128
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3330  [TestClass]
    3431  public class ArgumentDeleterTest {
    3532    private const int POPULATION_SIZE = 1000;
    36     private const int MAX_TREE_SIZE = 100;
    37     private const int MAX_TREE_HEIGHT = 10;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3835    private TestContext testContextInstance;
    3936
     
    5350    [TestMethod()]
    5451    public void ArgumentDeleterDistributionsTest() {
    55       var trees = new List<SymbolicExpressionTree>();
     52      var trees = new List<ISymbolicExpressionTree>();
    5653      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5754      var random = new MersenneTwister(31415);
    5855      for (int i = 0; i < POPULATION_SIZE; i++) {
    59         SymbolicExpressionTree tree = null;
     56        ISymbolicExpressionTree tree = null;
    6057        do {
    61           tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     58          tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6259        } while (!TreeHasAdfWithArguments(tree));
    63         var success = ArgumentDeleter.DeleteArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     60        var success = ArgumentDeleter.DeleteArgument(random, tree, 3, 3);
    6461        Assert.IsTrue(success);
    6562        Util.IsValid(tree);
     
    7370        );
    7471    }
    75     private bool TreeHasAdfWithArguments(SymbolicExpressionTree tree) {
    76       return tree.Root.SubTrees.Count == 2 &&
    77         tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() >= 2;
     72    private bool TreeHasAdfWithArguments(ISymbolicExpressionTree tree) {
     73      if (tree.Root.SubTrees.Count() != 2) return false;
     74      var firstAdf = tree.Root.GetSubTree(1);
     75      return firstAdf.Grammar.GetAllowedSymbols(firstAdf.Symbol, 0).Where(x => x is Argument).Count() >= 2;
    7876    }
    7977  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentDuplicaterTest.cs

    r5445 r5549  
    2424using System.Collections.Generic;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2826using HeuristicLab.Random;
    2927using Microsoft.VisualStudio.TestTools.UnitTesting;
    30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3128
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3330  [TestClass]
    3431  public class ArgumentDuplicaterTest {
    3532    private const int POPULATION_SIZE = 1000;
    36     private const int MAX_TREE_SIZE = 100;
    37     private const int MAX_TREE_HEIGHT = 10;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3835    private TestContext testContextInstance;
    3936
     
    5350    [TestMethod()]
    5451    public void ArgumentDuplicaterDistributionsTest() {
    55       var trees = new List<SymbolicExpressionTree>();
     52      var trees = new List<ISymbolicExpressionTree>();
    5653      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5754      var random = new MersenneTwister(31415);
    5855      for (int i = 0; i < POPULATION_SIZE; i++) {
    59         SymbolicExpressionTree tree = null;
     56        ISymbolicExpressionTree tree = null;
    6057        do {
    61           tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     58          tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6259        } while(!HasAdfWithArguments(tree));
    63         var success = ArgumentDuplicater.DuplicateArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     60        var success = ArgumentDuplicater.DuplicateArgument(random, tree, 3, 3);
    6461        Assert.IsTrue(success);
    6562        Util.IsValid(tree);
     
    7471    }
    7572
    76     private bool HasAdfWithArguments(SymbolicExpressionTree tree) {
    77       return tree.Root.SubTrees.Count == 2 &&
    78         tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() == 1;
     73    private bool HasAdfWithArguments(ISymbolicExpressionTree tree) {
     74      if (tree.Root.SubTrees.Count() != 2) return false;
     75      var firstAdf = tree.Root.GetSubTree(1);
     76      return firstAdf.Grammar.GetAllowedSymbols(firstAdf.Symbol, 0).Where(x => x is Argument).Count() == 1;
    7977    }
    8078  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ChangeNodeTypeManipulationTest.cs

    r5445 r5549  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
    2725using HeuristicLab.Random;
    2826using Microsoft.VisualStudio.TestTools.UnitTesting;
    2927
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3129  [TestClass]
    3230  public class ChangeNodeTypeManipulationTest {
    3331    private const int POPULATION_SIZE = 1000;
    34     private const int MAX_TREE_SIZE = 100;
    35     private const int MAX_TREE_HEIGHT = 10;
     32    private const int MAX_TREE_LENGTH = 100;
     33    private const int MAX_TREE_DEPTH = 10;
    3634    private TestContext testContextInstance;
    3735
     
    5149    [TestMethod()]
    5250    public void ChangeNodeTypeManipulationDistributionsTest() {
    53       var trees = new List<SymbolicExpressionTree>();
     51      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
     52      var trees = new List<ISymbolicExpressionTree>();
    5453      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5554      var random = new MersenneTwister(31415);
    5655      for (int i = 0; i < POPULATION_SIZE; i++) {
    57         var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    58         bool success;
    59         ChangeNodeTypeManipulation.ChangeNodeType(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success);
    60         Assert.IsTrue(success);
     56        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
     57        string originalTree = formatter.Format(tree);
     58        ChangeNodeTypeManipulation.ChangeNodeType(random, tree);
     59        string manipulatedTree = formatter.Format(tree);
     60        Assert.IsFalse(originalTree == manipulatedTree);
    6161        Util.IsValid(tree);
    6262        trees.Add(tree);
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs

    r5445 r5549  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2625
    27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    2827  public static class Grammars {
    2928    private class Addition : Symbol {
     
    109108    }
    110109
    111     public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
     110    public static ISymbolicExpressionTreeGrammar CreateSimpleArithmeticGrammar() {
    112111      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
    113112      g.MaxFunctionArguments = 0;
     
    118117    }
    119118
    120     public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
     119    public static ISymbolicExpressionTreeGrammar CreateArithmeticAndAdfGrammar() {
    121120      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
    122121      g.MaxFunctionArguments = 3;
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests.csproj

    r5477 r5549  
    99    <OutputType>Library</OutputType>
    1010    <AppDesignerFolder>Properties</AppDesignerFolder>
    11     <RootNamespace>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests</RootNamespace>
     11    <RootNamespace>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests</RootNamespace>
    1212    <AssemblyName>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests</AssemblyName>
    1313    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
     
    153153      <Name>HeuristicLab.Random-3.3</Name>
    154154    </ProjectReference>
    155     <ProjectReference Include="..\..\3.3\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj">
    156       <Project>{125D3006-67F5-48CB-913E-73C0548F17FA}</Project>
    157       <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3</Name>
     155    <ProjectReference Include="..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
     156      <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project>
     157      <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name>
    158158    </ProjectReference>
    159159  </ItemGroup>
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ProbabilisticTreeCreaterTest.cs

    r5445 r5549  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2625using HeuristicLab.Random;
    2726using Microsoft.VisualStudio.TestTools.UnitTesting;
     27using System.Diagnostics;
    2828
    29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3030  [TestClass]
    3131  public class ProbabilisticTreeCreaterTest {
    32     private const int POPULATION_SIZE = 1000;
    33     private const int MAX_TREE_SIZE = 100;
    34     private const int MAX_TREE_HEIGHT = 10;
     32    private const int POPULATION_SIZE = 10000;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3535    private TestContext testContextInstance;
    3636
     
    5050    [TestMethod()]
    5151    public void ProbabilisticTreeCreaterDistributionsTest() {
    52       var randomTrees = new List<SymbolicExpressionTree>();
     52      var randomTrees = new List<ISymbolicExpressionTree>();
    5353      var grammar = Grammars.CreateSimpleArithmeticGrammar();
    5454      var random = new MersenneTwister(31415);
     55      var stopwatch = new Stopwatch();
     56      stopwatch.Start();
    5557      for (int i = 0; i < POPULATION_SIZE; i++) {
    56         randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 0, 0));
     58        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 0, 0));
    5759      }
     60      stopwatch.Stop();
    5861
    5962      foreach (var tree in randomTrees) {
    6063        Util.IsValid(tree);
    6164      }
     65      double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
     66
    6267      Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine +
     68        msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
    6369        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
    6470        Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
     
    6672        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
    6773        );
     74      Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 2000); // must achieve more than 2000 random trees / s
    6875    }
    6976
     
    7178    [TestMethod()]
    7279    public void ProbabilisticTreeCreaterWithAdfDistributionsTest() {
    73       var randomTrees = new List<SymbolicExpressionTree>();
     80      var randomTrees = new List<ISymbolicExpressionTree>();
    7481      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    7582      var random = new MersenneTwister(31415);
     83      var stopwatch = new Stopwatch();
     84      stopwatch.Start();
    7685      for (int i = 0; i < POPULATION_SIZE; i++) {
    77         var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    78         Util.IsValid(tree);
     86        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    7987        randomTrees.Add(tree);
    8088      }
     89      stopwatch.Stop();
     90      foreach (var tree in randomTrees)
     91        Util.IsValid(tree);
     92
     93      double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
     94
    8195      Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine +
     96        msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
    8297        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
    8398        Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
     
    85100        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
    86101        );
     102
     103      Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 2000); // must achieve more than 2000 random trees / s
    87104    }
    88105  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ReplaceBranchManipulationTest.cs

    r5445 r5549  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
    2725using HeuristicLab.Random;
    2826using Microsoft.VisualStudio.TestTools.UnitTesting;
    2927
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3129  [TestClass]
    3230  public class ReplaceBranchManipulationTest {
    3331    private const int POPULATION_SIZE = 1000;
    34     private const int MAX_TREE_SIZE = 100;
    35     private const int MAX_TREE_HEIGHT = 10;
     32    private const int MAX_TREE_LENGTH = 100;
     33    private const int MAX_TREE_DEPTH = 10;
    3634    private TestContext testContextInstance;
    3735
     
    5149    [TestMethod()]
    5250    public void ReplaceBranchManipulationDistributionsTest() {
    53       var trees = new List<SymbolicExpressionTree>();
     51      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
     52      var trees = new List<ISymbolicExpressionTree>();
    5453      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5554      var random = new MersenneTwister(31415);
    5655      for (int i = 0; i < POPULATION_SIZE; i++) {
    57         var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
    58         bool success;
    59         ReplaceBranchManipulation.ReplaceRandomBranch(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success);
    60         Assert.IsTrue(success);
     56        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
     57        string originalTree = formatter.Format(tree);
     58        ReplaceBranchManipulation.ReplaceRandomBranch(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
     59        string manipulatedTree = formatter.Format(tree);
     60        Assert.IsFalse(originalTree == manipulatedTree);
    6161        Util.IsValid(tree);
    6262        trees.Add(tree);
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineCreaterTest.cs

    r5445 r5549  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections.Generic;
    2425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2726using HeuristicLab.Random;
    2827using Microsoft.VisualStudio.TestTools.UnitTesting;
    2928
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3130  [TestClass]
    3231  public class SubroutineCreaterTest {
    3332    private const int POPULATION_SIZE = 1000;
    34     private const int MAX_TREE_SIZE = 100;
    35     private const int MAX_TREE_HEIGHT = 10;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3635    private TestContext testContextInstance;
    3736
     
    5150    [TestMethod()]
    5251    public void SubroutineCreaterDistributionsTest() {
    53       var trees = new List<SymbolicExpressionTree>();
     52      var trees = new List<ISymbolicExpressionTree>();
    5453      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5554      var random = new MersenneTwister(31415);
    5655      for (int i = 0; i < POPULATION_SIZE; i++) {
    57         SymbolicExpressionTree tree = null;
     56        ISymbolicExpressionTree tree = null;
    5857        do {
    59           tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     58          tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6059        } while ( !OneMoreAdfAllowed(tree));
    61         var success = SubroutineCreater.CreateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     60        var success = SubroutineCreater.CreateSubroutine(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6261        Assert.IsTrue(success);
    6362        Util.IsValid(tree);
     
    7271    }
    7372
    74     private bool OneMoreAdfAllowed(SymbolicExpressionTree tree) {
    75       return tree.Size < 80 && tree.Root.SubTrees.Count < 4;
     73    private bool OneMoreAdfAllowed(ISymbolicExpressionTree tree) {
     74      return tree.Length < 80 && tree.Root.SubTrees.Count() < 4;
    7675    }
    7776  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineDeleterTest.cs

    r5445 r5549  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections.Generic;
    2425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2726using HeuristicLab.Random;
    2827using Microsoft.VisualStudio.TestTools.UnitTesting;
    2928
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3130  [TestClass]
    3231  public class SubroutineDeleterTest {
    3332    private const int POPULATION_SIZE = 1000;
    34     private const int MAX_TREE_SIZE = 100;
    35     private const int MAX_TREE_HEIGHT = 10;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3635    private TestContext testContextInstance;
    3736
     
    5150    [TestMethod()]
    5251    public void SubroutineDeleterDistributionsTest() {
    53       var trees = new List<SymbolicExpressionTree>();
     52      var trees = new List<ISymbolicExpressionTree>();
    5453      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5554      var random = new MersenneTwister(31415);
    5655      for (int i = 0; i < POPULATION_SIZE; i++) {
    57         SymbolicExpressionTree tree = null;
     56        ISymbolicExpressionTree tree = null;
    5857        do {
    59           tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     58          tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6059        } while (!HasAtLeastOneAdf(tree));
    61         var success = SubroutineDeleter.DeleteSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     60        var success = SubroutineDeleter.DeleteSubroutine(random, tree, 3, 3);
    6261        Assert.IsTrue(success);
    6362        Util.IsValid(tree);
     
    7271    }
    7372
    74     private bool HasAtLeastOneAdf(SymbolicExpressionTree tree) {
    75       return tree.Root.SubTrees.Count > 1;
     73    private bool HasAtLeastOneAdf(ISymbolicExpressionTree tree) {
     74      return tree.Root.SubTrees.Count() > 1;
    7675    }
    7776  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineDuplicaterTest.cs

    r5445 r5549  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections.Generic;
    2425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    2726using HeuristicLab.Random;
    2827using Microsoft.VisualStudio.TestTools.UnitTesting;
    2928
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3130  [TestClass]
    3231  public class SubroutineDuplicaterTest {
    3332    private const int POPULATION_SIZE = 1000;
    34     private const int MAX_TREE_SIZE = 100;
    35     private const int MAX_TREE_HEIGHT = 10;
     33    private const int MAX_TREE_LENGTH = 100;
     34    private const int MAX_TREE_DEPTH = 10;
    3635    private TestContext testContextInstance;
    3736
     
    5150    [TestMethod()]
    5251    public void SubroutineDuplicaterDistributionsTest() {
    53       var trees = new List<SymbolicExpressionTree>();
     52      var trees = new List<ISymbolicExpressionTree>();
    5453      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5554      var random = new MersenneTwister();
    5655      for (int i = 0; i < POPULATION_SIZE; i++) {
    57         SymbolicExpressionTree tree = null;
     56        ISymbolicExpressionTree tree = null;
    5857        do {
    59           tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     58          tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
    6059        } while (!HasOneAdf(tree));
    61         var success = SubroutineDuplicater.DuplicateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
     60        var success = SubroutineDuplicater.DuplicateSubroutine(random, tree, 3, 3);
    6261        Assert.IsTrue(success);
    6362        Util.IsValid(tree);
     
    7271    }
    7372
    74     private bool HasOneAdf(SymbolicExpressionTree tree) {
    75       return tree.Root.SubTrees.Count == 2;
     73    private bool HasOneAdf(ISymbolicExpressionTree tree) {
     74      return tree.Root.SubTrees.Count() == 2;
    7675    }
    7776  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubtreeCrossoverTest.cs

    r5445 r5549  
    2424using System.Diagnostics;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
    2826using HeuristicLab.Random;
    2927using Microsoft.VisualStudio.TestTools.UnitTesting;
    3028
    31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3230  [TestClass]
    3331  public class SubtreeCrossoverTest {
     
    5149    public void SubtreeCrossoverDistributionsTest() {
    5250      int generations = 5;
    53       var trees = new List<SymbolicExpressionTree>();
     51      var trees = new List<ISymbolicExpressionTree>();
    5452      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5553      var random = new MersenneTwister(31415);
    56       List<SymbolicExpressionTree> crossoverTrees;
     54      List<ISymbolicExpressionTree> crossoverTrees;
    5755      double msPerCrossoverEvent;
    5856
     
    6361      stopwatch.Start();
    6462      for (int gCount = 0; gCount < generations; gCount++) {
    65         var newPopulation = new List<SymbolicExpressionTree>();
     63        var newPopulation = new List<ISymbolicExpressionTree>();
    6664        for (int i = 0; i < POPULATION_SIZE; i++) {
    6765          var par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
    6866          var par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
    69           bool success;
    70           newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success));
    71           Assert.IsTrue(success);
     67          newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10));
    7268        }
    7369        crossoverTrees = newPopulation;
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs

    r5445 r5549  
    2525using System.Text;
    2626using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2827using Microsoft.VisualStudio.TestTools.UnitTesting;
    2928
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    3130  public static class Util {
    32     public static string GetSizeDistributionString(IList<SymbolicExpressionTree> trees, int maxTreeSize, int binSize) {
    33       int[] histogram = new int[maxTreeSize / binSize];
     31    public static string GetSizeDistributionString(IList<ISymbolicExpressionTree> trees, int maxTreeLength, int binSize) {
     32      int[] histogram = new int[maxTreeLength / binSize];
    3433      for (int i = 0; i < trees.Count; i++) {
    35         int binIndex = Math.Min(histogram.Length - 1, trees[i].Size / binSize);
     34        int binIndex = Math.Min(histogram.Length - 1, trees[i].Length / binSize);
    3635        histogram[binIndex]++;
    3736      }
     
    4948    }
    5049
    51     public static string GetFunctionDistributionString(IList<SymbolicExpressionTree> trees) {
     50    public static string GetFunctionDistributionString(IList<ISymbolicExpressionTree> trees) {
    5251      Dictionary<string, int> occurances = new Dictionary<string, int>();
    5352      double n = 0.0;
    5453      for (int i = 0; i < trees.Count; i++) {
    5554        foreach (var node in trees[i].IterateNodesPrefix()) {
    56           if (node.SubTrees.Count > 0) {
     55          if (node.SubTrees.Count() > 0) {
    5756            if (!occurances.ContainsKey(node.Symbol.Name))
    5857              occurances[node.Symbol.Name] = 0;
     
    7170    }
    7271
    73     public static string GetNumberOfSubTreesDistributionString(IList<SymbolicExpressionTree> trees) {
     72    public static string GetNumberOfSubTreesDistributionString(IList<ISymbolicExpressionTree> trees) {
    7473      Dictionary<int, int> occurances = new Dictionary<int, int>();
    7574      double n = 0.0;
    7675      for (int i = 0; i < trees.Count; i++) {
    7776        foreach (var node in trees[i].IterateNodesPrefix()) {
    78           if (!occurances.ContainsKey(node.SubTrees.Count))
    79             occurances[node.SubTrees.Count] = 0;
    80           occurances[node.SubTrees.Count]++;
     77          if (!occurances.ContainsKey(node.SubTrees.Count()))
     78            occurances[node.SubTrees.Count()] = 0;
     79          occurances[node.SubTrees.Count()]++;
    8180          n++;
    8281        }
     
    9291
    9392
    94     public static string GetTerminalDistributionString(IList<SymbolicExpressionTree> trees) {
     93    public static string GetTerminalDistributionString(IList<ISymbolicExpressionTree> trees) {
    9594      Dictionary<string, int> occurances = new Dictionary<string, int>();
    9695      double n = 0.0;
    9796      for (int i = 0; i < trees.Count; i++) {
    9897        foreach (var node in trees[i].IterateNodesPrefix()) {
    99           if (node.SubTrees.Count == 0) {
     98          if (node.SubTrees.Count() == 0) {
    10099            if (!occurances.ContainsKey(node.Symbol.Name))
    101100              occurances[node.Symbol.Name] = 0;
     
    114113    }
    115114
    116     public static void IsValid(SymbolicExpressionTree tree) {
    117       int reportedSize = tree.Size;
     115    public static void IsValid(ISymbolicExpressionTree tree) {
     116      int reportedSize = tree.Length;
    118117      int actualSize = tree.IterateNodesPostfix().Count();
    119118      Assert.AreEqual(actualSize, reportedSize);
     
    135134    }
    136135
    137     public static void IsValid(SymbolicExpressionTreeNode treeNode) {
     136    public static void IsValid(ISymbolicExpressionTreeNode treeNode) {
    138137      var matchingSymbol = (from symb in treeNode.Grammar.Symbols
    139138                            where symb.Name == treeNode.Symbol.Name
    140139                            select symb).SingleOrDefault();
    141       Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));
    142       Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));
     140      Assert.IsTrue(treeNode.SubTrees.Count() >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));
     141      Assert.IsTrue(treeNode.SubTrees.Count() <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));
    143142      Assert.AreNotEqual(0.0, matchingSymbol.InitialFrequency); // check that no deactivated symbols occur in the tree
    144       for (int i = 0; i < treeNode.SubTrees.Count; i++) {
    145         Assert.IsTrue(treeNode.GetAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));
    146         IsValid(treeNode.SubTrees[i]);
     143      for (int i = 0; i < treeNode.SubTrees.Count(); i++) {
     144        Assert.IsTrue(treeNode.Grammar.GetAllowedSymbols(treeNode.Symbol, i).Select(x => x.Name).Contains(treeNode.GetSubTree(i).Symbol.Name));
     145        IsValid(treeNode.GetSubTree(i));
    147146      }
    148147    }
Note: See TracChangeset for help on using the changeset viewer.