1
|
//
|
2
|
// Author: Roman Kalivoda
|
3
|
//
|
4
|
|
5
|
using System;
|
6
|
using System.Collections.Generic;
|
7
|
using ServerApp.Connection.XMLProtocolHandler;
|
8
|
using ServerApp.Parser.Parsers;
|
9
|
using Newtonsoft.Json;
|
10
|
using ServerApp.WeatherPredictionParser;
|
11
|
using ServerApp.Parser.OutputInfo;
|
12
|
using System.Reflection;
|
13
|
using log4net;
|
14
|
|
15
|
namespace ServerApp.Predictor
|
16
|
{
|
17
|
/// <summary>
|
18
|
/// Implentation of the <c>IPredicitionController</c> interface.
|
19
|
/// </summary>
|
20
|
public class PredictionController : IPredictionController
|
21
|
{
|
22
|
private static readonly ILog _log = LogManager.GetLogger(typeof(PredictionController));
|
23
|
|
24
|
/// <summary>
|
25
|
/// Configuration of the <c>Predictor</c>
|
26
|
/// </summary>
|
27
|
private PredictorConfiguration Configuration;
|
28
|
|
29
|
private List<IPredictor> Predictors;
|
30
|
|
31
|
/// <summary>
|
32
|
/// A reference to a data parser.
|
33
|
/// </summary>
|
34
|
private IDataParser DataParser;
|
35
|
|
36
|
/// <summary>
|
37
|
/// A feature extractor instance.
|
38
|
/// </summary>
|
39
|
private FeatureExtractor FeatureExtractor;
|
40
|
|
41
|
/// <summary>
|
42
|
/// A weather prediction parser service
|
43
|
/// </summary>
|
44
|
private IJsonParser weatherService;
|
45
|
|
46
|
/// <summary>
|
47
|
/// Instantiates new prediction controller.
|
48
|
/// </summary>
|
49
|
/// <param name="dataParser">A data parser used to get training data.</param>
|
50
|
public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null)
|
51
|
{
|
52
|
_log.Info("Constructing a new PredictionController instance.");
|
53
|
this.weatherService = weatherService;
|
54
|
// load config or get the default one
|
55
|
if (pathToConfig is null)
|
56
|
{
|
57
|
pathToConfig = PredictorConfiguration.DEFAULT_CONFIG_PATH;
|
58
|
}
|
59
|
try
|
60
|
{
|
61
|
string json = System.IO.File.ReadAllText(pathToConfig);
|
62
|
this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json);
|
63
|
}
|
64
|
catch (System.IO.IOException e)
|
65
|
{
|
66
|
Console.WriteLine("Warning: could not find a configuration file, creating a new one:");
|
67
|
Console.WriteLine(e.Message.PadLeft(4));
|
68
|
this.Configuration = PredictorConfiguration.GetDefaultConfig();
|
69
|
}
|
70
|
|
71
|
this.DataParser = dataParser;
|
72
|
this.Predictors = new List<IPredictor>();
|
73
|
this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration);
|
74
|
|
75
|
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
76
|
{
|
77
|
Predictors.Add(new NaiveBayesClassifier());
|
78
|
}
|
79
|
PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration);
|
80
|
}
|
81
|
public List<string> GetPredictors()
|
82
|
{
|
83
|
return new List<string>(this.Configuration.BuildingsToAreas.Keys);
|
84
|
}
|
85
|
|
86
|
public void Load(string locationKey = null, string path = null)
|
87
|
{
|
88
|
if (locationKey is null)
|
89
|
{
|
90
|
throw new NotImplementedException();
|
91
|
}
|
92
|
else
|
93
|
{
|
94
|
throw new NotImplementedException();
|
95
|
}
|
96
|
}
|
97
|
|
98
|
public Response Predict(Request request)
|
99
|
{
|
100
|
_log.Info($"Received a prediction request: endDate={request.useEndDate}, weather={request.useWeather}");
|
101
|
DateTime start = new DateTime(year: request.start.year, month: request.start.month, day: request.start.day, hour: request.start.hour, minute: 0, second: 0);
|
102
|
List<Prediction> predictions = new List<Prediction>();
|
103
|
if (request.useEndDate)
|
104
|
{
|
105
|
DateTime end = new DateTime(year: request.end.year, month: request.end.month, day: request.end.day, hour: request.end.hour, minute: 0, second: 0);
|
106
|
DateTime current = start;
|
107
|
while (current < end)
|
108
|
{
|
109
|
_log.Debug($"Predicting for date {current.Date.ToShortDateString()}");
|
110
|
while (current.Hour < Date.MAX_HOUR)
|
111
|
{
|
112
|
_log.Debug($"Predicting for time {current.TimeOfDay.ToString()}");
|
113
|
var prediction = PredictSingle(request, current);
|
114
|
predictions.Add(prediction);
|
115
|
current = current.AddHours(this.Configuration.TimeResolution);
|
116
|
}
|
117
|
current = current.AddHours(23 - current.Hour + Date.MIN_HOUR);
|
118
|
}
|
119
|
}
|
120
|
else
|
121
|
{
|
122
|
_log.Debug("Predicting for single DateTime.");
|
123
|
predictions.Add(PredictSingle(request, start));
|
124
|
}
|
125
|
var response = new Response();
|
126
|
response.hoursPerSegment = Configuration.TimeResolution;
|
127
|
response.predicitons = predictions.ToArray();
|
128
|
_log.Debug($"Created a response.");
|
129
|
return response;
|
130
|
}
|
131
|
|
132
|
private Prediction PredictSingle(Request request, DateTime current)
|
133
|
{
|
134
|
double[] predictedValues = new double[this.Configuration.BuildingsToAreas.Count];
|
135
|
string[] predictedLabels = new string[this.Predictors.Count];
|
136
|
for (int i = 0; i < this.Predictors.Count; i++)
|
137
|
{
|
138
|
if (request.useWeather)
|
139
|
{
|
140
|
_log.Debug("Predicting for requested weather.");
|
141
|
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
|
142
|
{
|
143
|
Rain = (float)request.rain,
|
144
|
Temp = (float)request.temperature,
|
145
|
Wind = (float)request.wind,
|
146
|
Hour = current.Hour,
|
147
|
Time = current
|
148
|
});
|
149
|
}
|
150
|
else
|
151
|
{
|
152
|
_log.Debug("Retrieving weather info from the weather service.");
|
153
|
weatherService.ParsePrediction();
|
154
|
WeatherInfo weatherInfo = weatherService.Current;
|
155
|
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
|
156
|
{
|
157
|
Rain = weatherInfo.rain,
|
158
|
Temp = (float)weatherInfo.temp,
|
159
|
Wind = (float)weatherInfo.wind,
|
160
|
Hour = current.Hour,
|
161
|
Time = current
|
162
|
});
|
163
|
}
|
164
|
}
|
165
|
for (int i = 0; i < predictedValues.Length; i++)
|
166
|
{
|
167
|
predictedValues[i] = this.FeatureExtractor.LabelToRatio(predictedLabels[this.Configuration.BuildingsToAreas[TagInfo.buildings[i]]]) * 100;
|
168
|
}
|
169
|
|
170
|
Prediction prediction = new Prediction();
|
171
|
prediction.dateTime = new Date
|
172
|
{
|
173
|
year = current.Year,
|
174
|
month = current.Month,
|
175
|
day = current.Day,
|
176
|
hour = current.Hour
|
177
|
};
|
178
|
prediction.predictions = predictedValues;
|
179
|
_log.Debug($"Created prediction for DateTime: {prediction.dateTime}");
|
180
|
return prediction;
|
181
|
}
|
182
|
|
183
|
public void Train(string locationKey = null)
|
184
|
{
|
185
|
if (locationKey is null)
|
186
|
// train all predictors
|
187
|
{
|
188
|
DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false);
|
189
|
for (int i = 0; i < this.Predictors.Count; i++)
|
190
|
{
|
191
|
// train on all available data
|
192
|
List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i);
|
193
|
Console.WriteLine("Training predictor with {0} samples.", data.Count);
|
194
|
this.Predictors[i].Fit(data);
|
195
|
}
|
196
|
}
|
197
|
else
|
198
|
// train specified predictor only
|
199
|
{
|
200
|
throw new NotImplementedException();
|
201
|
}
|
202
|
}
|
203
|
|
204
|
|
205
|
}
|
206
|
}
|