-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKrigingSerial.cpp
More file actions
182 lines (139 loc) · 5.75 KB
/
KrigingSerial.cpp
File metadata and controls
182 lines (139 loc) · 5.75 KB
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
#include "KrigingSerial.h"
#include "KrigingCommon.h"
#include "Timer.h"
#include <iostream>
#include <cmath>
#include <numeric>
using namespace std;
void Serialkriging::SerialKrigFit(const PointVector &InputPoints, int NumberOfPoints, int LagsCount)
{
this->NumberOfPoints = NumberOfPoints;
auto MinMaxXPair = minmax_element(InputPoints.begin(), InputPoints.end(), [](const PointXYZ& Point1, const PointXYZ& Point2)
{
return Point1.x < Point2.x;
});
auto MinMaxYPair = minmax_element(InputPoints.begin(), InputPoints.end(), [](const PointXYZ& Point1, const PointXYZ& Point2)
{
return Point1.y < Point2.y;
});
auto MinMaxZPair = minmax_element(InputPoints.begin(), InputPoints.end(), [](const PointXYZ& Point1, const PointXYZ& Point2)
{
return Point1.z < Point2.z;
});
MinPoint.x = (*MinMaxXPair.first).x;
MinPoint.y = (*MinMaxYPair.first).y;
MinPoint.z = (*MinMaxZPair.first).z;
MaxPoint.x = (*MinMaxXPair.second).x;
MaxPoint.y = (*MinMaxYPair.second).y;
MaxPoint.z = (*MinMaxZPair.second).z;
cout << "MinPoint: " << MinPoint << endl;
cout << "MaxPoint: " << MaxPoint << endl;
const float Cutoff = Dist(MaxPoint.x, MaxPoint.y, MinPoint.x, MinPoint.y) / 3.0f;
auto LagRanges = GetLagRanges(Cutoff, LagsCount);
Eigen::MatrixXf DistancesMatrix(NumberOfPoints, NumberOfPoints);
for(int i = 0; i < NumberOfPoints; i++)
{
const auto& PointI = InputPoints[i];
for(int j = 0; j < NumberOfPoints; ++j)
{
const auto& PointJ = InputPoints[j];
DistancesMatrix(i, j) = Dist(PointI.x, PointI.y, PointJ.x, PointJ.y);
}
}
cout << "Computing Semivariogram ... " << flush;
vector<float> EmpiricalSemivariogramX;
vector<float> EmpiricalSemivariogramY;
for (int LagIndex = 0; LagIndex < LagsCount; ++LagIndex)
{
const float RangeMin = LagRanges[LagIndex * 2 + 0];
const float RangeMax = LagRanges[LagIndex * 2 + 1];
vector<float> DistValues;
vector<float> SemivarValues;
for(int i = 0; i < NumberOfPoints; i++)
{
for(int j = 0; j < NumberOfPoints; ++j)
{
auto DistIJ = DistancesMatrix(i, j);
if(RangeMin < DistIJ && DistIJ < RangeMax)
{
const auto& PointIValue = InputPoints[i].z;
const auto& PointJValue = InputPoints[j].z;
auto SemivarValue = pow(PointIValue - PointJValue, 2);
DistValues.push_back(DistIJ);
SemivarValues.push_back(SemivarValue);
}
}
}
float AvgDistance = accumulate(DistValues.begin(), DistValues.end(), 0.0f);
float AvgSemivar = accumulate(SemivarValues.begin(), SemivarValues.end(), 0.0f);
AvgDistance /= DistValues.size();
AvgSemivar /= SemivarValues.size();
EmpiricalSemivariogramX.push_back(AvgDistance);
EmpiricalSemivariogramY.push_back(0.5f * AvgSemivar);
}
cout << "done" << endl;
cout << "Fitting Linear Model to Semivariogram ... " << flush;
auto LinearModel = LinearModelFit(EmpiricalSemivariogramX, EmpiricalSemivariogramY);
const float LinearModelA = LinearModel.first;
const float LinearModelB = LinearModel.second;
Nugget = LinearModelA;
Range = *max_element(EmpiricalSemivariogramX.begin(), EmpiricalSemivariogramX.end());
Sill = Nugget + LinearModelB * Range;
cout << "done" << endl;
cout << "Nugget: " << Nugget << endl;
cout << "Range : " << Range << endl;
cout << "Sill : " << Sill << endl;
cout << "Calculating Covariance Matrix ..." << flush;
Eigen::MatrixXf CovarianceMatrix(NumberOfPoints + 1, NumberOfPoints + 1);
CovarianceMatrix.fill(1.0f);
CovarianceMatrix(NumberOfPoints, NumberOfPoints) = 0.0f;
for(int i = 0; i < NumberOfPoints; i++)
{
for(int j = 0; j < NumberOfPoints; ++j)
{
auto DistIJ = DistancesMatrix(i, j);
CovarianceMatrix(i, j) = SphericalModel(DistIJ, Nugget, Range, Sill);
}
}
cout << "done" << endl;
cout << "Inverting Covariance Matrix ..." << flush;
InvCovMatrix = CovarianceMatrix.cast<double>();
InvCovMatrix = InvCovMatrix.inverse();
cout << "done" << endl;
}
PointVector Serialkriging::SerialKrigPred(const PointVector &InputPoints, int GridSize)
{
cout << "Predicting ... " << flush;
PointVector Grid(GridSize * GridSize);
float GridDeltaX = (MaxPoint.x - MinPoint.x) / GridSize;
float GridDeltaY = (MaxPoint.y - MinPoint.y) / GridSize;
Eigen::VectorXd ZValues(NumberOfPoints + 1);
Eigen::VectorXd RValues(NumberOfPoints + 1);
for(int i = 0; i < NumberOfPoints; ++i)
{
ZValues[i] = InputPoints[i].z;
}
ZValues[NumberOfPoints] = 1.0;
RValues[NumberOfPoints] = 1.0;
Eigen::VectorXd InvAXR(NumberOfPoints + 1);
for (int i = 0; i < GridSize; ++i)
{
cout << i << " " << flush;
for (int j = 0; j < GridSize; ++j)
{
float GridX = MinPoint.x + i * GridDeltaX;
float GridY = MinPoint.y + j * GridDeltaY;
for(int PIndex = 0; PIndex < NumberOfPoints; PIndex++)
{
const auto& Point = InputPoints[PIndex];
auto UDist = Dist(GridX, GridY, Point.x, Point.y);
RValues[PIndex] = SphericalModel(UDist, Nugget, Range, Sill);
}
InvAXR = InvCovMatrix * RValues;
double GridZ = InvAXR.dot(ZValues);
Grid[i + j * GridSize] = PointXYZ(GridX, GridY, GridZ);
}
}
cout << "done" << endl;
return Grid;
}