Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented calculation of variable impacts as integral over relative variable frequencies. #1036

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