Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1479: Merged grammar editor branch into trunk.

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
[6684]57      if (original.subtrees != null) {
58        subtrees = new List<ISymbolicExpressionTreeNode>(original.subtrees.Count);
59        foreach (var subtree in original.subtrees) {
60          var clonedSubtree = cloner.Clone(subtree);
61          subtrees.Add(clonedSubtree);
62          clonedSubtree.Parent = this;
63        }
[4722]64      }
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new SymbolicExpressionTreeNode(this, cloner);
68    }
69
70    internal SymbolicExpressionTreeNode()
71      : base() {
[3486]72      // don't allocate subtrees list here!
73      // because we don't want to allocate it in terminal nodes
74    }
[3484]75
[5510]76    public SymbolicExpressionTreeNode(ISymbol symbol)
[4722]77      : base() {
[5733]78      subtrees = new List<ISymbolicExpressionTreeNode>(3);
[3223]79      this.symbol = symbol;
80    }
81
82
[3486]83    [StorableHook(HookType.AfterDeserialization)]
[4722]84    private void AfterDeserialization() {
[5733]85      if (subtrees != null) {
86        foreach (var subtree in subtrees)
[5728]87          subtree.Parent = this;
[3486]88      }
89    }
90
[3244]91    public virtual bool HasLocalParameters {
[3223]92      get { return false; }
93    }
94
[5733]95    public virtual IEnumerable<ISymbolicExpressionTreeNode> Subtrees {
96      get { return subtrees; }
[3223]97    }
98
[5499]99    public virtual ISymbolicExpressionTreeGrammar Grammar {
[3369]100      get { return parent.Grammar; }
101    }
102
[5549]103    public int GetLength() {
104      if (length > 0) return length;
[3988]105      else {
[5549]106        length = 1;
[5733]107        if (subtrees != null) {
108          for (int i = 0; i < subtrees.Count; i++) {
109            checked { length += (ushort)subtrees[i].GetLength(); }
[4524]110          }
[4106]111        }
[5549]112        return length;
[3988]113      }
[3223]114    }
115
[5549]116    public int GetDepth() {
117      if (depth > 0) return depth;
[3988]118      else {
[5733]119        if (subtrees != null) {
120          for (int i = 0; i < subtrees.Count; i++) depth = Math.Max(depth, (ushort)subtrees[i].GetDepth());
[4106]121        }
[5549]122        depth++;
123        return depth;
[3988]124      }
[3223]125    }
[3294]126
[3338]127    public virtual void ResetLocalParameters(IRandom random) { }
128    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
129
[6803]130    public int SubtreeCount {
[5686]131      get {
[5733]132        if (subtrees == null) return 0;
133        return subtrees.Count;
[5686]134      }
135    }
136
[5733]137    public virtual ISymbolicExpressionTreeNode GetSubtree(int index) {
138      return subtrees[index];
[5510]139    }
[5733]140    public virtual int IndexOfSubtree(ISymbolicExpressionTreeNode tree) {
141      return subtrees.IndexOf(tree);
[5510]142    }
[5733]143    public virtual void AddSubtree(ISymbolicExpressionTreeNode tree) {
144      subtrees.Add(tree);
[3369]145      tree.Parent = this;
[3988]146      ResetCachedValues();
[3294]147    }
[5733]148    public virtual void InsertSubtree(int index, ISymbolicExpressionTreeNode tree) {
149      subtrees.Insert(index, tree);
[3369]150      tree.Parent = this;
[3988]151      ResetCachedValues();
[3294]152    }
[5733]153    public virtual void RemoveSubtree(int index) {
154      subtrees[index].Parent = null;
155      subtrees.RemoveAt(index);
[3988]156      ResetCachedValues();
[3294]157    }
158
[5510]159    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
160      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
[3997]161      ForEachNodePrefix((n) => list.Add(n));
162      return list;
[3294]163    }
164
[5510]165    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
[3988]166      a(this);
[5733]167      if (Subtrees != null) {
168        foreach (var subtree in Subtrees) {
[5510]169          subtree.ForEachNodePrefix(a);
[4106]170        }
[3988]171      }
172    }
173
[5510]174    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
175      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
[3997]176      ForEachNodePostfix((n) => list.Add(n));
177      return list;
[3294]178    }
[3988]179
[5510]180    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
[5733]181      if (Subtrees != null) {
182        foreach (var subtree in Subtrees) {
[5510]183          subtree.ForEachNodePostfix(a);
[4106]184        }
[3988]185      }
186      a(this);
187    }
188
[3442]189    public override string ToString() {
[6375]190      if (Symbol != null) return Symbol.Name;
191      return "SymbolicExpressionTreeNode";
[3442]192    }
[3988]193
194    private void ResetCachedValues() {
[5549]195      length = 0; depth = 0;
[5510]196      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
197      if (parentNode != null) parentNode.ResetCachedValues();
[3988]198    }
[3223]199  }
200}
Note: See TracBrowser for help on using the repository browser.