vrpRouting  0.3
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
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 "problem/tw_node.h"
27 
28 #include <limits>
29 #include <string>
30 
31 #include "cpp_common/pgr_assert.h"
32 #include "problem/matrix.h"
34 #include "c_types/vehicle_t.h"
35 
36 
37 namespace vrprouting {
38 namespace problem {
39 
40 const Matrix* Tw_node::m_time_matrix_ptr = nullptr;
41 
42 
51 Tw_node::travel_time_to(const Tw_node &other, TTimestamp time, Speed speed) const {
52  pgassert(speed != 0);
53  return static_cast<TInterval>(static_cast<Speed>(m_time_matrix_ptr->travel_time(id(), other.id(), time)) / speed);
54 }
55 
66 Tw_node::arrival_j_opens_i(const Tw_node &I, Speed speed) const {
67  if (m_type == kStart) return (std::numeric_limits<TTimestamp>::max)();
68  return I.opens() + I.service_time() + I.travel_time_to(*this, I.opens() + I.service_time(), speed);
69 }
70 
81 Tw_node::arrival_j_closes_i(const Tw_node &I, Speed speed) const {
82  if (m_type == kStart) return (std::numeric_limits<TTimestamp>::max)();
83  return I.closes() + I.service_time() + I.travel_time_to(*this, I.closes() + I.service_time(), speed);
84 }
85 
93 bool
94 Tw_node::is_compatible_IJ(const Tw_node &I, Speed speed) const {
95  /*
96  * I /-> kStart
97  */
98  if (m_type == kStart) return false;
99 
100  /*
101  * kEnd /-> (*this)
102  */
103  if (I.m_type == kEnd) return false;
104 
105  return !is_late_arrival(arrival_j_opens_i(I, speed));
106 }
107 
115 bool
117  return
118  is_compatible_IJ(I, speed)
119  && !is_early_arrival(arrival_j_opens_i(I, speed))
120  && is_late_arrival(arrival_j_closes_i(I, speed));
121 }
122 
130 bool
132  return
133  is_compatible_IJ(I, speed)
134  && !is_early_arrival(arrival_j_opens_i(I, speed))
135  && !is_late_arrival(arrival_j_closes_i(I, speed));
136 }
137 
145 bool
147  const Tw_node &I,
148  Speed speed) const {
149  return
150  is_compatible_IJ(I, speed)
151  && is_early_arrival(arrival_j_opens_i(I, speed));
152 }
153 
161 bool
163  return
164  is_compatible_IJ(I, speed)
165  && is_early_arrival(arrival_j_opens_i(I, speed));
166 }
167 
171 std::string Tw_node::type_str() const {
172  switch (type()) {
173  case kStart: return "S";
174  case kEnd: return "E";
175  case kDump: return "D";
176  case kLoad: return "L";
177  case kPickup: return "P";
178  case kDelivery: return "D";
179  default: return "UNKNOWN";
180  }
181 }
182 
186 bool
188  return
189  m_type == kStart
190  && (opens() < closes())
191  && (service_time() >= 0)
192  && (demand() == 0);
193 }
194 
198 bool
200  return m_type == kPickup
201  && (opens() < closes())
202  && (service_time() >= 0)
203  && (demand() > 0);
204 }
205 
209 bool
211  return m_type == kDelivery
212  && (opens() < closes())
213  && (service_time() >= 0)
214  && (demand() < 0);
215 }
216 
217 
221 bool
223  return m_type == kDump
224  && (opens() < closes())
225  && (service_time() >= 0)
226  && (demand() <= 0);
227 }
228 
229 
233 bool
235  return m_type == kLoad
236  && (opens() < closes())
237  && (service_time() >= 0)
238  && (demand() >= 0);
239 }
240 
244 bool
246  return m_type == kEnd
247  && (opens() < closes())
248  && (service_time() >= 0)
249  && (demand() == 0);
250 }
251 
252 
257 bool
258 Tw_node::operator ==(const Tw_node &other) const {
259  if (&other == this) return true;
260  return m_order == other.m_order
261  && m_opens == other.m_opens
262  && m_closes == other.m_closes
263  && m_service_time == other.m_service_time
264  && m_demand == other.m_demand
265  && m_type == other.m_type
266  && id() == other.id()
267  && idx() == other.idx();
268 }
269 
270 
274 bool Tw_node::is_valid() const {
275  switch (type()) {
276  case kStart:
277  return is_start();
278  break;
279 
280  case kEnd:
281  return is_end();
282  break;
283 
284  case kDump:
285  return is_dump();
286  break;
287 
288  case kDelivery:
289  return is_delivery();
290  break;
291 
292  case kPickup:
293  return is_pickup();
294  break;
295 
296  case kLoad:
297  return is_load();
298  break;
299 
300  default:
301  return false;
302  break;
303  }
304 
305  return false;
306 }
307 
314  size_t id,
315  const PickDeliveryOrders_t &data,
316  const NodeType &type) :
317  Identifier(id, data.pick_node_id),
318  m_order(data.id),
319  m_opens(data.pick_open_t),
320  m_closes(data.pick_close_t),
321  m_service_time(data.pick_service_t),
322  m_demand(data.demand),
323  m_type(type) {
324  if (m_type == kDelivery) {
326  m_opens = data.deliver_open_t;
327  m_closes = data.deliver_close_t;
329  m_demand *= -1;
330  }
331  }
332 
339  size_t id,
340  const Vehicle_t &data,
341  const NodeType &type) :
342  Identifier(id, data.start_node_id),
343  m_opens(data.start_open_t),
344  m_closes(data.start_close_t),
345  m_service_time(data.start_service_t),
346  m_demand(0),
347  m_type(type) {
348  if (m_type == kEnd) {
349  reset_id(data.end_node_id);
350  m_opens = data.end_open_t;
351  m_closes = data.end_close_t;
353  }
354  }
355 
356 
362 std::ostream& operator << (std::ostream &log, const Tw_node &n) {
363  log << n.id()
364  << "[opens = " << n.m_opens
365  << "\tcloses = " << n.m_closes
366  << "\tservice = " << n.m_service_time
367  << "\tdemand = " << n.m_demand
368  << "\ttype = " << n.type_str()
369  << "]"
370  << "\n";
371  return log;
372 }
373 
374 } // namespace problem
375 } // namespace vrprouting
376 
PickDeliveryOrders_t::deliver_node_id
Id deliver_node_id
Deliver y coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:72
vrprouting::problem::Matrix::travel_time
TInterval travel_time(Id, Id, TTimestamp) const
retrun the travel time times when using the time dependant multipliers
Definition: matrix.cpp:233
vrprouting::problem::kDump
@ kDump
dump site, empties truck
Definition: node_types.h:39
vrprouting::problem::Tw_node::m_order
int64_t m_order
order to which it belongs (idx)
Definition: tw_node.h:174
vrprouting::problem::Tw_node::is_delivery
bool is_delivery() const
Is the node a valid order's delivery node.
Definition: tw_node.cpp:210
pickDeliveryOrders_t.h
vrprouting::problem::Tw_node::arrival_j_opens_i
TTimestamp arrival_j_opens_i(const Tw_node &I, Speed=1.0) const
arrival time at This node, when arrived at I at opening time TODO refine description
Definition: tw_node.cpp:66
vrprouting::problem::Tw_node::m_closes
TTimestamp m_closes
closing time of the node
Definition: tw_node.h:180
vehicle_t.h
Vehicle_t::end_node_id
Id end_node_id
Definition: vehicle_t.h:65
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
vrprouting::Identifier::id
int64_t id() const
get the original id
Definition: identifier.cpp:42
vrprouting::problem::kStart
@ kStart
starting site
Definition: node_types.h:36
vrprouting::problem::Tw_node::demand
Amount demand() const
Returns the demand associated with this node.
Definition: tw_node.h:163
vrprouting::problem::Tw_node::is_partially_compatible_IJ
bool is_partially_compatible_IJ(const Tw_node &I, Speed=1.0) const
can arrive to this after visiting as late as possible I? TODO refine description
Definition: tw_node.cpp:116
vrprouting::problem::Tw_node::closes
TTimestamp closes() const
Returns the closing time.
Definition: tw_node.h:81
vrprouting::problem::Tw_node::operator==
bool operator==(const Tw_node &rhs) const
equality operator
Definition: tw_node.cpp:258
PickDeliveryOrders_t::deliver_close_t
TTimestamp deliver_close_t
Deliver open time.
Definition: pickDeliveryOrders_t.h:75
vrprouting::problem::Tw_node::is_partially_waitTime_compatible_IJ
bool is_partially_waitTime_compatible_IJ(const Tw_node &I, Speed=1.0) const
can arrive to this after visiting as late as possible I? TODO refine description
Definition: tw_node.cpp:146
Vehicle_t
vehicles's attributes
Definition: vehicle_t.h:50
vrprouting::problem::kDelivery
@ kDelivery
delivery site
Definition: node_types.h:38
vrprouting::problem::operator<<
std::ostream & operator<<(std::ostream &log, const Order &order)
Prints:
Definition: order.cpp:77
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
tw_node.h
vrprouting::problem::Tw_node::type_str
std::string type_str() const
returns a string code of the kind of node
Definition: tw_node.cpp:171
vrprouting::problem::Tw_node::service_time
TInterval service_time() const
Returns the service time for this node.
Definition: tw_node.h:84
vrprouting::problem::Tw_node::is_dump
bool is_dump() const
Is the node a valid vehicle's dumping node.
Definition: tw_node.cpp:222
Vehicle_t::end_open_t
TTimestamp end_open_t
End node's identifier.
Definition: vehicle_t.h:66
vrprouting::problem::Tw_node::type
auto type() const
Returns the type of this node.
Definition: tw_node.h:87
Speed
double Speed
Definition: typedefs.h:76
matrix.h
vrprouting::problem::Tw_node::is_late_arrival
bool is_late_arrival(TTimestamp arrival_time) const
True when arrivalTime is after it closes.
Definition: tw_node.h:121
vrprouting::problem::kLoad
@ kLoad
load site, fills the truck
Definition: node_types.h:40
vrprouting::Identifier::idx
size_t idx() const
get the internal index
Definition: identifier.cpp:37
vrprouting::problem::Tw_node::is_pickup
bool is_pickup() const
Is the node a valid order's pickup node.
Definition: tw_node.cpp:199
vrprouting::problem::Tw_node::is_compatible_IJ
bool is_compatible_IJ(const Tw_node &I, Speed=1.0) const
is possible to arrive to this after visiting other?
Definition: tw_node.cpp:94
vrprouting::problem::kPickup
@ kPickup
pickup site
Definition: node_types.h:37
pgr_assert.h
An assert functionality that uses C++ throw().
vrprouting::problem::kEnd
@ kEnd
ending site
Definition: node_types.h:41
TTimestamp
int64_t TTimestamp
Definition: typedefs.h:71
vrprouting::problem::Tw_node::m_demand
Amount m_demand
The demand for the Node.
Definition: tw_node.h:186
vrprouting::problem::Tw_node
Time window attributes of a node.
Definition: tw_node.h:54
vrprouting::problem::Tw_node::Tw_node
Tw_node()=delete
Creating a Tw_node is not permitted.
PickDeliveryOrders_t::deliver_open_t
TTimestamp deliver_open_t
Deliver node identifier.
Definition: pickDeliveryOrders_t.h:74
PickDeliveryOrders_t::deliver_service_t
TInterval deliver_service_t
Deliver close time.
Definition: pickDeliveryOrders_t.h:76
vrprouting::problem::Tw_node::arrival_j_closes_i
TTimestamp arrival_j_closes_i(const Tw_node &I, Speed=1.0) const
arrival time at This node, when arrived at I at closing time TODO refine description
Definition: tw_node.cpp:81
vrprouting::problem::Tw_node::is_end
bool is_end() const
Is the node a valid vehicle's ending node.
Definition: tw_node.cpp:245
Vehicle_t::end_close_t
TTimestamp end_close_t
End open time.
Definition: vehicle_t.h:67
vrprouting::problem::NodeType
NodeType
Definition: node_types.h:35
vrprouting::problem::Tw_node::is_waitTime_compatible_IJ
bool is_waitTime_compatible_IJ(const Tw_node &I, Speed=1.0) const
can arrive to this after visiting as late as possible I? TODO refine description
Definition: tw_node.cpp:162
TInterval
int64_t TInterval
Definition: typedefs.h:72
vrprouting::problem::Tw_node::m_time_matrix_ptr
static const Matrix * m_time_matrix_ptr
Definition: tw_node.h:139
vrprouting::problem::Tw_node::m_service_time
TInterval m_service_time
time it takes to be served
Definition: tw_node.h:183
vrprouting::Identifier
Class that stores the information about identifiers.
Definition: identifier.h:65
vrprouting::problem::Tw_node::is_load
bool is_load() const
Is the node a valid vehicle's loading node.
Definition: tw_node.cpp:234
vrprouting::problem::Tw_node::is_early_arrival
bool is_early_arrival(TTimestamp arrival_time) const
True when arrivalTime is before it opens.
Definition: tw_node.h:126
vrprouting::problem::Tw_node::is_tight_compatible_IJ
bool is_tight_compatible_IJ(const Tw_node &I, Speed=1.0) const
can arrive to this after visiting as late as possible I? TODO refine description
Definition: tw_node.cpp:131
vrprouting::problem::Tw_node::travel_time_to
TInterval travel_time_to(const Tw_node &, TTimestamp, Speed=1.0) const
travel time to other node.
Definition: tw_node.cpp:51
vrprouting::problem::Tw_node::opens
TTimestamp opens() const
Returns the opening time.
Definition: tw_node.h:78
Vehicle_t::end_service_t
TInterval end_service_t
End close time.
Definition: vehicle_t.h:68
vrprouting::Identifier::reset_id
void reset_id(int64_t)
change the original id
Definition: identifier.cpp:47
vrprouting::problem::Tw_node::is_start
bool is_start() const
Is the node a valid vehicle's starting node.
Definition: tw_node.cpp:187
vrprouting::problem::Tw_node::is_valid
bool is_valid() const
is the node valid?
Definition: tw_node.cpp:274
vrprouting::problem::Tw_node::m_type
NodeType m_type
The kind of Node.
Definition: tw_node.h:189
vrprouting
Definition: base_matrix.cpp:46
vrprouting::problem::Tw_node::m_opens
TTimestamp m_opens
opening time of the node
Definition: tw_node.h:177