-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonteCarloSpace.cc
165 lines (133 loc) · 4.22 KB
/
MonteCarloSpace.cc
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
// Copyright (c) 2021 Chanjung Kim. All rights reserved.
// Licensed under the MIT License.
#include <leth/MonteCarloSpace.hh>
#include <limits>
#include <random>
#include <vector>
char const* MonteCarloSpace::GetName() noexcept
{
return "Monte Carlo";
}
char const* MonteCarloSpace::GetErrorMessage(ErrorCode errorCode) noexcept
{
return GetErrorMessageInternal(static_cast<ErrorType>(errorCode));
}
char const* MonteCarloSpace::GetErrorMessageInternal(ErrorType errorCode) noexcept
{
switch (errorCode)
{
case ErrorType::Success: return "Success";
case ErrorType::InvalidEquation: return "Insufficient boundary condition";
}
return "Unknown error";
}
ErrorCode MonteCarloSpace::RunSimulation(Point const* input, float* output) noexcept
{
return static_cast<ErrorCode>(RunSimulationInternal(input, output));
}
MonteCarloSpace::ErrorType MonteCarloSpace::RunSimulationInternal(Point const* input,
float* output) noexcept
{
for (uint16_t i { 0 }, iEnd { height() }; i < iEnd; ++i)
{
for (uint16_t j { 0 }, jEnd { width() }; j < jEnd; ++j)
{
if (!CheckSanity(input, i, j))
return ErrorType::InvalidEquation;
}
}
for (uint16_t i { 0 }, iEnd { height() }; i < iEnd; ++i)
{
for (uint16_t j { 0 }, jEnd { width() }; j < jEnd; ++j)
{
float sum { 0 };
for (int repeat { 0 }; repeat < 1000; ++repeat) sum += DoMonteCarlo(input, i, j);
output[GetIndex(i, j)] = sum / 1000;
}
}
return ErrorType::Success;
}
bool MonteCarloSpace::CheckSanity(Point const* input, uint16_t i, uint16_t j) noexcept
{
int16_t x { static_cast<int16_t>(j) }, y { static_cast<int16_t>(i) };
if (!Inside(y, x))
return true;
auto idx { GetIndex(y, x) };
if (input[idx].type != PointType::GroundTruth)
return true;
constexpr int16_t offsets[4][2] {
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
};
_sanityCheckVisitMap.resize(static_cast<size_t>(width()) * height());
std::fill(_sanityCheckVisitMap.begin(), _sanityCheckVisitMap.end(), false);
_sanityCheckVisitMap[idx] = true;
std::vector<Pos> stack;
stack.push_back(Pos { static_cast<uint16_t>(x), static_cast<uint16_t>(y) });
while (!stack.empty())
{
Pos back { stack.back() };
stack.pop_back();
if (!Inside(back.y, back.x))
continue;
auto idx { GetIndex(back.y, back.x) };
if (input[idx].type == PointType::Boundary)
return true;
if (input[idx].type == PointType::OutOfRange)
continue;
for (auto& offset : offsets)
{
if (!Inside(back.y + offset[0], back.x + offset[1]))
continue;
auto newIdx { GetIndex(back.y + offset[0], back.x + offset[1]) };
if (_sanityCheckVisitMap[newIdx])
continue;
_sanityCheckVisitMap[newIdx] = true;
stack.push_back(Pos {
static_cast<uint16_t>(back.x + offset[1]),
static_cast<uint16_t>(back.y + offset[0]),
});
}
}
return false;
}
float MonteCarloSpace::DoMonteCarlo(Point const* input, uint16_t i, uint16_t j) noexcept
{
std::uniform_int_distribution<int> dist { 0, 3 };
int16_t beforeX, beforeY;
int16_t x { static_cast<int16_t>(j) }, y { static_cast<int16_t>(i) };
if (!Inside(y, x))
return 0.0f;
auto idx { GetIndex(y, x) };
if (input[idx].type == PointType::OutOfRange)
return 0.0f;
while (true)
{
if (!Inside(y, x))
{
x = beforeX;
y = beforeY;
continue;
}
auto idx { GetIndex(y, x) };
if (input[idx].type == PointType::Boundary)
return input[idx].temp;
if (input[idx].type == PointType::OutOfRange)
{
x = beforeX;
y = beforeY;
continue;
}
beforeX = x;
beforeY = y;
switch (dist(_eng))
{
case 0: --y; break;
case 1: ++x; break;
case 2: ++y; break;
case 3: --x; break;
}
}
}