Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed #131

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