Changeset 4249
- Timestamp:
- 08/18/10 14:57:11 (14 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineCreater.cs
r4068 r4249 111 111 symbolicExpressionTree.Root.AddSubTree(defunNode); 112 112 // the grammar in the newly defined function is a clone of the grammar of the originating branch 113 defunNode. Grammar = (ISymbolicExpressionGrammar)selectedBody.Grammar.Clone();113 defunNode.SetGrammar((ISymbolicExpressionGrammar)selectedBody.Grammar.Clone()); 114 114 // remove all argument symbols from grammar 115 115 var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList(); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDuplicater.cs
r4068 r4249 66 66 duplicatedDefunBranch.FunctionName = newFunctionName; 67 67 symbolicExpressionTree.Root.AddSubTree(duplicatedDefunBranch); 68 duplicatedDefunBranch. Grammar = (ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone();68 duplicatedDefunBranch.SetGrammar((ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone()); 69 69 // add an invoke symbol for each branch that is allowed to invoke the original function 70 70 foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) { -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
r4219 r4249 53 53 ) { 54 54 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 55 var rootNode = grammar.StartSymbol.CreateTreeNode();55 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 56 56 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 57 rootNode. Grammar = grammar;57 rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar)); 58 58 tree.Root = PTC2(random, rootNode, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments); 59 59 return tree; … … 186 186 // also assumes that newTree is already attached to root somewhere 187 187 if (IsTopLevelBranch(root, newTree)) { 188 newTree.Grammar = (ISymbolicExpressionGrammar)root.Grammar.Clone();188 ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionGrammar)root.Grammar.Clone()); 189 189 190 190 // allow invokes of existing ADFs with higher index -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs
r4068 r4249 90 90 public DefaultSymbolicExpressionGrammar() 91 91 : base() { 92 minSubTreeCount = new Dictionary<string, int>(); 93 maxSubTreeCount = new Dictionary<string, int>(); 94 allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 95 allSymbols = new Dictionary<string, Symbol>(); 96 97 cachedMinExpressionLength = new Dictionary<string, int>(); 98 cachedMaxExpressionLength = new Dictionary<string, int>(); 99 cachedMinExpressionDepth = new Dictionary<string, int>(); 100 101 startSymbol = new StartSymbol(); 102 AddSymbol(startSymbol); 103 SetMinSubtreeCount(startSymbol, 1); 104 SetMaxSubtreeCount(startSymbol, 1); 105 } 106 107 //copy constructor for cloning 108 protected DefaultSymbolicExpressionGrammar(DefaultSymbolicExpressionGrammar copy) 92 this.minSubTreeCount = new Dictionary<string, int>(); 93 this.maxSubTreeCount = new Dictionary<string, int>(); 94 this.allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 95 this.allSymbols = new Dictionary<string, Symbol>(); 96 this.cachedMinExpressionLength = new Dictionary<string, int>(); 97 this.cachedMaxExpressionLength = new Dictionary<string, int>(); 98 this.cachedMinExpressionDepth = new Dictionary<string, int>(); 99 100 this.startSymbol = new StartSymbol(); 101 this.AddSymbol(startSymbol); 102 this.SetMinSubtreeCount(startSymbol, 1); 103 this.SetMaxSubtreeCount(startSymbol, 1); 104 } 105 106 protected DefaultSymbolicExpressionGrammar(ISymbolicExpressionGrammar grammar) 109 107 : base() { 110 this.minSubTreeCount = new Dictionary<string, int>(copy.minSubTreeCount); 111 this.maxSubTreeCount = new Dictionary<string, int>(copy.maxSubTreeCount); 112 113 this.startSymbol = copy.startSymbol; 108 Cloner cloner = new Cloner(); 109 this.cachedMinExpressionLength = new Dictionary<string, int>(); 110 this.cachedMaxExpressionLength = new Dictionary<string, int>(); 111 this.cachedMinExpressionDepth = new Dictionary<string, int>(); 112 113 this.minSubTreeCount = new Dictionary<string, int>(); 114 this.maxSubTreeCount = new Dictionary<string, int>(); 114 115 this.allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 115 foreach (var entry in copy.allowedChildSymbols) { 116 this.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count); 117 foreach (var set in entry.Value) { 118 this.allowedChildSymbols[entry.Key].Add(new List<string>(set)); 116 this.allSymbols = new Dictionary<string, Symbol>(); 117 118 this.StartSymbol = (Symbol)cloner.Clone(grammar.StartSymbol); 119 120 foreach (Symbol symbol in grammar.Symbols) { 121 Symbol clonedSymbol = (Symbol)cloner.Clone(symbol); 122 this.AddSymbol(clonedSymbol); 123 this.SetMinSubtreeCount(clonedSymbol, grammar.GetMinSubtreeCount(symbol)); 124 this.SetMaxSubtreeCount(clonedSymbol, grammar.GetMaxSubtreeCount(symbol)); 125 } 126 127 foreach (Symbol parent in grammar.Symbols) { 128 for (int i = 0; i < grammar.GetMaxSubtreeCount(parent); i++) { 129 foreach (Symbol child in grammar.Symbols) { 130 if (grammar.IsAllowedChild(parent, child, i)) { 131 this.SetAllowedChild((Symbol)cloner.Clone(parent), (Symbol)cloner.Clone(child), i); 132 } 133 } 119 134 } 120 135 } 121 this.allSymbols = new Dictionary<string, Symbol>(copy.allSymbols);122 123 cachedMinExpressionLength = new Dictionary<string, int>();124 cachedMaxExpressionLength = new Dictionary<string, int>();125 cachedMinExpressionDepth = new Dictionary<string, int>();126 136 } 127 137 … … 151 161 152 162 #region ISymbolicExpressionGrammar Members 153 154 163 public Symbol StartSymbol { 155 164 get { return startSymbol; } … … 272 281 return maxSubTreeCount[symbol.Name]; 273 282 } 274 275 283 #endregion 276 284 … … 282 290 283 291 public override IDeepCloneable Clone(Cloner cloner) { 284 DefaultSymbolicExpressionGrammar clone = new DefaultSymbolicExpressionGrammar(this); 285 cloner.RegisterClonedObject(this, clone); 292 DefaultSymbolicExpressionGrammar clone = (DefaultSymbolicExpressionGrammar)base.Clone(cloner); 293 294 clone.minSubTreeCount = new Dictionary<string, int>(this.minSubTreeCount); 295 clone.maxSubTreeCount = new Dictionary<string, int>(this.maxSubTreeCount); 296 297 clone.allSymbols = new Dictionary<string, Symbol>(); 298 foreach (Symbol symbol in this.allSymbols.Values.Select(s => cloner.Clone(s))) 299 clone.allSymbols.Add(symbol.Name, symbol); 300 301 clone.startSymbol = (Symbol)cloner.Clone(this.startSymbol); 302 clone.allowedChildSymbols = new Dictionary<string, List<List<string>>>(); 303 foreach (var entry in this.allowedChildSymbols) { 304 clone.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count); 305 foreach (var set in entry.Value) { 306 clone.allowedChildSymbols[entry.Key].Add(new List<string>(set)); 307 } 308 } 309 286 310 return clone; 287 311 } 312 313 protected void InitializeShallowClone(DefaultSymbolicExpressionGrammar clone) { 314 clone.minSubTreeCount = new Dictionary<string, int>(this.minSubTreeCount); 315 clone.maxSubTreeCount = new Dictionary<string, int>(this.maxSubTreeCount); 316 317 clone.allSymbols = new Dictionary<string, Symbol>(this.allSymbols); 318 clone.startSymbol = this.startSymbol; 319 clone.allowedChildSymbols = new Dictionary<string, List<List<string>>>(this.allowedChildSymbols.Count); 320 foreach (var entry in this.allowedChildSymbols) { 321 clone.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count); 322 foreach (var set in entry.Value) { 323 clone.allowedChildSymbols[entry.Key].Add(new List<string>(set)); 324 } 325 } 326 } 327 288 328 } 289 329 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs
r4068 r4249 20 20 #endregion 21 21 22 using System; 23 using System.Linq; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; … … 67 69 private Defun defunSymbol; 68 70 69 70 71 71 public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar) 72 : base( ) {72 : base(mainBranchGrammar) { 73 73 maxFunctionArguments = 3; 74 74 maxFunctionDefinitions = 3; 75 Initialize(mainBranchGrammar); 75 76 ProgramRootSymbol programRootSymbol = Symbols.OfType<ProgramRootSymbol>().FirstOrDefault(); 77 if (programRootSymbol == null) { 78 programRootSymbol = new ProgramRootSymbol(); 79 AddSymbol(programRootSymbol); 80 } 81 StartSymbol = programRootSymbol; 82 83 defunSymbol = Symbols.OfType<Defun>().FirstOrDefault(); 84 if (defunSymbol == null) { 85 defunSymbol = new Defun(); 86 AddSymbol(defunSymbol); 87 } 88 89 SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1); 90 SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1); 91 SetMinSubtreeCount(defunSymbol, 1); 92 SetMaxSubtreeCount(defunSymbol, 1); 93 94 // the start symbol of the mainBranchGrammar is allowed as the result producing branch 95 SetAllowedChild(StartSymbol, Symbols.Where(s => s.Name == mainBranchGrammar.StartSymbol.Name).First(), 0); 96 97 // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun 98 foreach (var symb in mainBranchGrammar.Symbols) { 99 if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0)) 100 SetAllowedChild(defunSymbol, Symbols.Where(s => s.Name == symb.Name).First(), 0); 101 } 76 102 } 77 103 78 //copy constructor for cloning 79 protected GlobalSymbolicExpressionGrammar(GlobalSymbolicExpressionGrammar copy) 80 : base(copy) { 81 this.maxFunctionArguments = copy.maxFunctionArguments; 82 this.minFunctionArguments = copy.minFunctionArguments; 83 this.maxFunctionDefinitions = copy.maxFunctionDefinitions; 84 this.minFunctionDefinitions = copy.minFunctionDefinitions; 85 } 86 104 //ctor for cloning 105 protected GlobalSymbolicExpressionGrammar() : base() { } 87 106 [StorableConstructor] 88 107 protected GlobalSymbolicExpressionGrammar(bool deserializing) … … 90 109 } 91 110 111 [Obsolete] 92 112 private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) { 93 113 base.Clear(); … … 151 171 152 172 public override IDeepCloneable Clone(Cloner cloner) { 153 GlobalSymbolicExpressionGrammar clone = new GlobalSymbolicExpressionGrammar(this); 154 cloner.RegisterClonedObject(this, clone); 173 GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner); 174 clone.defunSymbol = (Defun)cloner.Clone(this.defunSymbol); 175 clone.maxFunctionArguments = this.maxFunctionArguments; 176 clone.minFunctionArguments = this.minFunctionArguments; 177 clone.maxFunctionDefinitions = this.maxFunctionDefinitions; 178 clone.minFunctionDefinitions = this.minFunctionDefinitions; 155 179 return clone; 156 180 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj
r4189 r4249 135 135 <Compile Include="Manipulators\OnePointShaker.cs" /> 136 136 <Compile Include="Manipulators\SymbolicExpressionTreeManipulator.cs" /> 137 <Compile Include="SymbolicExpressionTreeGrammar.cs" /> 137 138 <Compile Include="SymbolicExpressionTreeStringFormatter.cs" /> 138 139 <Compile Include="SymbolicExpressionTreeTopLevelNode.cs" /> -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
r4106 r4249 87 87 public virtual ISymbolicExpressionGrammar Grammar { 88 88 get { return parent.Grammar; } 89 set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); }90 89 } 91 90 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs
r4106 r4249 26 26 [StorableClass] 27 27 public class SymbolicExpressionTreeTopLevelNode : SymbolicExpressionTreeNode { 28 29 28 public SymbolicExpressionTreeTopLevelNode() 30 29 : base() { … … 39 38 public override ISymbolicExpressionGrammar Grammar { 40 39 get { return grammar; } 41 set { grammar = value; } 40 } 41 public void SetGrammar(ISymbolicExpressionGrammar grammar) { 42 this.grammar = grammar; 42 43 } 43 44 … … 47 48 if (original.Grammar != null) 48 49 grammar = (ISymbolicExpressionGrammar)original.Grammar.Clone(); 50 //grammar = original.grammar; 49 51 } 50 52 -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/ArithmeticExpressionGrammar.cs
r4068 r4249 30 30 [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")] 31 31 public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar { 32 [Storable]33 private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;34 35 32 public ArithmeticExpressionGrammar() 36 33 : base() { … … 49 46 constant.MinValue = -20; 50 47 constant.MaxValue = 20; 51 var iableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();48 var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable(); 52 49 53 50 var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol }; -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs
r4068 r4249 30 30 [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")] 31 31 public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar { 32 [Storable]33 private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;34 35 32 public FullFunctionalExpressionGrammar() 36 33 : base() { … … 60 57 constant.MinValue = -20; 61 58 constant.MaxValue = 20; 62 variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable(); 59 var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable(); 60 var laggedVariable = new LaggedVariable(); 61 laggedVariable.InitialFrequency = 0.0; 63 62 64 var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };63 var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol, laggedVariable }; 65 64 var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not }; 66 65 var binaryFunctionSymbols = new List<Symbol>() { gt, lt }; 67 66 var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or }; 67 var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable }; 68 68 69 69 foreach (var symb in allSymbols) … … 83 83 } 84 84 85 foreach (var terminalSymbol in terminalSymbols) { 86 SetMinSubtreeCount(terminalSymbol, 0); 87 SetMaxSubtreeCount(terminalSymbol, 0); 88 } 89 85 90 SetMinSubtreeCount(@if, 3); 86 91 SetMaxSubtreeCount(@if, 3); 87 SetMinSubtreeCount(constant, 0); 88 SetMaxSubtreeCount(constant, 0); 89 SetMinSubtreeCount(variableSymbol, 0); 90 SetMaxSubtreeCount(variableSymbol, 0); 92 91 93 92 94 // allow each symbol as child of the start symbol
Note: See TracChangeset
for help on using the changeset viewer.