[3223] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17434] | 3 | * Copyright (C) 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;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[3223] | 25 | using HeuristicLab.Core;
|
---|
[17434] | 26 | using HEAL.Attic;
|
---|
[3223] | 27 |
|
---|
| 28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[17434] | 29 | [StorableType("E3549A82-D4C5-4AEC-A44C-D3205A618A21")]
|
---|
[5499] | 30 | public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode {
|
---|
[3252] | 31 | [Storable]
|
---|
[5733] | 32 | private IList<ISymbolicExpressionTreeNode> subtrees;
|
---|
[3252] | 33 | [Storable]
|
---|
[5510] | 34 | private ISymbol symbol;
|
---|
[3988] | 35 |
|
---|
| 36 | // cached values to prevent unnecessary tree iterations
|
---|
[5549] | 37 | private ushort length;
|
---|
| 38 | private ushort depth;
|
---|
[3988] | 39 |
|
---|
[5510] | 40 | public ISymbol Symbol {
|
---|
[3484] | 41 | get { return symbol; }
|
---|
| 42 | protected set { symbol = value; }
|
---|
| 43 | }
|
---|
[3462] | 44 |
|
---|
[14312] | 45 | [Storable]
|
---|
[5510] | 46 | private ISymbolicExpressionTreeNode parent;
|
---|
| 47 | public ISymbolicExpressionTreeNode Parent {
|
---|
[3484] | 48 | get { return parent; }
|
---|
| 49 | set { parent = value; }
|
---|
| 50 | }
|
---|
[3223] | 51 |
|
---|
[12231] | 52 | [Storable]
|
---|
| 53 | public double NodeWeight { get; set; }
|
---|
| 54 |
|
---|
[4722] | 55 | [StorableConstructor]
|
---|
[17434] | 56 | protected SymbolicExpressionTreeNode(StorableConstructorFlag _) { }
|
---|
[4722] | 57 | protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
|
---|
[7836] | 58 | : base(original, cloner) {
|
---|
[4722] | 59 | symbol = original.symbol; // symbols are reused
|
---|
[7654] | 60 | length = original.length;
|
---|
| 61 | depth = original.depth;
|
---|
[12231] | 62 | NodeWeight = original.NodeWeight;
|
---|
[6684] | 63 | if (original.subtrees != null) {
|
---|
| 64 | subtrees = new List<ISymbolicExpressionTreeNode>(original.subtrees.Count);
|
---|
| 65 | foreach (var subtree in original.subtrees) {
|
---|
| 66 | var clonedSubtree = cloner.Clone(subtree);
|
---|
| 67 | subtrees.Add(clonedSubtree);
|
---|
| 68 | clonedSubtree.Parent = this;
|
---|
| 69 | }
|
---|
[4722] | 70 | }
|
---|
| 71 | }
|
---|
| 72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 73 | return new SymbolicExpressionTreeNode(this, cloner);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | internal SymbolicExpressionTreeNode()
|
---|
| 77 | : base() {
|
---|
[3486] | 78 | // don't allocate subtrees list here!
|
---|
| 79 | // because we don't want to allocate it in terminal nodes
|
---|
| 80 | }
|
---|
[3484] | 81 |
|
---|
[5510] | 82 | public SymbolicExpressionTreeNode(ISymbol symbol)
|
---|
[4722] | 83 | : base() {
|
---|
[5733] | 84 | subtrees = new List<ISymbolicExpressionTreeNode>(3);
|
---|
[3223] | 85 | this.symbol = symbol;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
[3486] | 89 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 90 | private void AfterDeserialization() {
|
---|
[5733] | 91 | if (subtrees != null) {
|
---|
| 92 | foreach (var subtree in subtrees)
|
---|
[14312] | 93 | if (subtree.Parent == null)
|
---|
| 94 | subtree.Parent = this;
|
---|
[3486] | 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[3244] | 98 | public virtual bool HasLocalParameters {
|
---|
[3223] | 99 | get { return false; }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[5733] | 102 | public virtual IEnumerable<ISymbolicExpressionTreeNode> Subtrees {
|
---|
| 103 | get { return subtrees; }
|
---|
[3223] | 104 | }
|
---|
| 105 |
|
---|
[5499] | 106 | public virtual ISymbolicExpressionTreeGrammar Grammar {
|
---|
[3369] | 107 | get { return parent.Grammar; }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[5549] | 110 | public int GetLength() {
|
---|
| 111 | if (length > 0) return length;
|
---|
[3988] | 112 | else {
|
---|
[13875] | 113 | ushort l = 1;
|
---|
[5733] | 114 | if (subtrees != null) {
|
---|
| 115 | for (int i = 0; i < subtrees.Count; i++) {
|
---|
[13875] | 116 | checked { l += (ushort)subtrees[i].GetLength(); }
|
---|
[4524] | 117 | }
|
---|
[4106] | 118 | }
|
---|
[13875] | 119 | length = l;
|
---|
[5549] | 120 | return length;
|
---|
[3988] | 121 | }
|
---|
[3223] | 122 | }
|
---|
| 123 |
|
---|
[5549] | 124 | public int GetDepth() {
|
---|
| 125 | if (depth > 0) return depth;
|
---|
[3988] | 126 | else {
|
---|
[13875] | 127 | ushort d = 0;
|
---|
[5733] | 128 | if (subtrees != null) {
|
---|
[13875] | 129 | for (int i = 0; i < subtrees.Count; i++) d = Math.Max(d, (ushort)subtrees[i].GetDepth());
|
---|
[4106] | 130 | }
|
---|
[13875] | 131 | d++;
|
---|
| 132 | depth = d;
|
---|
[5549] | 133 | return depth;
|
---|
[3988] | 134 | }
|
---|
[3223] | 135 | }
|
---|
[3294] | 136 |
|
---|
[7506] | 137 | public int GetBranchLevel(ISymbolicExpressionTreeNode child) {
|
---|
| 138 | return GetBranchLevel(this, child);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
|
---|
| 142 | if (root == point)
|
---|
| 143 | return 0;
|
---|
| 144 | foreach (var subtree in root.Subtrees) {
|
---|
| 145 | int branchLevel = GetBranchLevel(subtree, point);
|
---|
| 146 | if (branchLevel < int.MaxValue)
|
---|
| 147 | return 1 + branchLevel;
|
---|
| 148 | }
|
---|
| 149 | return int.MaxValue;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[3338] | 152 | public virtual void ResetLocalParameters(IRandom random) { }
|
---|
| 153 | public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
|
---|
| 154 |
|
---|
[6803] | 155 | public int SubtreeCount {
|
---|
[5686] | 156 | get {
|
---|
[5733] | 157 | if (subtrees == null) return 0;
|
---|
| 158 | return subtrees.Count;
|
---|
[5686] | 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[5733] | 162 | public virtual ISymbolicExpressionTreeNode GetSubtree(int index) {
|
---|
| 163 | return subtrees[index];
|
---|
[5510] | 164 | }
|
---|
[5733] | 165 | public virtual int IndexOfSubtree(ISymbolicExpressionTreeNode tree) {
|
---|
| 166 | return subtrees.IndexOf(tree);
|
---|
[5510] | 167 | }
|
---|
[5733] | 168 | public virtual void AddSubtree(ISymbolicExpressionTreeNode tree) {
|
---|
| 169 | subtrees.Add(tree);
|
---|
[3369] | 170 | tree.Parent = this;
|
---|
[3988] | 171 | ResetCachedValues();
|
---|
[3294] | 172 | }
|
---|
[5733] | 173 | public virtual void InsertSubtree(int index, ISymbolicExpressionTreeNode tree) {
|
---|
| 174 | subtrees.Insert(index, tree);
|
---|
[3369] | 175 | tree.Parent = this;
|
---|
[3988] | 176 | ResetCachedValues();
|
---|
[3294] | 177 | }
|
---|
[5733] | 178 | public virtual void RemoveSubtree(int index) {
|
---|
| 179 | subtrees[index].Parent = null;
|
---|
| 180 | subtrees.RemoveAt(index);
|
---|
[3988] | 181 | ResetCachedValues();
|
---|
[3294] | 182 | }
|
---|
| 183 |
|
---|
[7795] | 184 | public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesBreadth() {
|
---|
[12891] | 185 | var list = new List<ISymbolicExpressionTreeNode>(GetLength()) { this };
|
---|
[7795] | 186 | int i = 0;
|
---|
| 187 | while (i != list.Count) {
|
---|
| 188 | for (int j = 0; j != list[i].SubtreeCount; ++j)
|
---|
| 189 | list.Add(list[i].GetSubtree(j));
|
---|
| 190 | ++i;
|
---|
| 191 | }
|
---|
| 192 | return list;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[5510] | 195 | public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
|
---|
| 196 | List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
|
---|
[3997] | 197 | ForEachNodePrefix((n) => list.Add(n));
|
---|
| 198 | return list;
|
---|
[3294] | 199 | }
|
---|
| 200 |
|
---|
[5510] | 201 | public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
|
---|
[3988] | 202 | a(this);
|
---|
[12891] | 203 | if (subtrees != null) {
|
---|
| 204 | //avoid linq to reduce memory pressure
|
---|
| 205 | for (int i = 0; i < subtrees.Count; i++) {
|
---|
| 206 | subtrees[i].ForEachNodePrefix(a);
|
---|
[4106] | 207 | }
|
---|
[3988] | 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[5510] | 211 | public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
|
---|
| 212 | List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
|
---|
[3997] | 213 | ForEachNodePostfix((n) => list.Add(n));
|
---|
| 214 | return list;
|
---|
[3294] | 215 | }
|
---|
[3988] | 216 |
|
---|
[5510] | 217 | public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
|
---|
[12891] | 218 | if (subtrees != null) {
|
---|
| 219 | //avoid linq to reduce memory pressure
|
---|
| 220 | for (int i = 0; i < subtrees.Count; i++) {
|
---|
| 221 | subtrees[i].ForEachNodePostfix(a);
|
---|
[4106] | 222 | }
|
---|
[3988] | 223 | }
|
---|
| 224 | a(this);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[3442] | 227 | public override string ToString() {
|
---|
[6375] | 228 | if (Symbol != null) return Symbol.Name;
|
---|
| 229 | return "SymbolicExpressionTreeNode";
|
---|
[3442] | 230 | }
|
---|
[3988] | 231 |
|
---|
| 232 | private void ResetCachedValues() {
|
---|
[5549] | 233 | length = 0; depth = 0;
|
---|
[5510] | 234 | SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
|
---|
| 235 | if (parentNode != null) parentNode.ResetCachedValues();
|
---|
[3988] | 236 | }
|
---|
[3223] | 237 | }
|
---|
| 238 | }
|
---|