Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4249


Ignore:
Timestamp:
08/18/10 14:57:11 (14 years ago)
Author:
mkommend
Message:

Refactored grammars (ticket #1028).

Location:
trunk/sources
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineCreater.cs

    r4068 r4249  
    111111      symbolicExpressionTree.Root.AddSubTree(defunNode);
    112112      // 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());
    114114      // remove all argument symbols from grammar
    115115      var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList();
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/SubroutineDuplicater.cs

    r4068 r4249  
    6666      duplicatedDefunBranch.FunctionName = newFunctionName;
    6767      symbolicExpressionTree.Root.AddSubTree(duplicatedDefunBranch);
    68       duplicatedDefunBranch.Grammar = (ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone();
     68      duplicatedDefunBranch.SetGrammar((ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone());
    6969      // add an invoke symbol for each branch that is allowed to invoke the original function
    7070      foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs

    r4219 r4249  
    5353      ) {
    5454      SymbolicExpressionTree tree = new SymbolicExpressionTree();
    55       var rootNode = grammar.StartSymbol.CreateTreeNode();
     55      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
    5656      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
    57       rootNode.Grammar = grammar;
     57      rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar));
    5858      tree.Root = PTC2(random, rootNode, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);
    5959      return tree;
     
    186186      // also assumes that newTree is already attached to root somewhere
    187187      if (IsTopLevelBranch(root, newTree)) {
    188         newTree.Grammar = (ISymbolicExpressionGrammar)root.Grammar.Clone();
     188        ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionGrammar)root.Grammar.Clone());
    189189
    190190        // allow invokes of existing ADFs with higher index
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs

    r4068 r4249  
    9090    public DefaultSymbolicExpressionGrammar()
    9191      : 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)
    109107      : 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>();
    114115      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          }
    119134        }
    120135      }
    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>();
    126136    }
    127137
     
    151161
    152162    #region ISymbolicExpressionGrammar Members
    153 
    154163    public Symbol StartSymbol {
    155164      get { return startSymbol; }
     
    272281      return maxSubTreeCount[symbol.Name];
    273282    }
    274 
    275283    #endregion
    276284
     
    282290
    283291    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
    286310      return clone;
    287311    }
     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
    288328  }
    289329}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs

    r4068 r4249  
    2020#endregion
    2121
     22using System;
     23using System.Linq;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    6769    private Defun defunSymbol;
    6870
    69 
    70 
    7171    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar)
    72       : base() {
     72      : base(mainBranchGrammar) {
    7373      maxFunctionArguments = 3;
    7474      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      }
    76102    }
    77103
    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() { }
    87106    [StorableConstructor]
    88107    protected GlobalSymbolicExpressionGrammar(bool deserializing)
     
    90109    }
    91110
     111    [Obsolete]
    92112    private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
    93113      base.Clear();
     
    151171
    152172    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;
    155179      return clone;
    156180    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj

    r4189 r4249  
    135135    <Compile Include="Manipulators\OnePointShaker.cs" />
    136136    <Compile Include="Manipulators\SymbolicExpressionTreeManipulator.cs" />
     137    <Compile Include="SymbolicExpressionTreeGrammar.cs" />
    137138    <Compile Include="SymbolicExpressionTreeStringFormatter.cs" />
    138139    <Compile Include="SymbolicExpressionTreeTopLevelNode.cs" />
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs

    r4106 r4249  
    8787    public virtual ISymbolicExpressionGrammar Grammar {
    8888      get { return parent.Grammar; }
    89       set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); }
    9089    }
    9190
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs

    r4106 r4249  
    2626  [StorableClass]
    2727  public class SymbolicExpressionTreeTopLevelNode : SymbolicExpressionTreeNode {
    28 
    2928    public SymbolicExpressionTreeTopLevelNode()
    3029      : base() {
     
    3938    public override ISymbolicExpressionGrammar Grammar {
    4039      get { return grammar; }
    41       set { grammar = value; }
     40    }
     41    public void SetGrammar(ISymbolicExpressionGrammar grammar) {
     42      this.grammar = grammar;
    4243    }
    4344
     
    4748      if (original.Grammar != null)
    4849        grammar = (ISymbolicExpressionGrammar)original.Grammar.Clone();
     50      //grammar = original.grammar;
    4951    }
    5052
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/ArithmeticExpressionGrammar.cs

    r4068 r4249  
    3030  [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
    3131  public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar {
    32     [Storable]
    33     private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
    34 
    3532    public ArithmeticExpressionGrammar()
    3633      : base() {
     
    4946      constant.MinValue = -20;
    5047      constant.MaxValue = 20;
    51       variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
     48      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
    5249
    5350      var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs

    r4068 r4249  
    3030  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
    3131  public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {
    32     [Storable]
    33     private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
    34 
    3532    public FullFunctionalExpressionGrammar()
    3633      : base() {
     
    6057      constant.MinValue = -20;
    6158      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;
    6362
    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 };
    6564      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
    6665      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
    6766      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
     67      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
    6868
    6969      foreach (var symb in allSymbols)
     
    8383      }
    8484
     85      foreach (var terminalSymbol in terminalSymbols) {
     86        SetMinSubtreeCount(terminalSymbol, 0);
     87        SetMaxSubtreeCount(terminalSymbol, 0);
     88      }
     89
    8590      SetMinSubtreeCount(@if, 3);
    8691      SetMaxSubtreeCount(@if, 3);
    87       SetMinSubtreeCount(constant, 0);
    88       SetMaxSubtreeCount(constant, 0);
    89       SetMinSubtreeCount(variableSymbol, 0);
    90       SetMaxSubtreeCount(variableSymbol, 0);
     92
    9193
    9294      // allow each symbol as child of the start symbol
Note: See TracChangeset for help on using the changeset viewer.