Projekt

Obecné

Profil

Stáhnout (14.3 KB) Statistiky
| Větev: | Tag: | Revize:
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 mutual exclusive lock to pro
48
        /// </summary>
49
        private readonly object predictorsLock = new Object();
50

    
51
        /// <summary>
52
        /// A reference to a data parser.
53
        /// </summary>
54
        private IDataParser DataParser;
55

    
56
        /// <summary>
57
        /// A feature extractor instance.
58
        /// </summary>
59
        private FeatureExtractor FeatureExtractor;
60

    
61
        /// <summary>
62
        /// A weather prediction parser service
63
        /// </summary>
64
        private IJsonParser weatherService;
65

    
66
        /// <summary>
67
        /// Instantiates new prediction controller.
68
        /// </summary>
69
        /// <param name="dataParser">A data parser used to get training data.</param>
70
        public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null)
71
        {
72
            _log.Info("Constructing a new PredictionController instance.");
73
            this.weatherService = weatherService;
74
            // load config or get the default one
75
            if (pathToConfig is null)
76
            {
77
                pathToConfig = PredictorConfiguration.DEFAULT_CONFIG_PATH;
78
            }
79
            try
80
            {
81
                string json = File.ReadAllText(pathToConfig);
82
                this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json);
83
            }
84
            catch (System.IO.FileNotFoundException e)
85
            {
86
                Console.WriteLine("Warning: could not find a configuration file, creating a new one:");
87
                Console.WriteLine(e.Message.PadLeft(4));
88
                this.Configuration = PredictorConfiguration.GetDefaultConfig();
89
            }
90

    
91
            this.DataParser = dataParser;
92
            var predictors = new IPredictor[this.Configuration.PredictorCount];
93
            this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration);
94

    
95
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
96
            _log.Debug($"Looking for directory: {Configuration.ModelDataPath}");
97
            if (! di.Exists)
98
            {
99
                _log.Info($"Creating the model data directory: {Configuration.ModelDataPath}");
100
                di.Create();
101
            }
102
            FileInfo[] files = di.GetFiles();
103
            var orderedEnumerable = 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)));
104
            if (orderedEnumerable.Where(g => g.Count() == Configuration.PredictorCount).Any())
105
            {
106
                _log.Info("Found existing predictors, loading the newest.");
107
                var predictorID = orderedEnumerable.Last().Select(f => f.Name.Split("_".ToCharArray())[0]).First();
108
                this.Load(predictorID);
109
            }
110
            else
111
            {
112
                _log.Info("No predictors found, creating new ones");
113
                for (int i = 0; i < this.Configuration.PredictorCount; i++)
114
                {
115
                    predictors[i] = new SdcaMaximumEntropyClassifier();
116
                }
117
                lock (predictorsLock)
118
                {
119
                    this.Predictors = predictors;
120
                }
121
            }
122
            
123
            PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration);
124
        }
125
        public List<string> GetPredictors()
126
        {
127
            return new List<string>(this.Configuration.BuildingsToAreas.Keys);
128
        }
129

    
130
        public int Load(string predictorID)
131
        {
132
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); 
133
            if (!di.Exists)
134
            {
135
                _log.Warn("The model data directory could not be found.");
136
                return 2;
137
            }
138
            FileInfo[] files = di.GetFiles($"{predictorID}_*.zip");
139
            if (Array.FindAll(files, f => Regex.IsMatch(f.Name, $@"{predictorID}_\d+.zip")).Any()){
140
                IPredictor[] newPredictors = new IPredictor[this.Configuration.PredictorCount];
141
                try
142
                {
143
                    for (int i = 0; i < this.Configuration.PredictorCount; i++)
144
                    {
145
                        newPredictors[i] = new SdcaMaximumEntropyClassifier(Array.Find(files, f => Regex.IsMatch(f.Name, $"{predictorID}_{i}.zip")).FullName);
146
                    }
147
                    files = di.GetFiles($"{predictorID}.txt");
148
                    var dataFilenames = File.ReadLines(files[0].FullName); 
149
                    lock (predictorsLock)
150
                    {
151
                        this.Predictors = newPredictors;
152
                        this.DataFilenames = dataFilenames;
153
                        this.PredictorID = predictorID;
154
                    }
155
                } catch (FileNotFoundException e)
156
                {
157
                    _log.Error(e.ToString());
158
                    return 2;
159
                }
160
            } else
161
            {
162
                _log.Debug("Could not find predictor with given predictorID");
163
                return 1;
164
            }
165
            return 0;
166
        }
167

    
168
        public void Save()
169
        {
170
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
171
            if (!di.Exists)
172
            {
173
                _log.Warn("The model data directory could not be found.");
174
                return;
175
            }
176

    
177
            lock (predictorsLock)
178
            {
179
                for (int i = 0; i < this.Configuration.PredictorCount; i++)
180
                {
181
                    Predictors[i].Save(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip"));
182
                }
183
                File.WriteAllLinesAsync(Path.Combine(di.FullName, $"{PredictorID}.txt"), this.DataFilenames);
184
            }
185
        }
186

    
187
        public int Rollback()
188
        {
189
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
190
            if (!di.Exists)
191
            {
192
                _log.Warn("The model data directory could not be found.");
193
                return 2;
194
            }
195
            FileInfo[] files = di.GetFiles();
196
            var orderedEnumerable = 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))).Where(f => ! f.Key.Equals(this.PredictorID));
197
            if (orderedEnumerable.Where(g => g.Count() == Configuration.PredictorCount).Any())
198
            {
199
                string RollbackedPredictorID = orderedEnumerable.Last().Select(f => f.Name.Split("_".ToCharArray())[0]).First();
200
                this.Delete(this.PredictorID);
201
                return this.Load(RollbackedPredictorID);
202
            } else
