PGROUTING  2.6-dev
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 #include "vrp/solution.h"
27 
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 
32 #include "vrp/pgr_pickDeliver.h"
34 
35 namespace pgrouting {
36 namespace vrp {
37 
38 std::vector<General_vehicle_orders_t>
40  std::vector<General_vehicle_orders_t> result;
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);
46  result.insert(result.end(), data.begin(), data.end());
47 
48  ++i;
49  }
50  return result;
51 }
52 
53 
54 
55 bool
57  for (const auto v : fleet) {
58  if (v.is_feasable()) continue;
59  return false;
60  }
61  return true;
62 }
63 
64 double
66  double total(0);
67  for (const auto v : fleet) {
68  total += v.duration();
69  }
70  return total;
71 }
72 
73 int
75  int total(0);
76  for (const auto v : fleet) {
77  total += v.twvTot();
78  }
79  return total;
80 }
81 
82 double
84  double total(0);
85  for (const auto v : fleet) {
86  total += v.total_wait_time();
87  }
88  return total;
89 }
90 
91 double
93  double total(0);
94  for (const auto v : fleet) {
95  total += v.total_travel_time();
96  }
97  return total;
98 }
99 
100 double
102  double total(0);
103  for (const auto v : fleet) {
104  total += v.total_service_time();
105  }
106  return total;
107 }
108 
109 int
111  int total(0);
112  for (const auto v : fleet) {
113  total += v.cvTot();
114  }
115  return total;
116 }
117 
119 Solution::cost() const {
120  double total_duration(0);
121  double total_wait_time(0);
122  int total_twv(0);
123  int total_cv(0);
124  for (const auto v : fleet) {
125  total_duration += v.duration();
126  total_wait_time += v.total_wait_time();
127  total_twv += v.twvTot();
128  total_cv += v.cvTot();
129  }
130  return std::make_tuple(
131  total_twv, total_cv, fleet.size(),
132  total_wait_time, total_duration);
133 }
134 
135 
136 
137 std::string
139  Vehicle::Cost s_cost(cost());
140  std::ostringstream log;
141 
142  log << "(twv, cv, fleet, wait, duration) = ("
143  << std::get<0>(s_cost) << ", "
144  << std::get<1>(s_cost) << ", "
145  << std::get<2>(s_cost) << ", "
146  << std::get<3>(s_cost) << ", "
147  << std::get<4>(s_cost) << ")";
148 
149  return log.str();
150 }
151 
152 std::string
153 Solution::tau(const std::string &title) const {
154  Vehicle::Cost s_cost(cost());
155  std::ostringstream log;
156 
157  log << "\n" << title << ": " << std::endl;
158  for (const auto v : fleet) {
159  log << "\n" << v.tau();
160  }
161  log << "\n" << cost_str() << "\n";
162  return log.str();
163 }
164 
165 void
167  std::sort(fleet.begin(), fleet.end(), []
168  (const Vehicle_pickDeliver &lhs, const Vehicle_pickDeliver &rhs)
169  -> bool {
170  return lhs.idx() < rhs.idx();
171  });
172 }
173 
174 std::ostream&
175 operator << (std::ostream &log, const Solution &solution) {
176  for (const auto vehicle : solution.fleet) {
177  log << vehicle;
178  }
179 
180  log << "\n SOLUTION:\n\n "
181  << solution.tau();
182 
183  return log;
184 }
185 
186 bool
187 Solution::operator<(const Solution &s_rhs) const {
188  Vehicle::Cost lhs(cost());
189  Vehicle::Cost rhs(s_rhs.cost());
190 
191  /*
192  * capacity violations
193  */
194  if (std::get<0>(lhs) < std::get<0>(rhs))
195  return true;
196  if (std::get<0>(lhs) > std::get<0>(rhs))
197  return false;
198 
199  /*
200  * time window violations
201  */
202  if (std::get<1>(lhs) < std::get<1>(rhs))
203  return true;
204  if (std::get<1>(lhs) > std::get<1>(rhs))
205  return false;
206 
207  /*
208  * fleet size
209  */
210  if (std::get<2>(lhs) < std::get<2>(rhs))
211  return true;
212  if (std::get<2>(lhs) > std::get<2>(rhs))
213  return false;
214 
215  /*
216  * waiting time
217  */
218  if (std::get<3>(lhs) < std::get<3>(rhs))
219  return true;
220  if (std::get<3>(lhs) > std::get<3>(rhs))
221  return false;
222 
223  /*
224  * duration
225  */
226  if (std::get<4>(lhs) < std::get<4>(rhs))
227  return true;
228  if (std::get<4>(lhs) > std::get<4>(rhs))
229  return false;
230 
231  return false;
232 }
233 
235  EPSILON(0.0001),
236  trucks(problem->trucks()) {
237  ENTERING();
238  for (const auto &t : trucks) {
239  msg.log << t.tau() << "\n";
240  }
241  EXITING();
242 }
243 
244 } // namespace vrp
245 } // namespace pgrouting
bool is_feasable() const
Definition: solution.cpp:56
bool operator<(const Solution &s_rhs) const
Definition: solution.cpp:187
static Pgr_pickDeliver * problem
Definition: pd_problem.h:51
Vehicle::Cost cost() const
Definition: solution.cpp:119
std::ostringstream log
Stores the hint information.
Definition: pgr_messages.h:102
std::deque< Vehicle_pickDeliver > fleet
Definition: solution.h:49
#define EXITING()
Definition: pgr_messages.h:119
#define ENTERING()
Definition: pgr_messages.h:118
double wait_time() const
Definition: solution.cpp:83
std::vector< General_vehicle_orders_t > get_postgres_result() const
Definition: solution.cpp:39
std::string tau(const std::string &title="Tau") const
Definition: solution.cpp:153
friend std::ostream & operator<<(std::ostream &log, const Solution &solution)
Definition: solution.cpp:175
Book keeping class for swapping orders between vehicles.
Definition: basic_edge.cpp:28
static Pgr_messages msg
Definition: pd_problem.h:48
double total_travel_time() const
Definition: solution.cpp:92
std::string cost_str() const
Definition: solution.cpp:138
double duration() const
Definition: solution.cpp:65
double total_service_time() const
Definition: solution.cpp:101
std::tuple< int, int, size_t, double, double > Cost
Definition: vehicle.h:86
size_t idx() const
Definition: identifier.cpp:37