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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.DataAnalysis;
|
---|
28 | using System.Xml;
|
---|
29 |
|
---|
30 | namespace 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 | double[] evaluationResults;
|
---|
96 | BakedFunctionTree bakedTree = null;
|
---|
97 | public virtual double Evaluate(Dataset dataset, int sampleIndex) {
|
---|
98 | //// lazy creation of evaluationResults to unburden the GC
|
---|
99 | //if(evaluationResults == null) {
|
---|
100 | // evaluationResults = new double[SubTrees.Count];
|
---|
101 | //}
|
---|
102 | //for(int i = 0; i < evaluationResults.Length; i++) {
|
---|
103 | // evaluationResults[i] = SubTrees[i].Evaluate(dataset, sampleIndex);
|
---|
104 | //}
|
---|
105 | //return function.Apply(dataset, sampleIndex, evaluationResults);
|
---|
106 | if(bakedTree==null)
|
---|
107 | bakedTree = new BakedFunctionTree(this);
|
---|
108 | return bakedTree.Evaluate(dataset, sampleIndex);
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | public override XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
113 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
114 | node.AppendChild(PersistenceManager.Persist("Function", function, document, persistedObjects));
|
---|
115 | XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
|
---|
116 | for(int i = 0; i < subTrees.Count; i++)
|
---|
117 | subTreesNode.AppendChild(PersistenceManager.Persist(subTrees[i], document, persistedObjects));
|
---|
118 | node.AppendChild(subTreesNode);
|
---|
119 | XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
|
---|
120 | foreach(IVariable variable in localVariables)
|
---|
121 | variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
|
---|
122 | node.AppendChild(variablesNode);
|
---|
123 | return node;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
127 | base.Populate(node, restoredObjects);
|
---|
128 | function = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
|
---|
129 | XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
|
---|
130 | for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
|
---|
131 | AddSubTree((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
|
---|
132 | XmlNode variablesNode = node.SelectSingleNode("Variables");
|
---|
133 | foreach(XmlNode variableNode in variablesNode.ChildNodes)
|
---|
134 | AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
|
---|
135 | }
|
---|
136 |
|
---|
137 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
138 | FunctionTree clone = new FunctionTree();
|
---|
139 | clonedObjects.Add(clone.Guid, clone);
|
---|
140 | FillClone(clone, clonedObjects);
|
---|
141 | return clone;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void FillClone(FunctionTree clone, IDictionary<Guid, object> clonedObjects) {
|
---|
145 | clone.function = function;
|
---|
146 | foreach(IVariable variable in localVariables) {
|
---|
147 | clone.AddVariable((IVariable)variable.Clone(clonedObjects));
|
---|
148 | }
|
---|
149 | foreach(IFunctionTree tree in subTrees) {
|
---|
150 | clone.AddSubTree((IFunctionTree)tree.Clone(clonedObjects));
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public override IView CreateView() {
|
---|
155 | return new FunctionTreeView(this);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | } |
---|