Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs @ 2363

Last change on this file since 2363 was 2347, checked in by gkronber, 15 years ago

Worked on SVR algorithm for time-series. #705

File size: 6.1 KB
RevLine 
[2285]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.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using System.Globalization;
28using System.IO;
29using HeuristicLab.Modeling;
30using SVM;
31using HeuristicLab.DataAnalysis;
32
33namespace HeuristicLab.SupportVectorMachines {
[2328]34  public class Predictor : PredictorBase {
[2285]35    private SVMModel svmModel;
[2328]36    public SVMModel Model {
37      get { return svmModel; }
38    }
39
[2290]40    private Dictionary<string, int> variableNames = new Dictionary<string, int>();
[2285]41    private string targetVariable;
[2347]42    private int minTimeOffset;
43    private int maxTimeOffset;
[2285]44
45    public Predictor() : base() { } // for persistence
46
[2347]47    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) :
48      this(model, targetVariable, variableNames, 0, 0) {
49    }
50
51    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames, int minTimeOffset, int maxTimeOffset)
[2285]52      : base() {
53      this.svmModel = model;
54      this.targetVariable = targetVariable;
[2290]55      this.variableNames = variableNames;
[2347]56      this.minTimeOffset = minTimeOffset;
57      this.maxTimeOffset = maxTimeOffset;
[2285]58    }
59
[2328]60    public override double[] Predict(Dataset input, int start, int end) {
[2285]61      if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
62      if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
63      RangeTransform transform = svmModel.RangeTransform;
64      Model model = svmModel.Model;
[2290]65      // maps columns of the current input dataset to the columns that were originally used in training
66      Dictionary<int, int> newIndex = new Dictionary<int, int>();
67      foreach (var pair in variableNames) {
68        newIndex[input.GetVariableIndex(pair.Key)] = pair.Value;
69      }
[2285]70
[2347]71      Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex,
72        start, end, minTimeOffset, maxTimeOffset);
[2285]73      Problem scaledProblem = SVM.Scaling.Scale(p, transform);
74
75      int rows = end - start;
76      int columns = input.Columns;
77      double[] result = new double[rows];
78      for (int row = 0; row < rows; row++) {
[2328]79        result[row] = Math.Max(Math.Min(SVM.Prediction.Predict(model, scaledProblem.X[row]), UpperPredictionLimit), LowerPredictionLimit);
[2285]80      }
81      return result;
82    }
83
84    public override IView CreateView() {
[2328]85      return new PredictorView(this);
[2285]86    }
87
88    public override object Clone(IDictionary<Guid, object> clonedObjects) {
89      Predictor clone = (Predictor)base.Clone(clonedObjects);
90      clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects);
91      clone.targetVariable = targetVariable;
[2290]92      clone.variableNames = new Dictionary<string, int>(variableNames);
[2347]93      clone.minTimeOffset = minTimeOffset;
94      clone.maxTimeOffset = maxTimeOffset;
[2285]95      return clone;
96    }
97
98    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
99      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
100      XmlAttribute targetVarAttr = document.CreateAttribute("TargetVariable");
101      targetVarAttr.Value = targetVariable;
102      node.Attributes.Append(targetVarAttr);
[2347]103      XmlAttribute minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
104      XmlAttribute maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
105      minTimeOffsetAttr.Value = XmlConvert.ToString(minTimeOffset);
106      maxTimeOffsetAttr.Value = XmlConvert.ToString(maxTimeOffset);
107      node.Attributes.Append(minTimeOffsetAttr);
108      node.Attributes.Append(maxTimeOffsetAttr);
[2285]109      node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
[2290]110      XmlNode variablesNode = document.CreateElement("Variables");
111      foreach (var pair in variableNames) {
112        XmlNode pairNode = document.CreateElement("Variable");
113        XmlAttribute nameAttr = document.CreateAttribute("Name");
114        XmlAttribute indexAttr = document.CreateAttribute("Index");
115        nameAttr.Value = pair.Key;
116        indexAttr.Value = XmlConvert.ToString(pair.Value);
117        pairNode.Attributes.Append(nameAttr);
118        pairNode.Attributes.Append(indexAttr);
119        variablesNode.AppendChild(pairNode);
120      }
121      node.AppendChild(variablesNode);
[2285]122      return node;
123    }
124
125    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
126      base.Populate(node, restoredObjects);
127      targetVariable = node.Attributes["TargetVariable"].Value;
128      svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
[2290]129
[2347]130      if (node.Attributes["MinTimeOffset"] != null) minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
131      if (node.Attributes["MaxTimeOffset"] != null) maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
[2290]132      variableNames = new Dictionary<string, int>();
133      XmlNode variablesNode = node.ChildNodes[1];
134      foreach (XmlNode pairNode in variablesNode.ChildNodes) {
135        variableNames[pairNode.Attributes["Name"].Value] = XmlConvert.ToInt32(pairNode.Attributes["Index"].Value);
136      }
[2285]137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.