Changeset 5549 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests
- Timestamp:
- 02/22/11 19:04:54 (14 years ago)
- 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 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;30 27 using HeuristicLab.Random; 31 28 using Microsoft.VisualStudio.TestTools.UnitTesting; 32 29 33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 34 31 [TestClass] 35 32 public class AllArchitectureAlteringOperatorsTest { 36 33 private const int POPULATION_SIZE = 1000; 37 34 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; 40 37 private TestContext testContextInstance; 41 38 … … 55 52 [TestMethod()] 56 53 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>(); 59 56 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 60 57 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); 63 60 IntValue maxDefuns = new IntValue(3); 64 61 IntValue maxArgs = new IntValue(3); 65 62 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); 67 64 Util.IsValid(tree); 68 65 trees.Add(tree); … … 70 67 Stopwatch stopwatch = new Stopwatch(); 71 68 stopwatch.Start(); 72 var combinedAAOperator = new MultiSymbolicExpressionTreeArchitectureManipulator();73 69 int failedEvents = 0; 74 70 for (int g = 0; g < N_ITERATIONS; g++) { … … 76 72 if (random.NextDouble() < 0.5) { 77 73 // manipulate 78 var selectedTree = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 79 var op = combinedAAOperator.Operators.SelectRandom(random); 74 var selectedTree = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone(); 80 75 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++; 83 85 Util.IsValid(selectedTree); 84 86 newTrees.Add(selectedTree); … … 90 92 par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 91 93 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)); 96 96 } 97 97 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentCreaterTest.cs
r5445 r5549 24 24 using System.Collections.Generic; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;31 28 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 33 30 [TestClass] 34 31 public class ArgumentCreaterTest { 35 32 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; 38 35 private TestContext testContextInstance; 39 36 … … 53 50 [TestMethod()] 54 51 public void ArgumentCreaterDistributionsTest() { 55 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 56 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 54 var random = new MersenneTwister(31415); 58 55 int failedOps = 0; 59 56 for (int i = 0; i < POPULATION_SIZE; i++) { 60 SymbolicExpressionTree tree;57 ISymbolicExpressionTree tree; 61 58 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); 63 60 } 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); 65 62 if (!success) failedOps++; 66 63 Util.IsValid(tree); … … 79 76 } 80 77 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; 84 82 } 85 83 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentDeleterTest.cs
r5445 r5549 24 24 using System.Collections.Generic; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;31 28 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 33 30 [TestClass] 34 31 public class ArgumentDeleterTest { 35 32 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; 38 35 private TestContext testContextInstance; 39 36 … … 53 50 [TestMethod()] 54 51 public void ArgumentDeleterDistributionsTest() { 55 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 56 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 54 var random = new MersenneTwister(31415); 58 55 for (int i = 0; i < POPULATION_SIZE; i++) { 59 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 60 57 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); 62 59 } 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); 64 61 Assert.IsTrue(success); 65 62 Util.IsValid(tree); … … 73 70 ); 74 71 } 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; 78 76 } 79 77 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentDuplicaterTest.cs
r5445 r5549 24 24 using System.Collections.Generic; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;31 28 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 33 30 [TestClass] 34 31 public class ArgumentDuplicaterTest { 35 32 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; 38 35 private TestContext testContextInstance; 39 36 … … 53 50 [TestMethod()] 54 51 public void ArgumentDuplicaterDistributionsTest() { 55 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 56 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 54 var random = new MersenneTwister(31415); 58 55 for (int i = 0; i < POPULATION_SIZE; i++) { 59 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 60 57 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); 62 59 } 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); 64 61 Assert.IsTrue(success); 65 62 Util.IsValid(tree); … … 74 71 } 75 72 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; 79 77 } 80 78 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ChangeNodeTypeManipulationTest.cs
r5445 r5549 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;27 25 using HeuristicLab.Random; 28 26 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 27 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 29 [TestClass] 32 30 public class ChangeNodeTypeManipulationTest { 33 31 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; 36 34 private TestContext testContextInstance; 37 35 … … 51 49 [TestMethod()] 52 50 public void ChangeNodeTypeManipulationDistributionsTest() { 53 var trees = new List<SymbolicExpressionTree>(); 51 SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter(); 52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 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); 61 61 Util.IsValid(tree); 62 62 trees.Add(tree); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs
r5445 r5549 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;26 25 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 28 27 public static class Grammars { 29 28 private class Addition : Symbol { … … 109 108 } 110 109 111 public static ISymbolicExpression Grammar CreateSimpleArithmeticGrammar() {110 public static ISymbolicExpressionTreeGrammar CreateSimpleArithmeticGrammar() { 112 111 var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar()); 113 112 g.MaxFunctionArguments = 0; … … 118 117 } 119 118 120 public static ISymbolicExpression Grammar CreateArithmeticAndAdfGrammar() {119 public static ISymbolicExpressionTreeGrammar CreateArithmeticAndAdfGrammar() { 121 120 var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar()); 122 121 g.MaxFunctionArguments = 3; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests.csproj
r5477 r5549 9 9 <OutputType>Library</OutputType> 10 10 <AppDesignerFolder>Properties</AppDesignerFolder> 11 <RootNamespace>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests</RootNamespace>11 <RootNamespace>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests</RootNamespace> 12 12 <AssemblyName>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests</AssemblyName> 13 13 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> … … 153 153 <Name>HeuristicLab.Random-3.3</Name> 154 154 </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> 158 158 </ProjectReference> 159 159 </ItemGroup> -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ProbabilisticTreeCreaterTest.cs
r5445 r5549 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;26 25 using HeuristicLab.Random; 27 26 using Microsoft.VisualStudio.TestTools.UnitTesting; 27 using System.Diagnostics; 28 28 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 30 30 [TestClass] 31 31 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; 35 35 private TestContext testContextInstance; 36 36 … … 50 50 [TestMethod()] 51 51 public void ProbabilisticTreeCreaterDistributionsTest() { 52 var randomTrees = new List< SymbolicExpressionTree>();52 var randomTrees = new List<ISymbolicExpressionTree>(); 53 53 var grammar = Grammars.CreateSimpleArithmeticGrammar(); 54 54 var random = new MersenneTwister(31415); 55 var stopwatch = new Stopwatch(); 56 stopwatch.Start(); 55 57 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)); 57 59 } 60 stopwatch.Stop(); 58 61 59 62 foreach (var tree in randomTrees) { 60 63 Util.IsValid(tree); 61 64 } 65 double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE; 66 62 67 Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine + 68 msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine + 63 69 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + 64 70 Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine + … … 66 72 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 67 73 ); 74 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 2000); // must achieve more than 2000 random trees / s 68 75 } 69 76 … … 71 78 [TestMethod()] 72 79 public void ProbabilisticTreeCreaterWithAdfDistributionsTest() { 73 var randomTrees = new List< SymbolicExpressionTree>();80 var randomTrees = new List<ISymbolicExpressionTree>(); 74 81 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 75 82 var random = new MersenneTwister(31415); 83 var stopwatch = new Stopwatch(); 84 stopwatch.Start(); 76 85 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); 79 87 randomTrees.Add(tree); 80 88 } 89 stopwatch.Stop(); 90 foreach (var tree in randomTrees) 91 Util.IsValid(tree); 92 93 double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE; 94 81 95 Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine + 96 msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine + 82 97 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + 83 98 Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine + … … 85 100 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 86 101 ); 102 103 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 2000); // must achieve more than 2000 random trees / s 87 104 } 88 105 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ReplaceBranchManipulationTest.cs
r5445 r5549 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;27 25 using HeuristicLab.Random; 28 26 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 27 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 29 [TestClass] 32 30 public class ReplaceBranchManipulationTest { 33 31 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; 36 34 private TestContext testContextInstance; 37 35 … … 51 49 [TestMethod()] 52 50 public void ReplaceBranchManipulationDistributionsTest() { 53 var trees = new List<SymbolicExpressionTree>(); 51 SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter(); 52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 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); 61 61 Util.IsValid(tree); 62 62 trees.Add(tree); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineCreaterTest.cs
r5445 r5549 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 26 using HeuristicLab.Random; 28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 [TestClass] 32 31 public class SubroutineCreaterTest { 33 32 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; 36 35 private TestContext testContextInstance; 37 36 … … 51 50 [TestMethod()] 52 51 public void SubroutineCreaterDistributionsTest() { 53 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 58 57 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); 60 59 } 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); 62 61 Assert.IsTrue(success); 63 62 Util.IsValid(tree); … … 72 71 } 73 72 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; 76 75 } 77 76 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineDeleterTest.cs
r5445 r5549 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 26 using HeuristicLab.Random; 28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 [TestClass] 32 31 public class SubroutineDeleterTest { 33 32 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; 36 35 private TestContext testContextInstance; 37 36 … … 51 50 [TestMethod()] 52 51 public void SubroutineDeleterDistributionsTest() { 53 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 58 57 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); 60 59 } 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); 62 61 Assert.IsTrue(success); 63 62 Util.IsValid(tree); … … 72 71 } 73 72 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; 76 75 } 77 76 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineDuplicaterTest.cs
r5445 r5549 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 26 using HeuristicLab.Random; 28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 [TestClass] 32 31 public class SubroutineDuplicaterTest { 33 32 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; 36 35 private TestContext testContextInstance; 37 36 … … 51 50 [TestMethod()] 52 51 public void SubroutineDuplicaterDistributionsTest() { 53 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 58 57 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); 60 59 } 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); 62 61 Assert.IsTrue(success); 63 62 Util.IsValid(tree); … … 72 71 } 73 72 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; 76 75 } 77 76 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubtreeCrossoverTest.cs
r5445 r5549 24 24 using System.Diagnostics; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 28 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 32 30 [TestClass] 33 31 public class SubtreeCrossoverTest { … … 51 49 public void SubtreeCrossoverDistributionsTest() { 52 50 int generations = 5; 53 var trees = new List< SymbolicExpressionTree>();51 var trees = new List<ISymbolicExpressionTree>(); 54 52 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 53 var random = new MersenneTwister(31415); 56 List< SymbolicExpressionTree> crossoverTrees;54 List<ISymbolicExpressionTree> crossoverTrees; 57 55 double msPerCrossoverEvent; 58 56 … … 63 61 stopwatch.Start(); 64 62 for (int gCount = 0; gCount < generations; gCount++) { 65 var newPopulation = new List< SymbolicExpressionTree>();63 var newPopulation = new List<ISymbolicExpressionTree>(); 66 64 for (int i = 0; i < POPULATION_SIZE; i++) { 67 65 var par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 68 66 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)); 72 68 } 73 69 crossoverTrees = newPopulation; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs
r5445 r5549 25 25 using System.Text; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 public static class Util { 32 public static string GetSizeDistributionString(IList< SymbolicExpressionTree> trees, int maxTreeSize, int binSize) {33 int[] histogram = new int[maxTree Size/ binSize];31 public static string GetSizeDistributionString(IList<ISymbolicExpressionTree> trees, int maxTreeLength, int binSize) { 32 int[] histogram = new int[maxTreeLength / binSize]; 34 33 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); 36 35 histogram[binIndex]++; 37 36 } … … 49 48 } 50 49 51 public static string GetFunctionDistributionString(IList< SymbolicExpressionTree> trees) {50 public static string GetFunctionDistributionString(IList<ISymbolicExpressionTree> trees) { 52 51 Dictionary<string, int> occurances = new Dictionary<string, int>(); 53 52 double n = 0.0; 54 53 for (int i = 0; i < trees.Count; i++) { 55 54 foreach (var node in trees[i].IterateNodesPrefix()) { 56 if (node.SubTrees.Count > 0) {55 if (node.SubTrees.Count() > 0) { 57 56 if (!occurances.ContainsKey(node.Symbol.Name)) 58 57 occurances[node.Symbol.Name] = 0; … … 71 70 } 72 71 73 public static string GetNumberOfSubTreesDistributionString(IList< SymbolicExpressionTree> trees) {72 public static string GetNumberOfSubTreesDistributionString(IList<ISymbolicExpressionTree> trees) { 74 73 Dictionary<int, int> occurances = new Dictionary<int, int>(); 75 74 double n = 0.0; 76 75 for (int i = 0; i < trees.Count; i++) { 77 76 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()]++; 81 80 n++; 82 81 } … … 92 91 93 92 94 public static string GetTerminalDistributionString(IList< SymbolicExpressionTree> trees) {93 public static string GetTerminalDistributionString(IList<ISymbolicExpressionTree> trees) { 95 94 Dictionary<string, int> occurances = new Dictionary<string, int>(); 96 95 double n = 0.0; 97 96 for (int i = 0; i < trees.Count; i++) { 98 97 foreach (var node in trees[i].IterateNodesPrefix()) { 99 if (node.SubTrees.Count == 0) {98 if (node.SubTrees.Count() == 0) { 100 99 if (!occurances.ContainsKey(node.Symbol.Name)) 101 100 occurances[node.Symbol.Name] = 0; … … 114 113 } 115 114 116 public static void IsValid( SymbolicExpressionTree tree) {117 int reportedSize = tree. Size;115 public static void IsValid(ISymbolicExpressionTree tree) { 116 int reportedSize = tree.Length; 118 117 int actualSize = tree.IterateNodesPostfix().Count(); 119 118 Assert.AreEqual(actualSize, reportedSize); … … 135 134 } 136 135 137 public static void IsValid( SymbolicExpressionTreeNode treeNode) {136 public static void IsValid(ISymbolicExpressionTreeNode treeNode) { 138 137 var matchingSymbol = (from symb in treeNode.Grammar.Symbols 139 138 where symb.Name == treeNode.Symbol.Name 140 139 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)); 143 142 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.G etAllowedSymbols(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)); 147 146 } 148 147 }
Note: See TracChangeset
for help on using the changeset viewer.