203
            {
204
                // indicate that older model does not exist
205
                return 1;
206
            }
207
        }
208

    
209
        private void Delete(string predictorID)
210
        {
211
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
212
            if (!di.Exists)
213
            {
214
                _log.Warn("The model data directory could not be found.");
215
                return;
216
            }
217

    
218
            for (int i = 0; i < this.Configuration.PredictorCount; i++)
219
            {
220
                File.Delete(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip"));
221
            }
222
            File.Delete(Path.Combine(di.FullName, $"{PredictorID}.txt"));
223
        }
224

    
225
        public Response Predict(Request request)
226
        {
227
            _log.Info($"Received a prediction request: endDate={request.useEndDate}, weather={request.useWeather}");
228
            DateTime start = new DateTime(year: request.start.year, month: request.start.month, day: request.start.day, hour: request.start.hour, minute: 0, second: 0);
229
            List<Prediction> predictions = new List<Prediction>();
230
            if (request.useEndDate)
231
            {
232
                DateTime end = new DateTime(year: request.end.year, month: request.end.month, day: request.end.day, hour: request.end.hour, minute: 0, second: 0);
233
                DateTime current = start;
234
                while (current <= end)
235
                {
236
                    _log.Debug($"Predicting for date {current.Date.ToShortDateString()}");
237
                    while (current.Hour <= Date.MAX_HOUR)
238
                    {
239
                        if (current.Date == end.Date && current.Hour > end.Hour)
240
                        {
241
                            break;  // break if the end date was reached and hour is after the end time.
242
                        }
243
                        _log.Debug($"Predicting for time {current.TimeOfDay.ToString()}");
244
                        var prediction = PredictSingle(request, current);
245
                        predictions.Add(prediction);
246
                        current = current.AddHours(this.Configuration.TimeResolution);
247
                    }
248
                    current = current.AddHours(23 - current.Hour + Date.MIN_HOUR);
249
                }
250
            }
251
            else
252
            {
253
                _log.Debug("Predicting for single DateTime.");
254
                predictions.Add(PredictSingle(request, start));
255
            }
256
            var response = new Response();
257
            response.hoursPerSegment = Configuration.TimeResolution;
258
            response.predicitons = predictions.ToArray();
259
            _log.Debug($"Created a response.");
260
            return response;
261
        }
262

    
263
        private Prediction PredictSingle(Request request, DateTime predictionTime)
264
        {
265
            double[] predictedValues = new double[this.Configuration.BuildingsToAreas.Count];
266
            string[] predictedLabels = new string[this.Predictors.Length];
267
            for (int i = 0; i < this.Predictors.Length; i++)
268
            {
269
                if (request.useWeather)
270
                {
271
                    _log.Debug("Predicting for requested weather.");
272
                    lock (predictorsLock)
273
                    {
274
                        predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
275
                        {
276
                            Rain = (float)request.rain,
277
                            Temp = (float)request.temperature,
278
                            Wind = (float)request.wind,
279
                            Hour = predictionTime.Hour,
280
                            Time = predictionTime
281
                        });
282
                    }
283
                }
284
                else
285
                {
286
                    _log.Debug("Retrieving weather info from the weather service.");
287
                    weatherService.ParsePrediction();
288
                    WeatherInfo weatherInfo = weatherService.Predictions.Find(info => info.startTime.Date.Equals(predictionTime.Date) && predictionTime.TimeOfDay.Subtract(info.startTime.TimeOfDay).Hours < info.intervalLength);
289
                    if (weatherInfo is null)
290
                    {
291
                        predictedLabels[i] = null;
292
                    }
293
                    else
294
                    {
295
                        lock (predictorsLock)
296
                        {
297
                            predictedLabels[i] = this.Predictors[i].Predict(new ModelInput
298
                            {
299
                                Rain = weatherInfo.rain,
300
                                Temp = (float)weatherInfo.temp,
301
                                Wind = (float)weatherInfo.wind,
302
                                Hour = predictionTime.Hour,
303
                                Time = predictionTime
304
                            });
305
                        }
306
                    }
307
                }
308
            }
309
            for (int i = 0; i < predictedValues.Length; i++)
310
            {
311
                predictedValues[i] = this.FeatureExtractor.LabelToRatio(predictedLabels[this.Configuration.BuildingsToAreas[TagInfo.buildings[i]]]);
312
            }
313

    
314
            Prediction prediction = new Prediction();
315
            prediction.dateTime = new Date
316
            {
317
                year = predictionTime.Year,
318
                month = predictionTime.Month,
319
                day = predictionTime.Day,
320
                hour = predictionTime.Hour
321
            };
322
            prediction.predictions = predictedValues;
323
            _log.Debug($"Created prediction for DateTime: {prediction.dateTime}");
324
            return prediction;
325
        }
326

    
327
        public void Train()
328
        {
329
            DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false);
330
            for (int i = 0; i < this.Configuration.PredictorCount; i++)
331
            {
332
                // train on all available data
333
                List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i);
334
                Console.WriteLine("Training predictor with {0} samples.", data.Count);
335
                lock (predictorsLock)
336
                {
337
                    this.Predictors[i].Fit(data);
338
                }
339
            }
340
            lock (predictorsLock)
341
            {
342
                this.DataFilenames = this.DataParser.WeatherDataUsed.Concat(this.DataParser.ActivityDataUsed);
343
                this.PredictorID = DateTime.Now.ToBinary().ToString();
344
            }
345
            this.Save();
346
        }
347

    
348
        public IEnumerable<string> GetDataFileNames()
349
        {
350
            return this.DataFilenames;
351
        }
352
    }
353
}
(9-9/11)