-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_ITA_ECBS.cpp
154 lines (135 loc) · 5.61 KB
/
main_ITA_ECBS.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
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/tokenizer.hpp>
#include <string>
#include <yaml-cpp/yaml.h>
#include "include/ITA_ECBS/ITA_ECBS.hpp"
#include "include/common.hpp"
namespace po = boost::program_options;
unordered_set<Location> obstacles;
vector<unordered_set<Location> > goals;
unordered_map<Location, int> goal_to_idx;
unordered_map<int, Location> idx_to_goal;
vector<State> start_states;
po::variables_map vm;
int row_number,col_number;
string outputFile;
int init_map(int argc, char** argv)
{
po::options_description desc("Allowed options");
string inputFile;
desc.add_options()
("help", "produce help message")
// params for the input instance and experiment settings
("input,i", po::value<std::string>(&inputFile)->required(), "input file (YAML)")
("output,o", po::value<std::string>(&outputFile)->required(), "output file (YAML)")
("weight,w", po::value<float>()->default_value(1), "weight for ECBS")
("nodeLimit", po::value<int>()->default_value(MAX_NODES), "node limit")
("seed,d", po::value<int>()->default_value(0), "random seed")
("stats", po::value<bool>()->default_value(false), "write to files some statistics")
("restart", po::value<int>()->default_value(1), "number of restart times (at least 1)");
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help") != 0u) {
std::cout << desc << "\n";
return -1;
}
} catch (po::error &e) {
std::cerr << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return -1;
}
YAML::Node config = YAML::LoadFile(inputFile);
if (config["map"].IsMap()) {
const auto &dim = config["map"]["dimensions"];
row_number = dim[0].as<int>();
col_number = dim[1].as<int>();
for (const auto &node: config["map"]["obstacles"]) {
obstacles.insert(Location(node[0].as<int>(), node[1].as<int>()));
}
}
else {
const auto& file_name = config["map"].as<string>();
std::filesystem::path fullPath(inputFile);
std::filesystem::path folderPath = fullPath.parent_path();
std::filesystem::path map_file_path = folderPath / file_name;
vector<vector<bool> > ret_map;
read_map_file(map_file_path, ret_map);
row_number = ret_map.size();
col_number = ret_map[0].size();
for (int i=0;i<row_number;i++)
for (int j=0;j<col_number;j++)
if (ret_map[i][j])
obstacles.insert(Location(i, j));
}
unordered_set<Location> all_goal_location_set;
for (const auto &node: config["agents"]) {
const auto &start = node["start"];
start_states.emplace_back(State(0, start[0].as<int>(), start[1].as<int>()));
goals.resize(goals.size() + 1);
for (const auto &goal: node["potentialGoals"]) {
Location x = Location(goal[0].as<int>(), goal[1].as<int>());
goals.back().emplace(x);
all_goal_location_set.insert(x);
}
}
int cnt = 0;
for (const auto& location:all_goal_location_set)
{
goal_to_idx[location] = cnt;
idx_to_goal[cnt] = location;
cnt ++;
}
// sanity check: no identical start states
unordered_set<State> all_start_states_set;
for (const auto &s: start_states) {
if (all_start_states_set.find(s) != all_start_states_set.end()) {
std::cout << "Identical start states detected -> no solution!" << std::endl;
return -1;
}
all_start_states_set.insert(s);
}
return 0;
}
int main(int argc, char** argv) {
if (init_map(argc, argv) < 0)
{
std::cout<< "Error Map" <<std::endl;
return 0;
}
std::cout<< "Load Map Done" <<std::endl;
ITA_ECBS ita_ecbs(row_number, col_number, obstacles, goals, start_states, goal_to_idx, idx_to_goal, vm["weight"].as<float>());
int runs = vm["restart"].as<int>();
for (int i = 0; i < runs; i++) {
ita_ecbs.clear();
ita_ecbs.total_timer.reset();
ita_ecbs.solve();
ita_ecbs.total_timer.stop();
ita_ecbs.total_runtime = ita_ecbs.total_timer.elapsedSeconds();
if (ita_ecbs.solution_found) break;
}
std::ofstream out(outputFile);
out << "statistics:" << std::endl;
out << " cost: " << ita_ecbs.cost << std::endl;
out << " TA_runtime: " << ita_ecbs.ta_runtime << std::endl;
out << " runtime: " << ita_ecbs.total_runtime << std::endl;
out << " newnode_runtime: " << ita_ecbs.newnode_time << std::endl;
out << " firstconflict_runtime: " << ita_ecbs.conflict_num_time << std::endl;
out << " lowlevel_search_time: " << ita_ecbs.lowlevel_search_time << std::endl;
out << " focal_score_time: " << ita_ecbs.focal_score_time << std::endl;
out << " total_lowlevel_node: " << ita_ecbs.cbsnode_num << std::endl;
out << " lowLevelExpanded: " << ita_ecbs.lowLevelExpanded << std::endl;
out << " numTaskAssignments: " << ita_ecbs.num_ta << std::endl;
out << " shortest_path_times: " << ita_ecbs.invoke_shortest_path_times << std::endl;
out << "schedule:" << std::endl;
for (size_t a = 0; a < ita_ecbs.out_solution.size(); ++a) {
out << " agent" << a << ":" << std::endl;
for (const auto &state: *(ita_ecbs.out_solution[a])) {
out << " - x: " << state.state.x << std::endl
<< " y: " << state.state.y << std::endl
<< " t: " << state.state.time << std::endl;
}
}
return 0;
}