PGROUTING  3.2
tour.h
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: tour.h
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 #ifndef INCLUDE_TSP_TOUR_H_
27 #define INCLUDE_TSP_TOUR_H_
28 #pragma once
29 
30 #include <stdlib.h>
31 #include <algorithm>
32 #include <numeric>
33 #include <vector>
34 
35 #include "cpp_common/Dmatrix.h"
36 
37 
38 namespace pgrouting {
39 namespace tsp {
40 
41 
42 class Tour {
43  using difference_type = std::vector<size_t>::difference_type;
44 
45  public:
46  Tour(const Tour &) = default;
47 
48  explicit Tour(const std::vector<size_t> &cities_order) :
49  cities(cities_order) {
50  }
51 
52  explicit Tour(size_t n) {
53  cities.resize(n);
54  std::iota(std::begin(cities), std::end(cities), 0);
55  }
56 
57  inline size_t size() const {return cities.size();}
58 
59  friend std::ostream& operator<<(
60  std::ostream &log,
61  const Tour &tour);
62 
63 
64  friend double Dmatrix::tourCost(const Tour &tour) const;
65 
66 
67  /* @brief slides range [first + 1, last + 1) into place + 1
68  *
69  * 0 1 2 3 4 5 6 7 8 9
70  * p f l
71  * slides [4,5,6] to position p
72  *
73  * 0 1 4 5 6 2 3 7 8 9
74  *
75  *
76  * 0 1 2 3 4 5 6 7 8 9
77  * f l p
78  * slides [2,3,4] to position p
79  *
80  * 0 1 6 7 2 3 4 5 8 9
81  *
82  * uses std::reverse
83  *
84  * http://en.cppreference.com/w/cpp/algorithm/rotate
85  *
86  * first - the beginning of the original range
87  * last - the end of the original range
88  * place - location where to slide
89  *
90  *
91  *
92  * @params[IN] place index of place
93  * @params[IN] first - index of first
94  * @params[IN] last - index of last
95  *
96  * precondition:
97  * pgassert(first < cities.size();
98  * pgassert(last < cities.size();
99  * pgassert(place < cities.size();
100  *
101  */
102 
103  void slide(
104  size_t place,
105  size_t first,
106  size_t last);
107 
108 
109  /* @brief std::reverse on the cities
110  *
111  * http://en.cppreference.com/w/cpp/algorithm/reverse
112  *
113  * first - the beginning of the original range
114  * last - the end of the original range
115  *
116  * @params[IN] c1 - index of first
117  * @params[IN] c2 - index of lasst
118  *
119  * precondition:
120  * pgassert(c1 < c2);
121  *
122  */
123 
124  void reverse(
125  size_t c1,
126  size_t c2);
127 
128 
129  /* @brief std::rotate on the cities
130  *
131  * http://en.cppreference.com/w/cpp/algorithm/rotate
132  *
133  * first - the beginning of the original range
134  * n_first - the element that should appear at the beginning of the rotated range
135  * last - the end of the original range
136  *
137  * @params[IN] c1 - index of first
138  * @params[IN] c2 - index of n_first
139  * @params[IN] c3 - index of last
140  *
141  * precondition:
142  * pgassert(c2 && c2 < c3 && c3 < n);
143  *
144  */
145  void rotate(
146  size_t c1,
147  size_t c2,
148  size_t c3);
149 
150 
151  void swap(
152  size_t c1,
153  size_t c2);
154 
155  public:
156  std::vector<size_t> cities;
157 };
158 
159 } // namespace tsp
160 } // namespace pgrouting
161 
162 #endif // INCLUDE_TSP_TOUR_H_
pgrouting::tsp::Tour
Definition: tour.h:42
pgrouting::tsp::Dmatrix::tourCost
double tourCost(const Tour &tour) const
tour evaluation
Definition: Dmatrix.cpp:44
pgrouting::tsp::Tour::size
size_t size() const
Definition: tour.h:57
pgrouting::tsp::Tour::difference_type
std::vector< size_t >::difference_type difference_type
Definition: tour.h:43
pgrouting::tsp::Tour::cities
std::vector< size_t > cities
Definition: tour.h:156
pgrouting::tsp::Tour::rotate
void rotate(size_t c1, size_t c2, size_t c3)
Definition: tour.cpp:77
pgrouting::tsp::Tour::swap
void swap(size_t c1, size_t c2)
Definition: tour.cpp:90
pgrouting::tsp::Tour::slide
void slide(size_t place, size_t first, size_t last)
Definition: tour.cpp:56
pgrouting::tsp::Tour::Tour
Tour(const std::vector< size_t > &cities_order)
Definition: tour.h:48
pgrouting::tsp::Tour::Tour
Tour(size_t n)
Definition: tour.h:52
Dmatrix.h
pgrouting::tsp::Tour::Tour
Tour(const Tour &)=default
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::tsp::Tour::reverse
void reverse(size_t c1, size_t c2)
Definition: tour.cpp:47
pgrouting::tsp::Tour::operator<<
friend std::ostream & operator<<(std::ostream &log, const Tour &tour)
Definition: tour.cpp:36