Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs @ 5519

Last change on this file since 5519 was 5510, checked in by gkronber, 14 years ago

#1418 Fixed compiler errors in symbolic expression tree encoding

File size: 6.3 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3223]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[4068]23using System.Collections.Generic;
[3294]24using System.Linq;
[4722]25using HeuristicLab.Common;
[3223]26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
30  [StorableClass]
[5499]31  public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode {
[3252]32    [Storable]
[5510]33    private IList<ISymbolicExpressionTreeNode> subTrees;
[3252]34    [Storable]
[5510]35    private ISymbol symbol;
[3988]36
37    // cached values to prevent unnecessary tree iterations
[5411]38    private ushort size;
39    private ushort height;
[3988]40
[5510]41    public ISymbol Symbol {
[3484]42      get { return symbol; }
43      protected set { symbol = value; }
44    }
[3462]45
46    // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree)
[5510]47    private ISymbolicExpressionTreeNode parent;
48    public ISymbolicExpressionTreeNode Parent {
[3484]49      get { return parent; }
50      set { parent = value; }
51    }
[3223]52
[4722]53    [StorableConstructor]
54    protected SymbolicExpressionTreeNode(bool deserializing) { }
55    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
56      : base() {
57      symbol = original.symbol; // symbols are reused
[5510]58      subTrees = new List<ISymbolicExpressionTreeNode>(original.subTrees.Count);
59      foreach (var subtree in original.subTrees) {
[4722]60        var clonedSubTree = cloner.Clone(subtree);
61        subTrees.Add(clonedSubTree);
62        clonedSubTree.Parent = this;
63      }
64    }
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SymbolicExpressionTreeNode(this, cloner);
67    }
68
69    internal SymbolicExpressionTreeNode()
70      : base() {
[3486]71      // don't allocate subtrees list here!
72      // because we don't want to allocate it in terminal nodes
73    }
[3484]74
[5510]75    public SymbolicExpressionTreeNode(ISymbol symbol)
[4722]76      : base() {
[5510]77      subTrees = new List<ISymbolicExpressionTreeNode>(3);
[3223]78      this.symbol = symbol;
79    }
80
81
[3486]82    [StorableHook(HookType.AfterDeserialization)]
[4722]83    private void AfterDeserialization() {
[5510]84      foreach (var subtree in subTrees) {
[3486]85        subtree.Parent = this;
86      }
87    }
88
[3244]89    public virtual bool HasLocalParameters {
[3223]90      get { return false; }
91    }
92
[5510]93    public virtual IEnumerable<ISymbolicExpressionTreeNode> SubTrees {
[3223]94      get { return subTrees; }
95    }
96
[5499]97    public virtual ISymbolicExpressionTreeGrammar Grammar {
[3369]98      get { return parent.Grammar; }
99    }
100
[3244]101    public int GetSize() {
[3988]102      if (size > 0) return size;
103      else {
104        size = 1;
[5510]105        if (subTrees != null) {
106          for (int i = 0; i < subTrees.Count; i++) {
107            checked { size += (ushort)subTrees[i].GetSize(); }
[4524]108          }
[4106]109        }
[3988]110        return size;
111      }
[3223]112    }
113
[3244]114    public int GetHeight() {
[3988]115      if (height > 0) return height;
116      else {
[5510]117        if (subTrees != null) {
118          for (int i = 0; i < subTrees.Count; i++) height = Math.Max(height, (ushort)subTrees[i].GetHeight());
[4106]119        }
[3988]120        height++;
121        return height;
122      }
[3223]123    }
[3294]124
[3338]125    public virtual void ResetLocalParameters(IRandom random) { }
126    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
127
[5510]128    public virtual ISymbolicExpressionTreeNode GetSubTree(int index) {
129      return subTrees[index];
130    }
131    public virtual int IndexOfSubTree(ISymbolicExpressionTreeNode tree) {
132      return subTrees.IndexOf(tree);
133    }
134    public virtual void AddSubTree(ISymbolicExpressionTreeNode tree) {
135      subTrees.Add(tree);
[3369]136      tree.Parent = this;
[3988]137      ResetCachedValues();
[3294]138    }
[5510]139    public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) {
140      subTrees.Insert(index, tree);
[3369]141      tree.Parent = this;
[3988]142      ResetCachedValues();
[3294]143    }
[3338]144    public virtual void RemoveSubTree(int index) {
[5510]145      subTrees[index].Parent = null;
146      subTrees.RemoveAt(index);
[3988]147      ResetCachedValues();
[3294]148    }
149
[5510]150    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
151      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
[3997]152      ForEachNodePrefix((n) => list.Add(n));
153      return list;
[3294]154    }
155
[5510]156    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
[3988]157      a(this);
[4106]158      if (SubTrees != null) {
[5510]159        foreach (var subtree in SubTrees) {
160          subtree.ForEachNodePrefix(a);
[4106]161        }
[3988]162      }
163    }
164
[5510]165    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
166      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
[3997]167      ForEachNodePostfix((n) => list.Add(n));
168      return list;
[3294]169    }
[3988]170
[5510]171    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
[4106]172      if (SubTrees != null) {
[5510]173        foreach (var subtree in SubTrees) {
174          subtree.ForEachNodePostfix(a);
[4106]175        }
[3988]176      }
177      a(this);
178    }
179
[5499]180    public IEnumerable<ISymbol> GetAllowedSymbols(int argumentIndex) {
[3338]181      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
[3294]182    }
[5014]183
[3338]184    public int GetMinSubtreeCount() {
185      return Grammar.GetMinSubtreeCount(Symbol);
[3223]186    }
[3338]187    public int GetMaxSubtreeCount() {
188      return Grammar.GetMaxSubtreeCount(Symbol);
[3223]189    }
[3442]190    public override string ToString() {
191      return Symbol.Name;
192    }
[3988]193
194    private void ResetCachedValues() {
195      size = 0; height = 0;
[5510]196      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
197      if (parentNode != null) parentNode.ResetCachedValues();
[3988]198    }
[3223]199  }
200}
Note: See TracBrowser for help on using the repository browser.