Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineCreater.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineCreater.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineCreater.cs	(revision 4249)
@@ -111,5 +111,5 @@
       symbolicExpressionTree.Root.AddSubTree(defunNode);
       // the grammar in the newly defined function is a clone of the grammar of the originating branch
-      defunNode.Grammar = (ISymbolicExpressionGrammar)selectedBody.Grammar.Clone();
+      defunNode.SetGrammar((ISymbolicExpressionGrammar)selectedBody.Grammar.Clone());
       // remove all argument symbols from grammar
       var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList();
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDuplicater.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDuplicater.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDuplicater.cs	(revision 4249)
@@ -66,5 +66,5 @@
       duplicatedDefunBranch.FunctionName = newFunctionName;
       symbolicExpressionTree.Root.AddSubTree(duplicatedDefunBranch);
-      duplicatedDefunBranch.Grammar = (ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone();
+      duplicatedDefunBranch.SetGrammar((ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone());
       // add an invoke symbol for each branch that is allowed to invoke the original function
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 4249)
@@ -53,7 +53,7 @@
       ) {
       SymbolicExpressionTree tree = new SymbolicExpressionTree();
-      var rootNode = grammar.StartSymbol.CreateTreeNode();
+      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
       if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
-      rootNode.Grammar = grammar;
+      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
       tree.Root = PTC2(random, rootNode, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);
       return tree;
@@ -186,5 +186,5 @@
       // also assumes that newTree is already attached to root somewhere
       if (IsTopLevelBranch(root, newTree)) {
-        newTree.Grammar = (ISymbolicExpressionGrammar)root.Grammar.Clone();
+        ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionGrammar)root.Grammar.Clone());
 
         // allow invokes of existing ADFs with higher index
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs	(revision 4249)
@@ -90,38 +90,48 @@
     public DefaultSymbolicExpressionGrammar()
       : base() {
-      minSubTreeCount = new Dictionary<string, int>();
-      maxSubTreeCount = new Dictionary<string, int>();
-      allowedChildSymbols = new Dictionary<string, List<List<string>>>();
-      allSymbols = new Dictionary<string, Symbol>();
-
-      cachedMinExpressionLength = new Dictionary<string, int>();
-      cachedMaxExpressionLength = new Dictionary<string, int>();
-      cachedMinExpressionDepth = new Dictionary<string, int>();
-
-      startSymbol = new StartSymbol();
-      AddSymbol(startSymbol);
-      SetMinSubtreeCount(startSymbol, 1);
-      SetMaxSubtreeCount(startSymbol, 1);
-    }
-
-    //copy constructor for cloning
-    protected DefaultSymbolicExpressionGrammar(DefaultSymbolicExpressionGrammar copy)
+      this.minSubTreeCount = new Dictionary<string, int>();
+      this.maxSubTreeCount = new Dictionary<string, int>();
+      this.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
+      this.allSymbols = new Dictionary<string, Symbol>();
+      this.cachedMinExpressionLength = new Dictionary<string, int>();
+      this.cachedMaxExpressionLength = new Dictionary<string, int>();
+      this.cachedMinExpressionDepth = new Dictionary<string, int>();
+
+      this.startSymbol = new StartSymbol();
+      this.AddSymbol(startSymbol);
+      this.SetMinSubtreeCount(startSymbol, 1);
+      this.SetMaxSubtreeCount(startSymbol, 1);
+    }
+
+    protected DefaultSymbolicExpressionGrammar(ISymbolicExpressionGrammar grammar)
       : base() {
-      this.minSubTreeCount = new Dictionary<string, int>(copy.minSubTreeCount);
-      this.maxSubTreeCount = new Dictionary<string, int>(copy.maxSubTreeCount);
-
-      this.startSymbol = copy.startSymbol;
+      Cloner cloner = new Cloner();
+      this.cachedMinExpressionLength = new Dictionary<string, int>();
+      this.cachedMaxExpressionLength = new Dictionary<string, int>();
+      this.cachedMinExpressionDepth = new Dictionary<string, int>();
+
+      this.minSubTreeCount = new Dictionary<string, int>();
+      this.maxSubTreeCount = new Dictionary<string, int>();
       this.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
-      foreach (var entry in copy.allowedChildSymbols) {
-        this.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
-        foreach (var set in entry.Value) {
-          this.allowedChildSymbols[entry.Key].Add(new List<string>(set));
+      this.allSymbols = new Dictionary<string, Symbol>();
+
+      this.StartSymbol = (Symbol)cloner.Clone(grammar.StartSymbol);
+
+      foreach (Symbol symbol in grammar.Symbols) {
+        Symbol clonedSymbol = (Symbol)cloner.Clone(symbol);
+        this.AddSymbol(clonedSymbol);
+        this.SetMinSubtreeCount(clonedSymbol, grammar.GetMinSubtreeCount(symbol));
+        this.SetMaxSubtreeCount(clonedSymbol, grammar.GetMaxSubtreeCount(symbol));
+      }
+
+      foreach (Symbol parent in grammar.Symbols) {
+        for (int i = 0; i < grammar.GetMaxSubtreeCount(parent); i++) {
+          foreach (Symbol child in grammar.Symbols) {
+            if (grammar.IsAllowedChild(parent, child, i)) {
+              this.SetAllowedChild((Symbol)cloner.Clone(parent), (Symbol)cloner.Clone(child), i);
+            }
+          }
         }
       }
-      this.allSymbols = new Dictionary<string, Symbol>(copy.allSymbols);
-
-      cachedMinExpressionLength = new Dictionary<string, int>();
-      cachedMaxExpressionLength = new Dictionary<string, int>();
-      cachedMinExpressionDepth = new Dictionary<string, int>();
     }
 
@@ -151,5 +161,4 @@
 
     #region ISymbolicExpressionGrammar Members
-
     public Symbol StartSymbol {
       get { return startSymbol; }
@@ -272,5 +281,4 @@
       return maxSubTreeCount[symbol.Name];
     }
-
     #endregion
 
@@ -282,8 +290,40 @@
 
     public override IDeepCloneable Clone(Cloner cloner) {
-      DefaultSymbolicExpressionGrammar clone = new DefaultSymbolicExpressionGrammar(this);
-      cloner.RegisterClonedObject(this, clone);
+      DefaultSymbolicExpressionGrammar clone = (DefaultSymbolicExpressionGrammar)base.Clone(cloner);
+
+      clone.minSubTreeCount = new Dictionary<string, int>(this.minSubTreeCount);
+      clone.maxSubTreeCount = new Dictionary<string, int>(this.maxSubTreeCount);
+
+      clone.allSymbols = new Dictionary<string, Symbol>();
+      foreach (Symbol symbol in this.allSymbols.Values.Select(s => cloner.Clone(s)))
+        clone.allSymbols.Add(symbol.Name, symbol);
+
+      clone.startSymbol = (Symbol)cloner.Clone(this.startSymbol);
+      clone.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
+      foreach (var entry in this.allowedChildSymbols) {
+        clone.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
+        foreach (var set in entry.Value) {
+          clone.allowedChildSymbols[entry.Key].Add(new List<string>(set));
+        }
+      }
+
       return clone;
     }
+
+    protected void InitializeShallowClone(DefaultSymbolicExpressionGrammar clone) {
+      clone.minSubTreeCount = new Dictionary<string, int>(this.minSubTreeCount);
+      clone.maxSubTreeCount = new Dictionary<string, int>(this.maxSubTreeCount);
+
+      clone.allSymbols = new Dictionary<string, Symbol>(this.allSymbols);
+      clone.startSymbol = this.startSymbol;
+      clone.allowedChildSymbols = new Dictionary<string, List<List<string>>>(this.allowedChildSymbols.Count);
+      foreach (var entry in this.allowedChildSymbols) {
+        clone.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
+        foreach (var set in entry.Value) {
+          clone.allowedChildSymbols[entry.Key].Add(new List<string>(set));
+        }
+      }
+    }
+
   }
 }
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs	(revision 4249)
@@ -20,4 +20,6 @@
 #endregion
 
