47 size_t total_coordinates,
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< Coordinate_t > coordinates(
72 coordinates_data + total_coordinates);
77 size_t idx_start = costs.
has_id(start_vid) ?
80 size_t idx_end = costs.
has_id(end_vid) ?
85 if (costs.
has_id(start_vid)
87 && start_vid != end_vid) {
91 real_cost = costs.
distance(idx_start, idx_end);
95 costs.
set(idx_start, idx_end, 0);
99 log <<
"Processing Information\n"
100 <<
"Initializing tsp class --->";
104 log <<
" tsp.greedyInitial --->";
109 log <<
" tsp.annealing --->";
114 tries_per_temperature,
115 max_changes_per_temperature,
116 max_consecutive_non_changes,
127 if (costs.
has_id(start_vid)
129 && start_vid != end_vid) {
133 costs.
set(idx_start, idx_end, real_cost);
136 log <<
"\nBest cost reached = " << costs.
tourCost(bestTour);
138 auto start_ptr = std::find(
139 bestTour.cities.begin(),
140 bestTour.cities.end(),
144 bestTour.cities.begin(),
146 bestTour.cities.end());
148 if (costs.
has_id(start_vid)
150 && start_vid != end_vid) {
151 if (*(bestTour.cities.begin() + 1) == idx_end) {
153 bestTour.cities.begin() + 1,
154 bestTour.cities.end());
159 std::vector< General_path_element_t > result;
160 result.reserve(bestTour.cities.size() + 1);
163 bestTour.cities.push_back(bestTour.cities.front());
165 auto prev_id = bestTour.cities.front();
167 for (
const auto &
id : bestTour.cities) {
168 if (
id == prev_id)
continue;
171 data.
edge =
static_cast<int64_t
>(prev_id);
174 result.push_back(data);
175 agg_cost += data.
cost;
182 data.
node = costs.
get_id(bestTour.cities.front());
183 data.
edge =
static_cast<int64_t
>(bestTour.cities.front());
184 data.
cost = costs.
distance(prev_id, bestTour.cities.front());
185 agg_cost += data.
cost;
187 result.push_back(data);
190 pgassert(result.size() == bestTour.cities.size());
191 *return_count = bestTour.size();
192 (*return_tuples) =
pgr_alloc(result.size(), (*return_tuples));
196 for (
const auto &row : result) {
197 (*return_tuples)[seq] = row;
202 *log_msg = log.str().empty()?
205 *notice_msg = notice.str().empty()?
209 (*return_tuples) =
pgr_free(*return_tuples);
211 err << except.
what();
212 *err_msg =
pgr_msg(err.str().c_str());
213 *log_msg =
pgr_msg(log.str().c_str());
214 }
catch (std::exception &except) {
215 (*return_tuples) =
pgr_free(*return_tuples);
217 err << except.what();
218 *err_msg =
pgr_msg(err.str().c_str());
219 *log_msg =
pgr_msg(log.str().c_str());
221 (*return_tuples) =
pgr_free(*return_tuples);
223 err <<
"Caught unknown exception!";
224 *err_msg =
pgr_msg(err.str().c_str());
225 *log_msg =
pgr_msg(log.str().c_str());