Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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