Projekt

Obecné

Profil

Stáhnout (3.63 KB) Statistiky
| Větev: | Tag: | Revize:
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.Connection.XMLProtocolHandler;
10
using ServerApp.Parser.OutputInfo;
11
using ServerApp.Parser.Parsers;
12

    
13
namespace ServerApp.Predictor
14
{
15
    /// <summary>
16
    /// Implentation of the <c>IPredicitionController</c> interface.
17
    /// </summary>
18
    class PredictionController : IPredictionController
19
    {
20
        /// <summary>
21
        /// A dictionary for storing trained predictors.
22
        /// </summary>
23
        private Dictionary<string, int> buildingsToAreas;
24

    
25
        private List<IPredictor> predictors;
26

    
27
        /// <summary>
28
        /// A reference to a data parser.
29
        /// </summary>
30
        private IDataParser dataParser;
31

    
32
        /// <summary>
33
        /// A feature extractor instance.
34
        /// </summary>
35
        private FeatureExtractor featureExtractor;
36

    
37
        /// <summary>
38
        /// Instantiates new prediction controller.
39
        /// </summary>
40
        /// <param name="dataParser">A data parser used to get training data.</param>
41
        public PredictionController(IDataParser dataParser)
42
        {
43
            this.dataParser = dataParser;
44
            this.predictors = new List<IPredictor>();
45
            this.buildingsToAreas = new Dictionary<string, int>();
46
            this.featureExtractor = new FeatureExtractor(dataParser, buildingsToAreas);
47

    
48
            // fill predictors with all available locationKeys
49
            // TODO Currently all locations use the same predictor. Try dividing locations into subareas with separate predictors.
50
            var locationKeys = TagInfo.buildings;
51
            foreach (string key in locationKeys)
52
            {
53
                buildingsToAreas.Add(key, 0);
54
            }
55
            IPredictor predictor = new NaiveBayesClassifier();
56
            predictors.Add(predictor);
57
        }
58
        public List<string> GetPredictors()
59
        {
60
            return new List<string>(buildingsToAreas.Keys);
61
        }
62

    
63
        public void Load(string locationKey = null, string path = null)
64
        {
65
            if (locationKey is null)
66
            {
67
                throw new NotImplementedException();
68
            }
69
            else
70
            {
71
                throw new NotImplementedException();
72
            }
73
        }
74

    
75
        public Response Predict(Request request)
76
        {
77
            throw new NotImplementedException();
78
        }
79

    
80

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

    
97
                    Console.WriteLine("Evaluating predictor with {0} samples.", testData.Count);
98
                    this.predictors[i].Evaluate(testData);
99
                }
100
            } else
101
            // train specified predictor only
102
            {
103
                throw new NotImplementedException();
104
            }
105
        }
106

    
107

    
108
    }
109
}
(7-7/7)