-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtw_node.cpp
More file actions
376 lines (326 loc) · 9.33 KB
/
Copy pathtw_node.cpp
File metadata and controls
376 lines (326 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*PGR-GNU*****************************************************************
File: tw_node.cpp
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#include "problem/tw_node.hpp"
#include <limits>
#include <string>
#include "cpp_common/assert.hpp"
#include "problem/matrix.hpp"
#include "cpp_common/orders_t.hpp"
#include "cpp_common/vehicle_t.hpp"
namespace vrprouting {
namespace problem {
const Matrix* Tw_node::m_time_matrix_ptr = nullptr;
/**
* @param [in] other - pointer to the other Tw_node
* @param [in] time - time of departure from previous node
* @param [in] speed - speed used for calculation
*
* @returns the travel time from @b this to @b other]
*/
TInterval
Tw_node::travel_time_to(const Tw_node &other, TTimestamp time, Speed speed) const {
pgassert(speed != 0);
return static_cast<TInterval>(static_cast<Speed>(m_time_matrix_ptr->travel_time(id(), other.id(), time)) / speed);
}
/**
* @param [in] I the previous node before @b this node
* @param [in] speed - speed used for calculation
*
* @returns the arrival time at @b this node after visiting @b I node when @b I opens
*
* I -> @b this
* Value of TWC when arriving to @b this node after visiting node I at opening time
*/
TTimestamp
Tw_node::arrival_j_opens_i(const Tw_node &I, Speed speed) const {
if (m_type == kStart) return (std::numeric_limits<TTimestamp>::max)();
return I.opens() + I.service_time() + I.travel_time_to(*this, I.opens() + I.service_time(), speed);
}
/**
* @param [in] I the previous node before @b this node
* @param [in] speed - speed used for calculation
*
* @returns the arrival time at @b this node after visiting @b I node when @b I closes
*
* I -> @b this
* Value of TWC when arriving to @b this node after visiting node I at closing time
*/
TTimestamp
Tw_node::arrival_j_closes_i(const Tw_node &I, Speed speed) const {
if (m_type == kStart) return (std::numeric_limits<TTimestamp>::max)();
return I.closes() + I.service_time() + I.travel_time_to(*this, I.closes() + I.service_time(), speed);
}
/**
* @param [in] I node visited before visiting @b this node
* @param [in] speed - speed used for calculation
* @returns true when arriving @b this node after visiting node I does not cause a time window violation
* @returns false @b this node is a starting node (no node can be visited before a starting node)
* @returns false I node is an ending node (no node can be visited after an ending node)
*/
bool
Tw_node::is_compatible_IJ(const Tw_node &I, Speed speed) const {
/*
* I /-> kStart
*/
if (m_type == kStart) return false;
/*
* kEnd /-> (*this)
*/
if (I.m_type == kEnd) return false;
return !is_late_arrival(arrival_j_opens_i(I, speed));
}
/**
* @param [in] I node visited before visiting @b this node
* @param [in] speed - speed used for calculation
* @returns true when TODO TBD
* @returns false @b this node is TODO TBD
* @returns false I node is TODO TBD
*/
bool
Tw_node::is_partially_compatible_IJ(const Tw_node &I, Speed speed) const {
return
is_compatible_IJ(I, speed)
&& !is_early_arrival(arrival_j_opens_i(I, speed))
&& is_late_arrival(arrival_j_closes_i(I, speed));
}
/**
* @param [in] I node visited before visiting @b this node
* @param [in] speed - speed used for calculation
* @returns true when TODO TBD
* @returns false @b this node is TODO TBD
* @returns false I node is TODO TBD
*/
bool
Tw_node::is_tight_compatible_IJ(const Tw_node &I, Speed speed) const {
return
is_compatible_IJ(I, speed)
&& !is_early_arrival(arrival_j_opens_i(I, speed))
&& !is_late_arrival(arrival_j_closes_i(I, speed));
}
/**
* @param [in] I node visited before visiting @b this node
* @param [in] speed - speed used for calculation
* @returns true when TODO TBD
* @returns false @b this node is TODO TBD
* @returns false I node is TODO TBD
*/
bool
Tw_node::is_partially_waitTime_compatible_IJ(
const Tw_node &I,
Speed speed) const {
return
is_compatible_IJ(I, speed)
&& is_early_arrival(arrival_j_opens_i(I, speed));
}
/**
* @param [in] I node visited before visiting @b this node
* @param [in] speed - speed used for calculation
* @returns true when TODO TBD
* @returns false @b this node is TODO TBD
* @returns false I node is TODO TBD
*/
bool
Tw_node::is_waitTime_compatible_IJ(const Tw_node &I, Speed speed) const {
return
is_compatible_IJ(I, speed)
&& is_early_arrival(arrival_j_opens_i(I, speed));
}
/**
* @returns the short hand of the kind of node
*/
std::string Tw_node::type_str() const {
switch (type()) {
case kStart: return "S";
case kEnd: return "E";
case kDump: return "D";
case kLoad: return "L";
case kPickup: return "P";
case kDelivery: return "D";
default: return "UNKNOWN";
}
}
/**
* @returns true when the node is a starting node and is valid
*/
bool
Tw_node::is_start() const {
return
m_type == kStart
&& (opens() < closes())
&& (service_time() >= 0)
&& (demand() == 0);
}
/**
* @returns true when the node is a pickup node and is valid
*/
bool
Tw_node::is_pickup() const {
return m_type == kPickup
&& (opens() < closes())
&& (service_time() >= 0)
&& (demand() > 0);
}
/**
* @returns true when the node is a delivery node and is valid
*/
bool
Tw_node::is_delivery() const {
return m_type == kDelivery
&& (opens() < closes())
&& (service_time() >= 0)
&& (demand() < 0);
}
/**
* @returns true when the node is a dump node and is valid
*/
bool
Tw_node::is_dump() const {
return m_type == kDump
&& (opens() < closes())
&& (service_time() >= 0)
&& (demand() <= 0);
}
/**
* @returns true when the node is a load node and is valid
*/
bool
Tw_node::is_load() const {
return m_type == kLoad
&& (opens() < closes())
&& (service_time() >= 0)
&& (demand() >= 0);
}
/**
* @returns true when the node is an end node and is valid
*/
bool
Tw_node::is_end() const {
return m_type == kEnd
&& (opens() < closes())
&& (service_time() >= 0)
&& (demand() == 0);
}
/**
@returns true when the other node is the same as @b this node
@param [in] other
*/
bool
Tw_node::operator ==(const Tw_node &other) const {
if (&other == this) return true;
return m_order == other.m_order
&& m_opens == other.m_opens
&& m_closes == other.m_closes
&& m_service_time == other.m_service_time
&& m_demand == other.m_demand
&& m_type == other.m_type
&& id() == other.id()
&& idx() == other.idx();
}
/**
@returns true when the node depending on the kind is a valid node
*/
bool Tw_node::is_valid() const {
switch (type()) {
case kStart:
return is_start();
break;
case kEnd:
return is_end();
break;
case kDump:
return is_dump();
break;
case kDelivery:
return is_delivery();
break;
case kPickup:
return is_pickup();
break;
case kLoad:
return is_load();
break;
default:
return false;
break;
}
return false;
}
/**
@param [in] id the internal id of the node
@param [in] data the postgrSQL order information
@param [in] type the kind of node to be created
*/
Tw_node::Tw_node(
size_t id,
const Orders_t &data,
const NodeType &type) :
Identifier(id, data.pick_node_id),
m_order(data.id),
m_opens(data.pick_open_t),
m_closes(data.pick_close_t),
m_service_time(data.pick_service_t),
m_demand(data.demand),
m_type(type) {
if (m_type == kDelivery) {
reset_id(data.deliver_node_id);
m_opens = data.deliver_open_t;
m_closes = data.deliver_close_t;
m_service_time = data.deliver_service_t;
m_demand *= -1;
}
}
/**
@param [in] id the internal id of the node
@param [in] data the postgrSQL vehicle information
@param [in] type the kind of node to be created
*/
Tw_node::Tw_node(
size_t id,
const Vehicle_t &data,
const NodeType &type) :
Identifier(id, data.start_node_id),
m_opens(data.start_open_t),
m_closes(data.start_close_t),
m_service_time(data.start_service_t),
m_demand(0),
m_type(type) {
if (m_type == kEnd) {
reset_id(data.end_node_id);
m_opens = data.end_open_t;
m_closes = data.end_close_t;
m_service_time = data.end_service_t;
}
}
/**
@returns the printed information of the node
@param [in] log
@param [in] n
*/
std::ostream& operator << (std::ostream &log, const Tw_node &n) {
log << n.id()
<< "[opens = " << n.m_opens
<< "\tcloses = " << n.m_closes
<< "\tservice = " << n.m_service_time
<< "\tdemand = " << n.m_demand
<< "\ttype = " << n.type_str()
<< "]"
<< "\n";
return log;
}
} // namespace problem
} // namespace vrprouting