-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (147 loc) · 5.8 KB
/
main.cpp
File metadata and controls
181 lines (147 loc) · 5.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
#include "ILP_gurobi.h"
#include "ReadData.h"
#include "warm_starts.h"
#include <chrono>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <filesystem>
#include <numeric>
double print_path(const std::vector<std::vector<double>>& costs, const std::vector<double>& probability,
std::vector<int> rank, std::vector<int> best_path, double dwell_time) {
if (best_path.empty()) {
std::cout << "No valid path found." << std::endl;
return 0.0;
}
if (rank.empty()) {
rank.resize(best_path.size());
std::iota(rank.begin(), rank.end(), 1);
}
double sum_probability = 0.0;
std::cout << "Path(tile rank): ";
for (int tile : best_path) {
std::cout << tile << " ";
sum_probability += probability[tile];
}
std::cout << std::endl;
double total_cost = 0.0;
for (size_t i = 0; i < best_path.size() - 1; i++) {
// std::cout << best_path[i] << " " << best_path[i + 1]<< " " << costs[best_path[i]][best_path[i + 1]] << ", ";
total_cost += costs[best_path[i]][best_path[i + 1]];
}
std::cout << "Num Tiles in Path: " << best_path.size() << std::endl;
std::cout << "Sum Probability: " << sum_probability << std::endl;
std::cout << "Total Cost: " << total_cost << std::endl;
return sum_probability;
}
void write_result(const std::string& filename,
const std::string& method,
const std::string& data,
double budget, double slew_rate, double dwell_time,
const std::vector<std::vector<double>>& costs,
const std::vector<double>& probability,
std::vector<int> rank,
std::vector<int> best_path,
double elapsed_time, double padding) {
bool file_exists = std::filesystem::exists(filename);
std::ofstream outfile(filename, std::ios::app);
if (!outfile.is_open()) {
std::cerr << "Error: Could not open results file: " << filename << std::endl;
return;
}
//Write header
if (!file_exists) {
outfile << "Method,Dataset,Budget,SlewRate,DwellTime,NumTiles,SumProb,TotalCost,TimeSec,Path\n";
}
double sum_probability = 0.0;
double total_cost = -padding;
if (!best_path.empty()) {
for (size_t i = 0; i < best_path.size(); i++) {
sum_probability += probability[best_path[i]];
if (i < best_path.size() - 1) {
total_cost += costs[best_path[i]][best_path[i + 1]];
}
}
}
if (rank.empty()) {
rank.resize(best_path.size());
std::iota(rank.begin(), rank.end(), 1);
}
// Write result to CSV
outfile << method << ","
<< data << ","
<< budget << ","
<< slew_rate << ","
<< dwell_time << ","
<< best_path.size() << ","
<< sum_probability << ","
<< total_cost << ","
<< elapsed_time << ",";
// Write path
for (size_t i = 0; i < best_path.size(); i++) {
outfile << best_path[i];
if (i < best_path.size() - 1) {
outfile << " ";
}
}
outfile << "\n";
outfile.close();
}
void test_gurobi (std::string file, std::string out_file, double budget,
double slew_rate, double dwell_time, bool warm_start=false) {
std::vector<std::vector<double>> costs;
std::vector<double> probability;
std::vector<int> ranks;
std::vector<double> dwell_times;
bool is_deepslow = false;
double time_limit = 1200;
double accu_thr = 0.001;
int init_pos_idx = 0;
auto [start_idx, end_idx, padding] = buildGraphOrienteering(file, costs, probability, ranks, dwell_times,
slew_rate, is_deepslow, init_pos_idx);
std::cout << "padding: " << padding << "\n";
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point end;
std::chrono::duration<double> elapsed_seconds;
std::vector<int> warmstart_path = {};
if(warm_start) {
warmstart_path = init_path;
}
// gurobi with time limit
std::cout << "*********gurobi solution with time limit*********" << std::endl;
start = std::chrono::high_resolution_clock::now();
double obj_value, result_gap;
std::vector<int> ilp_path = gurobiSolveST(costs, probability, start_idx, end_idx, budget+padding, accu_thr, time_limit, warmstart_path);
end = std::chrono::high_resolution_clock::now();
print_path(costs, probability, ranks, ilp_path, dwell_time);
elapsed_seconds = end - start;
std::cout << "running time (wallclock): " << elapsed_seconds.count() << "seconds" << std::endl;
// write_result(out_file, "Gurobi", file, budget, slew_rate, dwell_time, costs, probability, ranks, ilp_path, elapsed_seconds.count(), padding);
}
int main (int argc, char** argv) {
// default paramaters
std::string out_file = "../result.csv";
std::string file = "../filtered_GW191127_050227_7dt.csv";
double budget = 1400;
double slew_rate = 50;
double dwell_time = 1;
bool warm_start = false;
if (argc > 1) {
file = std::string(argv[1]);
}
if (argc > 2) {
budget = std::stod(argv[2]);
}
if (argc > 3) {
warm_start = std::stoi(argv[3]);
}
std::cout << "Input Parameters:\n";
std::cout << " file = " << file << "\n";
std::cout << " budget = " << budget << "\n";
std::cout << " slew_rate = " << slew_rate << "\n";
std::cout << " dwell_time = " << dwell_time << "\n";
std::cout << " warm_start = " << warm_start << "\n\n";
test_gurobi (file, out_file, budget, slew_rate, dwell_time, warm_start);
return 0;
}