-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file.cpp
More file actions
242 lines (206 loc) · 6.88 KB
/
read_file.cpp
File metadata and controls
242 lines (206 loc) · 6.88 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*****************************************************************************
* @file read_file.cpp *
* @brief 文件读取类 *
* @details 用于读取路网信息、路阻信息和 OD 数据 *
* @author Dong Yu *
* @email 213191838@seu.edu.cn *
* @version 0.1.1 *
* @date 2022/10/28 *
* *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2022/10/28 | 0.1 | Dong Yu | Copy from User-Equilibrium v0.9 *
*----------------------------------------------------------------------------*
* 2022/10/28 | 0.1.1 | Dong Yu | Modified a little *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#include "stdafx.h"
#include "read_file.h"
/**
* @brief 用于对line进行格式化
* @param line 字符串line的引用
*/
void RemoveSpacese(string& line) {
//line.erase(0, line.find_first_not_of(" "));
line.erase(line.find_last_not_of(" ") + 1);
//line.erase(0, line.find_first_not_of(" "));
if (line.find_last_not_of(" ") < line.find_last_of(" "))
line.erase(line.find_last_not_of(" ") + 1);
}
/**
* @brief 从文件file中读取一行数据
* @param file 文件
* @param line 字符串line的引用,用于储存读取的数据
*/
void ReadLine(ifstream& file, string& line) {
getline(file, line);
RemoveSpacese(line);
}
/**
* @brief 分割MetaData的字符串为head和数值两部分
* @param line 待分割的字符串
* @param head 用于存储head部分
* @param num 用于存储数值部分
*/
void SplitMetaData(const string& line, string& head, int& num) {
int data = 0, pos, len;
len = line.length();
pos = line.find_last_of(" ");
head = line.substr(0, pos);
num = stoi(line.substr(pos + 1));
}
/**
* @brief 提取line中的数据
* @param line 待提取的字符串
* @return buffer 提取后的数据
*/
map<string, double> GetBuffer(string line) {
map<string, double> buffer;
while (line.find(" ") != string::npos)
line.erase(line.find(" "), 1);
while (line.find(" ") != string::npos)
line.erase(line.find(" "), 1);
int pos1 = line.find(":", 0), pos2 = -1;
do {
buffer[line.substr(pos2 + 1, pos1 - pos2 - 1)] = stod(line.substr(pos1 + 1, line.find(";", pos2 + 1) - pos1 - 1));
pos2 = line.find(";", pos2 + 1);
pos1 = line.find(":", pos1 + 1);
} while (line.find(":", pos1) != string::npos);
return buffer;
}
/**
* @brief 提取line中的数据,将line中信息转化为网络数据并进行储存
* @param line 待提取的字符串
*/
void ReadFile::set_network(const string& line) {
int pos_last = line.find(" ", 0), pos = line.find(" ", pos_last + 1);
string ori, des;
vector<string> item = {"init_node", "term_node", "capacity", "length", "free_flow_time", "b", "power", "speed", "toll", "link_type"};
ori = line.substr(pos_last + 1, pos - pos_last - 1);
pos_last = line.find(" ", pos_last + 1), pos = line.find(" ", pos_last + 1);
des = line.substr(pos_last + 1, pos - pos_last - 1);
this->all_nodes.insert(ori);
this->all_nodes.insert(des);
this->next_nodes[ori].insert(des);
for (int i = 2; i < 10; i++) {
pos_last = line.find(" ", pos_last + 1), pos = line.find(" ", pos_last + 1);
this->cost_parm[ori][des][item[i]] = stod(line.substr(pos_last + 1, pos - pos_last - 1));
}
}
void ReadFile::CheckData(const int& zones, const int& nodes, const int& links, const int& first_thru_node, const double& total_flow) {
if (this->od_matrix.size() != zones)
ExitMessage("Wrong zones!");
if (this->all_nodes.size() != nodes)
ExitMessage("Wrong nodes!");
int _links = 0;
for (auto i : this->next_nodes)
_links += i.second.size();
if (_links != links)
ExitMessage("Wrong links!");
double _flow = 0.0;
for (auto i : this->od_matrix)
for (auto j : i.second)
_flow += j.second;
if ((int)_flow != (int)total_flow)
ExitMessage("Wrong total flow!");
// first_thru_node ??
}
ReadFile::ReadFile(const string& network, const string& od) {
StatusMessage("读取文件: ");
ifstream network_file;
ifstream od_file;
network_file.open(network);
od_file.open(od);
string line = "";
string head;
int num;
int zones = 0, nodes = 0, links = 0, first_thru_node = 0;
double total_flow = 0.0;
// 读取meta数据
StatusMessageB("读取meta数据");
ReadLine(network_file, line);
while (line.find("<END OF METADATA>") == -1) {
if (line.find("~") != string::npos) {
ReadLine(network_file, line);
continue;
}
else {
SplitMetaData(line, head, num);
if (head == "<NUMBER OF ZONES>")
zones = num;
else if (head == "<NUMBER OF NODES>")
nodes = num;
else if (head == "<FIRST THRU NODE>")
first_thru_node = num;
else if (head == "<NUMBER OF LINKS>")
links = num;
ReadLine(network_file, line);
}
}
ReadLine(od_file, line);
while (line.find("<END OF METADATA>") == -1) {
if (line.find("~") != string::npos) {
ReadLine(od_file, line);
continue;
}
else {
SplitMetaData(line, head, num);
if (head == "<NUMBER OF ZONES>") {
if (zones != num)
ExitMessage("Wrong in file!");
}
else if (head == "<TOTAL OD FLOW>") {
total_flow = num;
}
ReadLine(od_file, line);
}
}
if (zones * nodes * links * first_thru_node * total_flow == 0)
ExitMessage("Wrong matadata input!");
StatusMessageA();
// 读取网络数据
StatusMessageB("读取网络数据");
while (!network_file.eof()) {
ReadLine(network_file, line);
if (line.empty() || line.find("~") != string::npos)
continue;
set_network(line);
}
StatusMessageA();
// 读取OD数据
StatusMessageB("读取OD数据");
int origin = 0;
map<string, double> buffer;
while (!od_file.eof()) {
ReadLine(od_file, line);
if (line.empty())
continue;
if (line.find("Origin") != string::npos) {
origin += 1;
continue;
}
buffer = GetBuffer(line);
for (auto des : buffer)
this->od_matrix[to_string(origin)][des.first] = des.second;
}
StatusMessageA();
network_file.close();
od_file.close();
CheckData(zones, nodes, links, first_thru_node, total_flow);
StatusMessage("文件读取成功!");
}
set<string> ReadFile::get_all_nodes() {
return this->all_nodes;
}
map<string, set<string>> ReadFile::get_next_nodes() {
return this->next_nodes;
}
map<string, map<string, double>> ReadFile::get_od_matrix() {
return this->od_matrix;
}
map<string, map<string, map<string, double>>> ReadFile::get_cost_parm() {
return this->cost_parm;
}