Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5593 was 5549, checked in by gkronber, 14 years ago

#1418 unified size/height vs. length/depth terminology and adapted unit tests for symbolic expression tree encoding version 3.4

File size: 6.0 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
[5549]38    private ushort length;
39    private ushort depth;
[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
[5549]101    public int GetLength() {
102      if (length > 0) return length;
[3988]103      else {
[5549]104        length = 1;
[5510]105        if (subTrees != null) {
106          for (int i = 0; i < subTrees.Count; i++) {
[5549]107            checked { length += (ushort)subTrees[i].GetLength(); }
[4524]108          }
[4106]109        }
[5549]110        return length;
[3988]111      }
[3223]112    }
113
[5549]114    public int GetDepth() {
115      if (depth > 0) return depth;
[3988]116      else {
[5510]117        if (subTrees != null) {
[5549]118          for (int i = 0; i < subTrees.Count; i++) depth = Math.Max(depth, (ushort)subTrees[i].GetDepth());
[4106]119        }
[5549]120        depth++;
121        return depth;
[3988]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
[3442]180    public override string ToString() {
181      return Symbol.Name;
182    }
[3988]183
184    private void ResetCachedValues() {
[5549]185      length = 0; depth = 0;
[5510]186      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
187      if (parentNode != null) parentNode.ResetCachedValues();
[3988]188    }
[3223]189  }
190}
Note: See TracBrowser for help on using the repository browser.