Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5686 was 5686, checked in by mkommend, 13 years ago

#1418: Finally added results from the grammar refactoring.

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  [StorableClass]
30  public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode {
31    [Storable]
32    private IList<ISymbolicExpressionTreeNode> subTrees;
33    [Storable]
34    private ISymbol symbol;
35
36    // cached values to prevent unnecessary tree iterations
37    private ushort length;
38    private ushort depth;
39
40    public ISymbol Symbol {
41      get { return symbol; }
42      protected set { symbol = value; }
43    }
44
45    // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree)
46    private ISymbolicExpressionTreeNode parent;
47    public ISymbolicExpressionTreeNode Parent {
48      get { return parent; }
49      set { parent = value; }
50    }
51
52    [StorableConstructor]
53    protected SymbolicExpressionTreeNode(bool deserializing) { }
54    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
55      : base() {
56      symbol = original.symbol; // symbols are reused
57      subTrees = new List<ISymbolicExpressionTreeNode>(original.subTrees.Count);
58      foreach (var subtree in original.subTrees) {
59        var clonedSubTree = cloner.Clone(subtree);
60        subTrees.Add(clonedSubTree);
61        clonedSubTree.Parent = this;
62      }
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new SymbolicExpressionTreeNode(this, cloner);
66    }
67
68    internal SymbolicExpressionTreeNode()
69      : base() {
70      // don't allocate subtrees list here!
71      // because we don't want to allocate it in terminal nodes
72    }
73
74    public SymbolicExpressionTreeNode(ISymbol symbol)
75      : base() {
76      subTrees = new List<ISymbolicExpressionTreeNode>(3);
77      this.symbol = symbol;
78    }
79
80
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() {
83      foreach (var subtree in subTrees) {
84        subtree.Parent = this;
85      }
86    }
87
88    public virtual bool HasLocalParameters {
89      get { return false; }
90    }
91
92    public virtual IEnumerable<ISymbolicExpressionTreeNode> SubTrees {
93      get { return subTrees; }
94    }
95
96    public virtual ISymbolicExpressionTreeGrammar Grammar {
97      get { return parent.Grammar; }
98    }
99
100    public int GetLength() {
101      if (length > 0) return length;
102      else {
103        length = 1;
104        if (subTrees != null) {
105          for (int i = 0; i < subTrees.Count; i++) {
106            checked { length += (ushort)subTrees[i].GetLength(); }
107          }
108        }
109        return length;
110      }
111    }
112
113    public int GetDepth() {
114      if (depth > 0) return depth;
115      else {
116        if (subTrees != null) {
117          for (int i = 0; i < subTrees.Count; i++) depth = Math.Max(depth, (ushort)subTrees[i].GetDepth());
118        }
119        depth++;
120        return depth;
121      }
122    }
123
124    public virtual void ResetLocalParameters(IRandom random) { }
125    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
126
127    public int SubtreesCount {
128      get {
129        if (subTrees == null) return 0;
130        return subTrees.Count;
131      }
132    }
133
134    public virtual ISymbolicExpressionTreeNode GetSubTree(int index) {
135      return subTrees[index];
136    }
137    public virtual int IndexOfSubTree(ISymbolicExpressionTreeNode tree) {
138      return subTrees.IndexOf(tree);
139    }
140    public virtual void AddSubTree(ISymbolicExpressionTreeNode tree) {
141      subTrees.Add(tree);
142      tree.Parent = this;
143      ResetCachedValues();
144    }
145    public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) {
146      subTrees.Insert(index, tree);
147      tree.Parent = this;
148      ResetCachedValues();
149    }
150    public virtual void RemoveSubTree(int index) {
151      subTrees[index].Parent = null;
152      subTrees.RemoveAt(index);
153      ResetCachedValues();
154    }
155
156    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
157      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
158      ForEachNodePrefix((n) => list.Add(n));
159      return list;
160    }
161
162    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
163      a(this);
164      if (SubTrees != null) {
165        foreach (var subtree in SubTrees) {
166          subtree.ForEachNodePrefix(a);
167        }
168      }
169    }
170
171    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
172      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
173      ForEachNodePostfix((n) => list.Add(n));
174      return list;
175    }
176
177    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
178      if (SubTrees != null) {
179        foreach (var subtree in SubTrees) {
180          subtree.ForEachNodePostfix(a);
181        }
182      }
183      a(this);
184    }
185
186    public override string ToString() {
187      return Symbol.Name;
188    }
189
190    private void ResetCachedValues() {
191      length = 0; depth = 0;
192      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
193      if (parentNode != null) parentNode.ResetCachedValues();
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.