Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6375 was 6375, checked in by mkommend, 13 years ago

#1549: Minor changes in SymbolicExpressionTreeNode and the SymbolicExpressionTreeChart that are necessary for the debugger visualizer to work correctly.

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