47 size_t total_distances,
51 double initial_temperature,
52 double final_temperature,
53 double cooling_factor,
54 int64_t tries_per_temperature,
55 int64_t max_changes_per_temperature,
56 int64_t max_consecutive_non_changes,
65 std::ostringstream log;
66 std::ostringstream notice;
67 std::ostringstream err;
70 std::vector <Matrix_cell_t> data_costs(
72 distances + total_distances);
77 err <<
"An Infinity value was found on the Matrix";
78 *err_msg =
pgr_msg(err.str().c_str());
83 err <<
"A Non symmetric Matrix was given as input";
84 *err_msg =
pgr_msg(err.str().c_str());
88 double real_cost = -1;
90 size_t idx_start = costs.
has_id(start_vid) ?
93 size_t idx_end = costs.
has_id(end_vid) ?
96 if (costs.
has_id(start_vid)
98 && start_vid != end_vid) {
100 real_cost = costs.
distance(idx_start, idx_end);
101 costs.
set(idx_start, idx_end, 0);
105 log <<
"Processing Information \n"
106 <<
"Initializing tsp class --->";
110 log <<
" tsp.greedyInitial --->";
115 log <<
" tsp.annealing --->";
120 tries_per_temperature,
121 max_changes_per_temperature,
122 max_consecutive_non_changes,
131 if (costs.
has_id(start_vid)
133 && start_vid != end_vid) {
134 costs.
set(idx_start, idx_end, real_cost);
138 log <<
"\nBest cost reached = " << costs.
tourCost(bestTour);
140 auto start_ptr = std::find(
141 bestTour.cities.begin(),
142 bestTour.cities.end(),
147 bestTour.cities.begin(),
149 bestTour.cities.end());
151 if (costs.
has_id(start_vid)
153 && start_vid != end_vid) {
154 if (*(bestTour.cities.begin() + 1) == idx_end) {
156 bestTour.cities.begin() + 1,
157 bestTour.cities.end());
162 std::vector< General_path_element_t > result;
163 result.reserve(bestTour.cities.size() + 1);
166 bestTour.cities.push_back(bestTour.cities.front());
168 auto prev_id = bestTour.cities.front();
170 for (
const auto &
id : bestTour.cities) {
171 if (
id == prev_id)
continue;
174 data.
edge =
static_cast<int64_t
>(prev_id);
177 result.push_back(data);
178 agg_cost += data.
cost;
185 data.
node = costs.
get_id(bestTour.cities.front());
186 data.
edge =
static_cast<int64_t
>(bestTour.cities.front());
187 data.
cost = costs.
distance(prev_id, bestTour.cities.front());
188 agg_cost += data.
cost;
190 result.push_back(data);
193 pgassert(result.size() == bestTour.cities.size());
194 *return_count = bestTour.size();
195 (*return_tuples) =
pgr_alloc(result.size(), (*return_tuples));
199 for (
const auto &row : result) {
200 (*return_tuples)[seq] = row;
204 *log_msg = log.str().empty()?
207 *notice_msg = notice.str().empty()?
211 (*return_tuples) =
pgr_free(*return_tuples);
213 err << except.
what();
214 *err_msg =
pgr_msg(err.str().c_str());
215 *log_msg =
pgr_msg(log.str().c_str());
216 }
catch (std::exception &except) {
217 (*return_tuples) =
pgr_free(*return_tuples);
219 err << except.what();
220 *err_msg =
pgr_msg(err.str().c_str());
221 *log_msg =
pgr_msg(log.str().c_str());
223 (*return_tuples) =
pgr_free(*return_tuples);
225 err <<
"Caught unknown exception!";
226 *err_msg =
pgr_msg(err.str().c_str());
227 *log_msg =
pgr_msg(log.str().c_str());