#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { [StorableClass] public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode { [Storable] private IList subTrees; [Storable] private ISymbol symbol; // cached values to prevent unnecessary tree iterations private ushort length; private ushort depth; public ISymbol Symbol { get { return symbol; } protected set { symbol = value; } } // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree) private ISymbolicExpressionTreeNode parent; public ISymbolicExpressionTreeNode Parent { get { return parent; } set { parent = value; } } [StorableConstructor] protected SymbolicExpressionTreeNode(bool deserializing) { } protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner) : base() { symbol = original.symbol; // symbols are reused subTrees = new List(original.subTrees.Count); foreach (var subtree in original.subTrees) { var clonedSubTree = cloner.Clone(subtree); subTrees.Add(clonedSubTree); clonedSubTree.Parent = this; } } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreeNode(this, cloner); } internal SymbolicExpressionTreeNode() : base() { // don't allocate subtrees list here! // because we don't want to allocate it in terminal nodes } public SymbolicExpressionTreeNode(ISymbol symbol) : base() { subTrees = new List(3); this.symbol = symbol; } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { foreach (var subtree in subTrees) { subtree.Parent = this; } } public virtual bool HasLocalParameters { get { return false; } } public virtual IEnumerable SubTrees { get { return subTrees; } } public virtual ISymbolicExpressionTreeGrammar Grammar { get { return parent.Grammar; } } public int GetLength() { if (length > 0) return length; else { length = 1; if (subTrees != null) { for (int i = 0; i < subTrees.Count; i++) { checked { length += (ushort)subTrees[i].GetLength(); } } } return length; } } public int GetDepth() { if (depth > 0) return depth; else { if (subTrees != null) { for (int i = 0; i < subTrees.Count; i++) depth = Math.Max(depth, (ushort)subTrees[i].GetDepth()); } depth++; return depth; } } public virtual void ResetLocalParameters(IRandom random) { } public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { } public virtual ISymbolicExpressionTreeNode GetSubTree(int index) { return subTrees[index]; } public virtual int IndexOfSubTree(ISymbolicExpressionTreeNode tree) { return subTrees.IndexOf(tree); } public virtual void AddSubTree(ISymbolicExpressionTreeNode tree) { subTrees.Add(tree); tree.Parent = this; ResetCachedValues(); } public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) { subTrees.Insert(index, tree); tree.Parent = this; ResetCachedValues(); } public virtual void RemoveSubTree(int index) { subTrees[index].Parent = null; subTrees.RemoveAt(index); ResetCachedValues(); } public IEnumerable IterateNodesPrefix() { List list = new List(); ForEachNodePrefix((n) => list.Add(n)); return list; } public void ForEachNodePrefix(Action a) { a(this); if (SubTrees != null) { foreach (var subtree in SubTrees) { subtree.ForEachNodePrefix(a); } } } public IEnumerable IterateNodesPostfix() { List list = new List(); ForEachNodePostfix((n) => list.Add(n)); return list; } public void ForEachNodePostfix(Action a) { if (SubTrees != null) { foreach (var subtree in SubTrees) { subtree.ForEachNodePostfix(a); } } a(this); } public override string ToString() { return Symbol.Name; } private void ResetCachedValues() { length = 0; depth = 0; SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode; if (parentNode != null) parentNode.ResetCachedValues(); } } }