- Timestamp:
- 04/09/10 17:28:32 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3
- Files:
-
- 26 added
- 1 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs ¶
r3270 r3294 37 37 38 38 protected override SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight) { 39 return Apply(random, grammar, maxTreeSize.Value, maxTreeHeight.Value);39 return Create(random, grammar, maxTreeSize.Value, maxTreeHeight.Value); 40 40 } 41 41 42 public SymbolicExpressionTree Apply(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) {42 public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) { 43 43 // tree size is limited by the grammar and by the explicit size constraints 44 int allowedMinSize = grammar. MinimalExpressionLength(grammar.StartSymbol);45 int allowedMaxSize = Math.Min(maxTreeSize, grammar. MaximalExpressionLength(grammar.StartSymbol));44 int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol); 45 int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(grammar.StartSymbol)); 46 46 // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar) 47 47 int treeSize = random.Next(allowedMinSize, allowedMaxSize); … … 49 49 do { 50 50 try { 51 tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1); 51 tree.Root = grammar.ProgramRootSymbol.CreateTreeNode(); 52 tree.Root.AddSubTree(PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1)); 52 53 } 53 54 catch (ArgumentException) { … … 55 56 treeSize = random.Next(allowedMinSize, allowedMaxSize); 56 57 } 57 } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight); 58 } while (tree.Root.SubTrees.Count == 0 || tree.Size > maxTreeSize || tree.Height > maxTreeHeight); 59 System.Diagnostics.Debug.Assert(grammar.IsValidExpression(tree)); 58 60 return tree; 59 61 } 60 62 61 private Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {63 private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) { 62 64 var symbolList = symbols.ToList(); 63 var ticketsSum = symbolList.Select(x => x. Tickets.Value).Sum();65 var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum(); 64 66 var r = random.NextDouble() * ticketsSum; 65 67 double aggregatedTickets = 0; 66 68 for (int i = 0; i < symbolList.Count; i++) { 67 aggregatedTickets += symbolList[i]. Tickets.Value;69 aggregatedTickets += symbolList[i].InitialFrequency; 68 70 if (aggregatedTickets >= r) { 69 71 return symbolList[i]; … … 75 77 76 78 77 p rivateSymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) {79 public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) { 78 80 SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode(); 79 81 if (size <= 1 || maxDepth <= 1) return root; 80 List<object[]> list= new List<object[]>();82 List<object[]> extensionPoints = new List<object[]>(); 81 83 int currentSize = 1; 82 int totalListMinSize = grammar. MinimalExpressionLength(rootSymbol) - 1;84 int totalListMinSize = grammar.GetMinExpressionLength(rootSymbol) - 1; 83 85 84 86 int actualArity = SampleArity(random, grammar, rootSymbol, size); … … 86 88 // insert a dummy sub-tree and add the pending extension to the list 87 89 root.AddSubTree(null); 88 list.Add(new object[] { root, i, 2 });90 extensionPoints.Add(new object[] { root, i, 2 }); 89 91 } 90 92 91 93 // while there are pending extension points and we have not reached the limit of adding new extension points 92 while ( list.Count > 0 && totalListMinSize + currentSize < size) {93 int randomIndex = random.Next( list.Count);94 object[] nextExtension = list[randomIndex];95 list.RemoveAt(randomIndex);94 while (extensionPoints.Count > 0 && totalListMinSize + currentSize < size) { 95 int randomIndex = random.Next(extensionPoints.Count); 96 object[] nextExtension = extensionPoints[randomIndex]; 97 extensionPoints.RemoveAt(randomIndex); 96 98 SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0]; 97 99 int argumentIndex = (int)nextExtension[1]; 98 100 int extensionDepth = (int)nextExtension[2]; 99 if (extensionDepth + grammar. MinimalExpressionDepth(parent.Symbol) >= maxDepth) {101 if (extensionDepth + grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) { 100 102 parent.RemoveSubTree(argumentIndex); 101 SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar. AllowedSymbols(parent.Symbol, argumentIndex));103 SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, argumentIndex)); 102 104 parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree 103 105 currentSize += branch.GetSize(); 104 106 totalListMinSize -= branch.GetSize(); 105 107 } else { 106 var allowedSubFunctions = from s in grammar. AllowedSymbols(parent.Symbol, argumentIndex)107 where grammar. MinimalExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth108 where grammar. MaximalExpressionLength(s) > size - totalListMinSize - currentSize ||108 var allowedSubFunctions = from s in grammar.GetAllowedSymbols(parent.Symbol, argumentIndex) 109 where grammar.GetMinExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth 110 where grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize || 109 111 totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow 110 112 // terminals or terminal-branches … … 121 123 // insert a dummy sub-tree and add the pending extension to the list 122 124 newTree.AddSubTree(null); 123 list.Add(new object[] { newTree, i, extensionDepth + 1 });125 extensionPoints.Add(new object[] { newTree, i, extensionDepth + 1 }); 124 126 } 125 totalListMinSize += grammar. MinimalExpressionLength(selectedSymbol);127 totalListMinSize += grammar.GetMinExpressionLength(selectedSymbol); 126 128 } 127 129 } 128 130 // fill all pending extension points 129 while ( list.Count > 0) {130 int randomIndex = random.Next( list.Count);131 object[] nextExtension = list[randomIndex];132 list.RemoveAt(randomIndex);131 while (extensionPoints.Count > 0) { 132 int randomIndex = random.Next(extensionPoints.Count); 133 object[] nextExtension = extensionPoints[randomIndex]; 134 extensionPoints.RemoveAt(randomIndex); 133 135 SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0]; 134 136 int a = (int)nextExtension[1]; … … 136 138 parent.RemoveSubTree(a); 137 139 parent.InsertSubTree(a, 138 CreateMinimalTree(random, grammar, grammar. AllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height140 CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height 139 141 } 140 142 return root; 141 143 } 142 144 143 private int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) {145 private static int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) { 144 146 // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough 145 int minArity = grammar. MinSubTrees(symbol);146 int maxArity = grammar. MaxSubTrees(symbol);147 int minArity = grammar.GetMinSubTreeCount(symbol); 148 int maxArity = grammar.GetMaxSubTreeCount(symbol); 147 149 if (maxArity > targetSize) { 148 150 maxArity = targetSize; … … 152 154 long aggregatedLongestExpressionLength = 0; 153 155 for (int i = 0; i < maxArity; i++) { 154 aggregatedLongestExpressionLength += (from s in grammar. AllowedSymbols(symbol, i)155 select grammar. MaximalExpressionLength(s)).Max();156 aggregatedLongestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i) 157 select grammar.GetMaxExpressionLength(s)).Max(); 156 158 if (aggregatedLongestExpressionLength < targetSize) minArity = i; 157 159 else break; … … 162 164 long aggregatedShortestExpressionLength = 0; 163 165 for (int i = 0; i < maxArity; i++) { 164 aggregatedShortestExpressionLength += (from s in grammar. AllowedSymbols(symbol, i)165 select grammar. MinimalExpressionLength(s)).Min();166 aggregatedShortestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i) 167 select grammar.GetMinExpressionLength(s)).Min(); 166 168 if (aggregatedShortestExpressionLength > targetSize) { 167 169 maxArity = i; … … 173 175 } 174 176 175 private SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) {177 private static SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) { 176 178 // determine possible symbols that will lead to the smallest possible tree 177 179 var possibleSymbols = (from s in symbols 178 group s by grammar. MinimalExpressionLength(s) into g180 group s by grammar.GetMinExpressionLength(s) into g 179 181 orderby g.Key 180 182 select g).First(); … … 182 184 // build minimal tree by recursive application 183 185 var tree = selectedSymbol.CreateTreeNode(); 184 for (int i = 0; i < grammar. MinSubTrees(selectedSymbol); i++)185 tree.AddSubTree(CreateMinimalTree(random, grammar, grammar. AllowedSymbols(selectedSymbol, i)));186 for (int i = 0; i < grammar.GetMinSubTreeCount(selectedSymbol); i++) 187 tree.AddSubTree(CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(selectedSymbol, i))); 186 188 return tree; 187 189 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs ¶
r3237 r3294 27 27 using System; 28 28 using HeuristicLab.Parameters; 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols; 29 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 30 31 … … 39 40 [StorableClass] 40 41 public class SubtreeCrossover : SymbolicExpressionTreeCrossover { 41 private const int MAX_TRIES = 100;42 43 42 public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter { 44 43 get { return (IValueLookupParameter<PercentValue>)Parameters["InternalCrossoverPointProbability"]; } … … 52 51 protected override SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar, 53 52 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 54 IntValue maxTreeSize, IntValue maxTreeHeight ) {55 return Apply(random, grammar, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value);53 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) { 54 return Cross(random, grammar, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success); 56 55 } 57 56 58 public static SymbolicExpressionTree Apply(IRandom random, ISymbolicExpressionGrammar grammar,57 public static SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar, 59 58 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 60 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight) { 61 int tries = 0; 62 while (tries++ < MAX_TRIES) { 63 // select a random crossover point in the first parent 64 SymbolicExpressionTreeNode crossoverPoint0; 65 int replacedSubtreeIndex; 66 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, out crossoverPoint0, out replacedSubtreeIndex); 59 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight, out bool success) { 60 // select a random crossover point in the first parent 61 SymbolicExpressionTreeNode crossoverPoint0; 62 int replacedSubtreeIndex; 63 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, out crossoverPoint0, out replacedSubtreeIndex); 67 64 68 69 70 65 // calculate the max size and height that the inserted branch can have 66 int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0.SubTrees[replacedSubtreeIndex].GetSize()); 67 int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0); 71 68 72 var allowedBranches = from branch in IterateNodes(parent1.Root) 73 where branch.GetSize() < maxInsertedBranchSize 74 where branch.GetHeight() < maxInsertedBranchHeight 75 where grammar.AllowedSymbols(crossoverPoint0.Symbol, replacedSubtreeIndex).Contains(branch.Symbol) 76 select branch; 69 var allowedBranches = from branch in IterateNodes(parent1.Root) 70 where branch.GetSize() < maxInsertedBranchSize 71 where branch.GetHeight() < maxInsertedBranchHeight 72 where grammar.GetAllowedSymbols(crossoverPoint0.Symbol, replacedSubtreeIndex).Contains(branch.Symbol) 73 where IsMatchingPointType(parent0, crossoverPoint0, branch) 74 select branch; 77 75 78 79 76 if (allowedBranches.Count() > 0) { 77 var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability); 80 78 81 82 83 84 85 return parent0;86 }79 // manipulate the tree of parent0 in place 80 // replace the branch in tree0 with the selected branch from tree1 81 crossoverPoint0.RemoveSubTree(replacedSubtreeIndex); 82 crossoverPoint0.InsertSubTree(replacedSubtreeIndex, selectedBranch); 83 success = true; 84 return parent0; 87 85 } 88 86 89 // TODO: we should have a way to track the number of failed crossover attempts 90 // for now just return the first parent unchanged 87 success = false; 91 88 return parent0; 89 } 90 91 private static bool IsMatchingPointType(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent, SymbolicExpressionTreeNode newBranch) { 92 var functionCalls = (from node in IterateNodes(newBranch) 93 let invokeNode = node as InvokeFunctionTreeNode 94 let argNode = node as ArgumentTreeNode 95 where invokeNode != null || argNode != null 96 let name = invokeNode != null ? invokeNode.InvokedFunctionName : "ARG" + argNode.ArgumentIndex 97 let argCount = invokeNode != null ? invokeNode.SubTrees.Count : 0 98 select new { FunctionName = name, FunctionArgumentCount = argCount }).Distinct(); 99 var definingBranch = GetDefiningBranch(tree, parent); 100 if (definingBranch == null) return false; 101 foreach (var functionCall in functionCalls) { 102 if (!definingBranch.DynamicSymbols.Contains(functionCall.FunctionName) || 103 definingBranch.GetDynamicSymbolArgumentCount(functionCall.FunctionName) != functionCall.FunctionArgumentCount) 104 return false; 105 } 106 return true; 107 } 108 109 private static SymbolicExpressionTreeNode GetDefiningBranch(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent) { 110 foreach (var treeNode in tree.Root.SubTrees) { 111 if (IterateNodes(treeNode).Contains(parent)) return treeNode; 112 } 113 return null; 92 114 } 93 115 … … 95 117 var crossoverPoints = from branch in IterateNodes(parent0.Root) 96 118 where branch.SubTrees.Count > 0 119 where !(branch.Symbol is ProgramRootSymbol) 97 120 from index in Enumerable.Range(0, branch.SubTrees.Count) 98 121 let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 } … … 101 124 where !p.IsLeaf 102 125 select p).ToList(); 103 // select internal crossover point or leaf 104 if (random.NextDouble() < internalNodeProbability && internalCrossoverPoints.Count > 0) { 126 var leafCrossoverPoints = (from p in crossoverPoints 127 where p.IsLeaf 128 select p).ToList(); 129 if (internalCrossoverPoints.Count == 0) { 130 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 131 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 132 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 133 } else if (leafCrossoverPoints.Count == 0) { 134 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 135 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 136 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 137 } else if (random.NextDouble() < internalNodeProbability && internalCrossoverPoints.Count > 0) { 138 // select internal crossover point or leaf 105 139 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 106 140 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 107 141 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 108 142 } else { 109 var leafCrossoverPoints = (from p in crossoverPoints110 where p.IsLeaf111 select p).ToList();112 143 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 113 144 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; … … 125 156 from branch in g 126 157 select branch).ToList(); 127 if (random.NextDouble() < internalNodeProbability && allowedInternalBranches.Count > 0) { 158 var allowedLeafBranches = (from g in groupedBranches 159 where g.Key == 0 160 from leaf in g 161 select leaf).ToList(); 162 if (allowedInternalBranches.Count == 0) { 163 return allowedLeafBranches[random.Next(allowedLeafBranches.Count)]; 164 } else if (allowedLeafBranches.Count == 0) { 165 return allowedInternalBranches[random.Next(allowedInternalBranches.Count)]; 166 } else if (random.NextDouble() < internalNodeProbability) { 167 // when leaf and internal nodes are possible then choose either a leaf or internal node with internalNodeProbability 128 168 return allowedInternalBranches[random.Next(allowedInternalBranches.Count)]; 129 169 } else { 130 var allowedLeafBranches = (from g in groupedBranches131 where g.Key == 0132 from leaf in g133 select leaf).ToList();134 170 return allowedLeafBranches[random.Next(allowedLeafBranches.Count)]; 135 171 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj ¶
r3256 r3294 83 83 </ItemGroup> 84 84 <ItemGroup> 85 <Compile Include="ArchitectureAlteringOperators\ArgumentDuplicater.cs" /> 86 <Compile Include="ArchitectureAlteringOperators\ArgumentCreater.cs" /> 87 <Compile Include="ArchitectureAlteringOperators\ArgumentDeleter.cs" /> 88 <Compile Include="ArchitectureAlteringOperators\RandomArchitectureAlteringOperator.cs" /> 89 <Compile Include="ArchitectureAlteringOperators\SubroutineDeleter.cs" /> 90 <Compile Include="ArchitectureAlteringOperators\SubroutineCreater.cs"> 91 <SubType>Code</SubType> 92 </Compile> 93 <Compile Include="ArchitectureAlteringOperators\SubroutineDuplicater.cs"> 94 <SubType>Code</SubType> 95 </Compile> 96 <Compile Include="DefaultSymbolicExpressionGrammar.cs"> 97 <SubType>Code</SubType> 98 </Compile> 99 <Compile Include="SymbolicExpressionTreeArchitectureAlteringOperator.cs" /> 100 <Compile Include="SymbolicExpressionTreeOperator.cs" /> 101 <Compile Include="Instruction.cs" /> 102 <Compile Include="SymbolicExpressionTreeCompiler.cs" /> 103 <Compile Include="SymbolicExpressionTreeManipulator.cs" /> 85 104 <Compile Include="SymbolicExpressionTreeTerminalNode.cs" /> 86 105 <Compile Include="Crossovers\SubtreeCrossover.cs" /> … … 89 108 <Compile Include="HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs" /> 90 109 <Compile Include="Interfaces\ISymbolicExpressionTreeOperator.cs" /> 91 <Compile Include="SymbolicExpressionGrammar.cs" />92 110 <Compile Include="SymbolicExpressionTreeCreator.cs" /> 93 111 <Compile Include="SymbolicExpressionTree.cs" /> … … 97 115 <Compile Include="SymbolicExpressionTreeNode.cs" /> 98 116 <Compile Include="Symbols\Addition.cs" /> 99 <Compile Include="Symbols\ Prog.cs" />100 <Compile Include=" GeneralSymbolicExpressionTreeEvaluator.cs" />117 <Compile Include="Symbols\Argument.cs" /> 118 <Compile Include="Symbols\ArgumentTreeNode.cs" /> 101 119 <Compile Include="Symbols\StartSymbol.cs" /> 120 <Compile Include="Symbols\InvokeFunction.cs" /> 121 <Compile Include="Symbols\InvokeFunctionTreeNode.cs" /> 122 <Compile Include="Symbols\DefunTreeNode.cs" /> 123 <Compile Include="Symbols\Defun.cs" /> 124 <Compile Include="Symbols\ProgramRootSymbol.cs" /> 102 125 <Compile Include="Symbols\Division.cs" /> 103 126 <Compile Include="Symbols\Multiplication.cs" /> 104 127 <Compile Include="Symbols\Subtraction.cs" /> 128 <Compile Include="Symbols\Values.cs"> 129 <SubType>Code</SubType> 130 </Compile> 131 <Compile Include="Symbols\ValuesTreeNode.cs"> 132 <SubType>Code</SubType> 133 </Compile> 105 134 </ItemGroup> 106 135 <ItemGroup> -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Interfaces/ISymbolicExpressionGrammar.cs ¶
r3237 r3294 31 31 public interface ISymbolicExpressionGrammar : IItem { 32 32 Symbol StartSymbol { get; } 33 // IEnumerable<Symbol> Symbols { get; } 34 35 // void AddSymbol(Symbol symbol); 36 // void RemoveSymbol(Symbol symbol); 37 38 IEnumerable<Symbol> AllowedSymbols(Symbol parent, int argumentIndex); 39 int MinimalExpressionLength(Symbol start); 40 int MaximalExpressionLength(Symbol start); 41 int MinimalExpressionDepth(Symbol start); 42 int MinSubTrees(Symbol start); 43 int MaxSubTrees(Symbol start); 33 Symbol ProgramRootSymbol { get; } 34 IEnumerable<Symbol> GetAllowedSymbols(Symbol parent, int argumentIndex); 35 int GetMinExpressionLength(Symbol start); 36 int GetMaxExpressionLength(Symbol start); 37 int GetMinExpressionDepth(Symbol start); 38 int GetMinSubTreeCount(Symbol start); 39 int GetMaxSubTreeCount(Symbol start); 44 40 45 41 bool IsValidExpression(SymbolicExpressionTree expression); -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbol.cs ¶
r3269 r3294 32 32 [StorableClass] 33 33 [Item("Symbol", "Represents a symbol in a symbolic function tree.")] 34 public abstract class Symbol : ParameterizedNamedItem { 35 #region Parameter Properties 36 public IValueParameter<DoubleValue> TicketsParameter { 37 get { return (IValueParameter<DoubleValue>)Parameters["Tickets"]; } 38 } 39 #endregion 40 34 public abstract class Symbol : NamedItem { 41 35 #region Properties 42 public DoubleValue Tickets { 43 get { return TicketsParameter.Value; } 36 private double initialFrequency; 37 public double InitialFrequency { 38 get { return initialFrequency; } 44 39 set { 45 if (value .Value < 0.0) throw new ArgumentException("Number of ticketsmust be positive");46 TicketsParameter.Value= value;40 if (value < 0.0) throw new ArgumentException("InitialFrequency must be positive"); 41 initialFrequency = value; 47 42 } 48 43 } … … 51 46 protected Symbol() 52 47 : base() { 53 Parameters.Add(new ValueParameter<DoubleValue>("Tickets", new DoubleValue(1.0))); 48 this.name = ItemName; 49 this.description = ItemDescription; 50 initialFrequency = 1.0; 54 51 } 55 52 -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs ¶
r3252 r3294 32 32 [StorableClass] 33 33 [Item("SymbolicExpressionTree", "Represents a symbolic expression tree.")] 34 public class SymbolicExpressionTree : Item { 34 public class SymbolicExpressionTree : Item { 35 35 [Storable] 36 36 private SymbolicExpressionTreeNode root; … … 46 46 } 47 47 48 public SymbolicExpressionTreeNode ResultProducingExpression { 49 get { return root.SubTrees[0].SubTrees[0]; } 50 } 51 52 [Storable] 53 private Dictionary<int, IEnumerable<string>> allowedFunctionsInBranch; 54 48 55 public int Size { 49 56 get { … … 58 65 } 59 66 60 public SymbolicExpressionTree() : base() { } 67 public SymbolicExpressionTree() 68 : base() { 69 allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>(); 70 } 61 71 62 public SymbolicExpressionTree(SymbolicExpressionTreeNode root) : base() { } 72 public SymbolicExpressionTree(SymbolicExpressionTreeNode root) 73 : base() { 74 allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>(); 75 } 63 76 64 77 public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() { … … 87 100 cloner.RegisterClonedObject(this, clone); 88 101 clone.root = (SymbolicExpressionTreeNode)this.root.Clone(); 102 clone.allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>(allowedFunctionsInBranch); 89 103 return clone; 90 104 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCreator.cs ¶
r3269 r3294 34 34 [Item("SymbolicExpressionTreeCreator", "A base class for operators creating symbolic expression trees.")] 35 35 [StorableClass] 36 public abstract class SymbolicExpressionTreeCreator : SingleSuccessorOperator, ISolutionCreator, IStochasticOperator, ISymbolicExpressionTreeOperator { 37 private const string RandomParameterName = "Random"; 38 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 39 private const string MaxTreeSizeParameterName = "MaxTreeSize"; 40 private const string MaxTreeHeightParameterName = "MaxTreeHeight"; 41 private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar"; 42 43 public override bool CanChangeName { 44 get { return false; } 45 } 46 47 public ILookupParameter<IRandom> RandomParameter { 48 get { return (LookupParameter<IRandom>)Parameters[RandomParameterName]; } 49 } 50 public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter { 51 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 52 } 53 public IValueLookupParameter<IntValue> MaxTreeSizeParameter { 54 get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; } 55 } 56 public IValueLookupParameter<IntValue> MaxTreeHeightParameter { 57 get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; } 58 } 59 public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter { 60 get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; } 61 } 62 36 public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISolutionCreator { 63 37 protected SymbolicExpressionTreeCreator() 64 38 : base() { 65 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The pseudo random number generator which should be used for stochastic solution creation operators."));66 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be initialized."));67 Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree that should be initialized."));68 Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree that should be initialized (a tree with one node has height = 0)."));69 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));70 39 } 71 40 -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCrossover.cs ¶
r3239 r3294 35 35 [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")] 36 36 [StorableClass] 37 public abstract class SymbolicExpressionTreeCrossover : SingleSuccessorOperator, ICrossover, IStochasticOperator, ISymbolicExpressionTreeOperator { 38 public override bool CanChangeName { 39 get { return false; } 37 public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ICrossover { 38 private const string ParentsParameterName = "Parents"; 39 private const string ChildParameterName = "Child"; 40 private const string FailedCrossoverEventsParameterName = "FailedCrossoverEvents"; 41 public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter { 42 get { return (SubScopesLookupParameter<SymbolicExpressionTree>)Parameters[ParentsParameterName]; } 43 } 44 public ILookupParameter<SymbolicExpressionTree> ChildParameter { 45 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ChildParameterName]; } 46 } 47 public IValueParameter<IntValue> FailedCrossoverEventsParameter { 48 get { return (ValueParameter<IntValue>)Parameters[FailedCrossoverEventsParameterName]; } 40 49 } 41 50 42 public I LookupParameter<IRandom> RandomParameter{43 get { return (LookupParameter<IRandom>)Parameters["Random"]; }51 public IntValue FailedCrossoverEvents { 52 get { return FailedCrossoverEventsParameter.Value; } 44 53 } 45 public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter {46 get { return (SubScopesLookupParameter<SymbolicExpressionTree>)Parameters["Parents"]; }47 }48 public ILookupParameter<SymbolicExpressionTree> ChildParameter {49 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["Child"]; }50 }51 public IValueLookupParameter<IntValue> MaxTreeSizeParameter {52 get { return (IValueLookupParameter<IntValue>)Parameters["MaxTreeSize"]; }53 }54 public IValueLookupParameter<IntValue> MaxTreeHeightParameter {55 get { return (IValueLookupParameter<IntValue>)Parameters["MaxTreeHeight"]; }56 }57 public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {58 get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters["SymbolicExpressionGrammar"]; }59 }60 61 54 protected SymbolicExpressionTreeCrossover() 62 55 : base() { 63 Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators.")); 64 Parameters.Add(new SubScopesLookupParameter<SymbolicExpressionTree>("Parents", "The parent symbolic expression trees which should be crossed.")); 65 Parameters.Add(new ValueLookupParameter<IntValue>("MaxTreeSize", "The maximal size (number of nodes) of the symbolic expression tree that should be initialized.")); 66 Parameters.Add(new ValueLookupParameter<IntValue>("MaxTreeHeight", "The maximal height of the symbolic expression tree that should be initialized (a tree with one node has height = 0).")); 67 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>("SymbolicExpressionGrammar", "The grammar that defines the allowed symbols and syntax of the symbolic expression trees.")); 68 Parameters.Add(new LookupParameter<SymbolicExpressionTree>("Child", "The child symbolic expression tree resulting from the crossover.")); 56 Parameters.Add(new SubScopesLookupParameter<SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); 57 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover.")); 58 Parameters.Add(new ValueParameter<IntValue>(FailedCrossoverEventsParameterName, "The number of failed crossover events (child is an exact copy of a parent)", new IntValue())); 69 59 } 70 60 … … 86 76 } 87 77 78 bool success; 88 79 SymbolicExpressionTree result = Cross(random, grammar, parent0, parent1, 89 MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue); 80 MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success); 81 82 if (!success) FailedCrossoverEvents.Value++; 83 90 84 Debug.Assert(result.Size <= MaxTreeSizeParameter.ActualValue.Value); 91 85 Debug.Assert(result.Height <= MaxTreeHeightParameter.ActualValue.Value); 92 Debug.Assert(grammar.IsValidExpression(result));86 // Debug.Assert(grammar.IsValidExpression(result)); 93 87 ChildParameter.ActualValue = result; 94 88 return base.Apply(); … … 97 91 protected abstract SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar, 98 92 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 99 IntValue maxTreeSize, IntValue maxTreeHeight );93 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success); 100 94 } 101 95 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeManipulator.cs ¶
r3239 r3294 31 31 /// A base class for operators that manipulate real-valued vectors. 32 32 /// </summary> 33 [Item(" RealVectorManipulator", "A base class for operators that manipulate real-valued vectors.")]33 [Item("SymbolicExpressionTreeManipulator", "A base class for operators that manipulate symbolic expression trees.")] 34 34 [StorableClass] 35 public abstract class RealVectorManipulator : SingleSuccessorOperator, IRealVectorManipulator, IStochasticOperator, ISymbolicExpressionTreeOperator { 36 public override bool CanChangeName { 37 get { return false; } 35 public abstract class SymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, IManipulator { 36 private const string FailedManipulationEventsParameterName = "FailedManipulationEvents"; 37 38 #region Parameter Properties 39 public IValueParameter<IntValue> FailedManipulationEventsParameter { 40 get { return (IValueParameter<IntValue>)Parameters[FailedManipulationEventsParameterName]; } 38 41 } 42 #endregion 39 43 40 public ILookupParameter<IRandom> RandomParameter { 41 get { return (LookupParameter<IRandom>)Parameters["Random"]; } 44 #region Properties 45 public IntValue FailedManipulationEvents { 46 get { return FailedManipulationEventsParameter.Value; } 42 47 } 43 public ILookupParameter<RealVector> RealVectorParameter { 44 get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; } 45 } 46 public IValueLookupParameter<DoubleMatrix> BoundsParameter { 47 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 48 } 48 #endregion 49 49 50 p rotected RealVectorManipulator()50 public SymbolicExpressionTreeManipulator() 51 51 : base() { 52 Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators.")); 53 Parameters.Add(new LookupParameter<RealVector>("RealVector", "The vector which should be manipulated.")); 54 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds of the real vector.")); 52 Parameters.Add(new ValueParameter<IntValue>(FailedManipulationEventsParameterName, "The number of failed manipulation events.", new IntValue())); 55 53 } 56 54 57 55 public sealed override IOperation Apply() { 58 RealVector vector = RealVectorParameter.ActualValue; 59 Manipulate(RandomParameter.ActualValue, vector); 60 DoubleMatrix bounds = BoundsParameter.ActualValue; 61 if (bounds != null) BoundsChecker.Apply(vector, bounds); 56 SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue; 57 bool success; 58 Manipulate(RandomParameter.ActualValue, tree, SymbolicExpressionGrammarParameter.ActualValue, 59 MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success); 60 61 if (!success) FailedManipulationEvents.Value++; 62 62 return base.Apply(); 63 63 } 64 64 65 protected abstract void Manipulate(IRandom random, RealVector realVector); 65 protected abstract void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, 66 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success); 66 67 } 67 68 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs ¶
r3269 r3294 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using System.Text; … … 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 using HeuristicLab.Data; 30 using System.Diagnostics; 29 31 30 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 46 48 protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) { 47 49 symbol = original.symbol; 48 this.subTrees = new List<SymbolicExpressionTreeNode>();50 subTrees = new List<SymbolicExpressionTreeNode>(); 49 51 foreach (var subtree in original.SubTrees) { 50 52 AddSubTree((SymbolicExpressionTreeNode)subtree.Clone()); 51 53 } 54 dynamicSymbols = new Dictionary<string, int>(original.dynamicSymbols); 52 55 } 53 56 … … 76 79 return maxHeight + 1; 77 80 } 78 81 82 [Storable] 83 private Dictionary<string, int> dynamicSymbols = new Dictionary<string, int>(); 84 public void AddDynamicSymbol(string symbolName) { 85 Debug.Assert(!dynamicSymbols.ContainsKey(symbolName)); 86 dynamicSymbols[symbolName] = 0; 87 } 88 89 public void AddDynamicSymbol(string symbolName, int nArguments) { 90 AddDynamicSymbol(symbolName); 91 SetDynamicSymbolArgumentCount(symbolName, nArguments); 92 } 93 94 public void RemoveDynamicSymbol(string symbolName) { 95 dynamicSymbols.Remove(symbolName); 96 } 97 98 public IEnumerable<string> DynamicSymbols { 99 get { return dynamicSymbols.Keys; } 100 } 101 102 public int GetDynamicSymbolArgumentCount(string symbolName) { 103 return dynamicSymbols[symbolName]; 104 } 105 public void SetDynamicSymbolArgumentCount(string symbolName, int nArguments) { 106 Debug.Assert(dynamicSymbols.ContainsKey(symbolName)); 107 dynamicSymbols[symbolName] = nArguments; 108 } 109 79 110 public virtual void ResetLocalParameters(IRandom random) { } 80 111 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Addition.cs ¶
r3256 r3294 20 20 #endregion 21 21 22 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 using HeuristicLab.Core; 22 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols { 25 [StorableClass] 26 [Item("Addition", "Symbol that represents the + operator.")] 23 27 public sealed class Addition : Symbol { 24 28 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Division.cs ¶
r3256 r3294 20 20 #endregion 21 21 22 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 using HeuristicLab.Core; 22 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols { 25 [StorableClass] 26 [Item("Division", "Symbol that represents the / operator.")] 23 27 public sealed class Division : Symbol { 24 28 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Multiplication.cs ¶
r3256 r3294 20 20 #endregion 21 21 22 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 using HeuristicLab.Core; 22 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols { 25 [StorableClass] 26 [Item("Multiplication", "Symbol that represents the * operator.")] 23 27 public sealed class Multiplication : Symbol { 24 28 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbol.cs ¶
r3256 r3294 20 20 #endregion 21 21 22 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 using HeuristicLab.Core; 22 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols { 25 [StorableClass] 26 [Item("StartSymbol", "Special symbol that represents the starting node of the result producing branch of a symbolic expression tree.")] 23 27 public sealed class StartSymbol : Symbol { 28 public override bool CanChangeName { 29 get { 30 return false; 31 } 32 } 24 33 } 25 34 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Subtraction.cs ¶
r3256 r3294 20 20 #endregion 21 21 22 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 using HeuristicLab.Core; 22 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols { 25 [StorableClass] 26 [Item("Subtraction", "Symbol that represents the - operator.")] 23 27 public sealed class Subtraction : Symbol { 24 28 }
Note: See TracChangeset
for help on using the changeset viewer.