Changeset 5411
- Timestamp:
- 02/03/11 13:47:16 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDeleter.cs
r4722 r5411 28 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using System.Collections.Generic; 30 31 31 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators { … … 97 98 // deletion by random regeneration 98 99 SymbolicExpressionTreeNode replacementTree = null; 99 // TODO: should weight symbols by tickets 100 var selectedSymbol = invocationCutPoint.Parent.GetAllowedSymbols(invocationCutPoint.ReplacedChildIndex).SelectRandom(random); 100 var allowedSymbolsList = invocationCutPoint.Parent.GetAllowedSymbols(invocationCutPoint.ReplacedChildIndex).ToList(); 101 var weights = allowedSymbolsList.Select(s => s.InitialFrequency); 102 var selectedSymbol = allowedSymbolsList.SelectRandom(weights, random); 101 103 102 104 int minPossibleSize = invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/EnumerableExtensions.cs
r4068 r5411 23 23 using System.Linq; 24 24 using HeuristicLab.Core; 25 using System; 25 26 26 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 35 36 } 36 37 } 38 public static T SelectRandom<T>(this IEnumerable<T> xs, IEnumerable<double> weights, IRandom random) { 39 var list = xs as IList<T>; 40 var weightsList = weights as IList<double>; 41 if (list == null) { 42 list = xs.ToList(); 43 } 44 if (weightsList == null) { 45 weightsList = weights.ToList(); 46 } 47 if (list.Count != weightsList.Count) throw new ArgumentException("Number of elements in enumerations doesn't match."); 48 if (list.Count == 0) throw new ArgumentException("Enumeration is empty", "xs"); 49 50 double sum = weightsList.Sum(); 51 double r = random.NextDouble() * sum; 52 double agg = 0; 53 for (int i = 0; i < list.Count; i++) { 54 agg += weightsList[i]; 55 if (agg > r) return list[i]; 56 } 57 // should never happen 58 throw new InvalidOperationException(); 59 } 37 60 } 38 61 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
r5015 r5411 37 37 38 38 // cached values to prevent unnecessary tree iterations 39 private short size;40 private short height;39 private ushort size; 40 private ushort height; 41 41 42 42 public Symbol Symbol { … … 106 106 if (SubTrees != null) { 107 107 for (int i = 0; i < SubTrees.Count; i++) { 108 checked { size += ( short)SubTrees[i].GetSize(); }108 checked { size += (ushort)SubTrees[i].GetSize(); } 109 109 } 110 110 } … … 117 117 else { 118 118 if (SubTrees != null) { 119 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, ( short)SubTrees[i].GetHeight());119 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (ushort)SubTrees[i].GetHeight()); 120 120 } 121 121 height++; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/AllArchitectureAlteringOperatorsTest.cs
r5367 r5411 80 80 bool success = false; 81 81 op.ModifyArchitecture(random, selectedTree, grammar, maxTreeSize, maxTreeHeigth, maxDefuns, maxArgs, out success); 82 if (!success) failedEvents++; 82 if (!success) failedEvents++; // architecture manipulation might fail 83 83 Util.IsValid(selectedTree); 84 84 newTrees.Add(selectedTree); … … 93 93 bool success; 94 94 newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success)); 95 Assert.IsTrue(success); 95 Assert.IsTrue(success); // crossover must succeed 96 96 } 97 97 } … … 102 102 Console.WriteLine("AllArchitectureAlteringOperators: " + Environment.NewLine + 103 103 "Operations / s: ~" + Math.Round(1000.0 / (msPerOperation)) + "operations / s)" + Environment.NewLine + 104 "Failed events: " + failedEvents / (double)(POPULATION_SIZE * N_ITERATIONS) + "%" + Environment.NewLine +104 "Failed events: " + failedEvents * 100.0 / (double)(POPULATION_SIZE * N_ITERATIONS * 2.0) + "%" + Environment.NewLine + 105 105 Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine + 106 106 Util.GetFunctionDistributionString(trees) + Environment.NewLine + … … 108 108 Util.GetTerminalDistributionString(trees) + Environment.NewLine 109 109 ); 110 111 Assert.IsTrue(failedEvents * 100.0 / (POPULATION_SIZE * N_ITERATIONS * 2.0) < 25.0); // 75% of architecture operations must succeed 112 Assert.IsTrue(Math.Round(1000.0 / (msPerOperation)) > 1000); // must achieve more than 1000 ops per second 110 113 } 111 114 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs
r5367 r5411 56 56 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 57 var random = new MersenneTwister(31415); 58 int failedOps = 0; 58 59 for (int i = 0; i < POPULATION_SIZE; i++) { 59 60 SymbolicExpressionTree tree; … … 61 62 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3); 62 63 } while (!TreeHasAdfWithParameter(tree, 3)); 63 var success = ArgumentCreater.CreateNewArgument(random, tree, grammar, 10000, 100, 3, 3);64 Assert.IsTrue(success);64 var success = ArgumentCreater.CreateNewArgument(random, tree, grammar, 60000, 100, 3, 3); 65 if (!success) failedOps++; 65 66 Util.IsValid(tree); 66 67 trees.Add(tree); 67 68 } 69 // difficult to make sure that create argument operations succeed because trees are macro-expanded can potentially become very big 70 // => just test if only a small proportion fails 71 Assert.IsTrue(failedOps < POPULATION_SIZE * 0.01 ); // only 1% may fail 68 72 Console.WriteLine("ArgumentCreator: " + Environment.NewLine + 73 "Failed operations: " + failedOps * 100.0 / POPULATION_SIZE + " %" + Environment.NewLine + 69 74 Util.GetSizeDistributionString(trees, 200, 20) + Environment.NewLine + 70 75 Util.GetFunctionDistributionString(trees) + Environment.NewLine + -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs
r4722 r5411 79 79 var mul = new Multiplication(); 80 80 var div = new Division(); 81 div.InitialFrequency = 0.0; // disable division symbol 81 82 var terminal = new Terminal(); 82 83 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs
r5367 r5411 86 86 Util.GetTerminalDistributionString(trees) + Environment.NewLine 87 87 ); 88 89 Assert.IsTrue(Math.Round(1000.0 / (msPerCrossoverEvent)) > 2000); // must achieve more than 2000 x-overs/s 88 90 } 89 91 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs
r4722 r5411 130 130 } 131 131 //Assert.AreEqual(tree.Root.Symbol, tree.Root.Grammar.StartSymbol); 132 //foreach (var subtree in tree.Root.SubTrees)133 //Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);132 foreach (var subtree in tree.Root.SubTrees) 133 Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar); 134 134 IsValid(tree.Root); 135 135 } … … 141 141 Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol)); 142 142 Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol)); 143 Assert.AreNotEqual(0.0, matchingSymbol.InitialFrequency); // check that no deactivated symbols occur in the tree 143 144 for (int i = 0; i < treeNode.SubTrees.Count; i++) { 144 145 Assert.IsTrue(treeNode.GetAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));
Note: See TracChangeset
for help on using the changeset viewer.