-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
302 lines (270 loc) · 11.8 KB
/
Copy pathmain.cpp
File metadata and controls
302 lines (270 loc) · 11.8 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* IITA Algorithm Test Program (C++ version)
* ==========================================
*
* Run Iterative Integer Traffic Assignment (I-ITA) with specified K value.
*
* Usage:
* ./iita --data <data_directory> --k <K_value> --output <output_directory>
* ./iita -d chicago-regional -k 2 -o results_iita_k2
*
* Options:
* --no-adaptive-theta Disable OD-specific theta calibration (default: on)
* --no-psl Disable Path Size Logit correction (default: on)
* --overlap-threshold F Overlap filtering threshold (default: 0.9)
* --theta F Theta / scale factor (default: 2.0)
*/
#include "src/parser.h"
#include "src/network.h"
#include "src/integer_assignment.h"
#include "src/metrics.h"
#include "src/export.h"
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <string>
#include <thread>
namespace fs = std::filesystem;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
static constexpr double IITA_THETA = 2.0;
static constexpr int IITA_MAX_ITER = 100;
static constexpr double IITA_CONV = 1e-4;
static constexpr int SEED = 42;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
static void printSection(const std::string& title) {
std::cout << "\n" << std::string(65, '=') << "\n";
std::cout << " " << title << "\n";
std::cout << std::string(65, '=') << "\n";
}
static std::string fmtComma(long long v) {
std::string s = std::to_string(v);
int n = (int)s.size();
for (int i = n - 3; i > 0; i -= 3)
s.insert(i, ",");
return s;
}
// ---------------------------------------------------------------------------
// run_iita_test
// ---------------------------------------------------------------------------
static void runIITATest(
const std::string& data_dir,
const std::string& output_dir,
int k,
double theta = IITA_THETA,
bool adaptive_theta = true,
bool use_psl = true,
double overlap_thresh = 0.9)
{
int num_workers = std::max(1, (int)std::thread::hardware_concurrency());
printSection("IITA Algorithm Test");
std::cout << " Data directory : " << data_dir << "\n";
std::cout << " Output directory: " << output_dir << "\n";
std::cout << " K value : " << k << "\n";
std::cout << " Theta : " << theta
<< (adaptive_theta ? " (adaptive)" : " (fixed)") << "\n";
std::cout << " Path Size Logit : " << (use_psl ? "ON" : "OFF") << "\n";
std::cout << " Overlap thresh : " << overlap_thresh << "\n";
std::cout << " Max iterations : " << IITA_MAX_ITER << "\n";
std::cout << std::scientific << std::setprecision(0);
std::cout << " Convergence : " << IITA_CONV << "\n";
std::cout << std::defaultfloat;
std::cout << " Threads : " << num_workers << "\n";
fs::create_directories(output_dir);
// ---- Step 1: Load network ----
printSection("Step 1: Network Information");
Network net = loadChicagoData(data_dir, /*round_demand=*/true);
std::cout << " Zones : " << fmtComma(net.num_zones) << "\n";
std::cout << " Nodes : " << fmtComma(net.num_nodes) << "\n";
std::cout << " Links : " << fmtComma((long long)net.links.size()) << "\n";
std::cout << " Total OD demand (rounded): "
<< fmtComma((long long)net.getTotalDemand()) << " trips\n";
// ---- Step 1.5: Complexity analysis ----
printSection("Step 1.5: Algorithm Complexity Analysis");
ComplexityReport cr = computeComplexityReport(net, k);
std::cout << " OD pairs : " << fmtComma(cr.od_pairs) << "\n";
std::cout << " K (path trees) : " << cr.K << "\n";
std::cout << " Dijkstra : " << cr.dijkstra_complexity << "\n";
std::cout << " Path tracing : " << cr.tracing_complexity << "\n";
std::cout << " Sampling : " << cr.sampling_complexity << "\n";
std::cout << " Total/iter : " << cr.total_per_iteration << "\n";
std::cout << " vs FW : " << cr.fw_comparison << "\n";
// ---- Step 2: Run I-ITA ----
printSection("Step 2: Running I-ITA-K" + std::to_string(k));
ITAConfig cfg;
cfg.num_paths = k;
cfg.theta = theta;
cfg.seed = SEED;
cfg.num_workers = num_workers;
cfg.max_iter = IITA_MAX_ITER;
cfg.convergence = IITA_CONV;
cfg.verbose = true;
cfg.adaptive_theta = adaptive_theta;
cfg.use_psl = use_psl;
cfg.overlap_threshold= overlap_thresh;
cfg.record_paths = true;
ITAResult ita = iterativeITA(net, cfg);
// ---- Step 3: Statistics ----
printSection("Step 3: Computing Statistics");
NetworkStats stats = calculateNetworkStatistics(net, ita.flows);
std::cout << std::fixed << std::setprecision(1);
std::cout << " VKT (km) : " << stats.vkt << "\n";
std::cout << " VHT (h) : " << stats.vht << "\n";
std::cout << " TSTT (h) : " << stats.tstt << "\n";
std::cout << std::fixed << std::setprecision(2);
std::cout << " Avg Travel Time: " << stats.avg_travel_time << " min\n";
std::cout << " Avg Distance : " << stats.avg_distance << " km\n";
std::cout << std::fixed << std::setprecision(4);
std::cout << " Avg VoC : " << stats.avg_voc << "\n";
std::cout << " Max VoC : " << stats.max_voc << "\n";
std::cout << " Congested Links: " << stats.congested_links << "\n";
// ---- Step 4: Export ----
printSection("Step 4: Exporting Results");
std::string method_name = "IITA_K" + std::to_string(k);
// Summary CSV
MethodResult mr;
mr.name = method_name;
mr.elapsed_time = ita.elapsed_time;
mr.vkt = stats.vkt;
mr.vht = stats.vht;
mr.tstt = stats.tstt;
mr.total_demand = net.getTotalDemand();
mr.avg_travel_time = stats.avg_travel_time;
mr.avg_distance = stats.avg_distance;
mr.avg_voc = stats.avg_voc;
mr.max_voc = stats.max_voc;
mr.congested_links = stats.congested_links;
mr.converged = ita.converged;
mr.iterations = ita.iterations_done;
mr.final_gap = ita.final_gap;
// Link flows CSV
std::string link_file = output_dir + "/" + method_name + "_link_flows.csv";
exportLinkFlowsCSV(net, ita.flows, link_file);
std::cout << " Link flows : " << link_file << "\n";
std::string summary_file = output_dir + "/summary_iita.csv";
exportSummaryCSV({mr}, summary_file);
std::cout << " Summary CSV: " << summary_file << "\n";
// Path flows & turning volumes
if (!ita.path_records.empty()) {
long long total_path_flow = 0;
for (auto& r : ita.path_records) total_path_flow += r.flow;
std::string path_file = output_dir + "/" + method_name + "_paths.csv";
exportPathFlowsCSV(ita.path_records, path_file);
std::cout << " Path flows : " << path_file << "\n";
std::cout << " (" << fmtComma((long long)ita.path_records.size())
<< " records, " << fmtComma(total_path_flow) << " trips)\n";
auto turning_vols = computeTurningVolumes(ita.path_records);
std::string turn_file = output_dir + "/" + method_name + "_turning_volumes.csv";
exportTurningVolumesCSV(turning_vols, turn_file);
std::cout << " Turning vol: " << turn_file << "\n";
std::cout << " (" << fmtComma((long long)turning_vols.size())
<< " turning movements)\n";
}
printSection("Test Complete");
std::cout << std::fixed << std::setprecision(1);
std::cout << " I-ITA-K" << k << " finished in " << ita.elapsed_time << "s\n";
std::cout << " Results saved to: " << output_dir << "\n";
}
// ---------------------------------------------------------------------------
// Argument parsing
// ---------------------------------------------------------------------------
struct Args {
std::string data_dir;
std::string output_dir;
int k = -1;
double theta = IITA_THETA;
bool adaptive_theta = true;
bool use_psl = true;
double overlap_threshold= 0.9;
};
static void printUsage(const char* prog) {
std::cout <<
"Usage:\n"
" " << prog << " --data <dir> --k <K> --output <dir> [options]\n"
"\n"
"Required:\n"
" -d, --data <path> Input data directory (Chicago Regional TNTP)\n"
" -k <int> Number of alternative paths (e.g. 2, 3, 4)\n"
" -o, --output <path> Output directory for results\n"
"\n"
"Options:\n"
" --theta <float> Theta / scale factor (default: 2.0)\n"
" --no-adaptive-theta Use fixed theta (default: adaptive on)\n"
" --no-psl Disable Path Size Logit (default: on)\n"
" --overlap-threshold <f> Overlap filter threshold 0-1 (default: 0.9)\n"
"\n"
"Examples:\n"
" " << prog << " -d chicago-regional -k 2 -o results_iita_k2\n"
" " << prog << " -d ./my_network -k 3 -o ./results --no-psl\n";
}
static Args parseArgs(int argc, char* argv[]) {
Args a;
if (argc < 2) {
printUsage(argv[0]);
std::exit(1);
}
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "-d" || arg == "--data") && i + 1 < argc) {
a.data_dir = argv[++i];
} else if ((arg == "-o" || arg == "--output") && i + 1 < argc) {
a.output_dir = argv[++i];
} else if ((arg == "-k" || arg == "--k") && i + 1 < argc) {
a.k = std::stoi(argv[++i]);
} else if (arg == "--theta" && i + 1 < argc) {
a.theta = std::stod(argv[++i]);
} else if (arg == "--no-adaptive-theta") {
a.adaptive_theta = false;
} else if (arg == "--no-psl") {
a.use_psl = false;
} else if (arg == "--overlap-threshold" && i + 1 < argc) {
a.overlap_threshold = std::stod(argv[++i]);
} else if (arg == "-h" || arg == "--help") {
printUsage(argv[0]);
std::exit(0);
} else {
std::cerr << "Unknown argument: " << arg << "\n";
printUsage(argv[0]);
std::exit(1);
}
}
return a;
}
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
Args args = parseArgs(argc, argv);
if (args.data_dir.empty() || args.output_dir.empty() || args.k < 1) {
std::cerr << "Error: --data, --k, and --output are required.\n";
printUsage(argv[0]);
return 1;
}
// Resolve paths relative to executable directory if not absolute
auto resolvePath = [&](const std::string& p) -> std::string {
fs::path fp(p);
if (fp.is_absolute()) return p;
// Relative to cwd
return fs::absolute(fp).string();
};
std::string data_dir = resolvePath(args.data_dir);
std::string output_dir = resolvePath(args.output_dir);
if (!fs::exists(data_dir)) {
std::cerr << "Error: Data directory not found: " << data_dir << "\n";
return 1;
}
try {
runIITATest(data_dir, output_dir, args.k,
args.theta, args.adaptive_theta,
args.use_psl, args.overlap_threshold);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
return 0;
}