PGROUTING  3.2
pgr_pickDeliver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 FILE: pgr_pickDeliver.cpp
3 
4 Copyright (c) 2015 pgRouting developers
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 #include "vrp/pgr_pickDeliver.h"
26 
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 #include <algorithm>
31 #include <utility>
32 
33 #include "cpp_common/pgr_assert.h"
34 
35 #include "vrp/initials_code.h"
36 #include "vrp/vehicle_node.h"
38 #include "vrp/order.h"
39 #include "vrp/pd_orders.h"
40 #include "vrp/fleet.h"
41 #include "vrp/solution.h"
42 #include "vrp/initial_solution.h"
43 #include "vrp/optimize.h"
44 
45 namespace pgrouting {
46 namespace vrp {
47 
48 
49 
50 void
52  auto initial_sols = solutions;
53 
54  if (m_initial_id == 0) {
55  msg.log << "trying all \n";
56  for (int i = 1; i < 7; ++i) {
57  initial_sols.push_back(Initial_solution((Initials_code)i, m_orders.size()));
58  msg.log << "solution " << i << "\n" << initial_sols.back().tau();
59  // TODO(vicky) calculate the time it takes
60  msg.log << "Initial solution " << i
61  << " duration: " << initial_sols.back().duration();
62  }
63  } else {
64  msg.log << "only trying " << m_initial_id << "\n";
65  initial_sols.push_back(Initial_solution((Initials_code)m_initial_id, m_orders.size()));
66  // TODO(vicky) calculate the time it takes
67  msg.log << "Initial solution " << m_initial_id
68  << " duration: " << initial_sols[0].duration();
69  }
70 
71 
72  /*
73  * Sorting solutions: the best is at the back
74  */
75  pgassert(!initial_sols.empty());
76  std::sort(initial_sols.begin(), initial_sols.end(), []
77  (const Solution &lhs, const Solution &rhs) -> bool {
78  return rhs < lhs;
79  });
80 
81 #if 1
82  solutions.push_back(Optimize(initial_sols.back(), m_max_cycles));
83 #else
84  solutions.push_back(initial_sols.back());
85 #endif
86  pgassert(!solutions.empty());
87 
88  msg.log << "best solution duration = " << solutions.back().duration();
89 }
90 
91 
92 
93 std::vector< General_vehicle_orders_t >
95  auto result = solutions.back().get_postgres_result();
96 
97  General_vehicle_orders_t aggregates = {
98  /*
99  * Vehicle id = -2 indicates its an aggregate row
100  *
101  * (twv, cv, fleet, wait, duration)
102  */
103  -2, // summary row on vehicle_number
104  solutions.back().twvTot(), // on vehicle_id
105  solutions.back().cvTot(), // on vehicle_seq
106  -1, // on order_id
107  -1, // on stop_id
108  -2, // on stop_type (gets increased later by one so it gets -1)
109  -1, // not accounting total loads
110  solutions.back().total_travel_time(),
111  -1, // not accounting arrival_travel_time
112  solutions.back().wait_time(),
113  solutions.back().total_service_time(),
114  solutions.back().duration(),
115  };
116  result.push_back(aggregates);
117 
118 
119 #ifndef NDEBUG
120  for (const auto &sol : solutions) {
121  msg.log << sol.tau();
122  }
123 #endif
124  return result;
125 }
126 
127 void
129  m_nodes.push_back(node);
130 }
131 
136  const std::vector<PickDeliveryOrders_t> &pd_orders,
137  const std::vector<Vehicle_t> &vehicles,
138  const pgrouting::tsp::Dmatrix &cost_matrix,
139  double factor,
140  size_t p_max_cycles,
141  int initial) :
142  PD_problem(this),
143  m_initial_id(initial),
144  m_max_cycles(p_max_cycles),
145  m_nodes(),
146  m_cost_matrix(cost_matrix),
147  m_orders(pd_orders),
148  m_trucks(vehicles, factor) {
149  ENTERING(msg);
150  pgassert(!pd_orders.empty());
151  pgassert(!vehicles.empty());
153  if (!(m_initial_id > 0 && m_initial_id < OneDepot)) {
154  msg.log << "\n m_initial_id " << m_initial_id;
155  }
156  pgassertwm(m_initial_id > 0 && m_initial_id <= OneDepot, msg.get_log().c_str());
157  pgassert(!m_nodes.empty());
158 
159  if (!msg.get_error().empty()) {
160  return;
161  }
162 
163 #if 0
164  pgassert(m_trucks.msg().get_error().empty());
165  pgassert(msg().get_error().empty());
166 #endif
167 
168  msg.log << "\n Checking fleet ...";
169  if (!m_trucks.is_fleet_ok()) {
170  pgassert(msg.get_error().empty());
171 #if 0
172  pgassert(!m_trucks.msg.get_error().empty());
174 #endif
175  return;
176  }
177  msg.log << "fleet OK \n";
178 
179 #if 0
180  for (const auto t : m_trucks) {
181  msg.log << t << "\n";
182  }
183  for (const auto &o : m_orders) {
184  msg.log << o << "\n";
185  }
186 #endif
187 
188  /*
189  * check the (S, P, D, E) order on all vehicles
190  * stop when a feasible truck is found
191  */
192  msg.log << "\n Checking orders";
193  for (const auto &o : m_orders) {
194  if (!m_trucks.is_order_ok(o)) {
195  msg.error << "Order not feasible on any truck was found";
196  msg.log << "The order "
197  << o.id()
198  << " is not feasible on any truck";
199  msg.log << "\n" << o;
200 #if 0
201  double old_speed(0);
202  for (auto t : m_trucks) {
203  if (old_speed == t.speed()) continue;
204  old_speed = t.speed();
205  msg.log << "****** With speed: " << t.speed() << "\n";
206  msg.log << t.start_site() << "\n";
207  msg.log << o.pickup() << "\n";
208  msg.log << "travel time to "
209  << t.start_site().travel_time_to(
210  o.pickup(), t.speed()) << "\n";
211 
212  msg.log << o.delivery() << "\n";
213  msg.log << t.end_site() << "\n";
214  msg.log << "travel time to "
215  << t.start_site().travel_time_to(
216  o.delivery(), t.speed())
217  << "\n";
218  t.push_back(o);
219  }
220 #endif
221  return;
222  }
223  }
224  msg.log << "orders OK \n";
225 
227 #if 0
228  msg.error << "foo";
229  for (const auto t : m_trucks) {
230  msg.log << t << "\n";
231  }
232 #endif
233  EXITING(msg);
234  } // constructor
235 
236 
237 
238 } // namespace vrp
239 } // namespace pgrouting
pgrouting::vrp::Pgr_pickDeliver::m_max_cycles
size_t m_max_cycles
maximum cycles in the optimization
Definition: pgr_pickDeliver.h:108
pgr_pickDeliver.h
pgrouting::vrp::PD_Orders::size
size_t size() const
Definition: pd_orders.h:79
pgrouting::vrp::Solution
Definition: solution.h:44
initial_solution.h
pgrouting::vrp::Pgr_pickDeliver::solutions
std::vector< Solution > solutions
Definition: pgr_pickDeliver.h:115
EXITING
#define EXITING(x)
Definition: pgr_messages.h:94
pgrouting::vrp::Fleet::is_order_ok
bool is_order_ok(const Order &order) const
Given an order, Cycle trhugh all the trucks to verify if the order can be served by at least one truc...
Definition: fleet.cpp:288
pgrouting::vrp::Pgr_pickDeliver::m_trucks
Fleet m_trucks
Definition: pgr_pickDeliver.h:114
vehicle_node.h
ENTERING
#define ENTERING(x)
Definition: pgr_messages.h:93
pgrouting::vrp::Vehicle_node
Extend Tw_node to evaluate the vehicle at node level.
Definition: vehicle_node.h:48
pgassertwm
#define pgassertwm(expr, msg)
Adds a message to the assertion.
Definition: pgr_assert.h:117
initials_code.h
pd_orders.h
pgrouting::Pgr_messages::error
std::ostringstream error
Stores the error information.
Definition: pgr_messages.h:85
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::Fleet::is_fleet_ok
bool is_fleet_ok() const
Definition: fleet.cpp:253
pgrouting::Pgr_messages::get_error
std::string get_error() const
get_error
Definition: pgr_messages.cpp:53
pgrouting::vrp::Fleet::msg
static Pgr_messages & msg()
the problem message
Definition: fleet.cpp:46
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::vrp::Pgr_pickDeliver::m_cost_matrix
pgrouting::tsp::Dmatrix m_cost_matrix
Definition: pgr_pickDeliver.h:111
pgrouting::vrp::OneDepot
@ OneDepot
Push front order that allows more orders to be inserted at the front.
Definition: initials_code.h:44
pgrouting::vrp::Pgr_pickDeliver::m_nodes
std::vector< Vehicle_node > m_nodes
Definition: pgr_pickDeliver.h:110
pgrouting::Pgr_messages::get_log
std::string get_log() const
get_log
Definition: pgr_messages.cpp:36
optimize.h
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::vrp::Pgr_pickDeliver::Initial_solution
friend Initial_solution
Definition: pgr_pickDeliver.h:62
pgrouting::vrp::Pgr_pickDeliver::get_postgres_result
std::vector< General_vehicle_orders_t > get_postgres_result() const
Definition: pgr_pickDeliver.cpp:94
vehicle_pickDeliver.h
General_vehicle_orders_t
Definition: general_vehicle_orders_t.h:49
solution.h
pgrouting::tsp::Dmatrix::empty
bool empty() const
Definition: Dmatrix.h:118
pgrouting::vrp::Pgr_pickDeliver::m_orders
PD_Orders m_orders
Definition: pgr_pickDeliver.h:113
order.h
pgrouting::vrp::Pgr_pickDeliver::solve
void solve()
Definition: pgr_pickDeliver.cpp:51
pgrouting::vrp::Fleet::set_compatibles
void set_compatibles(const PD_Orders &orders)
Definition: fleet.cpp:313
pgrouting::vrp::Pgr_pickDeliver::add_node
void add_node(const Vehicle_node &node)
Definition: pgr_pickDeliver.cpp:128
pgrouting::vrp::Optimize
Definition: optimize.h:41
pgrouting::vrp::PD_problem
Definition: pd_problem.h:42
pgrouting::tsp::Dmatrix
Definition: Dmatrix.h:43
pgrouting::vrp::Initials_code
Initials_code
Different kinds to insert an order into the vehicle.
Definition: initials_code.h:36
pgrouting::vrp::Pgr_pickDeliver::m_initial_id
int m_initial_id
used define the initial solution algorithm to be used
Definition: pgr_pickDeliver.h:105
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::vrp::Pgr_pickDeliver::Pgr_pickDeliver
Pgr_pickDeliver(const std::vector< PickDeliveryOrders_t > &pd_orders, const std::vector< Vehicle_t > &vehicles, const pgrouting::tsp::Dmatrix &cost_matrix, double factor, size_t max_cycles, int initial)
Constructor for the matrix version.
Definition: pgr_pickDeliver.cpp:135
fleet.h