Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3338 was 3338, checked in by gkronber, 12 years ago

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File size: 5.8 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      subTrees = new List<SymbolicExpressionTreeNode>();
42    }
43
44    public SymbolicExpressionTreeNode(Symbol symbol)
45      : this() {
46      this.symbol = symbol;
47    }
48
49    // copy constructor
50    protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
51      symbol = original.symbol;
52      subTrees = new List<SymbolicExpressionTreeNode>();
53      grammar = original.grammar;
54      foreach (var subtree in original.SubTrees) {
55        SubTrees.Add((SymbolicExpressionTreeNode)subtree.Clone());
56      }
57    }
58
59    public virtual bool HasLocalParameters {
60      get { return false; }
61    }
62
63    public virtual IList<SymbolicExpressionTreeNode> SubTrees {
64      get { return subTrees; }
65    }
66
67    public Symbol Symbol {
68      get { return symbol; }
69      protected set { symbol = value; }
70    }
71
72    private ISymbolicExpressionGrammar grammar;
73    public virtual ISymbolicExpressionGrammar Grammar {
74      get { return grammar; }
75      set {
76        grammar = value;
77        foreach (var subtree in subTrees)
78          subtree.Grammar = value;
79      }
80    }
81
82    public int GetSize() {
83      int size = 1;
84      foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize();
85      return size;
86    }
87
88    public int GetHeight() {
89      int maxHeight = 0;
90      foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
91      return maxHeight + 1;
92    }
93
94    public virtual void ResetLocalParameters(IRandom random) { }
95    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
96
97    public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
98      SubTrees.Add(tree);
99      //if (tree != null)
100      tree.Grammar = Grammar;
101    }
102
103    public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
104      SubTrees.Insert(index, tree);
105      //if (tree != null)
106      tree.Grammar = Grammar;
107    }
108
109    public virtual void RemoveSubTree(int index) {
110      //if (SubTrees[index] != null)
111      SubTrees[index].Grammar = null;
112      SubTrees.RemoveAt(index);
113    }
114
115    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
116      yield return this;
117      foreach (var subtree in subTrees) {
118        foreach (var n in subtree.IterateNodesPrefix())
119          yield return n;
120      }
121    }
122
123    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
124      foreach (var subtree in subTrees) {
125        foreach (var n in subtree.IterateNodesPrefix())
126          yield return n;
127      }
128      yield return this;
129    }
130    //public int GetMinExpressionLength() {
131    //  return Grammar.GetMinExpressionLength(Symbol);
132    //}
133    //public int GetMaxExpressionLength() {
134    //  return Grammar.GetMaxExpressionLength(Symbol);
135    //}
136    //public int GetMinExpressionDepth() {
137    //  return Grammar.GetMinExpressionDepth(Symbol);
138    //}
139    public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
140      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
141    }
142    public int GetMinSubtreeCount() {
143      return Grammar.GetMinSubtreeCount(Symbol);
144    }
145    public int GetMaxSubtreeCount() {
146      return Grammar.GetMaxSubtreeCount(Symbol);
147    }
148    //public int GetMaxExpressionLength(Symbol s) {
149    //  return Grammar.GetMaxExpressionLength(s);
150    //}
151    //public int GetMinExpressionLength(Symbol s) {
152    //  return Grammar.GetMinExpressionLength(s);
153    //}
154    //public int GetMinExpressionDepth(Symbol s) {
155    //  return Grammar.GetMinExpressionDepth(s);
156    //}
157
158    #region ICloneable Members
159
160    public virtual object Clone() {
161      return new SymbolicExpressionTreeNode(this);
162    }
163
164    #endregion
165
166
167    public bool IsValidTree() {
168      var matchingSymbol = (from symb in Grammar.Symbols
169                            where symb.Name == Symbol.Name
170                            select symb).SingleOrDefault();
171
172      if (SubTrees.Count < Grammar.GetMinSubtreeCount(matchingSymbol)) return false;
173      else if (SubTrees.Count > Grammar.GetMaxSubtreeCount(matchingSymbol)) return false;
174      else for (int i = 0; i < SubTrees.Count; i++) {
175          if (!GetAllowedSymbols(i).Select(x => x.Name).Contains(SubTrees[i].Symbol.Name)) return false;
176          if (!SubTrees[i].IsValidTree()) return false;
177        }
178      return true;
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.