PGROUTING  2.4
 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 #include "./newTSP_driver.h"
31 
32 #include <string.h>
33 #include <sstream>
34 #include <vector>
35 #include <algorithm>
36 
37 #include "./Dmatrix.h"
38 #include "./pgr_tsp.hpp"
39 
40 #include "./../../common/src/pgr_alloc.hpp"
41 #include "./../../common/src/pgr_assert.h"
42 
43 void
45  Matrix_cell_t *distances,
46  size_t total_distances,
47  int64_t start_vid,
48  int64_t end_vid,
49 
50  double initial_temperature,
51  double final_temperature,
52  double cooling_factor,
53  int64_t tries_per_temperature,
54  int64_t max_changes_per_temperature,
55  int64_t max_consecutive_non_changes,
56  bool randomize,
57  double time_limit,
58 
59  General_path_element_t **return_tuples,
60  size_t *return_count,
61  char **log_msg,
62  char **notice_msg,
63  char **err_msg) {
64  std::ostringstream log;
65  std::ostringstream notice;
66  std::ostringstream err;
67 
68  try {
69  std::vector <Matrix_cell_t> data_costs(
70  distances,
71  distances + total_distances);
72 
73  pgrouting::tsp::Dmatrix costs(data_costs);
74 
75  if (!costs.has_no_infinity()) {
76  err << "An Infinity value was found on the Matrix";
77  *err_msg = pgr_msg(err.str().c_str());
78  return;
79  }
80 
81  if (!costs.is_symmetric()) {
82  err << "A Non symmetric Matrix was given as input";
83  *err_msg = pgr_msg(err.str().c_str());
84  return;
85  }
86 
87  double real_cost = -1;
88 
89  size_t idx_start = costs.has_id(start_vid) ?
90  costs.get_index(start_vid) : 0;
91 
92  size_t idx_end = costs.has_id(end_vid) ?
93  costs.get_index(end_vid) : 0;
94 
95  if (costs.has_id(start_vid)
96  && costs.has_id(end_vid)
97  && start_vid != end_vid) {
98  /* An ending vertex needs to be by the starting vertex */
99  real_cost = costs.distance(idx_start, idx_end);
100  costs.set(idx_start, idx_end, 0);
101  }
102 
103 
104  log << "pgr_eucledianTSP Processing Information \n"
105  << "Initializing tsp class --->";
107 
108 
109  log << " tsp.greedyInitial --->";
110  tsp.greedyInitial(idx_start);
111 
112 
113 
114  log << " tsp.annealing --->";
115  tsp.annealing(
116  initial_temperature,
117  final_temperature,
118  cooling_factor,
119  tries_per_temperature,
120  max_changes_per_temperature,
121  max_consecutive_non_changes,
122  randomize,
123  time_limit);
124  log << " OK\n";
125  log << tsp.get_log();
126  log << tsp.get_stats();
127 
128  auto bestTour(tsp.get_tour());
129 
130  if (costs.has_id(start_vid)
131  && costs.has_id(end_vid)
132  && 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)
151  && costs.has_id(end_vid)
152  && start_vid != end_vid) {
153  if (*(bestTour.cities.begin() + 1) == idx_end) {
154  std::reverse(
155  bestTour.cities.begin() + 1,
156  bestTour.cities.end());
157  }
158  }
159 
160 
161  std::vector< General_path_element_t > result;
162  result.reserve(bestTour.cities.size() + 1);
163  pgassert(bestTour.cities.size() == costs.size());
164 
165  bestTour.cities.push_back(bestTour.cities.front());
166 
167  auto prev_id = bestTour.cities.front();
168  double agg_cost = 0;
169  for (const auto &id : bestTour.cities) {
170  if (id == prev_id) continue;
172  data.node = costs.get_id(prev_id);
173  data.edge = prev_id;
174  data.cost = costs.distance(prev_id, id);
175  data.agg_cost = agg_cost;
176  result.push_back(data);
177  agg_cost += data.cost;
178  prev_id = id;
179  }
180 
181  /* inserting the returning to starting point */
182  {
184  data.node = costs.get_id(bestTour.cities.front());
185  data.edge = bestTour.cities.front();
186  data.cost = costs.distance(prev_id, bestTour.cities.front());
187  agg_cost += data.cost;
188  data.agg_cost = agg_cost;
189  result.push_back(data);
190  }
191 
192  pgassert(result.size() == bestTour.cities.size());
193  *return_count = bestTour.size();
194  (*return_tuples) = pgr_alloc(result.size(), (*return_tuples));
195 
196  /* store the results */
197  int seq = 0;
198  for (const auto &row : result) {
199  (*return_tuples)[seq] = row;
200  ++seq;
201  }
202 
203  *log_msg = log.str().empty()?
204  *log_msg :
205  pgr_msg(log.str().c_str());
206  *notice_msg = notice.str().empty()?
207  *notice_msg :
208  pgr_msg(notice.str().c_str());
209  } catch (AssertFailedException &except) {
210  (*return_tuples) = pgr_free(*return_tuples);
211  (*return_count) = 0;
212  err << except.what();
213  *err_msg = pgr_msg(err.str().c_str());
214  *log_msg = pgr_msg(log.str().c_str());
215  } catch (std::exception &except) {
216  (*return_tuples) = pgr_free(*return_tuples);
217  (*return_count) = 0;
218  err << except.what();
219  *err_msg = pgr_msg(err.str().c_str());
220  *log_msg = pgr_msg(log.str().c_str());
221  } catch(...) {
222  (*return_tuples) = pgr_free(*return_tuples);
223  (*return_count) = 0;
224  err << "Caught unknown exception!";
225  *err_msg = pgr_msg(err.str().c_str());
226  *log_msg = pgr_msg(log.str().c_str());
227  }
228 }
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:147
size_t size() const
|idx|
Definition: Dmatrix.h:84
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:73
void greedyInitial(size_t idx_start=0)
Definition: pgr_tsp.cpp:144
int64_t get_id(size_t idx) const
idx -> original id
Definition: Dmatrix.cpp:91
#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:430
bool has_no_infinity() const
Definition: Dmatrix.cpp:116
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 **notice_msg, char **err_msg)
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:29
double tourCost(const Tour &tour) const
tour evaluation
Definition: Dmatrix.cpp:43
std::string get_stats() const
Definition: pgr_tsp.hpp:79
bool has_id(int64_t id) const
original id -> true
Definition: Dmatrix.cpp:78
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:62
size_t get_index(int64_t id) const
original id -> idx
Definition: Dmatrix.cpp:85
virtual const char * what() const
Definition: pgr_assert.cpp:53
Tour get_tour() const
Definition: pgr_tsp.hpp:77
char * err_msg
Definition: BDATester.cpp:50
std::string get_log() const
Definition: pgr_tsp.hpp:88
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