Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP/3.3/GeneticProgrammingModel.cs @ 14427

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

Removed fields for tree size and tree height in GeneticProgrammingModel instead iterate through the tree to determine the size/height whenever it is needed. #1025 (Incorrect size/height values for trees)

File size: 4.4 KB
RevLine 
[2210]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;
[2216]27using System.Xml;
[2210]28
29namespace HeuristicLab.GP {
30  public class GeneticProgrammingModel : ItemBase, IGeneticProgrammingModel {
31    private IFunctionTree functionTree;
32    public IFunctionTree FunctionTree {
33      get {
34        return functionTree;
35      }
36      set {
37        functionTree = value;
38      }
39    }
[3873]40    public int Size { get { return functionTree.GetSize(); } }
41    public int Height { get { return functionTree.GetHeight(); } }
[2210]42
43    public GeneticProgrammingModel()
44      : base() {
45    }
46
[2235]47    public GeneticProgrammingModel(IFunctionTree tree)
48      : base() {
[2210]49      FunctionTree = tree;
50    }
51
52    public override object Clone(IDictionary<Guid, object> clonedObjects) {
53      GeneticProgrammingModel clone = (GeneticProgrammingModel)base.Clone(clonedObjects);
[2218]54      clone.FunctionTree = (IFunctionTree)FunctionTree.Clone();
[2210]55      return clone;
56    }
57
[2216]58    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
59      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
[2787]60      // persist the tree in linear form
[2219]61      PersistTree(node, document, persistedObjects, FunctionTree);
[2216]62      return node;
[2210]63    }
64
[2219]65    private void PersistTree(XmlNode node, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects, IFunctionTree tree) {
66      XmlNode fNode = PersistenceManager.Persist(tree.Function, document, persistedObjects);
[2787]67      // save the number of sub-trees
[2219]68      XmlAttribute subTreesAttr = document.CreateAttribute("Args");
69      subTreesAttr.Value = XmlConvert.ToString(tree.SubTrees.Count);
70      fNode.Attributes.Append(subTreesAttr);
[2787]71      // save the function symbol
[2219]72      node.AppendChild(fNode);
[2787]73      // if the tree node has local data save it into a child element called "data"
[2219]74      XmlNode treeNode = tree.GetXmlNode("Data", document, persistedObjects);
75      if (treeNode != null) fNode.AppendChild(treeNode);
[2787]76      // recursivly store the children into the same linear form
[2219]77      foreach (IFunctionTree subTree in tree.SubTrees) {
78        PersistTree(node, document, persistedObjects, subTree);
79      }
80    }
81
[2216]82    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
83      base.Populate(node, restoredObjects);
[2219]84      int nodeIndex = 0;
[2787]85      // restore linear form back into tree form
[2219]86      FunctionTree = RestoreTree(node, ref nodeIndex, restoredObjects);
[2210]87    }
88
[2219]89    private IFunctionTree RestoreTree(XmlNode node, ref int nodeIndex, IDictionary<Guid, IStorable> restoredObjects) {
90      XmlNode fNode = node.ChildNodes[nodeIndex];
[2787]91      // restore the number of child nodes
[2219]92      int subTrees = XmlConvert.ToInt32(fNode.Attributes["Args"].Value);
[2787]93      // restore the function symbol
[2219]94      IFunction f = (IFunction)PersistenceManager.Restore(fNode, restoredObjects);
[2787]95      // create a tree node from the function
[2219]96      IFunctionTree tree = f.GetTreeNode();
[2787]97      // check if there is data for the tree node that needs to be restored and restore the data if needed
98      var dataNode = fNode.SelectSingleNode("Data");
[3873]99      if (dataNode != null) tree.Populate(dataNode, restoredObjects);
[2219]100      nodeIndex++;
101      for (int i = 0; i < subTrees; i++) {
[2787]102        // recursively read children from linear representation
[2219]103        tree.AddSubTree(RestoreTree(node, ref nodeIndex, restoredObjects));
104      }
105      return tree;
106    }
107
[2210]108    public override IView CreateView() {
[2328]109      return new GeneticProgrammingModelView(this);
[2210]110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.