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