Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionTreeBase.cs @ 2218

Last change on this file since 2218 was 2218, checked in by gkronber, 15 years ago

Worked on persistence of function trees. #713

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.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    #endregion
97
98    #region IStorable Members
99
100    public Guid Guid {
101      get { throw new NotSupportedException(); }
102    }
103
104    public virtual object Clone() {
105      return new FunctionTreeBase(this);
106    }
107
108    public object Clone(IDictionary<Guid, object> clonedObjects) {
109      throw new NotImplementedException();
110    }
111
112    public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
113      XmlNode node = document.CreateElement(name);
114      //XmlAttribute typeAttr = document.CreateAttribute("Type");
115      //typeAttr.Value = PersistenceManager.BuildTypeString(GetType());
116      //node.Attributes.Append(typeAttr);
117      foreach (IFunctionTree subTree in SubTrees) {
118        XmlNode funNode = PersistenceManager.Persist(Function, document, persistedObjects);
119        node.AppendChild(funNode);
120        funNode.AppendChild(subTree.GetXmlNode("Tree", document, persistedObjects));
121      }
122      return node;
123    }
124
125    public virtual void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
126      for (int i = 0; i < node.ChildNodes.Count; i++) {
127        XmlNode childNode = node.ChildNodes[i];
128        IFunction function = (IFunction)PersistenceManager.Restore(childNode, restoredObjects);
129        IFunctionTree tree = function.GetTreeNode();
130        tree.Populate(childNode.SelectSingleNode("Tree"), restoredObjects);
131        AddSubTree(tree);
132      }
133    }
134
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.