Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs @ 3252

Last change on this file since 3252 was 3252, checked in by gkronber, 13 years ago

Fixed issues with persistence. #937 (Data types and operators for symbolic expression tree encoding)

File size: 3.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using System.Xml;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
31  [StorableClass]
32  public class SymbolicExpressionTreeNode : ICloneable {
33    [Storable]
34    private List<SymbolicExpressionTreeNode> subTrees;
35    [Storable]
36    private Symbol symbol;
37
38    public SymbolicExpressionTreeNode() { }
39
40    public SymbolicExpressionTreeNode(Symbol symbol) {
41      subTrees = new List<SymbolicExpressionTreeNode>();
42      this.symbol = symbol;
43    }
44
45    // copy constructor
46    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
47      symbol = original.symbol;
48      this.subTrees = new List<SymbolicExpressionTreeNode>();
49      foreach (var subtree in original.SubTrees) {
50        AddSubTree((SymbolicExpressionTreeNode)subtree.Clone());
51      }
52    }
53
54    public virtual bool HasLocalParameters {
55      get { return false; }
56    }
57
58    public virtual IList<SymbolicExpressionTreeNode> SubTrees {
59      get { return subTrees; }
60    }
61
62    public Symbol Symbol {
63      get { return symbol; }
64      protected set { symbol = value; }
65    }
66
67    public int GetSize() {
68      int size = 1;
69      foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize();
70      return size;
71    }
72
73    public int GetHeight() {
74      int maxHeight = 0;
75      foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
76      return maxHeight + 1;
77    }
78
79    //public virtual IOperation CreateShakingOperation(IScope scope) {
80    //  return null;
81    //}
82
83    //public virtual IOperation CreateInitOperation(IScope scope) {
84    //  return null;
85    //}
86
87    protected internal virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
88      SubTrees.Add(tree);
89    }
90
91    protected internal virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
92      SubTrees.Insert(index, tree);
93    }
94
95    protected internal virtual void RemoveSubTree(int index) {
96      SubTrees.RemoveAt(index);
97    }
98
99    #region ICloneable Members
100
101    public virtual object Clone() {
102      return new SymbolicExpressionTreeNode(this);
103    }
104
105    #endregion
106  }
107}
Note: See TracBrowser for help on using the repository browser.