Free cookie consent management tool by TermsFeed Policy Generator

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

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

Changed way the grammar is stored in tree nodes to make it more efficient and fixed bugs in symbolic expression tree operators. #290 (Implement ADFs)

File size: 4.5 KB
Line 
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;
23using System.Linq;
24using System.Collections.Generic;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Data;
30using System.Diagnostics;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
33  [StorableClass]
34  public class SymbolicExpressionTreeNode : ICloneable {
35    [Storable]
36    private List<SymbolicExpressionTreeNode> subTrees;
37    [Storable]
38    private Symbol symbol;
39    //[Storable]
40    private SymbolicExpressionTreeNode parent;
41
42    public SymbolicExpressionTreeNode() {
43      subTrees = new List<SymbolicExpressionTreeNode>();
44    }
45
46    public SymbolicExpressionTreeNode(Symbol symbol)
47      : this() {
48      this.symbol = symbol;
49    }
50
51    // copy constructor
52    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
53      symbol = original.symbol;
54      subTrees = new List<SymbolicExpressionTreeNode>();
55      foreach (var subtree in original.SubTrees) {
56        AddSubTree((SymbolicExpressionTreeNode)subtree.Clone());
57      }
58    }
59
60    public virtual bool HasLocalParameters {
61      get { return false; }
62    }
63
64    public virtual IList<SymbolicExpressionTreeNode> SubTrees {
65      get { return subTrees; }
66    }
67
68    public Symbol Symbol {
69      get { return symbol; }
70      protected set { symbol = value; }
71    }
72
73    internal SymbolicExpressionTreeNode Parent {
74      get { return parent; }
75      set { parent = value; }
76    }
77
78    internal virtual ISymbolicExpressionGrammar Grammar {
79      get { return parent.Grammar; }
80      set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); }
81    }
82
83    public int GetSize() {
84      int size = 1;
85      foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize();
86      return size;
87    }
88
89    public int GetHeight() {
90      int maxHeight = 0;
91      foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
92      return maxHeight + 1;
93    }
94
95    public virtual void ResetLocalParameters(IRandom random) { }
96    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
97
98    public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
99      subTrees.Add(tree);
100      tree.Parent = this;
101    }
102
103    public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
104      subTrees.Insert(index, tree);
105      tree.Parent = this;
106    }
107
108    public virtual void RemoveSubTree(int index) {
109      subTrees[index].Parent = null;
110      subTrees.RemoveAt(index);
111    }
112
113    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
114      yield return this;
115      foreach (var subtree in subTrees) {
116        foreach (var n in subtree.IterateNodesPrefix())
117          yield return n;
118      }
119    }
120
121    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
122      foreach (var subtree in subTrees) {
123        foreach (var n in subtree.IterateNodesPrefix())
124          yield return n;
125      }
126      yield return this;
127    }
128    public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
129      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
130    }
131    public int GetMinSubtreeCount() {
132      return Grammar.GetMinSubtreeCount(Symbol);
133    }
134    public int GetMaxSubtreeCount() {
135      return Grammar.GetMaxSubtreeCount(Symbol);
136    }
137
138    #region ICloneable Members
139
140    public virtual object Clone() {
141      return new SymbolicExpressionTreeNode(this);
142    }
143
144    #endregion
145
146
147
148  }
149}
Note: See TracBrowser for help on using the repository browser.