+using System;
+using System.Linq;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
@@ -67,22 +69,39 @@
     private Defun defunSymbol;
 
-
-
     public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar)
-      : base() {
+      : base(mainBranchGrammar) {
       maxFunctionArguments = 3;
       maxFunctionDefinitions = 3;
-      Initialize(mainBranchGrammar);
+
+      ProgramRootSymbol programRootSymbol = Symbols.OfType<ProgramRootSymbol>().FirstOrDefault();
+      if (programRootSymbol == null) {
+        programRootSymbol = new ProgramRootSymbol();
+        AddSymbol(programRootSymbol);
+      }
+      StartSymbol = programRootSymbol;
+
+      defunSymbol = Symbols.OfType<Defun>().FirstOrDefault();
+      if (defunSymbol == null) {
+        defunSymbol = new Defun();
+        AddSymbol(defunSymbol);
+      }
+
+      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
+      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
+      SetMinSubtreeCount(defunSymbol, 1);
+      SetMaxSubtreeCount(defunSymbol, 1);
+
+      // the start symbol of the mainBranchGrammar is allowed as the result producing branch
+      SetAllowedChild(StartSymbol, Symbols.Where(s => s.Name == mainBranchGrammar.StartSymbol.Name).First(), 0);
+
+      // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
+      foreach (var symb in mainBranchGrammar.Symbols) {
+        if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
+          SetAllowedChild(defunSymbol, Symbols.Where(s => s.Name == symb.Name).First(), 0);
+      }
     }
 
-    //copy constructor for cloning
-    protected GlobalSymbolicExpressionGrammar(GlobalSymbolicExpressionGrammar copy)
-      : base(copy) {
-      this.maxFunctionArguments = copy.maxFunctionArguments;
-      this.minFunctionArguments = copy.minFunctionArguments;
-      this.maxFunctionDefinitions = copy.maxFunctionDefinitions;
-      this.minFunctionDefinitions = copy.minFunctionDefinitions;
-    }
-
+    //ctor for cloning
+    protected GlobalSymbolicExpressionGrammar() : base() { }
     [StorableConstructor]
     protected GlobalSymbolicExpressionGrammar(bool deserializing)
@@ -90,4 +109,5 @@
     }
 
