1
|
//
|
2
|
// Author: Roman Kalivoda
|
3
|
//
|
4
|
|
5
|
using System;
|
6
|
using System.Collections.Generic;
|
7
|
using System.IO;
|
8
|
using Microsoft.ML;
|
9
|
using ServerApp.Parser.OutputInfo;
|
10
|
using ServerApp.Parser.Parsers;
|
11
|
|
12
|
namespace ServerApp.Predictor
|
13
|
{
|
14
|
/// <summary>
|
15
|
/// Implentation of the <c>IPredicitionController</c> interface.
|
16
|
/// </summary>
|
17
|
class PredictionController : IPredictionController
|
18
|
{
|
19
|
/// <summary>
|
20
|
/// A dictionary for storing trained predictors.
|
21
|
/// </summary>
|
22
|
private Dictionary<string, int> buildingsToAreas;
|
23
|
|
24
|
private List<IPredictor> predictors;
|
25
|
|
26
|
/// <summary>
|
27
|
/// A reference to a data parser.
|
28
|
/// </summary>
|
29
|
private IDataParser dataParser;
|
30
|
|
31
|
/// <summary>
|
32
|
/// A feature extractor instance.
|
33
|
/// </summary>
|
34
|
private FeatureExtractor featureExtractor;
|
35
|
|
36
|
/// <summary>
|
37
|
/// Instantiates new prediction controller.
|
38
|
/// </summary>
|
39
|
/// <param name="dataParser">A data parser used to get training data.</param>
|
40
|
public PredictionController(IDataParser dataParser)
|
41
|
{
|
42
|
this.dataParser = dataParser;
|
43
|
this.predictors = new List<IPredictor>();
|
44
|
this.buildingsToAreas = new Dictionary<string, int>();
|
45
|
this.featureExtractor = new FeatureExtractor(dataParser, buildingsToAreas);
|
46
|
|
47
|
// fill predictors with all available locationKeys
|
48
|
// TODO Currently all locations use the same predictor. Try dividing locations into subareas with separate predictors.
|
49
|
var locationKeys = TagInfo.buildings;
|
50
|
foreach (string key in locationKeys)
|
51
|
{
|
52
|
buildingsToAreas.Add(key, 0);
|
53
|
}
|
54
|
IPredictor predictor = new NaiveBayesClassifier();
|
55
|
predictors.Add(predictor);
|
56
|
}
|
57
|
public List<string> GetPredictors()
|
58
|
{
|
59
|
return new List<string>(buildingsToAreas.Keys);
|
60
|
}
|
61
|
|
62
|
public void Load(string locationKey = null, string path = null)
|
63
|
{
|
64
|
if (locationKey is null)
|
65
|
{
|
66
|
throw new NotImplementedException();
|
67
|
}
|
68
|
else
|
69
|
{
|
70
|
throw new NotImplementedException();
|
71
|
}
|
72
|
}
|
73
|
|
74
|
public IDataView Predict(string locationKey, WeatherInfo weather, DateTime dateTime)
|
75
|
{
|
76
|
IEnumerable<ModelInput> data = new List<ModelInput>
|
77
|
{
|
78
|
new ModelInput
|
79
|
{
|
80
|
Temp = (float)weather.temp,
|
81
|
}
|
82
|
};
|
83
|
return this.predictors[buildingsToAreas[locationKey]].Predict(data);
|
84
|
}
|
85
|
|
86
|
|
87
|
public void Train(string locationKey = null)
|
88
|
{
|
89
|
if (locationKey is null)
|
90
|
// train all predictors
|
91
|
{
|
92
|
// TODO A single predictor is used for all areas, so training is done only once now.
|
93
|
for (int i = 0; i < this.predictors.Count; i++)
|
94
|
{
|
95
|
// TODO change datetimes when parser interface is ready to parse only downloaded data.
|
96
|
//IEnumerable<ModelInput> data = featureExtractor.PrepareModelInput(i, DateTime.MinValue, DateTime.MaxValue);
|
97
|
IEnumerable<ModelInput> data = featureExtractor.PrepareModelInput(i, new DateTime(2019, 10, 5), new DateTime(2020, 6, 30));
|
98
|
this.predictors[i].Fit(data);
|
99
|
}
|
100
|
} else
|
101
|
// train specified predictor only
|
102
|
{
|
103
|
throw new NotImplementedException();
|
104
|
}
|
105
|
}
|
106
|
|
107
|
|
108
|
}
|
109
|
}
|