-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.cpp
188 lines (138 loc) · 3.98 KB
/
input.cpp
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
#include "input.h"
#include <fstream>
#include <cstdlib>
//#include <iomanip>
using namespace std;
Input::Input(const std::string &fileName, bool utm)
{
if (utm)
loadUTM(fileName);
else
load(fileName);
}
void Input::load(const std::string &fileName)
{
m_path = fileName;
std::ifstream ifinput(fileName);
if (!ifinput.is_open())
throw Exception("can't open input file");
double lat, lon;
std::string s;
while (getline(ifinput, s))
{
size_t posL;
size_t posR = s.find(IO_DELIM);
// ignoring id
posL = posR+1;
posR = s.find(IO_DELIM, posL);
lat = atof(s.substr(posL, posR-posL).c_str());
posL = posR+1;
lon = atof(s.substr(posL, s.length()-posL).c_str());
m_nodes.push_back(mmatch::toUTM(lat, lon));
}
}
void Input::loadUTM(const std::string &fileName)
{
m_path = fileName;
std::ifstream ifinput(fileName);
if (!ifinput.is_open())
throw Exception("can't open input file");
double lat, lon;
std::string s;
while (getline(ifinput, s))
{
size_t posL;
size_t posR = s.find(IO_DELIM);
// ignoring id
posL = posR+1;
posR = s.find(IO_DELIM, posL);
lat = atof(s.substr(posL, posR-posL).c_str());
posL = posR+1;
lon = atof(s.substr(posL, s.length()-posL).c_str());
m_nodes.push_back(UTMNode(lat, lon));
}
}
vector<Input> Input::split(size_t parts) const
{
vector<Input> result;
size_t step = m_nodes.size() / parts;
size_t i = 0;
while (i + step < m_nodes.size())
{
result.push_back(Input(vector<UTMNode>(m_nodes.begin() + i, m_nodes.begin() + i + step)));
i += step;
}
if (i < m_nodes.size())
result.push_back(Input(vector<UTMNode>(begin(m_nodes) + i, end(m_nodes))));
return result;
}
Input Input::merge(const vector<Input> &result) const
{
vector<UTMNode> res;
for (const Input &input : result)
res.insert(res.end(), input.m_nodes.begin(), input.m_nodes.end());
return Input(res);
}
Input::Input(const std::vector<UTMNode> &nodes):
m_nodes(nodes)
{
}
void Output::load(const std::string &fileName)
{
std::ifstream ifoutput(fileName);
if (!ifoutput.is_open())
throw Exception("can't open input file");
m_estmns.clear();
Estimate estmn;
std::string s;
while (getline(ifoutput, s))
{
size_t posL;
size_t posR = s.find(IO_DELIM);
// ignoring id
posL = posR+1;
posR = s.find(IO_DELIM, posL);
estmn.edge = atoi(s.substr(posL, posR-posL).c_str());
posL = posR+1;
estmn.confidence = atof(s.substr(posL, s.length()-posL).c_str());
m_estmns.push_back(estmn);
}
}
void Output::save(const std::string &fileName) const
{
std::ofstream ofoutput(fileName);
if (!ofoutput.is_open())
throw Exception("can't open file for output");
ofoutput.setf(std::ios_base::floatfield, std::ios_base::fixed);
ofoutput.precision(2);
for (int i = 0; i < m_estmns.size(); i++)
ofoutput << i << IO_DELIM << m_estmns[i].edge << IO_DELIM << m_estmns[i].confidence << '\n';
}
double Output::evaluate(const Output &according) const
{
if (estimates().size() != according.estimates().size())
throw Exception("sizes must be equal to evaluate");
double result = 0;
for (size_t i = 0; i < estimates().size(); ++i)
{
if (edge(i) == according.edge(i))
result += confidence(i);
}
return result / estimates().size();
}
Output Output::merge(const vector<Output> &result) const
{
vector<Estimate> res;
double error = 0;
for (const Output &output : result)
{
error = max(error, output.maxError());
res.insert(res.end(), output.m_estmns.begin(), output.m_estmns.end());
}
return Output(res, error);
}
Output::Output(std::vector<Estimate> &estimates, double maxError):
m_estmns(estimates),
m_maxError(maxError)
{
}