Revize ce0940b5
Přidáno uživatelem Roman Kalivoda před více než 3 roky(ů)
Server/ServerApp/Predictor/PredictionController.cs | ||
---|---|---|
4 | 4 |
|
5 | 5 |
using System; |
6 | 6 |
using System.Collections.Generic; |
7 |
using System.IO; |
|
8 |
using Microsoft.ML; |
|
9 | 7 |
using ServerApp.Connection.XMLProtocolHandler; |
8 |
using ServerApp.Parser.Parsers; |
|
9 |
using Newtonsoft.Json; |
|
10 |
using ServerApp.WeatherPredictionParser; |
|
10 | 11 |
using ServerApp.Parser.OutputInfo; |
11 | 12 |
using System.Reflection; |
12 | 13 |
using log4net; |
... | ... | |
16 | 17 |
/// <summary> |
17 | 18 |
/// Implentation of the <c>IPredicitionController</c> interface. |
18 | 19 |
/// </summary> |
19 |
class PredictionController : IPredictionController |
|
20 |
public class PredictionController : IPredictionController
|
|
20 | 21 |
{ |
21 | 22 |
private static readonly ILog _log = LogManager.GetLogger(typeof(PredictionController)); |
22 | 23 |
|
23 | 24 |
/// <summary> |
24 |
/// A dictionary for storing trained predictors.
|
|
25 |
/// Configuration of the <c>Predictor</c>
|
|
25 | 26 |
/// </summary> |
26 |
private Dictionary<string, int> buildingsToAreas;
|
|
27 |
private PredictorConfiguration Configuration;
|
|
27 | 28 |
|
28 |
private List<IPredictor> predictors;
|
|
29 |
private List<IPredictor> Predictors;
|
|
29 | 30 |
|
30 | 31 |
/// <summary> |
31 | 32 |
/// A reference to a data parser. |
32 | 33 |
/// </summary> |
33 |
private IDataParser dataParser;
|
|
34 |
private IDataParser DataParser;
|
|
34 | 35 |
|
35 | 36 |
/// <summary> |
36 | 37 |
/// A feature extractor instance. |
37 | 38 |
/// </summary> |
38 |
private FeatureExtractor featureExtractor; |
|
39 |
private FeatureExtractor FeatureExtractor; |
|
40 |
|
|
41 |
/// <summary> |
|
42 |
/// A weather prediction parser service |
|
43 |
/// </summary> |
|
44 |
private IJsonParser weatherService; |
|
39 | 45 |
|
40 | 46 |
/// <summary> |
41 | 47 |
/// Instantiates new prediction controller. |
42 | 48 |
/// </summary> |
43 | 49 |
/// <param name="dataParser">A data parser used to get training data.</param> |
44 |
public PredictionController(IDataParser dataParser)
|
|
50 |
public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null)
|
|
45 | 51 |
{ |
46 | 52 |
_log.Info("Constructing a new PredictionController instance."); |
47 | 53 |
this.weatherService = weatherService; |
... | ... | |
68 | 74 |
|
69 | 75 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
70 | 76 |
{ |
71 |
buildingsToAreas.Add(key, 0);
|
|
77 |
Predictors.Add(new NaiveBayesClassifier());
|
|
72 | 78 |
} |
73 |
IPredictor predictor = new NaiveBayesClassifier(); |
|
74 |
predictors.Add(predictor); |
|
79 |
PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration); |
|
75 | 80 |
} |
76 | 81 |
public List<string> GetPredictors() |
77 | 82 |
{ |
78 |
return new List<string>(buildingsToAreas.Keys);
|
|
83 |
return new List<string>(this.Configuration.BuildingsToAreas.Keys);
|
|
79 | 84 |
} |
80 | 85 |
|
81 | 86 |
public void Load(string locationKey = null, string path = null) |
... | ... | |
180 | 185 |
if (locationKey is null) |
181 | 186 |
// train all predictors |
182 | 187 |
{ |
183 |
// TODO A single predictor is used for all areas, so training is done only once now.
|
|
184 |
for (int i = 0; i < this.predictors.Count; i++)
|
|
188 |
DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false);
|
|
189 |
for (int i = 0; i < this.Predictors.Count; i++)
|
|
185 | 190 |
{ |
186 | 191 |
// train on all available data |
187 |
// TODO the train/test split is used just temporarily for demonstration. |
|
188 |
List<ModelInput> data = featureExtractor.PrepareTrainingInput(i, DateTime.MinValue, DateTime.MaxValue); |
|
189 |
List<ModelInput> trainingData = data.GetRange(index: 0, count: 500); |
|
190 |
List<ModelInput> testData = data.GetRange(index: 500, count: 94); |
|
191 |
Console.WriteLine("Training predictor with {0} samples.", trainingData.Count); |
|
192 |
this.predictors[i].Fit(trainingData); |
|
193 |
|
|
194 |
Console.WriteLine("Evaluating predictor with {0} samples.", testData.Count); |
|
195 |
this.predictors[i].Evaluate(testData); |
|
192 |
List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i); |
|
193 |
Console.WriteLine("Training predictor with {0} samples.", data.Count); |
|
194 |
this.Predictors[i].Fit(data); |
|
196 | 195 |
} |
197 |
} else |
|
196 |
} |
|
197 |
else |
|
198 | 198 |
// train specified predictor only |
199 | 199 |
{ |
200 | 200 |
throw new NotImplementedException(); |
Také k dispozici: Unified diff
Re #9034 fixing files corrupted by merge