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 log4net;
|
13
|
using System.IO;
|
14
|
using System.Text.RegularExpressions;
|
15
|
using System.Linq;
|
16
|
|
17
|
namespace ServerApp.Predictor
|
18
|
{
|
19
|
/// <summary>
|
20
|
/// Implentation of the <c>IPredicitionController</c> interface.
|
21
|
/// </summary>
|
22
|
public class PredictionController : IPredictionController
|
23
|
{
|
24
|
private static readonly ILog _log = LogManager.GetLogger(typeof(PredictionController));
|
25
|
|
26
|
/// <summary>
|
27
|
/// ID of the current predictor instances.
|
28
|
/// </summary>
|
29
|
private string PredictorID;
|
30
|
|
31
|
/// <summary>
|
32
|
/// Configuration of the <c>Predictor</c>
|
33
|
/// </summary>
|
34
|
public PredictorConfiguration Configuration { get; set; }
|
35
|
|
36
|
/// <summary>
|
37
|
/// Names of the files used to train the current predictor instances.
|
38
|
/// </summary>
|
39
|
private IEnumerable<string> DataFilenames;
|
40
|
|
41
|
/// <summary>
|
42
|
/// Current predictor instances
|
43
|
/// </summary>
|
44
|
private IPredictor[] Predictors;
|
45
|
|
46
|
/// <summary>
|
47
|
/// A reference to a data parser.
|
48
|
/// </summary>
|
49
|
private IDataParser DataParser;
|
50
|
|
51
|
/// <summary>
|
52
|
/// A feature extractor instance.
|
53
|
/// </summary>
|
54
|
private FeatureExtractor FeatureExtractor;
|
55
|
|
56
|
/// <summary>
|
57
|
/// A weather prediction parser service
|
58
|
/// </summary>
|
59
|
private IJsonParser weatherService;
|
60
|
|
61
|
/// <summary>
|
62
|
/// Instantiates new prediction controller.
|
63
|
/// </summary>
|
64
|
/// <param name="dataParser">A data parser used to get training data.</param>
|
65
|
public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null)
|
66
|
{
|
67
|
// TODO look for existing predictors
|
68
|
_log.Info("Constructing a new PredictionController instance.");
|
69
|
this.weatherService = weatherService;
|
70
|
// load config or get the default one
|
71
|
if (pathToConfig is null)
|
72
|
{
|
73
|
pathToConfig = PredictorConfiguration.DEFAULT_CONFIG_PATH;
|
74
|
}
|
75
|
try
|
76
|
{
|
77
|
string json = File.ReadAllText(pathToConfig);
|
78
|
this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json);
|
79
|
}
|
80
|
catch (System.IO.FileNotFoundException e)
|
81
|
{
|
82
|
Console.WriteLine("Warning: could not find a configuration file, creating a new one:");
|
83
|
Console.WriteLine(e.Message.PadLeft(4));
|
84
|
this.Configuration = PredictorConfiguration.GetDefaultConfig();
|
85
|
}
|
86
|
|
87
|
this.DataParser = dataParser;
|
88
|
this.Predictors = new IPredictor[this.Configuration.PredictorCount];
|
89
|
this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration);
|
90
|
|
91
|
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
|
92
|
FileInfo[] files = di.GetFiles();
|
93
|
if (Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Any())
|
94
|
{
|
95
|
_log.Info("Found existing predictors, loading the newest.");
|
96
|
this.Load(Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Last().Select(f => f.Name.Split("_".ToCharArray())[0]).First());
|
97
|
}
|
98
|
else
|
99
|
{
|
100
|
_log.Info("No predictors found, creating new ones");
|
101
|
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
102
|
{
|
103
|
Predictors[i] = new NaiveBayesClassifier();
|
104
|
}
|
105
|
}
|
106
|
PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration);
|
107
|
}
|
108
|
public List<string> GetPredictors()
|
109
|
{
|
110
|
return new List<string>(this.Configuration.BuildingsToAreas.Keys);
|
111
|
}
|
112
|
|
113
|
public int Load(string predictorID)
|
114
|
{
|
115
|
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
|
116
|
FileInfo[] files = di.GetFiles($"{predictorID}_*.zip");
|
117
|
if (Array.FindAll(files, f => Regex.IsMatch(f.Name, $@"{predictorID}_\d+.zip")).Any()){
|
118
|
IPredictor[] newPredictors = new IPredictor[this.Configuration.PredictorCount];
|
119
|
try
|
120
|
{
|
121
|
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
122
|
{
|
123
|
newPredictors[i] = new NaiveBayesClassifier(Array.Find(files, f => Regex.IsMatch(f.Name, $"{predictorID}_{i}.zip")).FullName);
|
124
|
}
|
125
|
this.Predictors = newPredictors;
|
126
|
files = di.GetFiles($"{predictorID}.txt");
|
127
|
this.DataFilenames = File.ReadLines(files[0].FullName);
|
128
|
this.PredictorID = predictorID;
|
129
|
} catch (FileNotFoundException e)
|
130
|
{
|
131
|
_log.Error(e.ToString());
|
132
|
return 2;
|
133
|
}
|
134
|
} else
|
135
|
{
|
136
|
// TODO indicate exception
|
137
|
_log.Debug("Could not find predictor with given predictorID");
|
138
|
return 1;
|
139
|
}
|
140
|
return 0;
|
141
|
}
|
142
|
|
143
|
public void Save()
|
144
|
{
|
145
|
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
|
146
|
|
147
|
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
148
|
{
|
149
|
Predictors[i].Save(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip"));
|
150
|
}
|
151
|
File.WriteAllLinesAsync(Path.Combine(di.FullName, $"{PredictorID}.txt"), this.DataFilenames);
|
152
|
}
|
153
|
|
154
|
public int Rollback()
|
155
|
{
|
156
|
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
|
157
|
FileInfo[] files = di.GetFiles();
|
158
|
if (Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Any())
|
159
|
{
|
160
|
string RollbackedPredictorID = Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Last().Select(f => f.Name.Split("_".ToCharArray())[0]).First();
|
161
|
this.Delete(this.PredictorID);
|
162
|
return this.Load(RollbackedPredictorID);
|
163
|
} else
|
164
|
{
|
165
|
// indicate that older model does not exist
|
166
|
return 1;
|
167
|
}
|
168
|
}
|
169
|
|
170
|
private void Delete(string predictorID)
|
171
|
{
|
172
|
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
|
173
|
|
174
|
for (int i = 0; i < this.Configuration.PredictorCount; i++)
|
175
|
{
|
176
|
File.Delete(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip"));
|
177
|
}
|
178
|
File.Delete(Path.Combine(di.FullName, $"{PredictorID}.txt"));
|
179
|
}
|
180
|
|
181
|
public Response Predict(Request request)
|
182
|
{
|
183
|
_log.Info($"Received a prediction request: endDate={request.useEndDate}, weather={request.useWeather}");
|
184
|
DateTime start = new DateTime(year: request.start.year, month: request.start.month, day: request.start.day, hour: request.start.hour, minute: 0, second: 0);
|
185
|
List<Prediction> predictions = new List<Prediction>();
|
186
|
if (request.useEndDate)
|
187
|
{
|
188
|
DateTime end = new DateTime(year: request.end.year, month: request.end.month, day: request.end.day, hour: request.end.hour, minute: 0, second: 0);
|
189
|
DateTime current = start;
|
190
|
while (current < end)
|
191
|
{
|
192
|
_log.Debug($"Predicting for date {current.Date.ToShortDateString()}");
|
193
|
while (current.Hour < Date.MAX_HOUR)
|
194
|
{
|
195
|
_log.Debug($"Predicting for time {current.TimeOfDay.ToString()}");
|
196
|
var prediction = PredictSingle(request, current);
|
197
|
predictions.Add(prediction);
|
198
|
current = current.AddHours(this.Configuration.TimeResolution);
|
199
|
}
|
200
|
current = current.AddHours(23 - current.Hour + Date.MIN_HOUR);
|
201
|
}
|
202
|
}
|
203
|
else
|
204
|
{
|
205
|
_log.Debug("Predicting for single DateTime.");
|
206
|
predictions.Add(PredictSingle(request, start));
|
207
|
}
|
208
|
var response = new Response();
|
209
|
response.hoursPerSegment = Configuration.TimeResolution;
|
210
|
response.predicitons = predictions.ToArray();
|
211
|
_log.Debug($"Created a response.");
|
212
|
return response;
|
213
|
}
|
214
|
|
215
|
private Prediction PredictSingle(Request request, DateTime predictionTime)
|
216
|
{
|
217
|
double[] predictedValues = new double[this.Configuration.BuildingsToAreas.Count];
|
218
|
string[] predictedLabels = new string[this.Predictors.Length];
|
219
|
for (int i = 0; i < this.Predictors.Length; i++)
|
220
|
{
|
221
|
if (request.useWeather)
|
222
|
{
|
223
|
_log.Debug("Predicting for requested weather.");
|
224
|
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
|
225
|
{
|
226
|
Rain = (float)request.rain,
|
227
|
Temp = (float)request.temperature,
|
228
|
Wind = (float)request.wind,
|
229
|
Hour = predictionTime.Hour,
|
230
|
Time = predictionTime
|
231
|
});
|
232
|
}
|
233
|
else
|
234
|
{
|
235
|
_log.Debug("Retrieving weather info from the weather service.");
|
236
|
weatherService.ParsePrediction();
|
237
|
WeatherInfo weatherInfo = weatherService.Predictions.Find(info => info.startTime.Date.Equals(predictionTime.Date) && predictionTime.TimeOfDay.Subtract(info.startTime.TimeOfDay).Hours < info.intervalLength);
|
238
|
if (weatherInfo is null)
|
239
|
{
|
240
|
predictedLabels[i] = null;
|
241
|
}
|
242
|
else
|
243
|
{
|
244
|
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
|
245
|
{
|
246
|
Rain = weatherInfo.rain,
|
247
|
Temp = (float)weatherInfo.temp,
|
248
|
Wind = (float)weatherInfo.wind,
|
249
|
Hour = predictionTime.Hour,
|
250
|
Time = predictionTime
|
251
|
});
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
for (int i = 0; i < predictedValues.Length; i++)
|
256
|
{
|
257
|
predictedValues[i] = this.FeatureExtractor.LabelToRatio(predictedLabels[this.Configuration.BuildingsToAreas[TagInfo.buildings[i]]]);
|
258
|
}
|
259
|
|
260
|
Prediction prediction = new Prediction();
|
261
|
prediction.dateTime = new Date
|
262
|
{
|
263
|
year = predictionTime.Year,
|
264
|
month = predictionTime.Month,
|
265
|
day = predictionTime.Day,
|
266
|
hour = predictionTime.Hour
|
267
|
};
|
268
|
prediction.predictions = predictedValues;
|
269
|
_log.Debug($"Created prediction for DateTime: {prediction.dateTime}");
|
270
|
return prediction;
|
271
|
}
|
272
|
|
273
|
public void Train()
|
274
|
{
|
275
|
DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false);
|
276
|
for (int i = 0; i < this.Predictors.Length; i++)
|
277
|
{
|
278
|
// train on all available data
|
279
|
List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i);
|
280
|
Console.WriteLine("Training predictor with {0} samples.", data.Count);
|
281
|
this.Predictors[i].Fit(data);
|
282
|
}
|
283
|
this.DataFilenames = this.DataParser.WeatherDataUsed.Concat(this.DataParser.ActivityDataUsed);
|
284
|
this.PredictorID = DateTime.Now.ToBinary().ToString();
|
285
|
this.Save();
|
286
|
}
|
287
|
|
288
|
public IEnumerable<string> GetDataFileNames()
|
289
|
{
|
290
|
return this.DataFilenames;
|
291
|
}
|
292
|
}
|
293
|
}
|