+    [Obsolete]
     private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
       base.Clear();
@@ -151,6 +171,10 @@
 
     public override IDeepCloneable Clone(Cloner cloner) {
-      GlobalSymbolicExpressionGrammar clone = new GlobalSymbolicExpressionGrammar(this);
-      cloner.RegisterClonedObject(this, clone);
+      GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
+      clone.defunSymbol = (Defun)cloner.Clone(this.defunSymbol);
+      clone.maxFunctionArguments = this.maxFunctionArguments;
+      clone.minFunctionArguments = this.minFunctionArguments;
+      clone.maxFunctionDefinitions = this.maxFunctionDefinitions;
+      clone.minFunctionDefinitions = this.minFunctionDefinitions;
       return clone;
     }
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj	(revision 4249)
@@ -135,4 +135,5 @@
     <Compile Include="Manipulators\OnePointShaker.cs" />
     <Compile Include="Manipulators\SymbolicExpressionTreeManipulator.cs" />
+    <Compile Include="SymbolicExpressionTreeGrammar.cs" />
     <Compile Include="SymbolicExpressionTreeStringFormatter.cs" />
     <Compile Include="SymbolicExpressionTreeTopLevelNode.cs" />
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeGrammar.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeGrammar.cs	(revision 4249)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeGrammar.cs	(revision 4249)
@@ -0,0 +1,42 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using HeuristicLab.Common;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
+  public sealed class SymbolicExpressionTreeGrammar : DefaultSymbolicExpressionGrammar {
+    public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
+      : base(grammar) {
+    }
+    [StorableConstructor]
+    private SymbolicExpressionTreeGrammar(bool deserializing) : base(deserializing) { }
+    //default ctor for cloning
+    private SymbolicExpressionTreeGrammar() : base(false) { }
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      SymbolicExpressionTreeGrammar clone = new SymbolicExpressionTreeGrammar();
+      cloner.RegisterClonedObject(this, clone);
+      InitializeShallowClone(clone);
+      return clone;
+    }
+  }
+}
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs	(revision 4249)
@@ -87,5 +87,4 @@
     public virtual ISymbolicExpressionGrammar Grammar {
       get { return parent.Grammar; }
-      set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); }
     }
 
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs	(revision 4249)
@@ -26,5 +26,4 @@
   [StorableClass]
   public class SymbolicExpressionTreeTopLevelNode : SymbolicExpressionTreeNode {
-
     public SymbolicExpressionTreeTopLevelNode()
       : base() {
@@ -39,5 +38,7 @@
     public override ISymbolicExpressionGrammar Grammar {
       get { return grammar; }
-      set { grammar = value; }
+    }
+    public void SetGrammar(ISymbolicExpressionGrammar grammar) {
+      this.grammar = grammar;
     }
 
@@ -47,4 +48,5 @@
       if (original.Grammar != null)
         grammar = (ISymbolicExpressionGrammar)original.Grammar.Clone();
+      //grammar = original.grammar;
     }
 
Index: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/ArithmeticExpressionGrammar.cs
===================================================================
--- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/ArithmeticExpressionGrammar.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/ArithmeticExpressionGrammar.cs	(revision 4249)
@@ -30,7 +30,4 @@
   [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
   public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar {
-    [Storable]
-    private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
-
     public ArithmeticExpressionGrammar()
       : base() {
@@ -49,5 +46,5 @@
       constant.MinValue = -20;
       constant.MaxValue = 20;
-      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
+      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
 
       var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
Index: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs
===================================================================
--- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs	(revision 4246)
+++ trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs	(revision 4249)
@@ -30,7 +30,4 @@
   [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
   public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {
-    [Storable]
-    private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
-
     public FullFunctionalExpressionGrammar()
       : base() {
@@ -60,10 +57,13 @@
       constant.MinValue = -20;
       constant.MaxValue = 20;
-      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
+      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
+      var laggedVariable = new LaggedVariable();
+      laggedVariable.InitialFrequency = 0.0;
 
-      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
+      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol, laggedVariable };
       var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
       var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
       var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
+      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
 
       foreach (var symb in allSymbols)
@@ -83,10 +83,12 @@
       }
 
+      foreach (var terminalSymbol in terminalSymbols) {
+        SetMinSubtreeCount(terminalSymbol, 0);
+        SetMaxSubtreeCount(terminalSymbol, 0);
+      }
+
       SetMinSubtreeCount(@if, 3);
       SetMaxSubtreeCount(@if, 3);
-      SetMinSubtreeCount(constant, 0);
-      SetMaxSubtreeCount(constant, 0);
-      SetMinSubtreeCount(variableSymbol, 0);
-      SetMaxSubtreeCount(variableSymbol, 0);
+
 
       // allow each symbol as child of the start symbol
