- Timestamp:
- 04/15/10 19:58:42 (15 years ago)
- 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 1 1 bin 2 2 obj 3 *.user
-
- Property svn:ignore
-
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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 34 55 var trees = new List<SymbolicExpressionTree>(); 35 56 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 36 var random = new MersenneTwister(); 57 var random = new MersenneTwister(31415); 58 int failedEvents = 0; 37 59 for (int i = 0; i < POPULATION_SIZE; i++) { 38 60 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); 41 64 trees.Add(tree); 42 65 } 43 66 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 + 45 69 Util.GetFunctionDistributionString(trees) + Environment.NewLine + 46 70 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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 34 55 var trees = new List<SymbolicExpressionTree>(); 35 56 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 36 var random = new MersenneTwister(); 57 var random = new MersenneTwister(31415); 58 int failedEvents = 0; 37 59 for (int i = 0; i < POPULATION_SIZE; i++) { 38 60 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); 41 64 trees.Add(tree); 42 65 } 43 66 Assert.Inconclusive("ArgumentDeleter: " + Environment.NewLine + 67 "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine + 44 68 Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine + 45 69 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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 74 95 return g; 75 96 } 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 76 120 } 77 121 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj
r3338 r3360 41 41 <Compile Include="ArgumentCreaterTest.cs" /> 42 42 <Compile Include="ArgumentDeleterTest.cs" /> 43 <Compile Include="ArgumentDuplicaterTest.cs" /> 44 <Compile Include="AllArchitectureAlteringOperatorsTest.cs" /> 45 <Compile Include="SubroutineDeleterTest.cs" /> 46 <Compile Include="SubroutineDuplicaterTest.cs" /> 43 47 <Compile Include="Grammars.cs" /> 48 <Compile Include="SubroutineCreaterTest.cs"> 49 <SubType>Code</SubType> 50 </Compile> 44 51 <Compile Include="SubtreeCrossoverTest.cs"> 45 52 <SubType>Code</SubType> … … 50 57 </ItemGroup> 51 58 <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> 52 63 <ProjectReference Include="..\..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> 53 64 <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project> 54 65 <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> 55 70 </ProjectReference> 56 71 <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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 38 59 } 39 60 40 foreach (var tree in randomTrees) 41 Assert.IsTrue(tree.IsValidExpression()); 61 foreach (var tree in randomTrees) { 62 Util.IsValid(tree); 63 } 42 64 Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine + 43 65 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + … … 55 77 var random = new MersenneTwister(); 56 78 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); 58 83 } 59 foreach (var tree in randomTrees)60 Assert.IsTrue(tree.IsValidExpression());61 84 Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine + 62 85 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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 8 29 using System.Diagnostics; 9 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators; 10 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;11 31 12 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests { 13 33 [TestClass] 14 34 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; 19 38 private TestContext testContextInstance; 20 39 … … 32 51 } 33 52 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 separately45 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); 46 65 } 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 ); 82 73 } 83 74 } -
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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 11 32 [TestClass] 12 33 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; 17 35 private TestContext testContextInstance; 18 36 … … 30 48 } 31 49 32 [ClassInitialize()] 33 public static void SubtreeCrossoverTestInitialize(TestContext testContext) { 34 crossoverTrees = new List<SymbolicExpressionTree>(); 35 int populationSize = 1000; 50 [TestMethod()] 51 public void SubtreeCrossoverDistributionsTest() { 36 52 int generations = 5; 53 var trees = new List<SymbolicExpressionTree>(); 54 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 var random = new MersenneTwister(); 37 56 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)); 42 62 } 43 63 Stopwatch stopwatch = new Stopwatch(); … … 45 65 for (int gCount = 0; gCount < generations; gCount++) { 46 66 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(); 50 70 bool success; 51 71 newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success)); … … 55 75 } 56 76 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); 61 79 80 msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)generations; 62 81 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 ); 90 90 } 91 91 } -
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 22 using System; 2 23 using System.Text; 3 24 using System.Collections.Generic; … … 13 34 int[] histogram = new int[maxTreeSize / binSize]; 14 35 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]++; 16 38 } 17 39 StringBuilder strBuilder = new StringBuilder(); 18 for (int i = 0; i < histogram.Length ; i++) {40 for (int i = 0; i < histogram.Length - 1; i++) { 19 41 strBuilder.Append(Environment.NewLine); 20 42 strBuilder.Append("< "); strBuilder.Append((i + 1) * binSize); 21 43 strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)trees.Count); 22 44 } 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 23 49 return "Size distribution: " + strBuilder; 24 50 } … … 88 114 return "Terminal distribution: " + strBuilder; 89 115 } 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 } 90 136 } 91 137 }
Note: See TracChangeset
for help on using the changeset viewer.