#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.GP.Interfaces; using HeuristicLab.GP; using HeuristicLab.Modeling; using System; using System.Xml; using HeuristicLab.DataAnalysis; namespace HeuristicLab.GP.StructureIdentification { public class Predictor : ItemBase, IPredictor { private ITreeEvaluator treeEvaluator; public Predictor() : base() { } // for persistence public Predictor(ITreeEvaluator evaluator, IGeneticProgrammingModel tree) : base() { this.treeEvaluator = evaluator; this.functionTree = tree; } private IGeneticProgrammingModel functionTree; public IGeneticProgrammingModel FunctionTree { get { return functionTree; } } public double[] Predict(Dataset input, int start, int end) { if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end"); if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end"); treeEvaluator.PrepareForEvaluation(input, functionTree.FunctionTree); double[] result = new double[end - start]; for (int i = 0; i < result.Length; i++) { result[i] = treeEvaluator.Evaluate(i + start); } return result; } public override IView CreateView() { return functionTree.CreateView(); } public override object Clone(IDictionary clonedObjects) { Predictor clone = (Predictor)base.Clone(clonedObjects); clone.treeEvaluator = (ITreeEvaluator)Auxiliary.Clone(treeEvaluator, clonedObjects); clone.functionTree = (IGeneticProgrammingModel)Auxiliary.Clone(functionTree, clonedObjects); return clone; } public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary persistedObjects) { XmlNode node = base.GetXmlNode(name, document, persistedObjects); node.AppendChild(PersistenceManager.Persist("Evaluator", treeEvaluator, document, persistedObjects)); node.AppendChild(PersistenceManager.Persist("FunctionTree", functionTree, document, persistedObjects)); return node; } public override void Populate(System.Xml.XmlNode node, IDictionary restoredObjects) { base.Populate(node, restoredObjects); treeEvaluator = (ITreeEvaluator)PersistenceManager.Restore(node.SelectSingleNode("Evaluator"), restoredObjects); functionTree = (IGeneticProgrammingModel)PersistenceManager.Restore(node.SelectSingleNode("FunctionTree"), restoredObjects); } } }