Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs @ 5959

Last change on this file since 5959 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

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      if (subtrees != null) {
84        foreach (var subtree in subtrees)
85          subtree.Parent = this;
86      }
87    }
88
89    public virtual bool HasLocalParameters {
90      get { return false; }
91    }
92
93    public virtual IEnumerable<ISymbolicExpressionTreeNode> Subtrees {
94      get { return subtrees; }
95    }
96
97    public virtual ISymbolicExpressionTreeGrammar Grammar {
98      get { return parent.Grammar; }
99    }
100
101    public int GetLength() {
102      if (length > 0) return length;
103      else {
104        length = 1;
105        if (subtrees != null) {
106          for (int i = 0; i < subtrees.Count; i++) {
107            checked { length += (ushort)subtrees[i].GetLength(); }
108          }
109        }
110        return length;
111      }
112    }
113
114    public int GetDepth() {
115      if (depth > 0) return depth;
116      else {
117        if (subtrees != null) {
118          for (int i = 0; i < subtrees.Count; i++) depth = Math.Max(depth, (ushort)subtrees[i].GetDepth());
119        }
120        depth++;
121        return depth;
122      }
123    }
124
125    public virtual void ResetLocalParameters(IRandom random) { }
126    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
127
128    public int SubtreesCount {
129      get {
130        if (subtrees == null) return 0;
131        return subtrees.Count;
132      }
133    }
134
135    public virtual ISymbolicExpressionTreeNode GetSubtree(int index) {
136      return subtrees[index];
137    }
138    public virtual int IndexOfSubtree(ISymbolicExpressionTreeNode tree) {
139      return subtrees.IndexOf(tree);
140    }
141    public virtual void AddSubtree(ISymbolicExpressionTreeNode tree) {
142      subtrees.Add(tree);
143      tree.Parent = this;
144      ResetCachedValues();
145    }
146    public virtual void InsertSubtree(int index, ISymbolicExpressionTreeNode tree) {
147      subtrees.Insert(index, tree);
148      tree.Parent = this;
149      ResetCachedValues();
150    }
151    public virtual void RemoveSubtree(int index) {
152      subtrees[index].Parent = null;
153      subtrees.RemoveAt(index);
154      ResetCachedValues();
155    }
156
157    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
158      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
159      ForEachNodePrefix((n) => list.Add(n));
160      return list;
161    }
162
163    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
164      a(this);
165      if (Subtrees != null) {
166        foreach (var subtree in Subtrees) {
167          subtree.ForEachNodePrefix(a);
168        }
169      }
170    }
171
172    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
173      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
174      ForEachNodePostfix((n) => list.Add(n));
175      return list;
176    }
177
178    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
179      if (Subtrees != null) {
180        foreach (var subtree in Subtrees) {
181          subtree.ForEachNodePostfix(a);
182        }
183      }
184      a(this);
185    }
186
187    public override string ToString() {
188      return Symbol.Name;
189    }
190
191    private void ResetCachedValues() {
192      length = 0; depth = 0;
193      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
194      if (parentNode != null) parentNode.ResetCachedValues();
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.