Changeset 2787


Ignore:
Timestamp:
02/12/10 14:03:47 (11 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP/3.3/GeneticProgrammingModel.cs

    r2328 r2787  
    6060    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6161      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     62      // persist the tree in linear form
    6263      PersistTree(node, document, persistedObjects, FunctionTree);
    6364      return node;
     
    6667    private void PersistTree(XmlNode node, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects, IFunctionTree tree) {
    6768      XmlNode fNode = PersistenceManager.Persist(tree.Function, document, persistedObjects);
     69      // save the number of sub-trees
    6870      XmlAttribute subTreesAttr = document.CreateAttribute("Args");
    6971      subTreesAttr.Value = XmlConvert.ToString(tree.SubTrees.Count);
    7072      fNode.Attributes.Append(subTreesAttr);
     73      // save the function symbol
    7174      node.AppendChild(fNode);
     75      // if the tree node has local data save it into a child element called "data"
    7276      XmlNode treeNode = tree.GetXmlNode("Data", document, persistedObjects);
    7377      if (treeNode != null) fNode.AppendChild(treeNode);
     78      // recursivly store the children into the same linear form
    7479      foreach (IFunctionTree subTree in tree.SubTrees) {
    7580        PersistTree(node, document, persistedObjects, subTree);
     
    8085      base.Populate(node, restoredObjects);
    8186      int nodeIndex = 0;
     87      // restore linear form back into tree form
    8288      FunctionTree = RestoreTree(node, ref nodeIndex, restoredObjects);
    8389    }
     
    8591    private IFunctionTree RestoreTree(XmlNode node, ref int nodeIndex, IDictionary<Guid, IStorable> restoredObjects) {
    8692      XmlNode fNode = node.ChildNodes[nodeIndex];
     93      // restore the number of child nodes
    8794      int subTrees = XmlConvert.ToInt32(fNode.Attributes["Args"].Value);
     95      // restore the function symbol
    8896      IFunction f = (IFunction)PersistenceManager.Restore(fNode, restoredObjects);
     97      // create a tree node from the function
    8998      IFunctionTree tree = f.GetTreeNode();
    90       if (fNode.ChildNodes.Count > 0) tree.Populate(fNode.ChildNodes[0], restoredObjects);
     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);
    91102      nodeIndex++;
    92103      for (int i = 0; i < subTrees; i++) {
     104        // recursively read children from linear representation
    93105        tree.AddSubTree(RestoreTree(node, ref nodeIndex, restoredObjects));
    94106      }
Note: See TracChangeset for help on using the changeset viewer.