[203] | 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 HeuristicLab.Data;
|
---|
[224] | 29 | using System.Xml;
|
---|
[203] | 30 |
|
---|
| 31 | namespace HeuristicLab.Functions {
|
---|
| 32 | class BakedFunctionTree : ItemBase, IFunctionTree {
|
---|
[220] | 33 | private List<int> code;
|
---|
| 34 | private List<double> data;
|
---|
[203] | 35 |
|
---|
[224] | 36 | public BakedFunctionTree() {
|
---|
[220] | 37 | code = new List<int>();
|
---|
| 38 | data = new List<double>();
|
---|
[203] | 39 | }
|
---|
| 40 |
|
---|
[206] | 41 | internal BakedFunctionTree(IFunction function)
|
---|
| 42 | : this() {
|
---|
[203] | 43 | code.Add(0);
|
---|
[223] | 44 | code.Add(BakedTreeEvaluator.MapFunction(function));
|
---|
[203] | 45 | code.Add(0);
|
---|
| 46 | treesExpanded = true;
|
---|
| 47 | subTrees = new List<IFunctionTree>();
|
---|
| 48 | variables = new List<IVariable>();
|
---|
| 49 | variablesExpanded = true;
|
---|
| 50 | foreach(IVariableInfo variableInfo in function.VariableInfos) {
|
---|
| 51 | if(variableInfo.Local) {
|
---|
| 52 | variables.Add((IVariable)function.GetVariable(variableInfo.FormalName).Clone());
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[206] | 57 | internal BakedFunctionTree(IFunctionTree tree)
|
---|
| 58 | : this() {
|
---|
[203] | 59 | code.Add(0);
|
---|
[223] | 60 | code.Add(BakedTreeEvaluator.MapFunction(tree.Function));
|
---|
[220] | 61 | code.Add(tree.LocalVariables.Count);
|
---|
[203] | 62 | foreach(IVariable variable in tree.LocalVariables) {
|
---|
| 63 | IItem value = variable.Value;
|
---|
[220] | 64 | data.Add(GetDoubleValue(value));
|
---|
[203] | 65 | }
|
---|
| 66 | foreach(IFunctionTree subTree in tree.SubTrees) {
|
---|
| 67 | AddSubTree(new BakedFunctionTree(subTree));
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private double GetDoubleValue(IItem value) {
|
---|
| 72 | if(value is DoubleData) {
|
---|
| 73 | return ((DoubleData)value).Data;
|
---|
| 74 | } else if(value is ConstrainedDoubleData) {
|
---|
| 75 | return ((ConstrainedDoubleData)value).Data;
|
---|
| 76 | } else if(value is IntData) {
|
---|
| 77 | return ((IntData)value).Data;
|
---|
| 78 | } else if(value is ConstrainedIntData) {
|
---|
| 79 | return ((ConstrainedIntData)value).Data;
|
---|
| 80 | } else throw new NotSupportedException("Invalid datatype of local variable for GP");
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[220] | 83 | private void BranchLength(int branchRoot, out int codeLength, out int dataLength) {
|
---|
| 84 | int arity = code[branchRoot];
|
---|
| 85 | int nLocalVariables = code[branchRoot + 2];
|
---|
| 86 | codeLength = 3;
|
---|
| 87 | dataLength = nLocalVariables;
|
---|
| 88 | int subBranchStart = branchRoot + codeLength;
|
---|
[203] | 89 | for(int i = 0; i < arity; i++) {
|
---|
[220] | 90 | int branchCodeLength;
|
---|
| 91 | int branchDataLength;
|
---|
| 92 | BranchLength(subBranchStart, out branchCodeLength, out branchDataLength);
|
---|
| 93 | subBranchStart += branchCodeLength;
|
---|
| 94 | codeLength += branchCodeLength;
|
---|
| 95 | dataLength += branchDataLength;
|
---|
[203] | 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private void FlattenTrees() {
|
---|
| 100 | if(treesExpanded) {
|
---|
| 101 | code[0] = subTrees.Count;
|
---|
| 102 | foreach(BakedFunctionTree subTree in subTrees) {
|
---|
| 103 | subTree.FlattenVariables();
|
---|
| 104 | subTree.FlattenTrees();
|
---|
| 105 | code.AddRange(subTree.code);
|
---|
[220] | 106 | data.AddRange(subTree.data);
|
---|
[203] | 107 | }
|
---|
| 108 | treesExpanded = false;
|
---|
[206] | 109 | subTrees = null;
|
---|
[203] | 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private void FlattenVariables() {
|
---|
| 114 | if(variablesExpanded) {
|
---|
| 115 | code[2] = variables.Count;
|
---|
| 116 | foreach(IVariable variable in variables) {
|
---|
[220] | 117 | data.Add(GetDoubleValue(variable.Value));
|
---|
[203] | 118 | }
|
---|
| 119 | variablesExpanded = false;
|
---|
[206] | 120 | variables = null;
|
---|
[203] | 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | private bool treesExpanded = false;
|
---|
| 125 | private List<IFunctionTree> subTrees;
|
---|
| 126 | public IList<IFunctionTree> SubTrees {
|
---|
| 127 | get {
|
---|
| 128 | if(!treesExpanded) {
|
---|
| 129 | subTrees = new List<IFunctionTree>();
|
---|
[220] | 130 | int arity = code[0];
|
---|
| 131 | int nLocalVariables = code[2];
|
---|
| 132 | int branchIndex = 3;
|
---|
| 133 | int dataIndex = nLocalVariables; // skip my local variables to reach the local variables of the first branch
|
---|
[203] | 134 | for(int i = 0; i < arity; i++) {
|
---|
| 135 | BakedFunctionTree subTree = new BakedFunctionTree();
|
---|
[220] | 136 | int codeLength;
|
---|
| 137 | int dataLength;
|
---|
| 138 | BranchLength(branchIndex, out codeLength, out dataLength);
|
---|
| 139 | subTree.code = code.GetRange(branchIndex, codeLength);
|
---|
| 140 | subTree.data = data.GetRange(dataIndex, dataLength);
|
---|
| 141 | branchIndex += codeLength;
|
---|
| 142 | dataIndex += dataLength;
|
---|
[203] | 143 | subTrees.Add(subTree);
|
---|
| 144 | }
|
---|
| 145 | treesExpanded = true;
|
---|
[220] | 146 | code.RemoveRange(3, code.Count - 3);
|
---|
[203] | 147 | code[0] = 0;
|
---|
[220] | 148 | data.RemoveRange(nLocalVariables, data.Count - nLocalVariables);
|
---|
[203] | 149 | }
|
---|
| 150 | return subTrees;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | private bool variablesExpanded = false;
|
---|
| 155 | private List<IVariable> variables;
|
---|
| 156 | public ICollection<IVariable> LocalVariables {
|
---|
| 157 | get {
|
---|
| 158 | if(!variablesExpanded) {
|
---|
| 159 | variables = new List<IVariable>();
|
---|
[223] | 160 | IFunction function = BakedTreeEvaluator.MapSymbol(code[1]);
|
---|
[220] | 161 | int localVariableIndex = 0;
|
---|
[203] | 162 | foreach(IVariableInfo variableInfo in function.VariableInfos) {
|
---|
| 163 | if(variableInfo.Local) {
|
---|
| 164 | IVariable clone = (IVariable)function.GetVariable(variableInfo.FormalName).Clone();
|
---|
| 165 | IItem value = clone.Value;
|
---|
| 166 | if(value is ConstrainedDoubleData) {
|
---|
[220] | 167 | ((ConstrainedDoubleData)value).Data = data[localVariableIndex];
|
---|
[203] | 168 | } else if(value is ConstrainedIntData) {
|
---|
[220] | 169 | ((ConstrainedIntData)value).Data = (int)data[localVariableIndex];
|
---|
[203] | 170 | } else if(value is DoubleData) {
|
---|
[220] | 171 | ((DoubleData)value).Data = data[localVariableIndex];
|
---|
[203] | 172 | } else if(value is IntData) {
|
---|
[220] | 173 | ((IntData)value).Data = (int)data[localVariableIndex];
|
---|
[203] | 174 | } else throw new NotSupportedException("Invalid local variable type for GP.");
|
---|
| 175 | variables.Add(clone);
|
---|
| 176 | localVariableIndex++;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | variablesExpanded = true;
|
---|
| 180 | code[2] = 0;
|
---|
[220] | 181 | data.RemoveRange(0, variables.Count);
|
---|
[203] | 182 | }
|
---|
| 183 | return variables;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | public IFunction Function {
|
---|
[223] | 188 | get { return BakedTreeEvaluator.MapSymbol(code[1]); }
|
---|
[203] | 189 | }
|
---|
| 190 |
|
---|
| 191 | public IVariable GetLocalVariable(string name) {
|
---|
[220] | 192 | foreach(IVariable var in LocalVariables) {
|
---|
| 193 | if(var.Name == name) return var;
|
---|
| 194 | }
|
---|
| 195 | return null;
|
---|
[203] | 196 | }
|
---|
| 197 |
|
---|
| 198 | public void AddVariable(IVariable variable) {
|
---|
[220] | 199 | throw new NotSupportedException();
|
---|
[203] | 200 | }
|
---|
| 201 |
|
---|
| 202 | public void RemoveVariable(string name) {
|
---|
[220] | 203 | throw new NotSupportedException();
|
---|
[203] | 204 | }
|
---|
| 205 |
|
---|
| 206 | public void AddSubTree(IFunctionTree tree) {
|
---|
[220] | 207 | if(!treesExpanded) throw new InvalidOperationException();
|
---|
| 208 | subTrees.Add(tree);
|
---|
[203] | 209 | }
|
---|
| 210 |
|
---|
| 211 | public void InsertSubTree(int index, IFunctionTree tree) {
|
---|
[220] | 212 | if(!treesExpanded) throw new InvalidOperationException();
|
---|
| 213 | subTrees.Insert(index, tree);
|
---|
[203] | 214 | }
|
---|
| 215 |
|
---|
| 216 | public void RemoveSubTree(int index) {
|
---|
[220] | 217 | // sanity check
|
---|
| 218 | if(!treesExpanded) throw new InvalidOperationException();
|
---|
| 219 | subTrees.RemoveAt(index);
|
---|
[203] | 220 | }
|
---|
| 221 |
|
---|
[239] | 222 | private BakedTreeEvaluator evaluator;
|
---|
[203] | 223 | public double Evaluate(Dataset dataset, int sampleIndex) {
|
---|
| 224 | FlattenVariables();
|
---|
| 225 | FlattenTrees();
|
---|
[239] | 226 | if(evaluator == null) evaluator = new BakedTreeEvaluator(code, data);
|
---|
| 227 | return evaluator.Evaluate(dataset, sampleIndex);
|
---|
[203] | 228 | }
|
---|
| 229 |
|
---|
| 230 |
|
---|
[224] | 231 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 232 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 233 | node.AppendChild(PersistenceManager.Persist("Function", Function, document, persistedObjects));
|
---|
| 234 | XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
|
---|
| 235 | for(int i = 0; i < SubTrees.Count; i++)
|
---|
| 236 | subTreesNode.AppendChild(PersistenceManager.Persist(SubTrees[i], document, persistedObjects));
|
---|
| 237 | node.AppendChild(subTreesNode);
|
---|
| 238 | XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
|
---|
| 239 | foreach(IVariable variable in LocalVariables)
|
---|
| 240 | variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
|
---|
| 241 | node.AppendChild(variablesNode);
|
---|
| 242 | return node;
|
---|
[203] | 243 | }
|
---|
| 244 |
|
---|
[224] | 245 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 246 | base.Populate(node, restoredObjects);
|
---|
| 247 | IFunction function = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
|
---|
| 248 | code.Add(0);
|
---|
| 249 | code.Add(BakedTreeEvaluator.MapFunction(function));
|
---|
| 250 | code.Add(0);
|
---|
| 251 | treesExpanded = true;
|
---|
| 252 | subTrees = new List<IFunctionTree>();
|
---|
| 253 | variables = new List<IVariable>();
|
---|
| 254 | variablesExpanded = true;
|
---|
| 255 | XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
|
---|
| 256 | for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
|
---|
| 257 | subTrees.Add((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
|
---|
| 258 | XmlNode variablesNode = node.SelectSingleNode("Variables");
|
---|
| 259 | foreach(XmlNode variableNode in variablesNode.ChildNodes)
|
---|
| 260 | variables.Add((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
|
---|
[203] | 261 | }
|
---|
| 262 |
|
---|
| 263 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 264 | BakedFunctionTree clone = new BakedFunctionTree();
|
---|
[224] | 265 | // in case the user (de)serialized the tree between evaluation and selection we have to flatten the tree again.
|
---|
| 266 | if(treesExpanded) FlattenTrees();
|
---|
| 267 | if(variablesExpanded) FlattenVariables();
|
---|
[220] | 268 | clone.code.AddRange(code);
|
---|
| 269 | clone.data.AddRange(data);
|
---|
[203] | 270 | return clone;
|
---|
| 271 | }
|
---|
[220] | 272 |
|
---|
| 273 | public override IView CreateView() {
|
---|
| 274 | return new FunctionTreeView(this);
|
---|
| 275 | }
|
---|
[203] | 276 | }
|
---|
| 277 | }
|
---|