1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
30 | [StorableClass]
|
---|
31 | public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode {
|
---|
32 | [Storable]
|
---|
33 | private IList<ISymbolicExpressionTreeNode> subTrees;
|
---|
34 | [Storable]
|
---|
35 | private ISymbol symbol;
|
---|
36 |
|
---|
37 | // cached values to prevent unnecessary tree iterations
|
---|
38 | private ushort length;
|
---|
39 | private ushort depth;
|
---|
40 |
|
---|
41 | public ISymbol 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 ISymbolicExpressionTreeNode parent;
|
---|
48 | public ISymbolicExpressionTreeNode Parent {
|
---|
49 | get { return parent; }
|
---|
50 | set { parent = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected SymbolicExpressionTreeNode(bool deserializing) { }
|
---|
55 | protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
|
---|
56 | : base() {
|
---|
57 | symbol = original.symbol; // symbols are reused
|
---|
58 | subTrees = new List<ISymbolicExpressionTreeNode>(original.subTrees.Count);
|
---|
59 | foreach (var subtree in original.subTrees) {
|
---|
60 | var clonedSubTree = cloner.Clone(subtree);
|
---|
61 | subTrees.Add(clonedSubTree);
|
---|
62 | clonedSubTree.Parent = this;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
66 | return new SymbolicExpressionTreeNode(this, cloner);
|
---|
67 | }
|
---|
68 |
|
---|
69 | internal SymbolicExpressionTreeNode()
|
---|
70 | : base() {
|
---|
71 | // don't allocate subtrees list here!
|
---|
72 | // because we don't want to allocate it in terminal nodes
|
---|
73 | }
|
---|
74 |
|
---|
75 | public SymbolicExpressionTreeNode(ISymbol symbol)
|
---|
76 | : base() {
|
---|
77 | subTrees = new List<ISymbolicExpressionTreeNode>(3);
|
---|
78 | this.symbol = symbol;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
83 | private void AfterDeserialization() {
|
---|
84 | foreach (var subtree in subTrees) {
|
---|
85 | subtree.Parent = this;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public virtual bool HasLocalParameters {
|
---|
90 | get { return false; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public virtual IEnumerable<ISymbolicExpressionTreeNode> SubTrees {
|
---|
94 | get { return subTrees; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public virtual ISymbolicExpressionTreeGrammar Grammar {
|
---|
98 | get { return parent.Grammar; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public int GetLength() {
|
---|
102 | if (length > 0) return length;
|
---|
103 | else {
|
---|
104 | length = 1;
|
---|
105 | if (subTrees != null) {
|
---|
106 | for (int i = 0; i < subTrees.Count; i++) {
|
---|
107 | checked { length += (ushort)subTrees[i].GetLength(); }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return length;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public int GetDepth() {
|
---|
115 | if (depth > 0) return depth;
|
---|
116 | else {
|
---|
117 | if (subTrees != null) {
|
---|
118 | for (int i = 0; i < subTrees.Count; i++) depth = Math.Max(depth, (ushort)subTrees[i].GetDepth());
|
---|
119 | }
|
---|
120 | depth++;
|
---|
121 | return depth;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public virtual void ResetLocalParameters(IRandom random) { }
|
---|
126 | public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
|
---|
127 |
|
---|
128 | public virtual ISymbolicExpressionTreeNode GetSubTree(int index) {
|
---|
129 | return subTrees[index];
|
---|
130 | }
|
---|
131 | public virtual int IndexOfSubTree(ISymbolicExpressionTreeNode tree) {
|
---|
132 | return subTrees.IndexOf(tree);
|
---|
133 | }
|
---|
134 | public virtual void AddSubTree(ISymbolicExpressionTreeNode tree) {
|
---|
135 | subTrees.Add(tree);
|
---|
136 | tree.Parent = this;
|
---|
137 | ResetCachedValues();
|
---|
138 | }
|
---|
139 | public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) {
|
---|
140 | subTrees.Insert(index, tree);
|
---|
141 | tree.Parent = this;
|
---|
142 | ResetCachedValues();
|
---|
143 | }
|
---|
144 | public virtual void RemoveSubTree(int index) {
|
---|
145 | subTrees[index].Parent = null;
|
---|
146 | subTrees.RemoveAt(index);
|
---|
147 | ResetCachedValues();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
|
---|
151 | List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
|
---|
152 | ForEachNodePrefix((n) => list.Add(n));
|
---|
153 | return list;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
|
---|
157 | a(this);
|
---|
158 | if (SubTrees != null) {
|
---|
159 | foreach (var subtree in SubTrees) {
|
---|
160 | subtree.ForEachNodePrefix(a);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
|
---|
166 | List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
|
---|
167 | ForEachNodePostfix((n) => list.Add(n));
|
---|
168 | return list;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
|
---|
172 | if (SubTrees != null) {
|
---|
173 | foreach (var subtree in SubTrees) {
|
---|
174 | subtree.ForEachNodePostfix(a);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | a(this);
|
---|
178 | }
|
---|
179 |
|
---|
180 | public override string ToString() {
|
---|
181 | return Symbol.Name;
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void ResetCachedValues() {
|
---|
185 | length = 0; depth = 0;
|
---|
186 | SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
|
---|
187 | if (parentNode != null) parentNode.ResetCachedValues();
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|