Free cookie consent management tool by TermsFeed Policy Generator

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

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