PGROUTING  3.2
Dmatrix.h
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: Dmatrix.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 
28 #ifndef INCLUDE_CPP_COMMON_DMATRIX_H_
29 #define INCLUDE_CPP_COMMON_DMATRIX_H_
30 #pragma once
31 
32 #include <iostream>
33 #include <vector>
34 #include <map>
35 
36 #include "c_types/matrix_cell_t.h"
37 
38 namespace pgrouting {
39 namespace tsp {
40 
41 class Tour; // for tourCost
42 
43 class Dmatrix {
44  public:
45  Dmatrix() = default;
46  explicit Dmatrix(const std::vector < Matrix_cell_t > &data_costs);
47  explicit Dmatrix(const std::map<std::pair<double, double>, int64_t> &euclidean_data);
48 
49  bool has_no_infinity() const;
50  bool obeys_triangle_inequality() const;
51  bool is_symmetric() const;
52 
60  void set(size_t i, size_t j, double dist) {
61  costs[i][j] = costs[j][i] = dist;}
62 
68  bool has_id(int64_t id) const;
69 
75  size_t get_index(int64_t id) const;
76 
82  int64_t get_id(size_t idx) const;
83 
88  size_t size() const {return ids.size();}
89 
95  double tourCost(const Tour &tour) const;
96 
102  const std::vector<double>& get_row(size_t idx) const {
103  return costs[idx];}
104 
105  double comparable_distance(size_t i, size_t j) const {
106  return distance(i, j);}
107 
108  double distance(int64_t i, int64_t j) const {
109  return distance(get_index(i), get_index(j));}
110 
111  double distance(size_t i, size_t j) const {
112  return costs[i][j];}
113 
114  friend std::ostream& operator<<(
115  std::ostream &log,
116  const Dmatrix &matrix);
117 
118  bool empty() const {
119  return ids.empty();
120  }
121 
122  protected:
123  void set_ids(const std::vector<matrix_cell> &data_costs);
124  std::vector<int64_t> ids;
125 
126  private:
127  typedef std::vector < std::vector < double > > Costs;
129  std::vector< double >& operator[] (size_t i) {return costs[i];}
130  const std::vector< double >& operator[] (size_t i) const {return costs[i];}
131 };
132 
133 } // namespace tsp
134 } // namespace pgrouting
135 
136 #endif // INCLUDE_CPP_COMMON_DMATRIX_H_
pgrouting::tsp::Dmatrix::get_index
size_t get_index(int64_t id) const
original id -> idx
Definition: Dmatrix.cpp:93
pgrouting::tsp::Tour
Definition: tour.h:42
pgrouting::tsp::Dmatrix::set
void set(size_t i, size_t j, double dist)
sets a special value for the distance(i,j)
Definition: Dmatrix.h:60
pgrouting::tsp::Dmatrix::tourCost
double tourCost(const Tour &tour) const
tour evaluation
Definition: Dmatrix.cpp:44
pgrouting::tsp::Dmatrix::comparable_distance
double comparable_distance(size_t i, size_t j) const
Definition: Dmatrix.h:105
pgrouting::tsp::Dmatrix::is_symmetric
bool is_symmetric() const
Definition: Dmatrix.cpp:193
pgrouting::tsp::Dmatrix::has_no_infinity
bool has_no_infinity() const
Definition: Dmatrix.cpp:162
pgrouting::tsp::Dmatrix::has_id
bool has_id(int64_t id) const
original id -> true
Definition: Dmatrix.cpp:79
pgrouting::tsp::Dmatrix::ids
std::vector< int64_t > ids
Definition: Dmatrix.h:124
pgrouting::tsp::Dmatrix::size
size_t size() const
|idx|
Definition: Dmatrix.h:88
pgrouting::tsp::Dmatrix::Costs
std::vector< std::vector< double > > Costs
Definition: Dmatrix.h:127
pgrouting::tsp::Dmatrix::get_id
int64_t get_id(size_t idx) const
idx -> original id
Definition: Dmatrix.cpp:101
pgrouting::tsp::Dmatrix::costs
Costs costs
Definition: Dmatrix.h:128
pgrouting::tsp::Dmatrix::get_row
const std::vector< double > & get_row(size_t idx) const
returns a row of distances
Definition: Dmatrix.h:102
pgrouting::tsp::Dmatrix::obeys_triangle_inequality
bool obeys_triangle_inequality() const
Triangle Inequality Theorem.
Definition: Dmatrix.cpp:181
pgrouting::tsp::Dmatrix::set_ids
void set_ids(const std::vector< matrix_cell > &data_costs)
Definition: Dmatrix.cpp:64
pgrouting::tsp::Dmatrix::empty
bool empty() const
Definition: Dmatrix.h:118
pgrouting::tsp::Dmatrix::distance
double distance(int64_t i, int64_t j) const
Definition: Dmatrix.h:108
pgrouting::tsp::Dmatrix::Dmatrix
Dmatrix()=default
pgrouting::tsp::Dmatrix::operator[]
std::vector< double > & operator[](size_t i)
Definition: Dmatrix.h:129
pgrouting::tsp::Dmatrix::operator<<
friend std::ostream & operator<<(std::ostream &log, const Dmatrix &matrix)
Definition: Dmatrix.cpp:216
pgrouting::tsp::Dmatrix::distance
double distance(size_t i, size_t j) const
Definition: Dmatrix.h:111
pgrouting::tsp::Dmatrix
Definition: Dmatrix.h:43
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
matrix_cell_t.h