[142] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.DataAnalysis;
|
---|
| 7 | using System.Xml;
|
---|
| 8 |
|
---|
| 9 | namespace HeuristicLab.Functions {
|
---|
| 10 | public class FunctionTree : ItemBase, IFunctionTree {
|
---|
| 11 |
|
---|
| 12 | private List<IFunctionTree> subTrees;
|
---|
| 13 | private List<IVariable> localVariables;
|
---|
| 14 | private IFunction function;
|
---|
| 15 |
|
---|
| 16 | public FunctionTree() : base() {
|
---|
| 17 | subTrees = new List<IFunctionTree>();
|
---|
| 18 | localVariables = new List<IVariable>();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | public FunctionTree(IFunction function) : this() {
|
---|
| 22 | this.function = function;
|
---|
| 23 | // create and store clones of all local variables of the function
|
---|
| 24 | foreach(VariableInfo info in function.VariableInfos) {
|
---|
| 25 | if(info.Local) {
|
---|
| 26 | AddVariable((IVariable)function.GetVariable(info.FormalName).Clone());
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | #region IFunctionTree Members
|
---|
| 32 |
|
---|
| 33 | public IList<IFunctionTree> SubTrees {
|
---|
| 34 | get { return subTrees.AsReadOnly(); }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public ICollection<IVariable> LocalVariables {
|
---|
| 38 | get { return localVariables.AsReadOnly(); }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public IFunction Function {
|
---|
| 42 | get { return function; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public IVariable GetLocalVariable(string name) {
|
---|
| 46 | foreach(IVariable var in localVariables) {
|
---|
| 47 | if(var.Name == name) return var;
|
---|
| 48 | }
|
---|
| 49 | return null;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public void AddVariable(IVariable variable) {
|
---|
| 53 | localVariables.Add(variable);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public void RemoveVariable(string name) {
|
---|
| 57 | localVariables.Remove(GetLocalVariable(name));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public void AddSubTree(IFunctionTree tree) {
|
---|
| 61 | subTrees.Add(tree);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public void InsertSubTree(int index, IFunctionTree tree) {
|
---|
| 65 | subTrees.Insert(index, tree);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public void RemoveSubTree(int index) {
|
---|
| 69 | subTrees.RemoveAt(index);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public double Evaluate(Dataset dataset, int sampleIndex) {
|
---|
| 73 | return function.Evaluate(dataset, sampleIndex, subTrees);
|
---|
| 74 | }
|
---|
| 75 | #endregion
|
---|
| 76 |
|
---|
| 77 | public override XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 78 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
[145] | 79 | node.AppendChild(PersistenceManager.Persist("Function", function, document, persistedObjects));
|
---|
[142] | 80 | XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
|
---|
| 81 | for(int i = 0; i < subTrees.Count; i++)
|
---|
| 82 | subTreesNode.AppendChild(PersistenceManager.Persist(subTrees[i], document, persistedObjects));
|
---|
| 83 | node.AppendChild(subTreesNode);
|
---|
| 84 | XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
|
---|
| 85 | foreach(IVariable variable in localVariables)
|
---|
| 86 | variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
|
---|
| 87 | node.AppendChild(variablesNode);
|
---|
| 88 | return node;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 92 | base.Populate(node, restoredObjects);
|
---|
[145] | 93 | function = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
|
---|
[142] | 94 | XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
|
---|
| 95 | for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
|
---|
| 96 | AddSubTree((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
|
---|
| 97 | XmlNode variablesNode = node.SelectSingleNode("Variables");
|
---|
| 98 | foreach(XmlNode variableNode in variablesNode.ChildNodes)
|
---|
| 99 | AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 103 | FunctionTree clone = (FunctionTree)base.Clone(clonedObjects);
|
---|
| 104 | foreach(IFunctionTree tree in subTrees) {
|
---|
| 105 | clone.AddSubTree((IFunctionTree)tree.Clone(clonedObjects));
|
---|
| 106 | }
|
---|
| 107 | foreach(IVariable variable in localVariables) {
|
---|
| 108 | clone.AddVariable((IVariable)variable.Clone(clonedObjects));
|
---|
| 109 | }
|
---|
[145] | 110 | clone.function = function;
|
---|
[142] | 111 | return clone;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public override IView CreateView() {
|
---|
| 115 | return new FunctionTreeView(this);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | } |
---|