vrpRouting  0.3
orders.h
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: orders.h
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 
28 #ifndef INCLUDE_PROBLEM_ORDERS_H_
29 #define INCLUDE_PROBLEM_ORDERS_H_
30 #pragma once
31 
32 #include <vector>
33 #include <algorithm>
34 
35 #include "problem/order.h"
37 #include "cpp_common/pgr_assert.h"
39 #include "problem/tw_node.h"
40 #include "problem/vehicle_node.h"
41 #include "problem/node_types.h"
42 
43 
44 
45 namespace vrprouting {
46 namespace problem {
47 
48 class Orders : public std::vector<Order> {
49  public:
50  using std::vector<Order>::size;
51  Orders() = default;
52  Orders(const Orders&) = default;
53 
54  // todo remove template its problem::PickDeliver
55  template <typename PTR>
56  Orders(PickDeliveryOrders_t* p_orders, size_t p_size_orders, const PTR problem_ptr) {
57  Tw_node::m_time_matrix_ptr = &problem_ptr->time_matrix();
58  build_orders(p_orders, p_size_orders, problem_ptr);
59  }
60 
62  size_t find_best_I(const Identifiers<size_t> &within_this_set) const;
63 
65  size_t find_best_J(const Identifiers<size_t> &within_this_set) const;
66 
68  size_t find_best_I_J(const Identifiers<size_t> &within_this_set) const;
69 
71  void set_compatibles(Speed = 1.0);
72 
74  bool is_valid(Speed = 1.0) const;
75 
76  friend std::ostream& operator<<(std::ostream &log, const Orders &p_orders) {
77  log << "Orders\n";
78  for (const auto &o : p_orders) log << o << "\n";
79  log << "end Orders\n";
80  return log;
81  }
82 
83  private:
84  template <typename PTR>
85  void build_orders(PickDeliveryOrders_t *, size_t, const PTR problem_ptr);
86 
88  void add_order(const PickDeliveryOrders_t &order,
89  const Vehicle_node &pick,
90  const Vehicle_node &drop) {
91  push_back(Order(size(), order.id, pick, drop));
92  }
93 };
94 
100 template <typename PTR>
101 void
102 Orders::build_orders(PickDeliveryOrders_t *orders, size_t size_orders, const PTR problem_ptr) {
106  std::sort(orders, orders + size_orders,
107  [] (const PickDeliveryOrders_t &lhs, const PickDeliveryOrders_t &rhs) {
108  if (lhs.pick_open_t == rhs.pick_open_t) {
109  if (lhs.deliver_close_t == rhs.deliver_close_t) {
110  return lhs.id < rhs.id;
111  } else {
112  return lhs.deliver_close_t < rhs.deliver_close_t;
113  }
114  } else {
115  return lhs.pick_open_t < rhs.pick_open_t;
116  }
117  });
118 
119  for (size_t i = 0; i < size_orders; ++i) {
120  auto order = orders[i];
121  Vehicle_node pick({problem_ptr->node_id()++, order, NodeType::kPickup});
122  Vehicle_node drop({problem_ptr->node_id()++, order, NodeType::kDelivery});
123 
124  problem_ptr->add_node(pick);
125  problem_ptr->add_node(drop);
126 
127  this->emplace_back(Order{size(), order.id, pick, drop});
128  }
129 }
130 
131 } // namespace problem
132 } // namespace vrprouting
133 
134 #endif // INCLUDE_PROBLEM_ORDERS_H_
vrprouting::problem::Orders::add_order
void add_order(const PickDeliveryOrders_t &order, const Vehicle_node &pick, const Vehicle_node &drop)
add in an order
Definition: orders.h:88
node_types.h
vrprouting::problem::Orders::build_orders
void build_orders(PickDeliveryOrders_t *, size_t, const PTR problem_ptr)
Definition: orders.h:102
pickDeliveryOrders_t.h
vrprouting::problem::Orders::is_valid
bool is_valid(Speed=1.0) const
is the order valid?
Definition: orders.cpp:110
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
vehicle_node.h
vrprouting::problem::Orders::operator<<
friend std::ostream & operator<<(std::ostream &log, const Orders &p_orders)
Definition: orders.h:76
vrprouting::problem::kDelivery
@ kDelivery
delivery site
Definition: node_types.h:38
vrprouting::problem::Orders::find_best_I_J
size_t find_best_I_J(const Identifiers< size_t > &within_this_set) const
find the best order -> this -> order
Definition: orders.cpp:89
vrprouting::problem::Orders::set_compatibles
void set_compatibles(Speed=1.0)
set the compatability between all orders
Definition: orders.cpp:128
vrprouting::problem::Orders::Orders
Orders(PickDeliveryOrders_t *p_orders, size_t p_size_orders, const PTR problem_ptr)
Definition: orders.h:56
tw_node.h
vrprouting::problem::Orders
Definition: orders.h:48
Speed
double Speed
Definition: typedefs.h:76
PickDeliveryOrders_t::id
Id id
Definition: pickDeliveryOrders_t.h:59
PickDeliveryOrders_t::pick_open_t
TTimestamp pick_open_t
Pickup node identifier.
Definition: pickDeliveryOrders_t.h:66
vrprouting::problem::Vehicle_node
Extend Tw_node to evaluate the vehicle at node level.
Definition: vehicle_node.h:49
vrprouting::problem::kPickup
@ kPickup
pickup site
Definition: node_types.h:37
pgr_assert.h
An assert functionality that uses C++ throw().
vrprouting::problem::Orders::Orders
Orders()=default
vrprouting::problem::Orders::find_best_J
size_t find_best_J(const Identifiers< size_t > &within_this_set) const
find the best order -> this -> order
Definition: orders.cpp:46
order.h
vrprouting::problem::Tw_node::m_time_matrix_ptr
static const Matrix * m_time_matrix_ptr
Definition: tw_node.h:139
vrprouting::problem::Orders::find_best_I
size_t find_best_I(const Identifiers< size_t > &within_this_set) const
find the best order -> this
Definition: orders.cpp:68
vrprouting::problem::Order
Definition: order.h:40
identifiers.hpp
Identifiers< size_t >
vrprouting
Definition: base_matrix.cpp:46