Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs @ 3317

Last change on this file since 3317 was 3317, checked in by swagner, 14 years ago

Implemented ReadOnlyView property for items (#969).

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [StorableClass]
33  [Item("SymbolicExpressionTree", "Represents a symbolic expression tree.")]
34  public class SymbolicExpressionTree : Item {
35    [Storable]
36    private SymbolicExpressionTreeNode root;
37    public SymbolicExpressionTreeNode Root {
38      get { return root; }
39      set {
40        if (value == null) throw new ArgumentNullException();
41        else if (value != root) {
42          root = value;
43          OnToStringChanged();
44        }
45      }
46    }
47
48    public SymbolicExpressionTreeNode ResultProducingExpression {
49      get { return root.SubTrees[0].SubTrees[0]; }
50    }
51
52    [Storable]
53    private Dictionary<int, IEnumerable<string>> allowedFunctionsInBranch;
54
55    public int Size {
56      get {
57        return root.GetSize();
58      }
59    }
60
61    public int Height {
62      get {
63        return root.GetHeight();
64      }
65    }
66
67    public SymbolicExpressionTree()
68      : base() {
69      allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>();
70    }
71
72    public SymbolicExpressionTree(SymbolicExpressionTreeNode root)
73      : base() {
74      allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>();
75    }
76
77    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
78      return IterateNodesPrefix(root);
79    }
80    private IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode node) {
81      yield return node;
82      foreach (var subtree in node.SubTrees) {
83        foreach (var n in IterateNodesPrefix(subtree))
84          yield return n;
85      }
86    }
87    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
88      return IterateNodesPostfix(root);
89    }
90    private IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix(SymbolicExpressionTreeNode node) {
91      foreach (var subtree in node.SubTrees) {
92        foreach (var n in IterateNodesPrefix(subtree))
93          yield return n;
94      }
95      yield return node;
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      SymbolicExpressionTree clone = new SymbolicExpressionTree();
100      cloner.RegisterClonedObject(this, clone);
101      clone.ReadOnlyView = ReadOnlyView;
102      clone.root = (SymbolicExpressionTreeNode)this.root.Clone();
103      clone.allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>(allowedFunctionsInBranch);
104      return clone;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.