Projekt

Obecné

Profil

« Předchozí | Další » 

Revize cdeee9f8

Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)

Re #8955 implemented multiple predictors support

Zobrazit rozdíly:

Server/ServerApp/Predictor/PredictionController.cs
6 6
using System.Collections.Generic;
7 7
using ServerApp.Connection.XMLProtocolHandler;
8 8
using ServerApp.Parser.Parsers;
9
using Newtonsoft.Json;
9 10

  
10 11
namespace ServerApp.Predictor
11 12
{
......
15 16
    class PredictionController : IPredictionController
16 17
    {
17 18
        /// <summary>
18
        /// A dictionary for storing trained predictors.
19
        /// Configuration of the <c>Predictor</c>
19 20
        /// </summary>
20
        private Dictionary<string, int> buildingsToAreas;
21
        private PredictorConfiguration Configuration;
21 22

  
22
        private List<IPredictor> predictors;
23
        private List<IPredictor> Predictors;
23 24

  
24 25
        /// <summary>
25 26
        /// A reference to a data parser.
26 27
        /// </summary>
27
        private IDataParser dataParser;
28
        private IDataParser DataParser;
28 29

  
29 30
        /// <summary>
30 31
        /// A feature extractor instance.
31 32
        /// </summary>
32
        private FeatureExtractor featureExtractor;
33
        private FeatureExtractor FeatureExtractor;
33 34

  
34 35
        /// <summary>
35 36
        /// Instantiates new prediction controller.
36 37
        /// </summary>
37 38
        /// <param name="dataParser">A data parser used to get training data.</param>
38
        public PredictionController(IDataParser dataParser)
39
        public PredictionController(IDataParser dataParser, string pathToConfig = null)
39 40
        {
40
            this.dataParser = dataParser;
41
            this.predictors = new List<IPredictor>();
42
            this.buildingsToAreas = new Dictionary<string, int>();
43
            this.featureExtractor = new FeatureExtractor(this.dataParser, buildingsToAreas);
41
            // load config or get the default one
42
            if (pathToConfig is null)
43
            {
44
                pathToConfig = PredictorConfiguration.DEFAULT_CONFIG_PATH;
45
            }
46
            try
47
            {
48
                string json = System.IO.File.ReadAllText(pathToConfig);
49
                this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json);
50
            } catch (System.IO.IOException e)
51
            {
52
                Console.WriteLine(e.ToString());
53
                this.Configuration = PredictorConfiguration.GetDefaultConfig();
54
            }
55

  
56
            this.DataParser = dataParser;
57
            this.Predictors = new List<IPredictor>();
58
            this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration);
44 59

  
45
            // fill predictors with all available locationKeys
46
            // TODO Currently all locations use the same predictor. Try dividing locations into subareas with separate predictors.
47
            var locationKeys = TagInfo.buildings;
48
            foreach (string key in locationKeys)
60
            for (int i = 0; i < this.Configuration.PredictorCount; i++)
49 61
            {
50
                buildingsToAreas.Add(key, 0);
62
                Predictors.Add(new NaiveBayesClassifier());
51 63
            }
52
            IPredictor predictor = new NaiveBayesClassifier();
53
            predictors.Add(predictor);
64
            PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration);
54 65
        }
55 66
        public List<string> GetPredictors()
56 67
        {
57
            return new List<string>(buildingsToAreas.Keys);
68
            return new List<string>(this.Configuration.BuildingsToAreas.Keys);
58 69
        }
59 70

  
60 71
        public void Load(string locationKey = null, string path = null)
......
81 92
            // train all predictors
82 93
            {
83 94
                // TODO A single predictor is used for all areas, so training is done only once now.
84
                for (int i = 0; i < this.predictors.Count; i++)
95
                for (int i = 0; i < this.Predictors.Count; i++)
85 96
                {
86 97
                    // train on all available data
87 98
                    // TODO the train/test split is used just temporarily for demonstration.
88
                    List<ModelInput> data = featureExtractor.PrepareTrainingInput(i, DateTime.MinValue, DateTime.MaxValue);
89
                    List<ModelInput> trainingData = data.GetRange(index: 0, count: 500);
90
                    List<ModelInput> testData = data.GetRange(index: 500, count: 94);
91
                    Console.WriteLine("Training predictor with {0} samples.", trainingData.Count);
92
                    this.predictors[i].Fit(trainingData);
93

  
94
                    Console.WriteLine("Evaluating predictor with {0} samples.", testData.Count);
95
                    this.predictors[i].Evaluate(testData);
99
                    List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i, DateTime.MinValue, DateTime.MaxValue);
100
                    Console.WriteLine("Training predictor with {0} samples.", data.Count);
101
                    this.Predictors[i].Fit(data);
96 102
                }
97 103
            }
98 104
            else

Také k dispozici: Unified diff