PGROUTING  3.2
euclideanDmatrix.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: euclideanDmatrix.cpp
4 
5 Copyright (c) 2015 pgRouting developers
7 
8 ------
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 
24  ********************************************************************PGR-GNU*/
25 
26 
27 #include "tsp/euclideanDmatrix.h"
28 
29 #include <algorithm>
30 #include <vector>
31 #include <cmath>
32 
33 #include "tsp/tour.h"
34 #include "cpp_common/pgr_assert.h"
35 
36 
37 namespace pgrouting {
38 namespace tsp {
39 
40 
41 const std::vector<double>
42 EuclideanDmatrix::get_row(size_t i) const {
43  std::vector<double> result;
44 
45  for (size_t j = 0; j < ids.size(); ++j) {
46  result.push_back(distance(i, j));
47  }
48 
49  pgassert(result.size() == ids.size());
50  return result;
51 }
52 
53 
54 double
55 EuclideanDmatrix::comparable_distance(size_t i, size_t j) const {
56  if (special_distance >= 0 &&
57  ((row == i && column == j)
58  ||(row == j && column == i))) {
60  }
61  auto dx = coordinates[i].x - coordinates[j].x;
62  auto dy = coordinates[i].y - coordinates[j].y;
63  return dx * dx + dy * dy;
64 }
65 
66 double
67 EuclideanDmatrix::distance(size_t i, size_t j) const {
68  if (special_distance >= 0 &&
69  ((row == i && column == j)
70  ||(row == j && column == i))) {
71  return special_distance;
72  }
73  if (i == j) return 0;
74  return std::sqrt(comparable_distance(i, j));
75 }
76 
77 double
78 EuclideanDmatrix::tourCost(const Tour &tour) const {
79  double total_cost(0);
80  if (tour.cities.empty()) return total_cost;
81 
82  auto prev_id = tour.cities.front();
83  for (const auto &id : tour.cities) {
84  if (id == tour.cities.front()) continue;
85 
86  total_cost += distance(prev_id, id);
87  prev_id = id;
88  }
89  total_cost += distance(prev_id, tour.cities.front());
90  return total_cost;
91 }
92 
93 
94 
95 bool
96 EuclideanDmatrix::has_id(int64_t id) const {
97  for (const auto &i : ids) {
98  if (i == id) return true;
99  }
100  return false;
101 }
102 
103 size_t
104 EuclideanDmatrix::get_index(int64_t id) const {
105  for (size_t pos = 0; pos < ids.size(); ++pos) {
106  if (ids[pos] == id) return pos;
107  }
108  return ids.size() + 1;
109 }
110 
111 int64_t
112 EuclideanDmatrix::get_id(size_t id) const {
113  return ids[id];
114 }
115 
116 /*
117  * constructor
118  */
120  const std::vector < Coordinate_t > &data_coordinates)
121  : coordinates(data_coordinates) {
122  set_ids();
123  std::sort(coordinates.begin(), coordinates.end(),
124  [](const Coordinate_t &lhs, const Coordinate_t &rhs)
125  {return lhs.id < rhs.id;});
126  }
127 
128 void
130  ids.reserve(coordinates.size());
131  for (const auto &data : coordinates) {
132  ids.push_back(data.id);
133  }
134  std::sort(ids.begin(), ids.end());
135 #ifndef NDEBUG
136  auto total = ids.size();
137 #endif
138  ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
139  pgassertwm(total == ids.size(), "Duplicated id found");
140 }
141 
142 bool
144  return true;
145 }
146 
147 bool
149  return true;
150 }
151 
152 bool
154  return true;
155 }
156 
157 std::ostream& operator<<(std::ostream &log, const EuclideanDmatrix &matrix) {
158  for (const auto id : matrix.ids) {
159  log << "\t" << id;
160  }
161  log << "\n";
162  for (const auto row : matrix.coordinates) {
163  log << row.id << "(" << row.x << "," << row.y << ")\n";
164  }
165  return log;
166 }
167 
168 
169 } // namespace tsp
170 } // namespace pgrouting
pgrouting::tsp::EuclideanDmatrix::ids
std::vector< int64_t > ids
Definition: euclideanDmatrix.h:112
pgrouting::tsp::EuclideanDmatrix::comparable_distance
double comparable_distance(size_t i, size_t j) const
Definition: euclideanDmatrix.cpp:55
pgrouting::tsp::EuclideanDmatrix::is_symmetric
bool is_symmetric() const
Definition: euclideanDmatrix.cpp:153
pgrouting::tsp::Tour
Definition: tour.h:42
pgrouting::tsp::EuclideanDmatrix::column
size_t column
Definition: euclideanDmatrix.h:117
pgrouting::tsp::EuclideanDmatrix::get_row
const std::vector< double > get_row(size_t idx) const
returns a row of distances
Definition: euclideanDmatrix.cpp:42
pgrouting::tsp::EuclideanDmatrix::distance
double distance(size_t i, size_t j) const
Definition: euclideanDmatrix.cpp:67
pgassertwm
#define pgassertwm(expr, msg)
Adds a message to the assertion.
Definition: pgr_assert.h:117
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
Coordinate_t
Definition: coordinate_t.h:37
pgrouting::tsp::operator<<
std::ostream & operator<<(std::ostream &log, const Dmatrix &matrix)
Definition: Dmatrix.cpp:216
pgrouting::tsp::Tour::cities
std::vector< size_t > cities
Definition: tour.h:156
pgrouting::tsp::EuclideanDmatrix
Definition: euclideanDmatrix.h:40
pgrouting::tsp::EuclideanDmatrix::get_index
size_t get_index(int64_t id) const
original id -> idx
Definition: euclideanDmatrix.cpp:104
euclideanDmatrix.h
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::tsp::EuclideanDmatrix::get_id
int64_t get_id(size_t idx) const
idx -> original id
Definition: euclideanDmatrix.cpp:112
pgrouting::tsp::EuclideanDmatrix::coordinates
std::vector< Coordinate_t > coordinates
Definition: euclideanDmatrix.h:115
pgrouting::tsp::EuclideanDmatrix::obeys_triangle_inequality
bool obeys_triangle_inequality() const
Definition: euclideanDmatrix.cpp:148
tour.h
pgrouting::tsp::EuclideanDmatrix::special_distance
double special_distance
Definition: euclideanDmatrix.h:118
pgrouting::tsp::EuclideanDmatrix::EuclideanDmatrix
EuclideanDmatrix()=default
pgrouting::tsp::EuclideanDmatrix::row
size_t row
Definition: euclideanDmatrix.h:116
pgrouting::tsp::EuclideanDmatrix::has_no_infinity
bool has_no_infinity() const
Definition: euclideanDmatrix.cpp:143
pgrouting::tsp::EuclideanDmatrix::set_ids
void set_ids()
Definition: euclideanDmatrix.cpp:129
pgrouting::tsp::EuclideanDmatrix::has_id
bool has_id(int64_t id) const
original id -> true
Definition: euclideanDmatrix.cpp:96
pgrouting::tsp::EuclideanDmatrix::tourCost
double tourCost(const Tour &tour) const
tour evaluation
Definition: euclideanDmatrix.cpp:78
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56