[157] | 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;
|
---|
[142] | 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 |
|
---|
[189] | 37 | public FunctionTree()
|
---|
| 38 | : base() {
|
---|
[142] | 39 | subTrees = new List<IFunctionTree>();
|
---|
| 40 | localVariables = new List<IVariable>();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[189] | 43 | internal FunctionTree(IFunction function)
|
---|
| 44 | : this() {
|
---|
[142] | 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 {
|
---|
[153] | 57 | get { return subTrees; }
|
---|
[142] | 58 | }
|
---|
| 59 |
|
---|
| 60 | public ICollection<IVariable> LocalVariables {
|
---|
[153] | 61 | get { return localVariables; }
|
---|
[142] | 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 |
|
---|
[192] | 95 | double[] evaluationResults;
|
---|
[189] | 96 | public virtual double Evaluate(Dataset dataset, int sampleIndex) {
|
---|
[192] | 97 | // lazy creation of evaluationResults to unburden the GC
|
---|
| 98 | if(evaluationResults == null) {
|
---|
| 99 | evaluationResults = new double[SubTrees.Count];
|
---|
| 100 | }
|
---|
[189] | 101 | for(int i = 0; i < evaluationResults.Length; i++) {
|
---|
| 102 | evaluationResults[i] = SubTrees[i].Evaluate(dataset, sampleIndex);
|
---|
| 103 | }
|
---|
| 104 | return function.Apply(dataset, sampleIndex, evaluationResults);
|
---|
[142] | 105 | }
|
---|
| 106 | #endregion
|
---|
| 107 |
|
---|
| 108 | public override XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 109 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
[145] | 110 | node.AppendChild(PersistenceManager.Persist("Function", function, document, persistedObjects));
|
---|
[142] | 111 | XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
|
---|
| 112 | for(int i = 0; i < subTrees.Count; i++)
|
---|
| 113 | subTreesNode.AppendChild(PersistenceManager.Persist(subTrees[i], document, persistedObjects));
|
---|
| 114 | node.AppendChild(subTreesNode);
|
---|
| 115 | XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
|
---|
| 116 | foreach(IVariable variable in localVariables)
|
---|
| 117 | variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
|
---|
| 118 | node.AppendChild(variablesNode);
|
---|
| 119 | return node;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 123 | base.Populate(node, restoredObjects);
|
---|
[145] | 124 | function = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
|
---|
[142] | 125 | XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
|
---|
| 126 | for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
|
---|
| 127 | AddSubTree((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
|
---|
| 128 | XmlNode variablesNode = node.SelectSingleNode("Variables");
|
---|
| 129 | foreach(XmlNode variableNode in variablesNode.ChildNodes)
|
---|
| 130 | AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
[192] | 134 | FunctionTree clone = new FunctionTree();
|
---|
| 135 | clonedObjects.Add(clone.Guid, clone);
|
---|
| 136 | FillClone(clone, clonedObjects);
|
---|
| 137 | return clone;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | public void FillClone(FunctionTree clone, IDictionary<Guid, object> clonedObjects) {
|
---|
| 141 | clone.function = function;
|
---|
| 142 | foreach(IVariable variable in localVariables) {
|
---|
| 143 | clone.AddVariable((IVariable)variable.Clone(clonedObjects));
|
---|
| 144 | }
|
---|
[142] | 145 | foreach(IFunctionTree tree in subTrees) {
|
---|
| 146 | clone.AddSubTree((IFunctionTree)tree.Clone(clonedObjects));
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public override IView CreateView() {
|
---|
| 151 | return new FunctionTreeView(this);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | } |
---|