PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tour.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE:
4 
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
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 #include "./tour.h"
27 
28 #include <algorithm>
29 
30 #include "../../common/src/pgr_assert.h"
31 
32 
33 namespace pgrouting {
34 namespace tsp {
35 
36 std::ostream& operator<<(
37  std::ostream &log,
38  const Tour &tour) {
39  for (const auto &city : tour.cities) {
40  log << city << ", ";
41  }
42  return log;
43 }
44 
45 
46 
48  size_t c1,
49  size_t c2) {
50  pgassert(c1 < c2 && c2 < cities.size());
51  std::reverse(
52  cities.begin() + (c1 + 1),
53  cities.begin() + (c2 + 1));
54 }
55 
57  size_t place,
58  size_t first,
59  size_t last) {
60  pgassert(first < cities.size());
61  pgassert(last < cities.size());
62  pgassert(place < cities.size());
63 
64  if (place < first) {
65  std::rotate(
66  cities.begin() + (place + 1),
67  cities.begin() + (first + 1),
68  cities.begin() + (last + 1));
69  } else {
70  std::rotate(
71  cities.begin() + (first + 1),
72  cities.begin() + (last + 1),
73  cities.begin() + (place + 1));
74  }
75 }
76 
78  size_t c1,
79  size_t c2,
80  size_t c3) {
81  pgassert(c1 < c2 && c2 < c3 && c3 < cities.size());
82 
83  std::rotate(
84  cities.begin() + (c1 + 1),
85  cities.begin() + (c2 + 1),
86  cities.begin() + (c3 + 1));
87 }
88 
89 
91  size_t c1,
92  size_t c2 ) {
93  pgassert(c1 < c2);
94 
95  std::iter_swap(cities.begin() + c1, cities.begin() + c2);
96 }
97 
98 
99 } // namespace tsp
100 } // namespace pgrouting
void rotate(size_t c1, size_t c2, size_t c3)
Definition: tour.cpp:77
std::ostream & operator<<(std::ostream &log, const Dmatrix &matrix)
Definition: Dmatrix.cpp:167
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
std::vector< size_t > cities
Definition: tour.h:154
void reverse(size_t c1, size_t c2)
Definition: tour.cpp:47
void swap(size_t c1, size_t c2)
Definition: tour.cpp:90
void slide(size_t place, size_t first, size_t last)
Definition: tour.cpp:56