Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.3/GeneticProgrammingModel.cs @ 2909

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

Implemented quick fix for #872 (Persistence of GeneticProgrammingModels is broken after changes related to #748).

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