Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs @ 4674

Last change on this file since 4674 was 4674, checked in by gkronber, 13 years ago

Refactored cloning in SymbolicExpressionTreeEncoding. #922

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