Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs @ 10479

Last change on this file since 10479 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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      if (original.subtrees != null) {
58        subtrees = new List<ISymbolicExpressionTreeNode>(original.subtrees.Count);
59        foreach (var subtree in original.subtrees) {
60          var clonedSubtree = cloner.Clone(subtree);
61          subtrees.Add(clonedSubtree);
62          clonedSubtree.Parent = this;
63        }
64      }
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new SymbolicExpressionTreeNode(this, cloner);
68    }
69
70    internal SymbolicExpressionTreeNode()
71      : base() {
72      // don't allocate subtrees list here!
73      // because we don't want to allocate it in terminal nodes
74    }
75
76    public SymbolicExpressionTreeNode(ISymbol symbol)
77      : base() {
78      subtrees = new List<ISymbolicExpressionTreeNode>(3);
79      this.symbol = symbol;
80    }
81
82
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      if (subtrees != null) {
86        foreach (var subtree in subtrees)
87          subtree.Parent = this;
88      }
89    }
90
91    public virtual bool HasLocalParameters {
92      get { return false; }
93    }
94
95    public virtual IEnumerable<ISymbolicExpressionTreeNode> Subtrees {
96      get { return subtrees; }
97    }
98
99    public virtual ISymbolicExpressionTreeGrammar Grammar {
100      get { return parent.Grammar; }
101    }
102
103    public int GetLength() {
104      if (length > 0) return length;
105      else {
106        length = 1;
107        if (subtrees != null) {
108          for (int i = 0; i < subtrees.Count; i++) {
109            checked { length += (ushort)subtrees[i].GetLength(); }
110          }
111        }
112        return length;
113      }
114    }
115
116    public int GetDepth() {
117      if (depth > 0) return depth;
118      else {
119        if (subtrees != null) {
120          for (int i = 0; i < subtrees.Count; i++) depth = Math.Max(depth, (ushort)subtrees[i].GetDepth());
121        }
122        depth++;
123        return depth;
124      }
125    }
126
127    public virtual void ResetLocalParameters(IRandom random) { }
128    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
129
130    public int SubtreeCount {
131      get {
132        if (subtrees == null) return 0;
133        return subtrees.Count;
134      }
135    }
136
137    public virtual ISymbolicExpressionTreeNode GetSubtree(int index) {
138      return subtrees[index];
139    }
140    public virtual int IndexOfSubtree(ISymbolicExpressionTreeNode tree) {
141      return subtrees.IndexOf(tree);
142    }
143    public virtual void AddSubtree(ISymbolicExpressionTreeNode tree) {
144      subtrees.Add(tree);
145      tree.Parent = this;
146      ResetCachedValues();
147    }
148    public virtual void InsertSubtree(int index, ISymbolicExpressionTreeNode tree) {
149      subtrees.Insert(index, tree);
150      tree.Parent = this;
151      ResetCachedValues();
152    }
153    public virtual void RemoveSubtree(int index) {
154      subtrees[index].Parent = null;
155      subtrees.RemoveAt(index);
156      ResetCachedValues();
157    }
158
159    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
160      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
161      ForEachNodePrefix((n) => list.Add(n));
162      return list;
163    }
164
165    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
166      a(this);
167      if (Subtrees != null) {
168        foreach (var subtree in Subtrees) {
169          subtree.ForEachNodePrefix(a);
170        }
171      }
172    }
173
174    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
175      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
176      ForEachNodePostfix((n) => list.Add(n));
177      return list;
178    }
179
180    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
181      if (Subtrees != null) {
182        foreach (var subtree in Subtrees) {
183          subtree.ForEachNodePostfix(a);
184        }
185      }
186      a(this);
187    }
188
189    public override string ToString() {
190      if (Symbol != null) return Symbol.Name;
191      return "SymbolicExpressionTreeNode";
192    }
193
194    private void ResetCachedValues() {
195      length = 0; depth = 0;
196      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
197      if (parentNode != null) parentNode.ResetCachedValues();
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.