Free cookie consent management tool by TermsFeed Policy Generator

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

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

Integrated graphical symbolic expression tree view from model analyzer. #937 (Data types and operators for symbolic expression tree encoding)

File size: 2.9 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    private SymbolicExpressionTreeNode root;
36    public SymbolicExpressionTreeNode Root {
37      get { return root; }
38      set {
39        if (value == null) throw new ArgumentNullException();
40        else if (value != root) {
41          root = value;
42          OnToStringChanged();
43        }
44      }
45    }
46
47    public int Size {
48      get {
49        return root.GetSize();
50      }
51    }
52
53    public int Height {
54      get {
55        return root.GetHeight();
56      }
57    }
58
59    public SymbolicExpressionTree() : base() { }
60
61    public SymbolicExpressionTree(SymbolicExpressionTreeNode root) : base() { }
62
63    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
64      return IterateNodesPrefix(root);
65    }
66    private IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode node) {
67      yield return node;
68      foreach (var subtree in node.SubTrees) {
69        foreach (var n in IterateNodesPrefix(subtree))
70          yield return n;
71      }
72    }
73    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
74      return IterateNodesPostfix(root);
75    }
76    private IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix(SymbolicExpressionTreeNode node) {
77      foreach (var subtree in node.SubTrees) {
78        foreach (var n in IterateNodesPrefix(subtree))
79          yield return n;
80      }
81      yield return node;
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      SymbolicExpressionTree clone = new SymbolicExpressionTree();
86      cloner.RegisterClonedObject(this, clone);
87      clone.root = (SymbolicExpressionTreeNode)this.root.Clone();
88      return clone;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.