Changeset 5792
- Timestamp:
- 03/22/11 15:07:20 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs
r5733 r5792 124 124 // the branch at the cut point is to be replaced by a new argument node 125 125 var cutPoints = (from node in defunBranch.IterateNodesPrefix() 126 where node.Subtrees.Count() > 0 126 where node.Subtrees.Count() > 0 && 127 !node.IterateNodesPrefix().OfType<ArgumentTreeNode>().Any() && 128 !node.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>().Any() 127 129 from subtree in node.Subtrees 128 130 select new CutPoint(node, subtree)).ToList(); … … 169 171 defunBranch.Grammar.SetSubtreeCount(newArgumentNode.Symbol, 0, 0); 170 172 // allow the argument as child of any other symbol 171 foreach (var symb in defunBranch.Grammar.Symbols) 172 for (int i = 0; i < defunBranch.Grammar.GetMaximumSubtreeCount(symb); i++) { 173 defunBranch.Grammar.AddAllowedChildSymbol(symb, newArgumentNode.Symbol, i); 174 } 173 GrammarModifier.SetAllowedParentSymbols(defunBranch.Grammar, selectedCutPoint.Child.Symbol, newArgumentNode.Symbol); 174 175 175 foreach (var subtree in tree.Root.Subtrees) { 176 176 // when the changed function is known in the branch then update the number of arguments … … 178 178 if (matchingSymbol != null) { 179 179 subtree.Grammar.SetSubtreeCount(matchingSymbol, defunBranch.NumberOfArguments, defunBranch.NumberOfArguments); 180 foreach (var child in subtree.Grammar.GetAllowedChildSymbols(subtree.Symbol, 0)) {181 for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingSymbol); i++) {182 subtree.Grammar.AddAllowedChildSymbol(matchingSymbol, child, i);183 }180 foreach (var symb in subtree.Grammar.Symbols) { 181 if (symb is StartSymbol || symb is ProgramRootSymbol) continue; 182 if (subtree.Grammar.IsAllowedChildSymbol(selectedCutPoint.Parent.Symbol, symb, selectedCutPoint.ChildIndex)) 183 subtree.Grammar.AddAllowedChildSymbol(matchingSymbol, symb, newArgumentNode.Symbol.ArgumentIndex); 184 184 } 185 185 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDuplicater.cs
r5733 r5792 108 108 selectedDefunBranch.Grammar.AddSymbol(newArgSymbol); 109 109 selectedDefunBranch.Grammar.SetSubtreeCount(newArgSymbol, 0, 0); 110 // allow the argument as child of any other symbol 111 foreach (var symb in selectedDefunBranch.Grammar.Symbols) 112 for (int i = 0; i < selectedDefunBranch.Grammar.GetMaximumSubtreeCount(symb); i++) { 113 selectedDefunBranch.Grammar.AddAllowedChildSymbol(symb, newArgSymbol, i); 114 } 110 // allow the duplicated argument as child of all other arguments where the orginal argument was allowed 111 GrammarModifier.SetAllowedParentSymbols(selectedDefunBranch.Grammar, selectedArgumentSymbol, newArgSymbol); 115 112 selectedDefunBranch.NumberOfArguments++; 116 113 … … 122 119 if (matchingInvokeSymbol != null) { 123 120 subtree.Grammar.SetSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments, selectedDefunBranch.NumberOfArguments); 124 foreach (var child in subtree.Grammar.GetAllowedChildSymbols(subtree.Symbol, 0)) {125 for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++) {126 subtree.Grammar.AddAllowedChildSymbol(matchingInvokeSymbol, child, i);127 }121 foreach (var symb in subtree.Grammar.Symbols) { 122 if (symb is StartSymbol || symb is ProgramRootSymbol) continue; 123 if (subtree.Grammar.IsAllowedChildSymbol(matchingInvokeSymbol, symb, selectedArgumentSymbol.ArgumentIndex)) 124 subtree.Grammar.AddAllowedChildSymbol(matchingInvokeSymbol, symb, newArgumentIndex); 128 125 } 129 126 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs
r5686 r5792 26 26 public static class GrammarModifier { 27 27 internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable<CutPoint> argumentCutPoints) { 28 if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) return; 29 28 30 var invokeSym = new InvokeFunction(functionName); 29 31 grammar.AddSymbol(invokeSym); … … 31 33 32 34 //allow invoke symbol everywhere, where the child of the startCutPoint was allowed 33 foreach (ISymbol parent in grammar.Symbols) { 34 if (grammar.IsAllowedChildSymbol(parent, startCutPoint.Child.Symbol)) 35 grammar.AddAllowedChildSymbol(parent, invokeSym); 36 else { 37 for (int i = 0; i < grammar.GetMaximumSubtreeCount(parent); i++) { 38 if (grammar.IsAllowedChildSymbol(parent, startCutPoint.Child.Symbol, i)) 39 grammar.AddAllowedChildSymbol(parent, invokeSym, i); 40 } 41 } 42 } 35 SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym); 43 36 44 37 if (nArgs > 0) { 45 38 //set allowed child symbols of invoke symbol 46 39 foreach (ISymbol child in grammar.Symbols) { 47 if (argumentCutPoints.All(x => grammar.IsAllowedChildSymbol(x.Parent.Symbol, child))) 48 grammar.AddAllowedChildSymbol(invokeSym, child); 49 else { 50 int i = 0; 51 foreach (CutPoint argumentCutPoint in argumentCutPoints) { 52 if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex)) 53 grammar.AddAllowedChildSymbol(invokeSym, child, i); 54 i++; 55 } 40 int i = 0; 41 foreach (CutPoint argumentCutPoint in argumentCutPoints) { 42 if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex)) 43 grammar.AddAllowedChildSymbol(invokeSym, child, i); 44 i++; 56 45 } 57 46 } 58 47 } 59 48 } 49 50 60 51 61 52 internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) { … … 65 56 grammar.SetSubtreeCount(argSymbol, 0, 0); 66 57 67 foreach ( ISymbol parent in originalGrammar.Symbols) {68 if ( parent is StartSymbol || parent is ProgramRootSymbol) continue;69 if (originalGrammar.IsAllowedChildSymbol( parent, pair.CutPoint.Child.Symbol))70 grammar.AddAllowedChildSymbol( parent, argSymbol);58 foreach (var symb in grammar.Symbols) { 59 if (symb is ProgramRootSymbol || symb is StartSymbol) continue; 60 if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol)) 61 grammar.AddAllowedChildSymbol(symb, argSymbol); 71 62 else { 72 for (int i = 0; i < originalGrammar.GetMaximumSubtreeCount(parent); i++) {73 if (originalGrammar.IsAllowedChildSymbol( parent, pair.CutPoint.Child.Symbol, i))74 grammar.AddAllowedChildSymbol( parent, argSymbol, i);63 for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) { 64 if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i)) 65 grammar.AddAllowedChildSymbol(symb, argSymbol, i); 75 66 } 67 } 68 } 69 } 70 } 71 72 internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) { 73 foreach (var symb in grammar.Symbols) { 74 if (symb is ProgramRootSymbol) continue; 75 if (newSymbol is Argument && symb is StartSymbol) continue; 76 if (grammar.IsAllowedChildSymbol(symb, symbol)) 77 grammar.AddAllowedChildSymbol(symb, newSymbol); 78 else { 79 for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) { 80 if (grammar.IsAllowedChildSymbol(symb, symbol, i)) 81 grammar.AddAllowedChildSymbol(symb, newSymbol, i); 76 82 } 77 83 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs
r5733 r5792 89 89 for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(symbol); i++) 90 90 if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol, i)) 91 subtree.Grammar.AddAllowedChildSymbol(symbol, matchingInvokeSymbol, i);91 subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol, i); 92 92 } 93 93 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
r5742 r5792 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 using System.Linq;24 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using System.Collections.Generic;26 25 27 26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 137 136 // ADF branches maxFunctionDefinitions 138 137 for (int argumentIndex = 1; argumentIndex < maximumFunctionDefinitions + 1; argumentIndex++) { 138 RemoveAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex); 139 139 AddAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex); 140 140 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r5712 r5792 152 152 allowedChildSymbols.Add(parent.Name, childSymbols); 153 153 } 154 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 154 155 childSymbols.Add(child.Name); 155 156 ClearCaches(); … … 164 165 } 165 166 167 if (IsAllowedChildSymbol(parent, child)) throw new ArgumentException(); 168 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 166 169 childSymbols.Add(child.Name); 167 170 ClearCaches(); … … 169 172 170 173 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 171 allowedChildSymbols[parent.Name].Remove(child.Name); 172 ClearCaches(); 174 List<string> childSymbols; 175 if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) { 176 if (allowedChildSymbols[parent.Name].Remove(child.Name)) 177 ClearCaches(); 178 } 173 179 } 174 180 175 181 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 176 182 var key = Tuple.Create(parent.Name, argumentIndex); 177 allowedChildSymbolsPerIndex[key].Remove(child.Name); 178 ClearCaches(); 183 List<string> childSymbols; 184 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 185 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) 186 ClearCaches(); 187 } 179 188 } 180 189 -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/AllArchitectureAlteringOperatorsTest.cs
r5759 r5792 32 32 public class AllArchitectureAlteringOperatorsTest { 33 33 private const int POPULATION_SIZE = 1000; 34 private const int N_ITERATIONS = 100;34 private const int N_ITERATIONS = 200; 35 35 private const int MAX_TREE_LENGTH = 100; 36 36 private const int MAX_TREE_DEPTH = 10; … … 99 99 par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 100 100 } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH); 101 newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH)); 101 var newTree = SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH); 102 Util.IsValid(newTree); 103 newTrees.Add(newTree); 102 104 } 103 105 } 104 trees = newTrees; 106 trees = new List<ISymbolicExpressionTree>(newTrees); 107 newTrees.Clear(); 105 108 } 106 109 var msPerOperation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)N_ITERATIONS; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubtreeCrossoverTest.cs
r5733 r5792 52 52 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 53 53 var random = new MersenneTwister(31415); 54 List<ISymbolicExpressionTree> crossoverTrees;55 54 double msPerCrossoverEvent; 56 55 … … 63 62 stopwatch.Start(); 64 63 for (int gCount = 0; gCount < generations; gCount++) { 65 var newPopulation = new List<ISymbolicExpressionTree>();66 64 for (int i = 0; i < POPULATION_SIZE; i++) { 67 var par0 = ( SymbolicExpressionTree)trees.SelectRandom(random).Clone();68 var par1 = ( SymbolicExpressionTree)trees.SelectRandom(random).Clone();69 newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10));65 var par0 = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone(); 66 var par1 = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone(); 67 SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10); 70 68 } 71 crossoverTrees = newPopulation;72 69 } 73 70 stopwatch.Stop(); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs
r5733 r5792 141 141 142 142 } 143 143 144 foreach (var subtree in tree.Root.Subtrees) { 144 145 Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar); … … 159 160 } 160 161 } 162 163 foreach (var symbol in grammar.ModifyableSymbols) { 164 //check if ever symbol has at least on 165 for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++) 166 Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Any()); 167 168 //if (symbol is ProgramRootSymbol) continue; 169 ////check if symbol is allowed as at least one child symbol 170 //bool result = false; 171 //foreach (var parentSymbol in grammar.Symbols) { 172 // if (result) break; 173 // for (int i = 0; i < grammar.GetMaximumSubtreeCount(parentSymbol); i++) 174 // result = result || grammar.IsAllowedChildSymbol(parentSymbol, symbol, i); 175 //} 176 //Assert.IsTrue(result); 177 178 } 161 179 } 162 180 -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tests/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs
r5686 r5792 70 70 SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 71 71 double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3); 72 Assert.IsTrue(nodesPerSec > 1 5.0e6); // evaluated nodes per seconds must be larger than 15mNodes/sec72 Assert.IsTrue(nodesPerSec > 12.5e6); // evaluated nodes per seconds must be larger than 15mNodes/sec 73 73 } 74 74 … … 89 89 SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 90 90 double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3); 91 Assert.IsTrue(nodesPerSec > 1 5.0e6); // evaluated nodes per seconds must be larger than 15mNodes/sec91 Assert.IsTrue(nodesPerSec > 12.5e6); // evaluated nodes per seconds must be larger than 15mNodes/sec 92 92 } 93 93
Note: See TracChangeset
for help on using the changeset viewer.