Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3541 was 3541, checked in by gkronber, 14 years ago

Fixed bugs in persistence of grammars.#937 (Data types and operators for symbolic expression tree encoding)

File size: 5.1 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
[3294]23using System.Linq;
[3223]24using System.Collections.Generic;
25using System.Text;
[3376]26using HeuristicLab.Common;
[3223]27using HeuristicLab.Core;
28using System.Xml;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3244]30using HeuristicLab.Data;
[3294]31using System.Diagnostics;
[3462]32using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
[3223]33
34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
35  [StorableClass]
[3252]36  public class SymbolicExpressionTreeNode : ICloneable {
37    [Storable]
[3541]38    private IList<SymbolicExpressionTreeNode> subTrees;
[3252]39    [Storable]
[3223]40    private Symbol symbol;
[3484]41    public Symbol Symbol {
42      get { return symbol; }
43      protected set { symbol = value; }
44    }
[3462]45
46    // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree)
[3369]47    private SymbolicExpressionTreeNode parent;
[3484]48    internal SymbolicExpressionTreeNode Parent {
49      get { return parent; }
50      set { parent = value; }
51    }
[3223]52
[3486]53    public SymbolicExpressionTreeNode() {
54      // don't allocate subtrees list here!
55      // because we don't want to allocate it in terminal nodes
56    }
[3484]57
58    public SymbolicExpressionTreeNode(Symbol symbol) {
[3338]59      subTrees = new List<SymbolicExpressionTreeNode>();
[3223]60      this.symbol = symbol;
61    }
62
[3252]63    // copy constructor
64    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
65      symbol = original.symbol;
[3294]66      subTrees = new List<SymbolicExpressionTreeNode>();
[3252]67      foreach (var subtree in original.SubTrees) {
[3369]68        AddSubTree((SymbolicExpressionTreeNode)subtree.Clone());
[3252]69      }
70    }
[3223]71
[3486]72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserializationHook() {
74      foreach (var subtree in SubTrees) {
75        subtree.Parent = this;
76      }
77    }
78
[3244]79    public virtual bool HasLocalParameters {
[3223]80      get { return false; }
81    }
82
83    public virtual IList<SymbolicExpressionTreeNode> SubTrees {
84      get { return subTrees; }
85    }
86
[3369]87    internal virtual ISymbolicExpressionGrammar Grammar {
88      get { return parent.Grammar; }
89      set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); }
90    }
91
[3244]92    public int GetSize() {
[3223]93      int size = 1;
94      foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize();
95      return size;
96    }
97
[3244]98    public int GetHeight() {
[3223]99      int maxHeight = 0;
100      foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
101      return maxHeight + 1;
102    }
[3294]103
[3338]104    public virtual void ResetLocalParameters(IRandom random) { }
105    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
106
107    public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
[3369]108      subTrees.Add(tree);
109      tree.Parent = this;
[3294]110    }
111
[3338]112    public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
[3369]113      subTrees.Insert(index, tree);
114      tree.Parent = this;
[3294]115    }
116
[3338]117    public virtual void RemoveSubTree(int index) {
[3369]118      subTrees[index].Parent = null;
119      subTrees.RemoveAt(index);
[3294]120    }
121
[3338]122    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
123      yield return this;
[3486]124      if (SubTrees != null) {
125        foreach (var subtree in SubTrees) {
126          foreach (var n in subtree.IterateNodesPrefix())
127            yield return n;
128        }
[3338]129      }
[3294]130    }
131
[3338]132    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
[3486]133      if (SubTrees != null) {
134        foreach (var subtree in SubTrees) {
135          foreach (var n in subtree.IterateNodesPrefix())
136            yield return n;
137        }
[3338]138      }
139      yield return this;
[3294]140    }
[3338]141    public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
142      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
[3294]143    }
[3338]144    public int GetMinSubtreeCount() {
145      return Grammar.GetMinSubtreeCount(Symbol);
[3223]146    }
[3338]147    public int GetMaxSubtreeCount() {
148      return Grammar.GetMaxSubtreeCount(Symbol);
[3223]149    }
150
[3252]151    #region ICloneable Members
152
153    public virtual object Clone() {
154      return new SymbolicExpressionTreeNode(this);
[3237]155    }
[3252]156
157    #endregion
[3338]158
[3442]159    public override string ToString() {
160      return Symbol.Name;
161    }
[3223]162  }
163}
Note: See TracBrowser for help on using the repository browser.