pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
newTSP_driver.cpp
Go to the documentation of this file.
1 /* PGR-GNU*****************************************************************
2  * File: newTSP_driver.cpp
3  *
4  * Generated with Template by:
5  * Copyright (c) 2015 pgRouting developers
6  * Mail: project@pgrouting.org
7  *
8  * Function's developer:
9  * Copyright (c) 2015 Celia Virginia Vergara Castillo
10  * Mail: vicky_vergara@hotmail.com
11  *
12  * ------
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27  *
28  * ******************************************************************** PGR-GNU*/
29 
30 #if defined(__MINGW32__) || defined(_MSC_VER)
31 #include <winsock2.h>
32 #include <windows.h>
33 #endif
34 
35 
36 #include <string.h>
37 #include <sstream>
38 #include <vector>
39 #include <algorithm>
40 
41 #include "./newTSP_driver.h"
42 #include "./Dmatrix.h"
43 
44 #include "./pgr_tsp.hpp"
45 #include "./../../common/src/pgr_assert.h"
46 #include "./../../common/src/pgr_alloc.hpp"
47 
48 void
50  Matrix_cell_t *distances,
51  size_t total_distances,
52  int64_t start_vid,
53  int64_t end_vid,
54 
55  double initial_temperature,
56  double final_temperature,
57  double cooling_factor,
58  int64_t tries_per_temperature,
59  int64_t max_changes_per_temperature,
60  int64_t max_consecutive_non_changes,
61  bool randomize,
62  double time_limit,
63 
64  General_path_element_t **return_tuples,
65  size_t *return_count,
66  char **log_msg,
67  char **err_msg) {
68  std::ostringstream err;
69  std::ostringstream log;
70 
71  try {
72  std::vector < Matrix_cell_t > data_costs(
73  distances,
74  distances + total_distances);
75 
76  pgrouting::tsp::Dmatrix costs(data_costs);
77 
78  if (!costs.has_no_infinity()) {
79  err << "An Infinity value was found on the Matrix";
80  *err_msg = strdup(err.str().c_str());
81  *log_msg = strdup(log.str().c_str());
82  return;
83  }
84 
85  if (!costs.is_symmetric()) {
86  err << "A Non symmetric Matrix was given as input";
87  *err_msg = strdup(err.str().c_str());
88  *log_msg = strdup(log.str().c_str());
89  return;
90  }
91 
92  double real_cost = -1;
93 
94  size_t idx_start = costs.has_id(start_vid) ?
95  costs.get_index(start_vid) : 0;
96 
97  size_t idx_end = costs.has_id(end_vid) ?
98  costs.get_index(end_vid) : 0;
99 
100  if (costs.has_id(start_vid) && costs.has_id(end_vid) && start_vid != end_vid) {
101  /* An ending vertex needs to be by the starting vertex */
102  real_cost = costs.distance(idx_start, idx_end);
103  costs.set(idx_start, idx_end, 0);
104  }
105 
106 
107  log << "pgr_eucledianTSP Processing Information \nInitializing tsp class --->";
109 
110 
111  log << " tsp.greedyInitial --->";
112  tsp.greedyInitial(idx_start);
113 
114 
115 
116  log << " tsp.annealing --->";
117  tsp.annealing(
118  initial_temperature,
119  final_temperature,
120  cooling_factor,
121  tries_per_temperature,
122  max_changes_per_temperature,
123  max_consecutive_non_changes,
124  randomize,
125  time_limit);
126  log << " OK\n";
127  log << tsp.get_log();
128  log << tsp.get_stats();
129 
130  auto bestTour(tsp.get_tour());
131 
132  if (costs.has_id(start_vid) && costs.has_id(end_vid) && start_vid != end_vid) {
133  costs.set(idx_start, idx_end, real_cost);
134  }
135 
136 
137  log << "\nBest cost reached = " << costs.tourCost(bestTour);
138 
139  auto start_ptr = std::find(
140  bestTour.cities.begin(),
141  bestTour.cities.end(),
142  idx_start);
143 
144 
145  std::rotate(
146  bestTour.cities.begin(),
147  start_ptr,
148  bestTour.cities.end());
149 
150  if (costs.has_id(start_vid) && costs.has_id(end_vid) && start_vid != end_vid) {
151  if (*(bestTour.cities.begin() + 1) == idx_end) {
152  std::reverse(
153  bestTour.cities.begin() + 1,
154  bestTour.cities.end());
155  }
156  }
157 
158 
159  std::vector< General_path_element_t > result;
160  result.reserve(bestTour.cities.size() + 1);
161  pgassert(bestTour.cities.size() == costs.size());
162 
163  bestTour.cities.push_back(bestTour.cities.front());
164 
165  auto prev_id = bestTour.cities.front();
166  double agg_cost = 0;
167  for (const auto &id : bestTour.cities) {
168  if (id == prev_id) continue;
170  data.node = costs.get_id(prev_id);
171  data.edge = prev_id;
172  data.cost = costs.distance(prev_id, id);
173  data.agg_cost = agg_cost;
174  result.push_back(data);
175  agg_cost += data.cost;
176  prev_id = id;
177  }
178 
179  /* inserting the returning to starting point */
180  {
182  data.node = costs.get_id(bestTour.cities.front());
183  data.edge = bestTour.cities.front();
184  data.cost = costs.distance(prev_id, bestTour.cities.front());
185  agg_cost += data.cost;
186  data.agg_cost = agg_cost;
187  result.push_back(data);
188  }
189 
190  pgassert(result.size() == bestTour.cities.size());
191  *return_count = bestTour.size();
192  (*return_tuples) = pgr_alloc(result.size(), (*return_tuples));
193 
194  /* store the results */
195  int seq = 0;
196  for (const auto &row : result) {
197  (*return_tuples)[seq] = row;
198  ++seq;
199  }
200 
201  *log_msg = strdup(log.str().c_str());
202  (*err_msg) = NULL;
203  return;
204  } catch (AssertFailedException &except) {
205  if (*return_tuples) free(*return_tuples);
206  (*return_count) = 0;
207  err << except.what() << "\n";
208  *err_msg = strdup(err.str().c_str());
209  *log_msg = strdup(log.str().c_str());
210  } catch (std::exception& except) {
211  if (*return_tuples) free(*return_tuples);
212  (*return_count) = 0;
213  err << except.what() << "\n";
214  *err_msg = strdup(err.str().c_str());
215  *log_msg = strdup(log.str().c_str());
216  } catch(...) {
217  if (*return_tuples) free(*return_tuples);
218  (*return_count) = 0;
219  err << "Caught unknown exception!\n";
220  *err_msg = strdup(err.str().c_str());
221  *log_msg = strdup(log.str().c_str());
222  }
223 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
bool is_symmetric() const
Definition: Dmatrix.cpp:153
size_t size() const
|idx|
Definition: Dmatrix.h:84
void greedyInitial(size_t idx_start=0)
Definition: pgr_tsp.cpp:149
int64_t get_id(size_t idx) const
idx -> original id
Definition: Dmatrix.cpp:97
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
void annealing(double initial_temperature, double final_temperature, double cooling_factor, int64_t tries_per_temperature, int64_t max_changes_per_temperature, int64_t max_consecutive_non_changes, bool randomize, double time_limit)
Definition: pgr_tsp.cpp:433
bool has_no_infinity() const
Definition: Dmatrix.cpp:123
void do_pgr_tsp(Matrix_cell_t *distances, size_t total_distances, int64_t start_vid, int64_t end_vid, double initial_temperature, double final_temperature, double cooling_factor, int64_t tries_per_temperature, int64_t max_changes_per_temperature, int64_t max_consecutive_non_changes, bool randomize, double time_limit, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)
double tourCost(const Tour &tour) const
tour evaluation
Definition: Dmatrix.cpp:49
std::string get_stats() const
Definition: pgr_tsp.hpp:76
bool has_id(int64_t id) const
original id -> true
Definition: Dmatrix.cpp:84
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
size_t get_index(int64_t id) const
original id -> idx
Definition: Dmatrix.cpp:91
virtual const char * what() const
Definition: pgr_assert.cpp:62
Tour get_tour() const
Definition: pgr_tsp.hpp:74
char * err_msg
Definition: BDATester.cpp:50
std::string get_log() const
Definition: pgr_tsp.hpp:85
static void reverse(int num, int *ids)
Definition: tsplib.c:457
void set(size_t i, size_t j, double dist)
sets a special value for the distance(i,j)
Definition: Dmatrix.h:56
double distance(size_t i, size_t j) const
Definition: Dmatrix.h:104