source: trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionTreeBase.cs @ 2365

Last change on this file since 2365 was 2365, checked in by gkronber, 10 years ago

Fixed #747 (GP engines create model with non-zero time-offsets).

File size: 3.5 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 HeuristicLab.GP.Interfaces;
27using System.Xml;
28
29namespace HeuristicLab.GP {
30  public class FunctionTreeBase : IFunctionTree {
31    private List<IFunctionTree> subTrees;
32    private IFunction function;
33
34    public FunctionTreeBase() {
35    }
36
37    public FunctionTreeBase(IFunction function) {
38      subTrees = new List<IFunctionTree>();
39      this.function = function;
40    }
41
42    protected FunctionTreeBase(FunctionTreeBase original) {
43      this.function = original.Function;
44      this.subTrees = new List<IFunctionTree>(original.SubTrees.Count);
45      foreach (IFunctionTree originalSubTree in original.SubTrees) {
46        this.SubTrees.Add((IFunctionTree)originalSubTree.Clone());
47      }
48    }
49
50    #region IFunctionTree Members
51    public virtual bool HasLocalParameters {
52      get { return false; }
53    }
54
55    public virtual IList<IFunctionTree> SubTrees {
56      get { return subTrees; }
57    }
58
59    public IFunction Function {
60      get { return function; }
61      protected set { function = value; }
62    }
63
64    public int GetSize() {
65      int size = 1;
66      foreach (IFunctionTree tree in SubTrees) size += tree.GetSize();
67      return size;
68    }
69
70    public int GetHeight() {
71      int maxHeight = 0;
72      foreach (IFunctionTree 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    public void AddSubTree(IFunctionTree tree) {
85      SubTrees.Add(tree);
86    }
87
88    public virtual void InsertSubTree(int index, IFunctionTree tree) {
89      SubTrees.Insert(index, tree);
90    }
91
92    public virtual void RemoveSubTree(int index) {
93      SubTrees.RemoveAt(index);
94    }
95
96    public override string ToString() {
97      return Function.Name;
98    }
99
100    #endregion
101
102    #region IStorable Members
103
104    public Guid Guid {
105      get { throw new NotSupportedException(); }
106    }
107
108    public virtual object Clone() {
109      return new FunctionTreeBase(this);
110    }
111
112    public object Clone(IDictionary<Guid, object> clonedObjects) {
113      throw new NotImplementedException();
114    }
115
116    public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
117      return null;
118    }
119
120    public virtual void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
121    }
122
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.