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