Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/FunctionTree.cs @ 157

Last change on this file since 157 was 157, checked in by gkronber, 16 years ago

added missing GPL license header

File size: 4.9 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.DataAnalysis;
28using System.Xml;
29
30namespace HeuristicLab.Functions {
31  public class FunctionTree : ItemBase, IFunctionTree {
32
33    private List<IFunctionTree> subTrees;
34    private List<IVariable> localVariables;
35    private IFunction function;
36
37    public FunctionTree() : base() {
38      subTrees = new List<IFunctionTree>();
39      localVariables = new List<IVariable>();
40    }
41
42    public FunctionTree(IFunction function) : this() {
43      this.function = function;
44      // create and store clones of all local variables of the function
45      foreach(VariableInfo info in function.VariableInfos) {
46        if(info.Local) {
47          AddVariable((IVariable)function.GetVariable(info.FormalName).Clone());
48        }
49      }
50    }
51
52    #region IFunctionTree Members
53
54    public IList<IFunctionTree> SubTrees {
55      get { return subTrees; }
56    }
57
58    public ICollection<IVariable> LocalVariables {
59      get { return localVariables; }
60    }
61
62    public IFunction Function {
63      get { return function; }
64    }
65
66    public IVariable GetLocalVariable(string name) {
67      foreach(IVariable var in localVariables) {
68        if(var.Name == name) return var;
69      }
70      return null;
71    }
72
73    public void AddVariable(IVariable variable) {
74      localVariables.Add(variable);
75    }
76
77    public void RemoveVariable(string name) {
78      localVariables.Remove(GetLocalVariable(name));
79    }
80
81    public void AddSubTree(IFunctionTree tree) {
82      subTrees.Add(tree);
83    }
84
85    public void InsertSubTree(int index, IFunctionTree tree) {
86      subTrees.Insert(index, tree);
87    }
88
89    public void RemoveSubTree(int index) {
90      subTrees.RemoveAt(index);
91    }
92
93    public double Evaluate(Dataset dataset, int sampleIndex) {
94      return function.Evaluate(dataset, sampleIndex, this);
95    }
96    #endregion
97
98    public override XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
99      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
100      node.AppendChild(PersistenceManager.Persist("Function", function, document, persistedObjects));
101      XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
102      for(int i = 0; i < subTrees.Count; i++)
103        subTreesNode.AppendChild(PersistenceManager.Persist(subTrees[i], document, persistedObjects));
104      node.AppendChild(subTreesNode);
105      XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
106      foreach(IVariable variable in localVariables)
107        variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
108      node.AppendChild(variablesNode);
109      return node;
110    }
111
112    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
113      base.Populate(node, restoredObjects);
114      function = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
115      XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
116      for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
117        AddSubTree((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
118      XmlNode variablesNode = node.SelectSingleNode("Variables");
119      foreach(XmlNode variableNode in variablesNode.ChildNodes)
120        AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
121    }
122
123    public override object Clone(IDictionary<Guid, object> clonedObjects) {
124      FunctionTree clone = (FunctionTree)base.Clone(clonedObjects);
125      foreach(IFunctionTree tree in subTrees) {
126        clone.AddSubTree((IFunctionTree)tree.Clone(clonedObjects));
127      }
128      foreach(IVariable variable in localVariables) {
129        clone.AddVariable((IVariable)variable.Clone(clonedObjects));
130      }
131      clone.function = function;
132      return clone;
133    }
134
135    public override IView CreateView() {
136      return new FunctionTreeView(this);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.