-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvehicle_node.cpp
More file actions
216 lines (181 loc) · 5.64 KB
/
Copy pathvehicle_node.cpp
File metadata and controls
216 lines (181 loc) · 5.64 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
/*PGR-GNU*****************************************************************
File: vehicle_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/vehicle_node.hpp"
#include "cpp_common/assert.hpp"
#include "c_types/solution_rt.h"
namespace vrprouting {
namespace problem {
/**
@returns the value of the objective function for this node
*/
double
Vehicle_node::objective() const {
return static_cast<double>(travel_time());
}
/**
* @param[in] cargoLimit of the vehicle
*
* @pre node is a starting node
*
* - evaluates the node
* - starts the cumulative information
*/
void
Vehicle_node::evaluate(PAmount cargoLimit) {
pgassert(is_start());
/* time */
m_travel_time = 0;
m_arrival_time = opens();
m_wait_time = 0;
m_departure_time = arrival_time() + service_time();
/* time aggregates */
m_tot_travel_time = 0;
m_tot_wait_time = 0;
m_tot_service_time = service_time();
/* cargo aggregates */
m_cargo = demand();
/* violation aggregates */
m_twvTot = m_cvTot = 0;
m_cvTot = has_cv(cargoLimit) ? 1 : 0;
m_delta_time = 0;
}
/**
@param[in] pred The node preceding this node in the path.
@param[in] cargoLimit of the vehicle.
@param[in] speed value to use for calculations
*/
void
Vehicle_node::evaluate(
const Vehicle_node &pred,
PAmount cargoLimit,
Speed speed
) {
/* time */
m_travel_time = pred.travel_time_to(*this, pred.departure_time(), speed);
m_arrival_time = pred.departure_time() + travel_time();
m_wait_time = is_early_arrival(arrival_time()) ?
opens() - m_arrival_time :
0;
m_departure_time = arrival_time() + wait_time() + service_time();
/* time aggregates */
m_tot_travel_time = pred.total_travel_time() + travel_time();
m_tot_wait_time = pred.total_wait_time() + wait_time();
m_tot_service_time = pred.total_service_time() + service_time();
/* cargo aggregates */
if (is_dump() && pred.cargo() >= 0) {
demand(-pred.cargo());
}
m_cargo = pred.cargo() + demand();
/* violations aggregates */
m_twvTot = has_twv() ? pred.twvTot() + 1 : pred.twvTot();
m_cvTot = has_cv(cargoLimit) ? pred.cvTot() + 1 : pred.cvTot();
m_delta_time = departure_time() - pred.departure_time();
}
/**
* @param [in,out] log Place to store the printed status of the node
* @param [in] v Vehicle node to print
* @returns the new string
*/
std::ostream&
operator << (std::ostream &log, const Vehicle_node &v) {
log << static_cast<const Tw_node&>(v)
<< " twv = " << v.has_twv()
<< ", twvTot = " << v.twvTot()
<< ", cvTot = " << v.cvTot()
<< ", cargo = " << v.cargo()
<< ", travel_time = " << v.travel_time()
<< ", arrival_time = " << v.arrival_time()
<< ", wait_time = " << v.wait_time()
<< ", service_time = " << v.service_time()
<< ", departure_time = " << v.departure_time();
return log;
}
/**
* Creates a disconnected vehicle node
* A node that is not served by any vehicle
*
* @param[in] node Time window node
*/
Vehicle_node::Vehicle_node(const Tw_node &node)
: Tw_node(node),
m_travel_time(0),
m_arrival_time(0),
m_wait_time(0),
m_departure_time(0),
m_delta_time(0),
m_cargo(0),
m_twvTot(0),
m_cvTot(0),
m_tot_wait_time(0),
m_tot_travel_time(0),
m_tot_service_time(0) {
}
bool
Vehicle_node::deltaGeneratesTWV(TInterval delta_time) const {
return is_late_arrival(m_arrival_time + delta_time);
}
/**
* @param[in] cargoLimit of the vehicle
* @returns true when the node violates the capacity constraints
*
* A capacity violations happens when:
* - the node is a start or end node and the cargo is not 0
* - the node's cargo is greater than the limit or is negative
*/
bool
Vehicle_node::has_cv(PAmount cargoLimit) const {
return is_end() || is_start() ? m_cargo != 0
: m_cargo > cargoLimit || m_cargo < 0;
}
/**
* @returns true when the arrival happens after the closing time
*/
bool
Vehicle_node::has_twv() const {
return is_late_arrival(m_arrival_time);
}
/**
* @returns the vehicle's path in a structure for postgres
* @param [in] vid it the vid-th vehicle in the solution
* @param [in] v_id it the vehicle identifier of the current node
* @param [in] stop_seq using this sequence value for the current node
*/
Solution_rt
Vehicle_node::get_postgres_result(int vid, int64_t v_id, int stop_seq) const {
return Solution_rt{
vid,
v_id,
stop_seq,
/* order_id
* The order_id is invalid for stops type 0 and 5
*/
(type() == 0 || type() == 5)? -1 : order(),
id(),
type(),
static_cast<int64_t>(cargo()),
static_cast<int64_t>(travel_time()),
static_cast<int64_t>(arrival_time()),
static_cast<int64_t>(wait_time()),
static_cast<int64_t>(arrival_time() + wait_time()),
static_cast<int64_t>(service_time()),
static_cast<int64_t>(departure_time()),
cvTot(),
twvTot()};
}
} // namespace problem
} // namespace vrprouting