PGROUTING  2.5
tw_node.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: tw_node.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/tw_node.h"
27 
28 #include <limits>
29 #include <string>
30 
31 #include "cpp_common/pgr_assert.h"
32 #include "vrp/pgr_pickDeliver.h"
33 
34 
35 namespace pgrouting {
36 namespace vrp {
37 
38 
39 double
40 Tw_node::travel_time_to(const Tw_node &to, double speed) const {
46  auto from = problem->m_base_nodes[idx()].get();
47  auto destination = problem->m_base_nodes[to.idx()].get();
48  pgassert(speed != 0);
50  return from->distance(destination) / speed;
51 }
52 
53 
54 /*
55  * I -> J = (*this)
56  */
57 double
58 Tw_node::arrival_j_opens_i(const Tw_node &I, double speed) const {
59  if (m_type == kStart) return (std::numeric_limits<double>::max)();
60  return I.opens() + I.service_time() + I.travel_time_to(*this, speed);
61 }
62 
63 double
64 Tw_node::arrival_j_closes_i(const Tw_node &I, double speed) const {
65  if (m_type == kStart) return (std::numeric_limits<double>::max)();
66  return I.closes() + I.service_time() + I.travel_time_to(*this, speed);
67 }
68 
69 
70 
71 
72 bool
73 Tw_node::is_compatible_IJ(const Tw_node &I, double speed) const {
74  /*
75  * I /-> kStart
76  */
77  if (m_type == kStart) return false;
78  /*
79  * kEnd /-> (*this)
80  */
81  if (I.m_type == kEnd) return false;
82 
83  return !is_late_arrival(arrival_j_opens_i(I, speed));
84 }
85 
86 bool
87 Tw_node::is_partially_compatible_IJ(const Tw_node &I, double speed) const {
88  return
89  is_compatible_IJ(I, speed)
90  && !is_early_arrival(arrival_j_opens_i(I, speed))
91  && is_late_arrival(arrival_j_closes_i(I, speed));
92 }
93 
94 bool
95 Tw_node::is_tight_compatible_IJ(const Tw_node &I, double speed) const {
96  return
97  is_compatible_IJ(I, speed)
98  && !is_early_arrival(arrival_j_opens_i(I, speed))
99  && !is_late_arrival(arrival_j_closes_i(I, speed));
100 }
101 
102 bool
104  const Tw_node &I,
105  double speed) const {
106  return
107  is_compatible_IJ(I, speed)
108  && is_early_arrival(arrival_j_opens_i(I, speed));
109 }
110 
111 bool
112 Tw_node::is_waitTime_compatible_IJ(const Tw_node &I, double speed) const {
113  return
114  is_compatible_IJ(I, speed)
115  && is_early_arrival(arrival_j_opens_i(I, speed));
116 }
117 
118 
119 std::string Tw_node::type_str() const {
120  switch (type()) {
121  case kStart: return "START"; break;
122  case kEnd: return "END"; break;
123  case kDump: return "DUMP"; break;
124  case kLoad: return "LOAD"; break;
125  case kPickup: return "PICKUP"; break;
126  case kDelivery: return "DELIVERY"; break;
127  default: return "UNKNOWN";
128  }
129 }
130 
131 bool
133  return
134  m_type == kStart
135  && (0 <= opens())
136  && (opens() < closes())
137  && (service_time() >= 0)
138  && (demand() == 0);
139 }
140 
141 bool
143  return m_type == kPickup
144  && (0 <= opens())
145  && (opens() < closes())
146  && (service_time() >= 0)
147  && (demand() > 0);
148 }
149 
150 
151 bool
153  return m_type == kDelivery
154  && (0 <= opens())
155  && (opens() < closes())
156  && (service_time() >= 0)
157  && (demand() < 0);
158 }
159 
160 
161 bool
163  return m_type == kDump
164  && (0 <= opens())
165  && (opens() < closes())
166  && (service_time() >= 0)
167  && (demand() <= 0);
168 }
169 
170 
171 bool
173  return m_type == kLoad
174  && (0 <= opens())
175  && (opens() < closes())
176  && (service_time() >= 0)
177  && (demand() >= 0);
178 }
179 
180 
181 bool
183  return m_type == kEnd
184  && (0 <= opens())
185  && (opens() < closes())
186  && (service_time() >= 0)
187  && (demand() == 0);
188 }
189 
190 
191 bool
192 Tw_node::operator ==(const Tw_node &other) const {
193  if (&other == this) return true;
194  auto lhs = static_cast<const Node&>(
195  *problem->m_base_nodes[idx()].get());
196  auto rhs = static_cast<const Node&>(
197  *problem->m_base_nodes[other.idx()].get());
198  return lhs == rhs;
199 }
200 
201 
202 
203 bool Tw_node::is_valid() const {
204  switch (type()) {
205  case kStart:
206  return is_start();
207  break;
208 
209  case kEnd:
210  return is_end();
211  break;
212 
213  case kDump:
214  return is_dump();
215  break;
216 
217  case kDelivery:
218  return is_delivery();
219  break;
220 
221  case kPickup:
222  return is_pickup();
223  break;
224 
225  case kLoad:
226  return is_load();
227  break;
228 
229  default:
230  return false;
231  break;
232  }
233 
234  return false;
235 }
236 
237 
239  size_t id,
241  NodeType type) :
242  Identifier(id, data.pick_node_id),
243  m_order(data.id),
244  m_opens(data.pick_open_t),
245  m_closes(data.pick_close_t),
246  m_service_time(data.pick_service_t),
247  m_demand(data.demand),
248  m_type(type) {
249  if (m_type == kDelivery) {
251  m_opens = data.deliver_open_t;
252  m_closes = data.deliver_close_t;
254  m_demand *= -1;
255  }
256  }
257 
259  size_t id,
260  Vehicle_t data,
261  NodeType type) :
262  Identifier(id, data.start_node_id),
263  m_opens(data.start_open_t),
264  m_closes(data.start_close_t),
265  m_service_time(data.start_service_t),
266  m_demand(0),
267  m_type(type) {
268  if (m_type == kEnd) {
269  reset_id(data.end_node_id);
270  m_opens = data.end_open_t;
271  m_closes = data.end_close_t;
273  }
274  }
275 
276 
278 std::ostream& operator << (std::ostream &log, const Tw_node &n) {
279  log << static_cast<const Node&>(
280  *n.problem->m_base_nodes[n.idx()].get())
281  << "[opens = " << n.m_opens
282  << "\tcloses = " << n.m_closes
283  << "\tservice = " << n.m_service_time
284  << "\tdemand = " << n.m_demand
285  << "\ttype = " << n.type_str()
286  << "]"
287  << "\n";
288  return log;
289 }
290 
291 } // namespace vrp
292 } // namespace pgrouting
293 
double m_opens
opening time of the node
Definition: tw_node.h:274
double demand() const
Returns the demand associated with this node.
Definition: tw_node.h:82
bool is_load() const
is_Load
Definition: tw_node.cpp:172
bool is_valid() const
Definition: tw_node.cpp:203
bool is_early_arrival(double arrival_time) const
True when arrivalTime is before it opens.
Definition: tw_node.h:187
double end_service_t
Definition: vehicle_t.h:85
bool is_end() const
is_end
Definition: tw_node.cpp:182
static Pgr_pickDeliver * problem
Definition: pd_problem.h:51
double m_demand
The demand for the Node.
Definition: tw_node.h:277
std::vector< std::unique_ptr< Base_node > > m_base_nodes
The Node class defines a point in 2D space with an id.
Definition: node.h:50
double end_close_t
Definition: vehicle_t.h:84
double end_open_t
Definition: vehicle_t.h:83
bool operator==(const Tw_node &rhs) const
Definition: tw_node.cpp:192
bool is_compatible_IJ(const Tw_node &I, double speed) const
Definition: tw_node.cpp:73
bool is_partially_waitTime_compatible_IJ(const Tw_node &I, double speed) const
Definition: tw_node.cpp:103
double service_time() const
Returns the service time for this node.
Definition: tw_node.h:86
double m_closes
closing time of the node
Definition: tw_node.h:275
dump site, empties truck
Definition: tw_node.h:63
double opens() const
Returns the opening time.
Definition: tw_node.h:76
load site, fills the truck
Definition: tw_node.h:64
int64_t m_order
order to which it belongs
Definition: tw_node.h:273
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
int64_t end_node_id
Definition: vehicle_t.h:81
bool is_late_arrival(double arrival_time) const
True when arrivalTime is after it closes.
Definition: tw_node.h:192
bool is_waitTime_compatible_IJ(const Tw_node &I, double speed) const
Definition: tw_node.cpp:112
std::string type_str() const
Definition: tw_node.cpp:119
bool is_start() const
@ {
Definition: tw_node.cpp:132
bool is_tight_compatible_IJ(const Tw_node &I, double speed) const
Definition: tw_node.cpp:95
NodeType type() const
Returns the type of this node.
Definition: tw_node.h:89
bool is_partially_compatible_IJ(const Tw_node &I, double speed) const
Definition: tw_node.cpp:87
double closes() const
Returns the closing time.
Definition: tw_node.h:79
bool is_pickup() const
is_pickup
Definition: tw_node.cpp:142
Book keeping class for swapping orders between vehicles.
Definition: basic_edge.cpp:28
Assertions Handling.
bool is_delivery() const
is_delivery
Definition: tw_node.cpp:152
bool is_dump() const
is_dump
Definition: tw_node.cpp:162
Extends the Node class to create a Node with time window attributes.
Definition: tw_node.h:57
void reset_id(int64_t)
Definition: identifier.cpp:47
NodeType m_type
The demand for the Node.
Definition: tw_node.h:278
size_t idx() const
Definition: identifier.cpp:37
double arrival_j_closes_i(const Tw_node &I, double speed) const
The actual arrival time at this node, given that: this node is visited directly after other node and ...
Definition: tw_node.cpp:64
double travel_time_to(const Tw_node &other, double speed) const
time = distance / speed.
Definition: tw_node.cpp:40
friend std::ostream & operator<<(std::ostream &log, const Tw_node &node)
Print the contents of a Twnode object.
Definition: tw_node.cpp:278
double arrival_j_opens_i(const Tw_node &I, double speed) const
@ {
Definition: tw_node.cpp:58