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.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using System.Globalization;
|
---|
29 | using System.IO;
|
---|
30 | using HeuristicLab.Modeling;
|
---|
31 | using SVM;
|
---|
32 | using HeuristicLab.DataAnalysis;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.SupportVectorMachines {
|
---|
35 | public class Predictor : PredictorBase {
|
---|
36 | private SVMModel svmModel;
|
---|
37 | public SVMModel Model {
|
---|
38 | get { return svmModel; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private List<string> variableNames;
|
---|
42 | private string targetVariable;
|
---|
43 | private int minTimeOffset;
|
---|
44 | public int MinTimeOffset {
|
---|
45 | get { return minTimeOffset; }
|
---|
46 | }
|
---|
47 | private int maxTimeOffset;
|
---|
48 | public int MaxTimeOffset {
|
---|
49 | get { return maxTimeOffset; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | // for persistence
|
---|
53 | public Predictor() : base() { }
|
---|
54 |
|
---|
55 | public Predictor(SVMModel model, string targetVariable, IEnumerable<string> variableNames) :
|
---|
56 | this(model, targetVariable, variableNames, 0, 0) {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public Predictor(SVMModel model, string targetVariable, IEnumerable<string> variableNames, int minTimeOffset, int maxTimeOffset)
|
---|
60 | : this() {
|
---|
61 | this.svmModel = model;
|
---|
62 | this.targetVariable = targetVariable;
|
---|
63 | this.minTimeOffset = minTimeOffset;
|
---|
64 | this.maxTimeOffset = maxTimeOffset;
|
---|
65 | this.variableNames = new List<string>(variableNames);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override IEnumerable<double> Predict(Dataset input, int start, int end) {
|
---|
69 | if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
|
---|
70 | if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
|
---|
71 | RangeTransform transform = svmModel.RangeTransform;
|
---|
72 | Model model = svmModel.Model;
|
---|
73 |
|
---|
74 | Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), variableNames,
|
---|
75 | start, end, minTimeOffset, maxTimeOffset);
|
---|
76 | Problem scaledProblem = transform.Scale(p);
|
---|
77 |
|
---|
78 | int targetVariableIndex = input.GetVariableIndex(targetVariable);
|
---|
79 | int rows = end - start;
|
---|
80 | //double[] result = new double[rows];
|
---|
81 | int problemRow = 0;
|
---|
82 | for (int resultRow = 0; resultRow < rows; resultRow++) {
|
---|
83 | if (double.IsNaN(input.GetValue(resultRow, targetVariableIndex)))
|
---|
84 | yield return UpperPredictionLimit;
|
---|
85 | else if (resultRow + maxTimeOffset < 0) {
|
---|
86 | problemRow++;
|
---|
87 | yield return UpperPredictionLimit;
|
---|
88 | } else {
|
---|
89 | yield return Math.Max(Math.Min(SVM.Prediction.Predict(model, scaledProblem.X[problemRow++]), UpperPredictionLimit), LowerPredictionLimit);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override IEnumerable<string> GetInputVariables() {
|
---|
95 | return variableNames;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override IView CreateView() {
|
---|
99 | return new PredictorView(this);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
103 | Predictor clone = (Predictor)base.Clone(clonedObjects);
|
---|
104 | clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects);
|
---|
105 | clone.targetVariable = targetVariable;
|
---|
106 | clone.variableNames = new List<string>(variableNames);
|
---|
107 | clone.minTimeOffset = minTimeOffset;
|
---|
108 | clone.maxTimeOffset = maxTimeOffset;
|
---|
109 | return clone;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
113 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
114 | XmlAttribute targetVarAttr = document.CreateAttribute("TargetVariable");
|
---|
115 | targetVarAttr.Value = targetVariable;
|
---|
116 | node.Attributes.Append(targetVarAttr);
|
---|
117 | XmlAttribute minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
|
---|
118 | XmlAttribute maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
|
---|
119 | minTimeOffsetAttr.Value = XmlConvert.ToString(minTimeOffset);
|
---|
120 | maxTimeOffsetAttr.Value = XmlConvert.ToString(maxTimeOffset);
|
---|
121 | node.Attributes.Append(minTimeOffsetAttr);
|
---|
122 | node.Attributes.Append(maxTimeOffsetAttr);
|
---|
123 | node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
|
---|
124 | XmlNode variablesNode = document.CreateElement("Variables");
|
---|
125 | foreach (var variableName in variableNames) {
|
---|
126 | XmlNode variableNameNode = document.CreateElement("Variable");
|
---|
127 | XmlAttribute nameAttr = document.CreateAttribute("Name");
|
---|
128 | nameAttr.Value = variableName;
|
---|
129 | variableNameNode.Attributes.Append(nameAttr);
|
---|
130 | variablesNode.AppendChild(variableNameNode);
|
---|
131 | }
|
---|
132 | node.AppendChild(variablesNode);
|
---|
133 | return node;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
137 | base.Populate(node, restoredObjects);
|
---|
138 | targetVariable = node.Attributes["TargetVariable"].Value;
|
---|
139 | svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
|
---|
140 |
|
---|
141 | if (node.Attributes["MinTimeOffset"] != null) minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
|
---|
142 | if (node.Attributes["MaxTimeOffset"] != null) maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
|
---|
143 | variableNames = new List<string>();
|
---|
144 | XmlNode variablesNode = node.ChildNodes[1];
|
---|
145 | foreach (XmlNode variableNameNode in variablesNode.ChildNodes) {
|
---|
146 | variableNames.Add(variableNameNode.Attributes["Name"].Value);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | public static void Export(Predictor p, Stream s) {
|
---|
151 | StreamWriter writer = new StreamWriter(s);
|
---|
152 | writer.Write("Targetvariable: "); writer.WriteLine(p.targetVariable);
|
---|
153 | writer.Write("LowerPredictionLimit: "); writer.WriteLine(p.LowerPredictionLimit.ToString("r", CultureInfo.InvariantCulture.NumberFormat));
|
---|
154 | writer.Write("UpperPredictionLimit: "); writer.WriteLine(p.UpperPredictionLimit.ToString("r", CultureInfo.InvariantCulture.NumberFormat));
|
---|
155 | writer.Write("MaxTimeOffset: "); writer.WriteLine(p.MaxTimeOffset.ToString());
|
---|
156 | writer.Write("MinTimeOffset: "); writer.WriteLine(p.MinTimeOffset.ToString());
|
---|
157 | writer.Write("InputVariables :");
|
---|
158 | writer.Write(p.GetInputVariables().First());
|
---|
159 | foreach (string variable in p.GetInputVariables().Skip(1)) {
|
---|
160 | writer.Write("; "); writer.Write(variable);
|
---|
161 | }
|
---|
162 | writer.WriteLine();
|
---|
163 | writer.Flush();
|
---|
164 | using (MemoryStream memStream = new MemoryStream()) {
|
---|
165 | SVMModel.Export(p.Model, memStream);
|
---|
166 | memStream.WriteTo(s);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static Predictor Import(TextReader reader) {
|
---|
171 | string[] targetVariableLine = reader.ReadLine().Split(':');
|
---|
172 | string[] lowerPredictionLimitLine = reader.ReadLine().Split(':');
|
---|
173 | string[] upperPredictionLimitLine = reader.ReadLine().Split(':');
|
---|
174 | string[] maxTimeOffsetLine = reader.ReadLine().Split(':');
|
---|
175 | string[] minTimeOffsetLine = reader.ReadLine().Split(':');
|
---|
176 | string[] inputVariableLine = reader.ReadLine().Split(':', ';');
|
---|
177 |
|
---|
178 | string targetVariable = targetVariableLine[1].Trim();
|
---|
179 | double lowerPredictionLimit = double.Parse(lowerPredictionLimitLine[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
180 | double upperPredictionLimit = double.Parse(upperPredictionLimitLine[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
181 | int maxTimeOffset = int.Parse(maxTimeOffsetLine[1]);
|
---|
182 | int minTimeOffset = int.Parse(minTimeOffsetLine[1]);
|
---|
183 | List<string> variableNames = new List<string>();
|
---|
184 | foreach (string inputVariable in inputVariableLine.Skip(1)) {
|
---|
185 | variableNames.Add(inputVariable.Trim());
|
---|
186 | }
|
---|
187 | SVMModel model = SVMModel.Import(reader);
|
---|
188 | Predictor p = new Predictor(model, targetVariable, variableNames, minTimeOffset, maxTimeOffset);
|
---|
189 | p.UpperPredictionLimit = upperPredictionLimit;
|
---|
190 | p.LowerPredictionLimit = lowerPredictionLimit;
|
---|
191 | return p;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|