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