Changeset 2218


Ignore:
Timestamp:
07/31/09 12:31:32 (12 years ago)
Author:
gkronber
Message:

Worked on persistence of function trees. #713

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

Legend:

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

    r2216 r2218  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.GP.Interfaces;
     26using System.Xml;
    2627
    2728namespace HeuristicLab.GP.Boolean {
     
    5859    }
    5960
    60     public override IFunctionTree Clone() {
     61    public override object Clone() {
    6162      return new VariableFunctionTree(this);
     63    }
     64
     65    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
     66      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     67      XmlAttribute varAttr = document.CreateAttribute("Variable");
     68      varAttr.Value = VariableName;
     69      node.Attributes.Append(varAttr);
     70      return node;
     71    }
     72
     73    public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
     74      base.Populate(node, restoredObjects);
     75      VariableName = node.Attributes["Variable"].Value;
    6276    }
    6377
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.Interfaces/3.3/HeuristicLab.GP.Interfaces-3.3.csproj

    r2216 r2218  
    8383      <SubType>Code</SubType>
    8484    </Compile>
    85     <Compile Include="IFunctionTreeSerializer.cs" />
     85    <Compile Include="IFunctionTreeSerializer.cs">
     86      <SubType>Code</SubType>
     87    </Compile>
    8688    <Compile Include="IGeneticProgrammingModel.cs" />
    8789    <Compile Include="HeuristicLabGPInterfacesPlugin.cs" />
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.Interfaces/3.3/IFunctionTree.cs

    r2210 r2218  
    2626
    2727namespace HeuristicLab.GP.Interfaces {
    28   public interface IFunctionTree {
     28  public interface IFunctionTree : IStorable {
    2929    bool HasLocalParameters { get; }
    3030    IList<IFunctionTree> SubTrees { get; }
     
    3737    void InsertSubTree(int index, IFunctionTree tree);
    3838    void RemoveSubTree(int index);
    39     IFunctionTree Clone();
    4039  }
    4140}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.Interfaces/3.3/IFunctionTreeSerializer.cs

    r2216 r2218  
    3030    string Export(IFunctionTree tree);
    3131    bool TryExport(IFunctionTree tree, out string exported);
    32     IFunctionTree Import(string tree);
    33     bool TryImport(string tree, out IFunctionTree importedTree);
     32    //IFunctionTree Import(string tree);
     33    //bool TryImport(string tree, out IFunctionTree importedTree);
    3434  }
    3535}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.Operators/3.3/HeuristicLab.GP.Operators-3.3.csproj

    r2212 r2218  
    1010    <AppDesignerFolder>Properties</AppDesignerFolder>
    1111    <RootNamespace>HeuristicLab.GP.Operators</RootNamespace>
    12     <AssemblyName>HeuristicLab.GP.Operators</AssemblyName>
     12    <AssemblyName>HeuristicLab.GP.Operators-3.3</AssemblyName>
    1313    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    1414    <FileAlignment>512</FileAlignment>
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/SymbolicExpressionExporter.cs

    r2216 r2218  
    5454    }
    5555
    56     public IFunctionTree Import(string tree) {
    57       Queue<string> tokens = new Queue<string>(tree.Split(' ', '\n', '\t'));
    58       return ParseTree(tokens);
    59     }
    60 
    61     private IFunctionTree ParseTree(Queue<string> tokens) {
    62       Expect("(", tokens);
    63       string funSymb = tokens.Peek();
    64       double constValue;
    65       if (double.TryParse(funSymb, out constValue)) {
    66         throw new UnknownFunctionException(funSymb);
    67       }
    68       switch (funSymb) {
    69         case "variable": return ParseVariable(tokens);
    70         case "differential": return ParseDifferential(tokens);
    71         default:
    72           IFunctionTree fNode = ParseFunction(tokens);
    73           Expect(")", tokens);
    74           return fNode;
    75       }
    76 
    77     }
    78 
    79     private IFunctionTree ParseFunction(Queue<string> tokens) {
    80       throw new UnknownFunctionException(tokens.Dequeue());
    81     }
    82 
    83     private IFunctionTree ParseDifferential(Queue<string> tokens) {
    84       throw new UnknownFunctionException(tokens.Dequeue());
    85     }
    86 
    87     private IFunctionTree ParseVariable(Queue<string> tokens) {
    88       throw new UnknownFunctionException(tokens.Dequeue());
    89     }
    90 
    91     private void Expect(string p, Queue<string> tokens) {
    92       throw new NotImplementedException();
    93     }
    94 
    95     public bool TryImport(string tree, out IFunctionTree importedTree) {
    96       try {
    97         importedTree = Import(tree);
    98         return true;
    99       }
    100       catch (UnknownFunctionException) {
    101         importedTree = null;
    102         return false;
    103       }
    104     }
     56    //public IFunctionTree Import(string tree) {
     57    //  Queue<string> tokens = new Queue<string>(tree.Split(' ', '\n', '\t'));
     58    //  return ParseTree(tokens);
     59    //}
     60
     61    //private IFunctionTree ParseTree(Queue<string> tokens) {
     62    //  Expect("(", tokens);
     63    //  string funSymb = tokens.Peek();
     64    //  double constValue;
     65    //  if (double.TryParse(funSymb, out constValue)) {
     66    //    throw new UnknownFunctionException(funSymb);
     67    //  }
     68    //  switch (funSymb) {
     69    //    case "variable": return ParseVariable(tokens);
     70    //    case "differential": return ParseDifferential(tokens);
     71    //    default:
     72    //      IFunctionTree fNode = ParseFunction(tokens);
     73    //      Expect(")", tokens);
     74    //      return fNode;
     75    //  }
     76
     77    //}
     78
     79    //private IFunctionTree ParseFunction(Queue<string> tokens) {
     80    //  string symb = tokens.Dequeue();
     81    //  IFunctionTree funTree;
     82    //  switch (symb) {
     83    //    case "+": funTree = new Addition().GetTreeNode(); break;
     84    //    case "-": funTree = new Subtraction().GetTreeNode(); break;
     85    //    case "*": funTree = new Multiplication().GetTreeNode(); break;
     86    //  }
     87       
     88    //  while (tokens.Peek() == "(") {
     89    //    funTree.AddSubTree(ParseTree(tokens));
     90    //  }
     91    //  return funTree;
     92    //}
     93
     94    //private IFunctionTree ParseDifferential(Queue<string> tokens) {
     95    //  string symb = tokens.Dequeue();
     96    //  double weight = double.Parse(tokens.Dequeue());
     97    //  string name = tokens.Dequeue();
     98    //  int sampleOffset = int.Parse(tokens.Dequeue());
     99    //  VariableFunctionTree varFunTree = new Differential().GetTreeNode();
     100    //  varFunTree.Weight = weight;
     101    //  varFunTree.SampleOffset = sampleOffset;
     102    //  varFunTree.VariableName = name;
     103    //  return varFunTree;
     104    //}
     105
     106    //private IFunctionTree ParseVariable(Queue<string> tokens) {
     107    //  string symb = tokens.Dequeue();
     108    //  double weight = double.Parse(tokens.Dequeue());
     109    //  string name = tokens.Dequeue();
     110    //  int sampleOffset = int.Parse(tokens.Dequeue());
     111    //  VariableFunctionTree varFunTree = new Variable().GetTreeNode();
     112    //  varFunTree.Weight = weight;
     113    //  varFunTree.SampleOffset = sampleOffset;
     114    //  varFunTree.VariableName = name;
     115    //  return varFunTree;
     116    //}
     117
     118    //private void Expect(string p, Queue<string> tokens) {
     119    //  var next = tokens.Dequeue();
     120    //  if (p != next) throw new FormatException("Expected symbol: " + p + ", found symbol: " + next);
     121    //}
     122
     123    //public bool TryImport(string tree, out IFunctionTree importedTree) {
     124    //  try {
     125    //    importedTree = Import(tree);
     126    //    return true;
     127    //  }
     128    //  catch (UnknownFunctionException) {
     129    //    importedTree = null;
     130    //    return false;
     131    //  }
     132    //}
    105133
    106134    private void BuildExportString(IFunctionTree tree) {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/ConstantFunctionTree.cs

    r2216 r2218  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.GP.Interfaces;
     26using System.Xml;
    2627
    2728namespace HeuristicLab.GP.StructureIdentification {
     
    5657    }
    5758
    58     public override IFunctionTree Clone() {
     59    public override object Clone() {
    5960      return new ConstantFunctionTree(this);
     61    }
     62
     63    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
     64      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     65      XmlAttribute valueAttr = document.CreateAttribute("Value");
     66      valueAttr.Value = XmlConvert.ToString(Value);
     67      node.Attributes.Append(valueAttr);
     68      return node;
     69    }
     70
     71    public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
     72      base.Populate(node, restoredObjects);
     73      Value = XmlConvert.ToDouble(node.Attributes["Value"].Value);
    6074    }
    6175
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/Differential.cs

    r2212 r2218  
    2121
    2222
     23using HeuristicLab.GP.Interfaces;
    2324namespace HeuristicLab.GP.StructureIdentification {
    2425  public sealed class Differential : Variable {
     
    3132
    3233    public Differential() : base() { }
     34
     35    public override IFunctionTree GetTreeNode() {
     36      return new VariableFunctionTree(this);
     37    }
    3338  }
    3439}
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/Variable.cs

    r2212 r2218  
    4040      }
    4141    }
     42
    4243
    4344    public override IFunctionTree GetTreeNode() {
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/VariableFunctionTree.cs

    r2216 r2218  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.GP.Interfaces;
     26using System.Xml;
    2627
    2728namespace HeuristicLab.GP.StructureIdentification {
     
    6667    }
    6768
    68     public override IFunctionTree Clone() {
     69    public override object Clone() {
    6970      return new VariableFunctionTree(this);
     71    }
     72
     73    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
     74      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     75      XmlAttribute weightAttr = document.CreateAttribute("Weight");
     76      weightAttr.Value = XmlConvert.ToString(Weight);
     77      XmlAttribute variableAttr = document.CreateAttribute("Variable");
     78      variableAttr.Value = VariableName;
     79      XmlAttribute sampleOffsetAttr = document.CreateAttribute("SampleOffset");
     80      sampleOffsetAttr.Value = XmlConvert.ToString(SampleOffset);
     81      node.Attributes.Append(weightAttr);
     82      node.Attributes.Append(sampleOffsetAttr);
     83      node.Attributes.Append(variableAttr);
     84      return node;
     85    }
     86
     87    public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
     88      base.Populate(node, restoredObjects);
     89      Weight = XmlConvert.ToDouble(node.Attributes["Weight"].Value);
     90      SampleOffset = XmlConvert.ToInt32(node.Attributes["SampleOffset"].Value);
     91      VariableName = node.Attributes["Variable"].Value;
    7092    }
    7193
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionTreeBase.cs

    r2216 r2218  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.GP.Interfaces;
     27using System.Xml;
    2728
    2829namespace HeuristicLab.GP {
     
    4344      this.subTrees = new List<IFunctionTree>(original.SubTrees.Count);
    4445      foreach (IFunctionTree originalSubTree in original.SubTrees) {
    45         this.SubTrees.Add(originalSubTree.Clone());
     46        this.SubTrees.Add((IFunctionTree)originalSubTree.Clone());
    4647      }
    4748    }
     
    9394    }
    9495
    95     public virtual IFunctionTree Clone() {
     96    #endregion
     97
     98    #region IStorable Members
     99
     100    public Guid Guid {
     101      get { throw new NotSupportedException(); }
     102    }
     103
     104    public virtual object Clone() {
    96105      return new FunctionTreeBase(this);
     106    }
     107
     108    public object Clone(IDictionary<Guid, object> clonedObjects) {
     109      throw new NotImplementedException();
     110    }
     111
     112    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;
     123    }
     124
     125    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      }
    97133    }
    98134
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/GeneticProgrammingModel.cs

    r2216 r2218  
    5353    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    5454      GeneticProgrammingModel clone = (GeneticProgrammingModel)base.Clone(clonedObjects);
    55       clone.FunctionTree = FunctionTree.Clone();
     55      clone.FunctionTree = (IFunctionTree)FunctionTree.Clone();
    5656      return clone;
    5757    }
     
    5959    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6060      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    61       node.Value = GeneralTreeSerializer.Export(FunctionTree);
     61      node.AppendChild(PersistenceManager.Persist("Function", FunctionTree.Function, document, persistedObjects));
     62      node.AppendChild(FunctionTree.GetXmlNode("FunctionTree", document, persistedObjects));
    6263      return node;
    6364    }
     
    6566    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    6667      base.Populate(node, restoredObjects);
    67       FunctionTree = GeneralTreeSerializer.Import(node.Value);
     68      IFunction rootFunction = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
     69      IFunctionTree rootNode = rootFunction.GetTreeNode();
     70      rootNode.Populate(node.SelectSingleNode("FunctionTree"), restoredObjects);
    6871    }
    6972
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/HeuristicLab.GP-3.3.csproj

    r2216 r2218  
    8989    <Compile Include="BaseClasses\Terminal.cs" />
    9090    <Compile Include="BaseClasses\UnaryFunction.cs" />
    91     <Compile Include="FunctionTreeSerializer.cs" />
    9291    <Compile Include="FunctionLibrary.cs" />
    9392    <Compile Include="FunctionLibraryEditor.cs">
Note: See TracChangeset for help on using the changeset viewer.