Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2219


Ignore:
Timestamp:
08/01/09 13:36:13 (15 years ago)
Author:
gkronber
Message:

fixed serialization of function trees and GPModels. (#713)

Location:
branches/GP-Refactoring-713/sources
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.Boolean/3.3/Symbols/VariableFunctionTree.cs

    r2218 r2219  
    6464
    6565    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
    66       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     66      XmlNode node = document.CreateElement(name);
    6767      XmlAttribute varAttr = document.CreateAttribute("Variable");
    6868      varAttr.Value = VariableName;
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/ConstantFunctionTree.cs

    r2218 r2219  
    6262
    6363    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
    64       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     64      XmlNode node = document.CreateElement(name);
    6565      XmlAttribute valueAttr = document.CreateAttribute("Value");
    6666      valueAttr.Value = XmlConvert.ToString(Value);
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/VariableFunctionTree.cs

    r2218 r2219  
    7272
    7373    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
    74       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     74      XmlNode node = document.CreateElement(name);
    7575      XmlAttribute weightAttr = document.CreateAttribute("Weight");
    7676      weightAttr.Value = XmlConvert.ToString(Weight);
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionBase.cs

    r2216 r2219  
    156156
    157157    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    158       throw new NotSupportedException();
     158      FunctionBase clone = (FunctionBase)base.Clone(clonedObjects);
     159      clone.initializer = (IOperator)Auxiliary.Clone(initializer, clonedObjects);
     160      clone.manipulator = (IOperator)Auxiliary.Clone(manipulator, clonedObjects);
     161      clone.maxArity = maxArity;
     162      clone.minArity = minArity;
     163      clone.minTreeHeight = minTreeHeight;
     164      clone.minTreeSize = minTreeSize;
     165      clone.tickets = tickets;
     166      for (int i = 0; i < MaxSubTrees; i++) {
     167        var allowedSubFunctionsForSlot = new List<IFunction>();
     168        foreach (IFunction f in GetAllowedSubFunctions(i)) {
     169          allowedSubFunctionsForSlot.Add((IFunction)Auxiliary.Clone(f, clonedObjects));
     170        }
     171        clone.allowedSubFunctions.Add(allowedSubFunctionsForSlot);
     172      }
     173      return clone;
    159174    }
    160175
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionTreeBase.cs

    r2218 r2219  
    111111
    112112    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;
     113      return null;
    123114    }
    124115
    125116    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       }
    133117    }
    134118
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/GeneticProgrammingModel.cs

    r2218 r2219  
    5959    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6060      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    61       node.AppendChild(PersistenceManager.Persist("Function", FunctionTree.Function, document, persistedObjects));
    62       node.AppendChild(FunctionTree.GetXmlNode("FunctionTree", document, persistedObjects));
     61      PersistTree(node, document, persistedObjects, FunctionTree);
    6362      return node;
     63    }
     64
     65    private void PersistTree(XmlNode node, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects, IFunctionTree tree) {
     66      XmlNode fNode = PersistenceManager.Persist(tree.Function, document, persistedObjects);
     67      XmlAttribute subTreesAttr = document.CreateAttribute("Args");
     68      subTreesAttr.Value = XmlConvert.ToString(tree.SubTrees.Count);
     69      fNode.Attributes.Append(subTreesAttr);
     70      node.AppendChild(fNode);
     71      XmlNode treeNode = tree.GetXmlNode("Data", document, persistedObjects);
     72      if (treeNode != null) fNode.AppendChild(treeNode);
     73      foreach (IFunctionTree subTree in tree.SubTrees) {
     74        PersistTree(node, document, persistedObjects, subTree);
     75      }
    6476    }
    6577
    6678    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    6779      base.Populate(node, restoredObjects);
    68       IFunction rootFunction = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
    69       IFunctionTree rootNode = rootFunction.GetTreeNode();
    70       rootNode.Populate(node.SelectSingleNode("FunctionTree"), restoredObjects);
     80      int nodeIndex = 0;
     81      FunctionTree = RestoreTree(node, ref nodeIndex, restoredObjects);
     82    }
     83
     84    private IFunctionTree RestoreTree(XmlNode node, ref int nodeIndex, IDictionary<Guid, IStorable> restoredObjects) {
     85      XmlNode fNode = node.ChildNodes[nodeIndex];
     86      int subTrees = XmlConvert.ToInt32(fNode.Attributes["Args"].Value);
     87      IFunction f = (IFunction)PersistenceManager.Restore(fNode, restoredObjects);
     88      IFunctionTree tree = f.GetTreeNode();
     89      if(fNode.ChildNodes.Count>0) tree.Populate(fNode.ChildNodes[0], restoredObjects);
     90      nodeIndex++;
     91      for (int i = 0; i < subTrees; i++) {
     92        tree.AddSubTree(RestoreTree(node, ref nodeIndex, restoredObjects));
     93      }
     94      return tree;
    7195    }
    7296
Note: See TracChangeset for help on using the changeset viewer.