Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3237 was 3237, checked in by gkronber, 14 years ago

Worked on symbolic expression tree encoding.
Added view for expression trees (simple textual view in s-exp format).
Implemented SubtreeCrossover.
Fixed bugs in ProbabilisticTreeCreator.
#937 (Data types and operators for symbolic expression tree encoding)

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