Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/17/11 13:51:04 (14 years ago)
Author:
gkronber
Message:

#1418 Fixed compiler errors in symbolic expression tree encoding

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs

    r5499 r5510  
    3131  public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode {
    3232    [Storable]
    33     private IList<SymbolicExpressionTreeNode> subTrees;
     33    private IList<ISymbolicExpressionTreeNode> subTrees;
    3434    [Storable]
    35     private Symbol symbol;
     35    private ISymbol symbol;
    3636
    3737    // cached values to prevent unnecessary tree iterations
     
    3939    private ushort height;
    4040
    41     public Symbol Symbol {
     41    public ISymbol Symbol {
    4242      get { return symbol; }
    4343      protected set { symbol = value; }
     
    4545
    4646    // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree)
    47     private SymbolicExpressionTreeNode parent;
    48     internal SymbolicExpressionTreeNode Parent {
     47    private ISymbolicExpressionTreeNode parent;
     48    public ISymbolicExpressionTreeNode Parent {
    4949      get { return parent; }
    5050      set { parent = value; }
     
    5656      : base() {
    5757      symbol = original.symbol; // symbols are reused
    58       subTrees = new List<SymbolicExpressionTreeNode>(original.SubTrees.Count);
    59       foreach (var subtree in original.SubTrees) {
     58      subTrees = new List<ISymbolicExpressionTreeNode>(original.subTrees.Count);
     59      foreach (var subtree in original.subTrees) {
    6060        var clonedSubTree = cloner.Clone(subtree);
    6161        subTrees.Add(clonedSubTree);
     
    7373    }
    7474
    75     public SymbolicExpressionTreeNode(Symbol symbol)
     75    public SymbolicExpressionTreeNode(ISymbol symbol)
    7676      : base() {
    77       subTrees = new List<SymbolicExpressionTreeNode>(3);
     77      subTrees = new List<ISymbolicExpressionTreeNode>(3);
    7878      this.symbol = symbol;
    7979    }
     
    8282    [StorableHook(HookType.AfterDeserialization)]
    8383    private void AfterDeserialization() {
    84       foreach (var subtree in SubTrees) {
     84      foreach (var subtree in subTrees) {
    8585        subtree.Parent = this;
    8686      }
     
    9191    }
    9292
    93     public virtual IList<SymbolicExpressionTreeNode> SubTrees {
     93    public virtual IEnumerable<ISymbolicExpressionTreeNode> SubTrees {
    9494      get { return subTrees; }
    9595    }
     
    103103      else {
    104104        size = 1;
    105         if (SubTrees != null) {
    106           for (int i = 0; i < SubTrees.Count; i++) {
    107             checked { size += (ushort)SubTrees[i].GetSize(); }
     105        if (subTrees != null) {
     106          for (int i = 0; i < subTrees.Count; i++) {
     107            checked { size += (ushort)subTrees[i].GetSize(); }
    108108          }
    109109        }
     
    115115      if (height > 0) return height;
    116116      else {
    117         if (SubTrees != null) {
    118           for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (ushort)SubTrees[i].GetHeight());
     117        if (subTrees != null) {
     118          for (int i = 0; i < subTrees.Count; i++) height = Math.Max(height, (ushort)subTrees[i].GetHeight());
    119119        }
    120120        height++;
     
    126126    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
    127127
    128     public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
    129       SubTrees.Add(tree);
     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);
    130136      tree.Parent = this;
    131137      ResetCachedValues();
    132138    }
    133 
    134     public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
    135       SubTrees.Insert(index, tree);
     139    public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) {
     140      subTrees.Insert(index, tree);
    136141      tree.Parent = this;
    137142      ResetCachedValues();
    138143    }
    139 
    140144    public virtual void RemoveSubTree(int index) {
    141       SubTrees[index].Parent = null;
    142       SubTrees.RemoveAt(index);
     145      subTrees[index].Parent = null;
     146      subTrees.RemoveAt(index);
    143147      ResetCachedValues();
    144148    }
    145149
    146     public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
    147       List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
     150    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
     151      List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>();
    148152      ForEachNodePrefix((n) => list.Add(n));
    149153      return list;
    150154    }
    151155
    152     public void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a) {
     156    public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) {
    153157      a(this);
    154158      if (SubTrees != null) {
    155         for (int i = 0; i < SubTrees.Count; i++) {
    156           SubTrees[i].ForEachNodePrefix(a);
    157         }
    158       }
    159     }
    160 
    161     public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
    162       List<SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();
     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>();
    163167      ForEachNodePostfix((n) => list.Add(n));
    164168      return list;
    165169    }
    166170
    167     public void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a) {
     171    public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) {
    168172      if (SubTrees != null) {
    169         for (int i = 0; i < SubTrees.Count; i++) {
    170           SubTrees[i].ForEachNodePostfix(a);
     173        foreach (var subtree in SubTrees) {
     174          subtree.ForEachNodePostfix(a);
    171175        }
    172176      }
     
    190194    private void ResetCachedValues() {
    191195      size = 0; height = 0;
    192       if (parent != null) parent.ResetCachedValues();
     196      SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;
     197      if (parentNode != null) parentNode.ResetCachedValues();
    193198    }
    194199  }
Note: See TracChangeset for help on using the changeset viewer.