Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 6.1 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;
[3294]24using System.Linq;
[4722]25using HeuristicLab.Common;
[3223]26using HeuristicLab.Core;
[4068]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
[3223]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
31  [StorableClass]
[4722]32  public class SymbolicExpressionTreeNode : DeepCloneable {
[3252]33    [Storable]
[3541]34    private IList<SymbolicExpressionTreeNode> subTrees;
[3252]35    [Storable]
[3223]36    private Symbol symbol;
[3988]37
38    // cached values to prevent unnecessary tree iterations
[5411]39    private ushort size;
40    private ushort height;
[3988]41
[3484]42    public Symbol Symbol {
43      get { return symbol; }
44      protected set { symbol = value; }
45    }
[3462]46
47    // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree)
[3369]48    private SymbolicExpressionTreeNode parent;
[3484]49    internal SymbolicExpressionTreeNode Parent {
50      get { return parent; }
51      set { parent = value; }
52    }
[3223]53
[4722]54    [StorableConstructor]
55    protected SymbolicExpressionTreeNode(bool deserializing) { }
56    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
57      : base() {
58      symbol = original.symbol; // symbols are reused
59      subTrees = new List<SymbolicExpressionTreeNode>(original.SubTrees.Count);
60      foreach (var subtree in original.SubTrees) {
61        var clonedSubTree = cloner.Clone(subtree);
62        subTrees.Add(clonedSubTree);
63        clonedSubTree.Parent = this;
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
[4722]76    public SymbolicExpressionTreeNode(Symbol symbol)
77      : base() {
[3997]78      subTrees = new List<SymbolicExpressionTreeNode>(3);
[3223]79      this.symbol = symbol;
80    }
81
82
[3486]83    [StorableHook(HookType.AfterDeserialization)]
[4722]84    private void AfterDeserialization() {
[3486]85      foreach (var subtree in SubTrees) {
86        subtree.Parent = this;
87      }
88    }
89
[3244]90    public virtual bool HasLocalParameters {
[3223]91      get { return false; }
92    }
93
94    public virtual IList<SymbolicExpressionTreeNode> SubTrees {
95      get { return subTrees; }
96    }
97
[4106]98    public virtual ISymbolicExpressionGrammar Grammar {
[3369]99      get { return parent.Grammar; }
100    }
101
[3244]102    public int GetSize() {
[3988]103      if (size > 0) return size;
104      else {
105        size = 1;
[4106]106        if (SubTrees != null) {
[4524]107          for (int i = 0; i < SubTrees.Count; i++) {
[5411]108            checked { size += (ushort)SubTrees[i].GetSize(); }
[4524]109          }
[4106]110        }
[3988]111        return size;
112      }
[3223]113    }
114
[3244]115    public int GetHeight() {
[3988]116      if (height > 0) return height;
117      else {
[4106]118        if (SubTrees != null) {
[5411]119          for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (ushort)SubTrees[i].GetHeight());
[4106]120        }
[3988]121        height++;
122        return height;
123      }
[3223]124    }
[3294]125
[3338]126    public virtual void ResetLocalParameters(IRandom random) { }
127    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
128
129    public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
[4106]130      SubTrees.Add(tree);
[3369]131      tree.Parent = this;
[3988]132      ResetCachedValues();
[3294]133    }
134
[3338]135    public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
[4106]136      SubTrees.Insert(index, tree);
[3369]137      tree.Parent = this;
[3988]138      ResetCachedValues();
[3294]139    }
140
[3338]141    public virtual void RemoveSubTree(int index) {
[4106]142      SubTrees[index].Parent = null;
143      SubTrees.RemoveAt(index);
[3988]144      ResetCachedValues();
[3294]145    }
146
[3338]147    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
[3997]148      List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
149      ForEachNodePrefix((n) => list.Add(n));
150      return list;
[3294]151    }
152
[3997]153    public void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a) {
[3988]154      a(this);
[4106]155      if (SubTrees != null) {
156        for (int i = 0; i < SubTrees.Count; i++) {
157          SubTrees[i].ForEachNodePrefix(a);
158        }
[3988]159      }
160    }
161
[3338]162    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
[3997]163      List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
164      ForEachNodePostfix((n) => list.Add(n));
165      return list;
[3294]166    }
[3988]167
[3997]168    public void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a) {
[4106]169      if (SubTrees != null) {
170        for (int i = 0; i < SubTrees.Count; i++) {
171          SubTrees[i].ForEachNodePostfix(a);
172        }
[3988]173      }
174      a(this);
175    }
176
[3338]177    public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
178      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
[3294]179    }
[5014]180
[3338]181    public int GetMinSubtreeCount() {
182      return Grammar.GetMinSubtreeCount(Symbol);
[3223]183    }
[3338]184    public int GetMaxSubtreeCount() {
185      return Grammar.GetMaxSubtreeCount(Symbol);
[3223]186    }
[3442]187    public override string ToString() {
188      return Symbol.Name;
189    }
[3988]190
191    private void ResetCachedValues() {
192      size = 0; height = 0;
193      if (parent != null) parent.ResetCachedValues();
194    }
[3223]195  }
196}
Note: See TracBrowser for help on using the repository browser.