Projekt

Obecné

Profil

Stáhnout (14.2 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
            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))).Count() == Configuration.PredictorCount)
104
            {
105
                _log.Info("Found existing predictors, loading the newest.");
106
                var predictorID = 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();
107
                this.Load(predictorID);
108
            }
109
            else
110
            {
111
                _log.Info("No predictors found, creating new ones");
112
                for (int i = 0; i < this.Configuration.PredictorCount; i++)
113
                {
114
                    predictors[i] = new SdcaMaximumEntropyClassifier();
115
                }
116
                lock (predictorsLock)
117
                {
118
                    this.Predictors = predictors;
119
                }
120
            }
121
            
122
            PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration);
123
        }
124
        public List<string> GetPredictors()
125
        {
126
            return new List<string>(this.Configuration.BuildingsToAreas.Keys);
127
        }
128

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

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

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

    
186
        public int Rollback()
187
        {
188
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
189
            if (!di.Exists)
190
            {
191
                _log.Warn("The model data directory could not be found.");
192
                return 2;
193
            }
194
            FileInfo[] files = di.GetFiles();
195
            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))).Count() == Configuration.PredictorCount)
196
            {
197
                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();
198
                this.Delete(this.PredictorID);
199
                return this.Load(RollbackedPredictorID);
200
            } else
201
            {
202
                // indicate that older model does not exist
203
                return 1;
204
            }
205
        }
206

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

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

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

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

    
308
            Prediction prediction = new Prediction();
309
            prediction.dateTime = new Date
310
            {
311
                year = predictionTime.Year,
312
                month = predictionTime.Month,
313
                day = predictionTime.Day,
314
                hour = predictionTime.Hour
315
            };
316
            prediction.predictions = predictedValues;
317
            _log.Debug($"Created prediction for DateTime: {prediction.dateTime}");
318
            return prediction;
319
        }
320

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

    
342
        public IEnumerable<string> GetDataFileNames()
343
        {
344
            return this.DataFilenames;
345
        }
346
    }
347
}
(9-9/11)