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 |
|
---|
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 | 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 | yield return this;
|
---|
124 | if (SubTrees != null) {
|
---|
125 | foreach (var subtree in SubTrees) {
|
---|
126 | foreach (var n in subtree.IterateNodesPrefix())
|
---|
127 | yield return n;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
|
---|
133 | if (SubTrees != null) {
|
---|
134 | foreach (var subtree in SubTrees) {
|
---|
135 | foreach (var n in subtree.IterateNodesPrefix())
|
---|
136 | yield return n;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | yield return this;
|
---|
140 | }
|
---|
141 | public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
|
---|
142 | return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
|
---|
143 | }
|
---|
144 | public int GetMinSubtreeCount() {
|
---|
145 | return Grammar.GetMinSubtreeCount(Symbol);
|
---|
146 | }
|
---|
147 | public int GetMaxSubtreeCount() {
|
---|
148 | return Grammar.GetMaxSubtreeCount(Symbol);
|
---|
149 | }
|
---|
150 |
|
---|
151 | #region ICloneable Members
|
---|
152 |
|
---|
153 | public virtual object Clone() {
|
---|
154 | return new SymbolicExpressionTreeNode(this);
|
---|
155 | }
|
---|
156 |
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | public override string ToString() {
|
---|
160 | return Symbol.Name;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|