PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Dmatrix.h
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: Dmatrix.h
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 
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 
35 #include "c_types/matrix_cell_t.h"
36 
37 namespace pgrouting {
38 namespace tsp {
39 
40 class Tour; // for tourCost
41 
42 class Dmatrix {
43  public:
44  Dmatrix() = default;
45  explicit Dmatrix(const std::vector < Matrix_cell_t > &data_costs);
46 
47  bool has_no_infinity() const;
48  bool obeys_triangle_inequality() const;
49  bool is_symmetric() const;
50 
58  void set(size_t i, size_t j, double dist) {
59  costs[i][j] = costs[j][i] = dist;}
60 
66  bool has_id(int64_t id) const;
67 
73  size_t get_index(int64_t id) const;
74 
80  int64_t get_id(size_t idx) const;
81 
86  size_t size() const {return ids.size();}
87 
93  double tourCost(const Tour &tour) const;
94 
100  const std::vector<double>& get_row(size_t idx) const {
101  return costs[idx];}
102 
103  double comparable_distance(size_t i, size_t j) const {
104  return distance(i, j);}
105 
106  double distance(int64_t i, int64_t j) const {
107  return distance(get_index(i), get_index(j));}
108 
109  double distance(size_t i, size_t j) const {
110  return costs[i][j];}
111 
112  friend std::ostream& operator<<(
113  std::ostream &log,
114  const Dmatrix &matrix);
115 
116  double empty() const {
117  return ids.empty();
118  }
119 
120  protected:
121  void set_ids(const std::vector<matrix_cell> &data_costs);
122  std::vector<int64_t> ids;
123 
124  private:
125  typedef std::vector < std::vector < double > > Costs;
127  std::vector< double >& operator[] (size_t i) {return costs[i];}
128  const std::vector< double >& operator[] (size_t i) const {return costs[i];}
129 };
130 
131 } // namespace tsp
132 } // namespace pgrouting
133 
134 #endif // INCLUDE_CPP_COMMON_DMATRIX_H_
std::vector< std::vector< double > > Costs
Definition: Dmatrix.h:125
bool is_symmetric() const
Definition: Dmatrix.cpp:152
bool obeys_triangle_inequality() const
Triangle Inequality Theorem.
Definition: Dmatrix.cpp:140
size_t size() const
|idx|
Definition: Dmatrix.h:86
void set_ids(const std::vector< matrix_cell > &data_costs)
Definition: Dmatrix.cpp:63
double distance(int64_t i, int64_t j) const
Definition: Dmatrix.h:106
std::vector< int64_t > ids
Definition: Dmatrix.h:122
std::vector< double > & operator[](size_t i)
Definition: Dmatrix.h:127
const std::vector< double > & get_row(size_t idx) const
returns a row of distances
Definition: Dmatrix.h:100
int64_t get_id(size_t idx) const
idx -> original id
Definition: Dmatrix.cpp:96
double empty() const
Definition: Dmatrix.h:116
bool has_no_infinity() const
Definition: Dmatrix.cpp:121
double tourCost(const Tour &tour) const
tour evaluation
Definition: Dmatrix.cpp:43
bool has_id(int64_t id) const
original id -> true
Definition: Dmatrix.cpp:78
size_t get_index(int64_t id) const
original id -> idx
Definition: Dmatrix.cpp:90
double comparable_distance(size_t i, size_t j) const
Definition: Dmatrix.h:103
void set(size_t i, size_t j, double dist)
sets a special value for the distance(i,j)
Definition: Dmatrix.h:58
double distance(size_t i, size_t j) const
Definition: Dmatrix.h:109
friend std::ostream & operator<<(std::ostream &log, const Dmatrix &matrix)
Definition: Dmatrix.cpp:175