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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using System.Xml;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using System.Diagnostics;
|
---|
32 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
35 | [StorableClass]
|
---|
36 | public class SymbolicExpressionTreeNode : ICloneable {
|
---|
37 | [Storable]
|
---|
38 | private IList<SymbolicExpressionTreeNode> subTrees;
|
---|
39 | [Storable]
|
---|
40 | private Symbol symbol;
|
---|
41 |
|
---|
42 | // cached values to prevent unnecessary tree iterations
|
---|
43 | private short size;
|
---|
44 | private short height;
|
---|
45 |
|
---|
46 | public Symbol Symbol {
|
---|
47 | get { return symbol; }
|
---|
48 | protected set { symbol = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree)
|
---|
52 | private SymbolicExpressionTreeNode parent;
|
---|
53 | internal SymbolicExpressionTreeNode Parent {
|
---|
54 | get { return parent; }
|
---|
55 | set { parent = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public SymbolicExpressionTreeNode() {
|
---|
59 | // don't allocate subtrees list here!
|
---|
60 | // because we don't want to allocate it in terminal nodes
|
---|
61 | }
|
---|
62 |
|
---|
63 | public SymbolicExpressionTreeNode(Symbol symbol) {
|
---|
64 | subTrees = new List<SymbolicExpressionTreeNode>(3);
|
---|
65 | this.symbol = symbol;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // copy constructor
|
---|
69 | protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
|
---|
70 | symbol = original.symbol;
|
---|
71 | subTrees = new List<SymbolicExpressionTreeNode>(original.SubTrees.Count);
|
---|
72 | foreach (var subtree in original.SubTrees) {
|
---|
73 | AddSubTree((SymbolicExpressionTreeNode)subtree.Clone());
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [StorableHook(HookType.AfterDeserialization)]
|
---|
78 | private void AfterDeserializationHook() {
|
---|
79 | foreach (var subtree in SubTrees) {
|
---|
80 | subtree.Parent = this;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public virtual bool HasLocalParameters {
|
---|
85 | get { return false; }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public virtual IList<SymbolicExpressionTreeNode> SubTrees {
|
---|
89 | get { return subTrees; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | internal virtual ISymbolicExpressionGrammar Grammar {
|
---|
93 | get { return parent.Grammar; }
|
---|
94 | set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public int GetSize() {
|
---|
98 | if (size > 0) return size;
|
---|
99 | else {
|
---|
100 | size = 1;
|
---|
101 | for (int i = 0; i < SubTrees.Count; i++) size += (short)SubTrees[i].GetSize();
|
---|
102 | return size;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public int GetHeight() {
|
---|
107 | if (height > 0) return height;
|
---|
108 | else {
|
---|
109 | for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (short)SubTrees[i].GetHeight());
|
---|
110 | height++;
|
---|
111 | return height;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public virtual void ResetLocalParameters(IRandom random) { }
|
---|
116 | public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
|
---|
117 |
|
---|
118 | public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
|
---|
119 | subTrees.Add(tree);
|
---|
120 | tree.Parent = this;
|
---|
121 | ResetCachedValues();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
|
---|
125 | subTrees.Insert(index, tree);
|
---|
126 | tree.Parent = this;
|
---|
127 | ResetCachedValues();
|
---|
128 | }
|
---|
129 |
|
---|
130 | public virtual void RemoveSubTree(int index) {
|
---|
131 | subTrees[index].Parent = null;
|
---|
132 | subTrees.RemoveAt(index);
|
---|
133 | ResetCachedValues();
|
---|
134 | }
|
---|
135 |
|
---|
136 | public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
|
---|
137 | List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
|
---|
138 | ForEachNodePrefix((n) => list.Add(n));
|
---|
139 | return list;
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a) {
|
---|
143 | a(this);
|
---|
144 | for (int i = 0; i < SubTrees.Count; i++) {
|
---|
145 | SubTrees[i].ForEachNodePrefix(a);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
|
---|
150 | List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
|
---|
151 | ForEachNodePostfix((n) => list.Add(n));
|
---|
152 | return list;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a) {
|
---|
156 | for (int i = 0; i < SubTrees.Count; i++) {
|
---|
157 | SubTrees[i].ForEachNodePrefix(a);
|
---|
158 | }
|
---|
159 | a(this);
|
---|
160 | }
|
---|
161 |
|
---|
162 | public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
|
---|
163 | return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
|
---|
164 | }
|
---|
165 | public int GetMinSubtreeCount() {
|
---|
166 | return Grammar.GetMinSubtreeCount(Symbol);
|
---|
167 | }
|
---|
168 | public int GetMaxSubtreeCount() {
|
---|
169 | return Grammar.GetMaxSubtreeCount(Symbol);
|
---|
170 | }
|
---|
171 |
|
---|
172 | #region ICloneable Members
|
---|
173 |
|
---|
174 | public virtual object Clone() {
|
---|
175 | return new SymbolicExpressionTreeNode(this);
|
---|
176 | }
|
---|
177 |
|
---|
178 | #endregion
|
---|
179 |
|
---|
180 | public override string ToString() {
|
---|
181 | return Symbol.Name;
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void ResetCachedValues() {
|
---|
185 | size = 0; height = 0;
|
---|
186 | if (parent != null) parent.ResetCachedValues();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|