Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored grammars (ticket #1028).

File size: 5.8 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    internal 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    public virtual ISymbolicExpressionGrammar Grammar {
88      get { return parent.Grammar; }
89    }
90
91    public int GetSize() {
92      if (size > 0) return size;
93      else {
94        size = 1;
95        if (SubTrees != null) {
96          for (int i = 0; i < SubTrees.Count; i++) size += (short)SubTrees[i].GetSize();
97        }
98        return size;
99      }
100    }
101
102    public int GetHeight() {
103      if (height > 0) return height;
104      else {
105        if (SubTrees != null) {
106          for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (short)SubTrees[i].GetHeight());
107        }
108        height++;
109        return height;
110      }
111    }
112
113    public virtual void ResetLocalParameters(IRandom random) { }
114    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
115
116    public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
117      SubTrees.Add(tree);
118      tree.Parent = this;
119      ResetCachedValues();
120    }
121
122    public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
123      SubTrees.Insert(index, tree);
124      tree.Parent = this;
125      ResetCachedValues();
126    }
127
128    public virtual void RemoveSubTree(int index) {
129      SubTrees[index].Parent = null;
130      SubTrees.RemoveAt(index);
131      ResetCachedValues();
132    }
133
134    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
135      List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
136      ForEachNodePrefix((n) => list.Add(n));
137      return list;
138    }
139
140    public void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a) {
141      a(this);
142      if (SubTrees != null) {
143        for (int i = 0; i < SubTrees.Count; i++) {
144          SubTrees[i].ForEachNodePrefix(a);
145        }
146      }
147    }
148
149    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
150      List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
151      ForEachNodePostfix((n) => list.Add(n));
152      return list;
153    }
154
155    public void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a) {
156      if (SubTrees != null) {
157        for (int i = 0; i < SubTrees.Count; i++) {
158          SubTrees[i].ForEachNodePostfix(a);
159        }
160      }
161      a(this);
162    }
163
164    public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
165      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
166    }
167    public int GetMinSubtreeCount() {
168      return Grammar.GetMinSubtreeCount(Symbol);
169    }
170    public int GetMaxSubtreeCount() {
171      return Grammar.GetMaxSubtreeCount(Symbol);
172    }
173
174    #region ICloneable Members
175
176    public virtual object Clone() {
177      return new SymbolicExpressionTreeNode(this);
178    }
179
180    #endregion
181
182    public override string ToString() {
183      return Symbol.Name;
184    }
185
186    private void ResetCachedValues() {
187      size = 0; height = 0;
188      if (parent != null) parent.ResetCachedValues();
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.