Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs @ 12891

Last change on this file since 12891 was 12891, checked in by bburlacu, 9 years ago

#1772: Merge trunk changes. Remove dead code from the genealogy analyzer.

File size: 7.4 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
[12155]3 * Copyright (C) 2002-2015 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
[12231]52    [Storable]
53    public double NodeWeight { get; set; }
54
[4722]55    [StorableConstructor]
56    protected SymbolicExpressionTreeNode(bool deserializing) { }
57    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
[7836]58      : base(original, cloner) {
[4722]59      symbol = original.symbol; // symbols are reused
[7654]60      length = original.length;
61      depth = original.depth;
[12231]62      NodeWeight = original.NodeWeight;
[6684]63      if (original.subtrees != null) {
64        subtrees = new List<ISymbolicExpressionTreeNode>(original.subtrees.Count);
65        foreach (var subtree in original.subtrees) {
66          var clonedSubtree = cloner.Clone(subtree);
67          subtrees.Add(clonedSubtree);
68          clonedSubtree.Parent = this;
69        }
[4722]70      }
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new SymbolicExpressionTreeNode(this, cloner);
74    }
75
76    internal SymbolicExpressionTreeNode()
77      : base() {
[3486]78      // don't allocate subtrees list here!
79      // because we don't want to allocate it in terminal nodes
80    }
[3484]81
[5510]82    public SymbolicExpressionTreeNode(ISymbol symbol)
[4722]83      : base() {
[5733]84      subtrees = new List<ISymbolicExpressionTreeNode>(3);
[3223]85      this.symbol = symbol;
86    }
87
88
[3486]89    [StorableHook(HookType.AfterDeserialization)]
[4722]90    private void AfterDeserialization() {
[5733]91      if (subtrees != null) {
92        foreach (var subtree in subtrees)
[5728]93          subtree.Parent = this;
[3486]94      }
95    }
96
[3244]97    public virtual bool HasLocalParameters {
[3223]98      get { return false; }
99    }
100
[5733]101    public virtual IEnumerable<ISymbolicExpressionTreeNode> Subtrees {
102      get { return subtrees; }
[3223]103    }
104
[5499]105    public virtual ISymbolicExpressionTreeGrammar Grammar {
[3369]106      get { return parent.Grammar; }
107    }
108
[5549]109    public int GetLength() {
110      if (length > 0) return length;
[3988]111      else {
[5549]112        length = 1;
[5733]113        if (subtrees != null) {
114          for (int i = 0; i < subtrees.Count; i++) {
115            checked { length += (ushort)subtrees[i].GetLength(); }
[4524]116          }
[4106]117        }
[5549]118        return length;
[3988]119      }
[3223]120    }
121
[5549]122    public int GetDepth() {
123      if (depth > 0) return depth;
[3988]124      else {
[5733]125        if (subtrees != null) {
126          for (int i = 0; i < subtrees.Count; i++) depth = Math.Max(depth, (ushort)subtrees[i].GetDepth());
[4106]127        }
[5549]128        depth++;
129        return depth;
[3988]130      }
[3223]131    }
[3294]132
[7506]133    public int GetBranchLevel(ISymbolicExpressionTreeNode child) {
134      return GetBranchLevel(this, child);
135    }
136
137    private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
138      if (root == point)
139        return 0;
140      foreach (var subtree in root.Subtrees) {
141        int branchLevel = GetBranchLevel(subtree, point);
142        if (branchLevel < int.MaxValue)
143          return 1 + branchLevel;
144      }
145      return int.MaxValue;
146    }
147
[3338]148    public virtual void ResetLocalParameters(IRandom random) { }
149    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
150
[6803]151    public int SubtreeCount {
[5686]152      get {
[5733]153        if (subtrees == null) return 0;
154        return subtrees.Count;
[5686]155      }
156    }
157
[5733]158    public virtual ISymbolicExpressionTreeNode GetSubtree(int index) {
159      return subtrees[index];
[5510]160    }
[5733]161    public virtual int IndexOfSubtree(ISymbolicExpressionTreeNode tree) {
162      return subtrees.IndexOf(tree);
[5510]163    }
[5733]164    public virtual void AddSubtree(ISymbolicExpressionTreeNode tree) {
165      subtrees.Add(tree);
[3369]166      tree.Parent = this;
[3988]167      ResetCachedValues();
[3294]168    }
[5733]169    public virtual void InsertSubtree(int index, ISymbolicExpressionTreeNode tree) {
170      subtrees.Insert(index, tree);
[3369]171      tree.Parent = this;
[3988]172      ResetCachedValues();
[3294]173    }
[5733]174    public virtual void RemoveSubtree(int index) {
175      subtrees[index].Parent = null;
176      subtrees.RemoveAt(index);
[3988]177      ResetCachedValues();
[3294]178    }
179
[7795]180    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesBreadth() {
[12891]181      var list = new List<ISymbolicExpressionTreeNode>(GetLength()) { this };
[7795]182      int i = 0;
183      while (i != list.Count) {
184        for (int j = 0; j != list[i].SubtreeCount; ++j)
185          list.Add(list[i].GetSubtree(j));
186        ++i;
187      }
188      return list;
189    }
190
[5510]191    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
192      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
[3997]193      ForEachNodePrefix((n) => list.Add(n));
194      return list;
[3294]195    }
196
[5510]197    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
[3988]198      a(this);
[12891]199      if (subtrees != null) {
200        //avoid linq to reduce memory pressure
201        for (int i = 0; i < subtrees.Count; i++) {
202          subtrees[i].ForEachNodePrefix(a);
[4106]203        }
[3988]204      }
205    }
206
[5510]207    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
208      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
[3997]209      ForEachNodePostfix((n) => list.Add(n));
210      return list;
[3294]211    }
[3988]212
[5510]213    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
[12891]214      if (subtrees != null) {
215        //avoid linq to reduce memory pressure
216        for (int i = 0; i < subtrees.Count; i++) {
217          subtrees[i].ForEachNodePostfix(a);
[4106]218        }
[3988]219      }
220      a(this);
221    }
222
[3442]223    public override string ToString() {
[6375]224      if (Symbol != null) return Symbol.Name;
225      return "SymbolicExpressionTreeNode";
[3442]226    }
[3988]227
228    private void ResetCachedValues() {
[5549]229      length = 0; depth = 0;
[5510]230      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
231      if (parentNode != null) parentNode.ResetCachedValues();
[3988]232    }
[3223]233  }
234}
Note: See TracBrowser for help on using the repository browser.