Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

File size: 4.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.Linq;
24using System.Collections.Generic;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Data;
30using System.Diagnostics;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
33  [StorableClass]
34  public class SymbolicExpressionTreeNode : ICloneable {
35    [Storable]
36    private List<SymbolicExpressionTreeNode> subTrees;
37    [Storable]
38    private Symbol symbol;
39
40    public SymbolicExpressionTreeNode() { }
41
42    public SymbolicExpressionTreeNode(Symbol symbol) {
43      subTrees = new List<SymbolicExpressionTreeNode>();
44      this.symbol = symbol;
45    }
46
47    // copy constructor
48    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
49      symbol = original.symbol;
50      subTrees = new List<SymbolicExpressionTreeNode>();
51      foreach (var subtree in original.SubTrees) {
52        AddSubTree((SymbolicExpressionTreeNode)subtree.Clone());
53      }
54      dynamicSymbols = new Dictionary<string, int>(original.dynamicSymbols);
55    }
56
57    public virtual bool HasLocalParameters {
58      get { return false; }
59    }
60
61    public virtual IList<SymbolicExpressionTreeNode> SubTrees {
62      get { return subTrees; }
63    }
64
65    public Symbol Symbol {
66      get { return symbol; }
67      protected set { symbol = value; }
68    }
69
70    public int GetSize() {
71      int size = 1;
72      foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize();
73      return size;
74    }
75
76    public int GetHeight() {
77      int maxHeight = 0;
78      foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
79      return maxHeight + 1;
80    }
81
82    [Storable]
83    private Dictionary<string, int> dynamicSymbols = new Dictionary<string, int>();
84    public void AddDynamicSymbol(string symbolName) {
85      Debug.Assert(!dynamicSymbols.ContainsKey(symbolName));
86      dynamicSymbols[symbolName] = 0;
87    }
88
89    public void AddDynamicSymbol(string symbolName, int nArguments) {
90      AddDynamicSymbol(symbolName);
91      SetDynamicSymbolArgumentCount(symbolName, nArguments);
92    }
93
94    public void RemoveDynamicSymbol(string symbolName) {
95      dynamicSymbols.Remove(symbolName);
96    }
97
98    public IEnumerable<string> DynamicSymbols {
99      get { return dynamicSymbols.Keys; }
100    }
101
102    public int GetDynamicSymbolArgumentCount(string symbolName) {
103      return dynamicSymbols[symbolName];
104    }
105    public void SetDynamicSymbolArgumentCount(string symbolName, int nArguments) {
106      Debug.Assert(dynamicSymbols.ContainsKey(symbolName));
107      dynamicSymbols[symbolName] = nArguments;
108    }
109
110    public virtual void ResetLocalParameters(IRandom random) { }
111    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
112
113    protected internal virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
114      SubTrees.Add(tree);
115    }
116
117    protected internal virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
118      SubTrees.Insert(index, tree);
119    }
120
121    protected internal virtual void RemoveSubTree(int index) {
122      SubTrees.RemoveAt(index);
123    }
124
125    #region ICloneable Members
126
127    public virtual object Clone() {
128      return new SymbolicExpressionTreeNode(this);
129    }
130
131    #endregion
132  }
133}
Note: See TracBrowser for help on using the repository browser.