pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
solution.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: solution.cpp
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 
27 
28 #include <string>
29 #include <vector>
30 
31 #include "./solution.h"
32 #include "./pgr_pickDeliver.h"
33 
34 namespace pgrouting {
35 namespace vrp {
36 
37 
38 void
40  std::vector< General_vehicle_orders_t > &result) const {
41  /* postgres numbering starts with 1 */
42  int i(1);
43  for (const auto truck : fleet) {
44  std::vector< General_vehicle_orders_t > data;
45  truck.get_postgres_result(i, data);
46  result.insert(result.end(), data.begin(), data.end());
47 
48  ++i;
49  }
50 }
51 
52 
53 
54 bool
56  for (const auto v : fleet) {
57  if (v.is_feasable()) continue;
58  return false;
59  }
60  return true;
61 }
62 
63 double
65  double total(0);
66  for (const auto v : fleet) {
67  total += v.duration();
68  }
69  return total;
70 }
71 
72 int
74  int total(0);
75  for (const auto v : fleet) {
76  total += v.twvTot();
77  }
78  return total;
79 }
80 
81 double
83  double total(0);
84  for (const auto v : fleet) {
85  total += v.total_wait_time();
86  }
87  return total;
88 }
89 
90 double
92  double total(0);
93  for (const auto v : fleet) {
94  total += v.total_travel_time();
95  }
96  return total;
97 }
98 
99 double
101  double total(0);
102  for (const auto v : fleet) {
103  total += v.total_service_time();
104  }
105  return total;
106 }
107 
108 int
110  int total(0);
111  for (const auto v : fleet) {
112  total += v.cvTot();
113  }
114  return total;
115 }
116 
118 Solution::cost() const {
119  double total_duration(0);
120  double total_wait_time(0);
121  int total_twv(0);
122  int total_cv(0);
123  for (const auto v : fleet) {
124  total_duration += v.duration();
125  total_wait_time += v.total_wait_time();
126  total_twv += v.twvTot();
127  total_cv += v.cvTot();
128  }
129  return std::make_tuple(
130  total_twv, total_cv, fleet.size(),
131  total_wait_time, total_duration);
132 }
133 
134 
135 
136 std::string
138  Vehicle::Cost s_cost(cost());
139  std::ostringstream log;
140 
141  log << "(twv, cv, fleet, wait, duration) = ("
142  << std::get<0>(s_cost) << ", "
143  << std::get<1>(s_cost) << ", "
144  << std::get<2>(s_cost) << ", "
145  << std::get<3>(s_cost) << ", "
146  << std::get<4>(s_cost) << ")";
147 
148  return log.str();
149 }
150 
151 std::string
152 Solution::tau(const std::string &title) const {
153  Vehicle::Cost s_cost(cost());
154  std::ostringstream log;
155 
156  log << "\n" << title << ": " << std::endl;
157  for (const auto v : fleet) {
158  log << "\n" << v.tau();
159  }
160  log << "\n" << cost_str() << "\n";
161  return log.str();
162 }
163 
164 std::ostream&
165 operator << (std::ostream &log, const Solution &solution) {
166  for (const auto vehicle : solution.fleet) {
167  log << vehicle;
168  }
169 
170  log << "\n SOLUTION:\n\n "
171  << solution.tau();
172 
173  return log;
174 }
175 
176 bool
177 Solution::operator<(const Solution &s_rhs) const {
178  Vehicle::Cost lhs(cost());
179  Vehicle::Cost rhs(s_rhs.cost());
180 
181  /*
182  * capacity violations
183  */
184  if (std::get<0>(lhs) < std::get<0>(rhs))
185  return true;
186  if (std::get<0>(lhs) > std::get<0>(rhs))
187  return false;
188 
189  /*
190  * time window violations
191  */
192  if (std::get<1>(lhs) < std::get<1>(rhs))
193  return true;
194  if (std::get<1>(lhs) > std::get<1>(rhs))
195  return false;
196 
197  /*
198  * fleet size
199  */
200  if (std::get<2>(lhs) < std::get<2>(rhs))
201  return true;
202  if (std::get<2>(lhs) > std::get<2>(rhs))
203  return false;
204 
205  /*
206  * waiting time
207  */
208  if (std::get<3>(lhs) < std::get<3>(rhs))
209  return true;
210  if (std::get<3>(lhs) > std::get<3>(rhs))
211  return false;
212 
213  /*
214  * duration
215  */
216  if (std::get<4>(lhs) < std::get<4>(rhs))
217  return true;
218  if (std::get<4>(lhs) > std::get<4>(rhs))
219  return false;
220 
221  return false;
222 }
223 
224 } // namespace vrp
225 } // namespace pgrouting
bool is_feasable() const
Definition: solution.cpp:55
bool operator<(const Solution &s_rhs) const
Definition: solution.cpp:177
void get_postgres_result(std::vector< General_vehicle_orders_t > &result) const
Definition: solution.cpp:39
Vehicle::Cost cost() const
Definition: solution.cpp:118
std::deque< Vehicle_pickDeliver > fleet
Definition: solution.h:46
PGDLLEXPORT Datum vrp(PG_FUNCTION_ARGS)
Definition: VRP.c:730
double wait_time() const
Definition: solution.cpp:82
std::string tau(const std::string &title="Tau") const
Definition: solution.cpp:152
double total_travel_time() const
Definition: solution.cpp:91
std::string cost_str() const
Definition: solution.cpp:137
double duration() const
Definition: solution.cpp:64
double total_service_time() const
Definition: solution.cpp:100
std::tuple< int, int, size_t, double, double > Cost
Definition: vehicle.h:74
std::ostream & operator<<(std::ostream &log, const Node &node)
Definition: node.cpp:41