RegressorOperator.h 4.03 KB
Newer Older
Alessio Netti's avatar
Alessio Netti committed
1
//================================================================================
2
// Name        : RegressorOperator.h
Alessio Netti's avatar
Alessio Netti committed
3
// Author      : Alessio Netti
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
Alessio Netti's avatar
Alessio Netti committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright   : Leibniz Supercomputing Centre
// Description :
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2019-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

28
29
#ifndef PROJECT_REGRESSOROPERATOR_H
#define PROJECT_REGRESSOROPERATOR_H
Alessio Netti's avatar
Alessio Netti committed
30
31


32
#include "../../includes/OperatorTemplate.h"
Alessio Netti's avatar
Alessio Netti committed
33
34
35
36
37
#include "RegressorSensorBase.h"
#include "opencv4/opencv2/core/mat.hpp"
#include "opencv4/opencv2/core/cvstd.hpp"
#include "opencv4/opencv2/ml.hpp"
#include <math.h>
38
#include <random>
Alessio Netti's avatar
Alessio Netti committed
39

Alessio Netti's avatar
Alessio Netti committed
40
#define REG_NUMFEATURES 6
Alessio Netti's avatar
Alessio Netti committed
41
42

/**
43
 * @brief Regressor operator plugin.
Alessio Netti's avatar
Alessio Netti committed
44
45
46
 *
 * @ingroup regressor
 */
47
class RegressorOperator : virtual public OperatorTemplate<RegressorSensorBase> {
Alessio Netti's avatar
Alessio Netti committed
48
49
50

public:
    
51
52
    RegressorOperator(const std::string& name);
    RegressorOperator(const RegressorOperator& other);
Alessio Netti's avatar
Alessio Netti committed
53
    
54
    virtual ~RegressorOperator();
Alessio Netti's avatar
Alessio Netti committed
55
56
57

    virtual restResponse_t REST(const string& action, const unordered_map<string, string>& queries) override;

58
    virtual void execOnInit() override;
Alessio Netti's avatar
Alessio Netti committed
59
60
61
62
63
64

    void setInputPath(std::string in)                    { _modelIn = in; }
    void setOutputPath(std::string out)                  { _modelOut = out; }
    void setAggregationWindow(unsigned long long a)      { _aggregationWindow = a; }
    void setTrainingSamples(unsigned long long t)        { _trainingSamples = t; }
    void setTargetDistance(unsigned long long d)         { _targetDistance = d;  }
65
    void setComputeImportances(bool i)                   { _importances = i; }
Alessio Netti's avatar
Alessio Netti committed
66
67
68
69
70
71
    void triggerTraining()                               { _trainingPending = true;}

    std::string getInputPath()                           { return _modelIn;}
    std::string getOutputPath()                          { return _modelOut; }
    unsigned long long getAggregationWindow()            { return _aggregationWindow; }
    unsigned long long getTrainingSamples()              { return _trainingSamples; }
72
    bool getComputeImportances()                         { return _importances; }
Alessio Netti's avatar
Alessio Netti committed
73

74
    virtual void printConfig(LOG_LEVEL ll) override;
Alessio Netti's avatar
Alessio Netti committed
75
76
77
78
79

protected:

    virtual void compute(U_Ptr unit)	 override;
    void computeFeatureVector(U_Ptr unit);
80
    void trainRandomForest(bool categorical=false);
81
    void shuffleTrainingSet();
82
    std::string getImportances();
Alessio Netti's avatar
Alessio Netti committed
83
84
85
86
87
88
89
    
    std::string _modelOut;
    std::string _modelIn;
    unsigned long long _aggregationWindow;
    unsigned long long _trainingSamples;
    unsigned long long _targetDistance;
    bool _trainingPending;
90
    bool _importances;
Alessio Netti's avatar
Alessio Netti committed
91
    bool _includeTarget;
Alessio Netti's avatar
Alessio Netti committed
92

93
    vector<reading_t> _buffer;
Alessio Netti's avatar
Alessio Netti committed
94
95
96
97
98
99
100
101
102
103
104
105
    cv::Ptr<cv::ml::RTrees> _rForest;
    cv::Mat *_trainingSet;
    cv::Mat *_responseSet;
    cv::Mat *_currentfVector;
    float _currentTarget;

    // Misc buffers
    int64_t _mean;
    int64_t _std;
    int64_t _diffsum;
    int64_t _qtl25;
    int64_t _qtl75;
106
    int64_t _latest;
107
108
109
110
111
112

    // Helper struct to store importance pairs
    struct ImportancePair {
        std::string name;
        float value;
    };
Alessio Netti's avatar
Alessio Netti committed
113
114
115
116

};


117
#endif //PROJECT_REGRESSOROPERATOR_H