-
Notifications
You must be signed in to change notification settings - Fork 0
/
regressionmodels.h
194 lines (175 loc) · 5.37 KB
/
regressionmodels.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef REGRESSIONMODELS_H
#define REGRESSIONMODELS_H
#include <vector>
#include "functions.h"
#include "taskdata.h"
#include <Eigen/Dense>
/// struct for interacting with solver
struct WorkingSet
{
WorkingSet(){};
WorkingSet(size_t taskSize, size_t nParams)
: J(Eigen::MatrixXd::Zero(taskSize, nParams))
, yMinusF(taskSize)
{
}
/// Task jacobi matrix \f$ J \f$. See (1)
Eigen::MatrixXd J;
/// \f$ y-f \f$ in terms of (1). Where \f$ Y=dQ/dTt = Q_{ij}/t_{ij} \f$
Eigen::VectorXd yMinusF;
};
struct OptimizedHoleData
{
std::vector<double> sumT;
std::vector<double> qDivT;
};
struct OptimizedTaskData
{
std::vector<OptimizedHoleData> holes;
OptimizedTaskData (const TaskData& taskData)
{
holes.resize(taskData.holes.size());
for(size_t i = 0; i< taskData.holes.size(); ++i)
{
OptimizedHoleData& hole = holes[i];
const HoleData& tHole = taskData.holes[i];
hole.sumT.resize(tHole.ts.size());
hole.qDivT.resize(tHole.ts.size());
for(size_t j = 0; j<tHole.ts.size(); ++j)
{
hole.qDivT[j] = tHole.qOils[j]/tHole.ts[j];
if(j==0)
hole.sumT[j] = tHole.ts[j]/2;
else
//We use half of time (ts) to get more precice Q derivative
hole.sumT[j] = hole.sumT[j-1] +tHole.ts[j-1]/2 + tHole.ts[j]/2;
}
}
};
};
class IRegressionModel
{
public:
virtual bool IsReady() const = 0;
virtual Eigen::VectorXd GenParams0Vec() = 0;
virtual WorkingSet InitWorkingSet() = 0;
virtual void CalcValue(const Eigen::VectorXd & params, WorkingSet & ws) = 0;
virtual size_t NormalizeParams(Eigen::VectorXd & params) = 0;
};
/// Regression model
/// \f$ ln q_i (t_j) = ln q_{0i} + ln(t_j)+\ksi_{ij} \f$
/// See also
/// (1) http://www.machinelearning.ru/wiki/index.php?title=Нелинейная_регрессия
template<class TFunc>
class RegressionModelLn: public IRegressionModel
{
private:
/// Input statistical data
TaskData _taskData;
/// Task data optimized for our purposes
OptimizedTaskData _oTD;
/// Task size: size of statistical data
const size_t _taskSize = 0;
const size_t _nQParams = 0;
const size_t _nFuncParams = 0;
const size_t _nParams = 0;
public:
RegressionModelLn() = delete;
RegressionModelLn(const TaskData& taskData)
//please be carefull with initialization order
: _taskData(taskData)
, _oTD(taskData)
, _taskSize(TaskDataHelper::GetTaskSize(taskData))
, _nQParams(taskData.holes.size())
, _nFuncParams(TFunc::nParams)
, _nParams(_nQParams + _nFuncParams)
{
}
bool IsReady() const
{
if(_taskData.holes.size()==0
&& _taskData.holes.size() == _oTD.holes.size()
)
return false;
return true;
}
Eigen::VectorXd GenParams0Vec()
{
Eigen::VectorXd params(_nParams);
for(size_t i = 0; i<_nQParams; ++i)
params[i] = _oTD.holes[i].qDivT[0];
for(size_t i = 0; i<_nFuncParams; ++i)
params[_taskData.holes.size() + i] = TFunc::GetDefaultParam(i);
return params;
}
WorkingSet InitWorkingSet()
{
return WorkingSet(_taskSize, _nParams);
}
void CalcValue(const Eigen::VectorXd& params, WorkingSet& ws)
{
//const Eigen::Map<const TFunc::VParams> funcParams(¶ms[_nQParams], _nFuncParams);
const Eigen::Map<const Eigen::VectorXd> funcParams(¶ms[_nQParams], _nFuncParams);
size_t it = 0;
for(size_t i = 0; i<_taskData.holes.size(); ++i)
{
const HoleData& holeData = _taskData.holes[i];
const OptimizedHoleData& optHoleData = _oTD.holes[i];
for(size_t j = 0; j<holeData.ts.size();++j)
{
// Find the way to minimize task size
ws.J(it, i) = 1.0/params[i];
const double tFromStart = optHoleData.sumT[j];
const double valFT = TFunc::CalcFT(funcParams, tFromStart);
for(size_t iParam = 0; iParam<_nFuncParams; ++iParam)
ws.J(it, _nQParams+iParam) = TFunc::CalcDFDIParam(iParam, funcParams, tFromStart)/valFT;
//ws.J(it, _nQParams+iParam) = TFunc::calcDFDIParamDivFT(iParam, funcParams, tFromStart);
const double qDivTVal = optHoleData.qDivT[j];
if(abs(qDivTVal) > 0)
//ws.yMinusF[it] = log(qDivTVal) - log(params[i]) - TFunc::calcLnFT(funcParams, tFromStart);
ws.yMinusF[it] = log(qDivTVal) - log(params[i]) - log(valFT);
else
{
// TODO: filter this line from input task data
ws.yMinusF[it] = 0.0;
for(size_t l = 0; l<_nParams; ++l)
ws.J(it, l) = 0.0;
}
++it;
}
}
}
size_t NormalizeParams(Eigen::VectorXd& params)
{
size_t nClip = 0;
for(size_t i = 0; i< _nQParams; ++i)
{
const double eps = 1e-16;
if(params[i] < eps)
{
params[i] = eps;
++nClip;
}
}
for(size_t i = 0; i< _nFuncParams; ++i)
{
if(params[i+_nQParams] < TFunc::GetParamLowerLimits(i))
{
params[i+_nQParams] = TFunc::GetParamLowerLimits(i);
++nClip;
}
if(params[i+_nQParams] > TFunc::GetParamUpperLimits(i))
{
params[i+_nQParams] = TFunc::GetParamUpperLimits(i);
++nClip;
}
}
return nClip;
}
friend class Tester;
};
typedef RegressionModelLn<Function1> RegressionModelLn1;
typedef RegressionModelLn<Function2> RegressionModelLn2;
typedef RegressionModelLn<Function3> RegressionModelLn3;
typedef RegressionModelLn<Function4> RegressionModelLn4;
#endif // REGRESSIONMODELS_H