vrpRouting  0.3
anonymous_namespace{optimize_driver.cpp} Namespace Reference

Functions

std::vector< Short_vehicleget_initial_stops (Vehicle_t *vehicles_arr, size_t total_vehicles)
 get the original stops More...
 
std::vector< Short_vehicleone_processing (PickDeliveryOrders_t *shipments_arr, size_t total_shipments, Vehicle_t *vehicles_arr, size_t total_vehicles, std::vector< Short_vehicle > new_stops, const vrprouting::problem::Matrix &time_matrix, int max_cycles, int64_t execution_date)
 Executes an optimization with the input data. More...
 
Identifiers< TTimestampprocessing_times_by_shipment (PickDeliveryOrders_t *shipments_arr, size_t total_shipments)
 : extract the times where the shipments opens or closes More...
 
Identifiers< TTimestampprocessing_times_by_vehicle (Vehicle_t *vehicles_arr, size_t total_vehicles)
 : extract the times where the vehicle opens or closes More...
 
std::vector< Short_vehiclesubdivide_processing (PickDeliveryOrders_t *shipments_arr, size_t total_shipments, Vehicle_t *vehicles_arr, size_t total_vehicles, const vrprouting::problem::Matrix &time_matrix, int max_cycles, int64_t execution_date, bool subdivide_by_vehicle, std::ostringstream &log)
 Executes an optimization by subdivision of data. More...
 
void update_stops (std::vector< Short_vehicle > &the_stops, const std::vector< Short_vehicle > new_values)
 Update the vehicle stops to the new values. More...
 

Function Documentation

◆ get_initial_stops()

std::vector<Short_vehicle> anonymous_namespace{optimize_driver.cpp}::get_initial_stops ( Vehicle_t vehicles_arr,
size_t  total_vehicles 
)

get the original stops

Parameters
[in]vehicles_arrA C Array of vehicles
[in]total_vehiclessize of the vehicles_arr
Returns
(vehicle id, stops vector) pair which hold the stops structure

Definition at line 165 of file optimize_driver.cpp.

167  {
168  std::vector<Short_vehicle> the_stops;
169  for (size_t i = 0; i < total_vehicles; ++i) {
170  std::vector<Id> stops;
171  for (size_t j = 0; j < vehicles_arr[i].stops_size; ++j) {
172  stops.push_back(vehicles_arr[i].stops[j]);
173  }
174  the_stops.push_back({vehicles_arr[i].id, stops});
175  }
176  std::sort(the_stops.begin(), the_stops.end(), []
177  (const Short_vehicle &lhs, const Short_vehicle &rhs) {return lhs.id < rhs.id;});
178  return the_stops;
179 }

References Vehicle_t::id, and Vehicle_t::stops_size.

Referenced by subdivide_processing().

◆ one_processing()

std::vector<Short_vehicle> anonymous_namespace{optimize_driver.cpp}::one_processing ( PickDeliveryOrders_t shipments_arr,
size_t  total_shipments,
Vehicle_t vehicles_arr,
size_t  total_vehicles,
std::vector< Short_vehicle new_stops,
const vrprouting::problem::Matrix time_matrix,
int  max_cycles,
int64_t  execution_date 
)

Executes an optimization with the input data.

Parameters
[in]shipments_arrA C Array of pickup and dropoff shipments
[in]total_shipmentssize of the shipments_arr
[in]vehicles_arrA C Array of vehicles
[in]total_vehiclessize of the vehicles_arr
[in]new_stopsstops that override the original stops.
[in]time_matrixThe unique time matrix
[in]max_cyclesnumber of cycles to perform during the optimization phase
[in]execution_dateValue used for not moving shipments that are before this date
Returns
(vehicle id, stops vector) pair which hold the the new stops structure

Definition at line 69 of file optimize_driver.cpp.

75  {
76  try {
77  /*
78  * Construct problem
79  */
81  shipments_arr, total_shipments,
82  vehicles_arr, total_vehicles,
83  new_stops,
84  time_matrix);
85 
86  /*
87  * Unknown problem when createing the pick Deliver problem
88  */
89  if (!pd_problem.msg.get_error().empty()) {
90  throw std::make_pair(pd_problem.msg.get_error(), pd_problem.msg.get_log());
91  }
92 
93  /*
94  * get initial solution
95  */
96  using Initial_solution = vrprouting::initialsol::tabu::Initial_solution;
97  using Solution = vrprouting::problem::Solution;
98  auto sol = static_cast<Solution>(Initial_solution(execution_date, true, &pd_problem));
99 
100  /*
101  * Optimize the initial solution:
102  * - false: do not stop on all served
103  * - true: optimize
104  */
106  sol = Optimize(sol, static_cast<size_t>(max_cycles), false, true);
107 
108  return sol.get_stops();
109  } catch(...) {
110  throw;
111  }
112 }

References vrprouting::Pgr_messages::get_error(), vrprouting::Pgr_messages::get_log(), and vrprouting::problem::PickDeliver::msg.

Referenced by do_optimize(), and subdivide_processing().

◆ processing_times_by_shipment()

Identifiers<TTimestamp> anonymous_namespace{optimize_driver.cpp}::processing_times_by_shipment ( PickDeliveryOrders_t shipments_arr,
size_t  total_shipments 
)

: extract the times where the shipments opens or closes

Parameters
[in]shipments_arrA C Array of pickup and dropoff shipments
[in]total_shipmentssize of the shipments_arr
Returns
processing times

Definition at line 123 of file optimize_driver.cpp.

125  {
126  Identifiers<TTimestamp> processing_times;
127  for (size_t i = 0; i < total_shipments; ++i) {
128  processing_times += shipments_arr[i].pick_open_t;
129  processing_times += shipments_arr[i].pick_close_t;
130  processing_times += shipments_arr[i].deliver_open_t;
131  processing_times += shipments_arr[i].deliver_close_t;
132  }
133  return processing_times;
134 }

References PickDeliveryOrders_t::deliver_close_t, PickDeliveryOrders_t::deliver_open_t, PickDeliveryOrders_t::pick_close_t, and PickDeliveryOrders_t::pick_open_t.

Referenced by subdivide_processing().

◆ processing_times_by_vehicle()

Identifiers<TTimestamp> anonymous_namespace{optimize_driver.cpp}::processing_times_by_vehicle ( Vehicle_t vehicles_arr,
size_t  total_vehicles 
)

: extract the times where the vehicle opens or closes

Parameters
[in]vehicles_arrA C Array of vehicles
[in]total_vehiclessize of the vehicles_arr
Returns
processing times

Definition at line 144 of file optimize_driver.cpp.

146  {
147  Identifiers<TTimestamp> processing_times;
148  for (size_t i = 0; i < total_vehicles; ++i) {
149  processing_times += vehicles_arr[i].start_open_t;
150  processing_times += vehicles_arr[i].start_close_t;
151  processing_times += vehicles_arr[i].end_open_t;
152  processing_times += vehicles_arr[i].end_close_t;
153  }
154  return processing_times;
155 }

References Vehicle_t::end_close_t, Vehicle_t::end_open_t, Vehicle_t::start_close_t, and Vehicle_t::start_open_t.

Referenced by subdivide_processing().

◆ subdivide_processing()

std::vector<Short_vehicle> anonymous_namespace{optimize_driver.cpp}::subdivide_processing ( PickDeliveryOrders_t shipments_arr,
size_t  total_shipments,
Vehicle_t vehicles_arr,
size_t  total_vehicles,
const vrprouting::problem::Matrix time_matrix,
int  max_cycles,
int64_t  execution_date,
bool  subdivide_by_vehicle,
std::ostringstream &  log 
)

Executes an optimization by subdivision of data.

Parameters
[in]shipments_arrA C Array of pickup and dropoff shipments
[in]total_shipmentssize of the shipments_arr
[in]vehicles_arrA C Array of vehicles
[in]total_vehiclessize of the vehicles_arr
[in]new_stopsstops that override the original stops.
[in]time_matrixThe unique time matrix
[in]max_cyclesnumber of cycles to perform during the optimization phase
[in]execution_dateValue used for not moving shipments that are before this date
Returns
(vehicle id, stops vector) pair which hold the the new stops structure

Definition at line 213 of file optimize_driver.cpp.

220  {
221  try {
222  auto the_stops = get_initial_stops(vehicles_arr, total_vehicles);
223 
224  auto processing_times = subdivide_by_vehicle?
225  processing_times_by_vehicle(vehicles_arr, total_vehicles)
226  : processing_times_by_shipment(shipments_arr, total_shipments);
227 
228  Identifiers<Id> prev_shipments_in_stops;
229  for (const auto &t : processing_times) {
231  /*
232  * Get active vehicles at time t
233  */
234  auto vehicles_to_process = static_cast<size_t>(std::distance(vehicles_arr,
235  std::partition(
236  vehicles_arr, vehicles_arr + total_vehicles,
237  [&](const Vehicle_t& v)
238  {return v.start_open_t <= t && t <= v.end_close_t;})));
239 
240  /* Get shipments in stops of active vehicles */
241  Identifiers<Id> shipments_in_stops;
242  for (size_t i = 0; i < vehicles_to_process; ++i) {
243  auto v_id = vehicles_arr[i].id;
244  auto v_to_modify = std::find_if(
245  the_stops.begin(), the_stops.end(), [&]
246  (const Short_vehicle& v) -> bool {return v.id == v_id;});
247 
248  for (const auto &s : v_to_modify->stops) {
249  shipments_in_stops += s;
250  }
251  }
252 
253  /*
254  * Nothing to do:
255  * - no shipments to process
256  * - last optimization had exavtly the same shipments
257  */
258  if ((shipments_in_stops.size() == 0)
259  || (prev_shipments_in_stops == shipments_in_stops)) continue;
260  log << "\nOptimizing at time: " << t;
261 
262  prev_shipments_in_stops = shipments_in_stops;
263 
264  auto shipments_to_process = static_cast<size_t>(std::distance(shipments_arr,
265  std::partition(shipments_arr, shipments_arr + total_shipments,
266  [&](const PickDeliveryOrders_t& s){return shipments_in_stops.has(s.id);})));
267 
268  pgassert(shipments_to_process > 0);
269  pgassert(shipments_in_stops.size() == static_cast<size_t>(shipments_to_process));
270 
271  auto new_stops = one_processing(
272  shipments_arr, shipments_to_process,
273  vehicles_arr, vehicles_to_process, the_stops,
274  time_matrix,
275  max_cycles, execution_date);
276 
277  update_stops(the_stops, new_stops);
278  }
279 
280  return the_stops;
281  } catch(...) {
282  throw;
283  }
284 }

References CHECK_FOR_INTERRUPTS, Vehicle_t::end_close_t, get_initial_stops(), Identifiers< T >::has(), Vehicle_t::id, PickDeliveryOrders_t::id, one_processing(), pgassert, processing_times_by_shipment(), processing_times_by_vehicle(), Identifiers< T >::size(), Vehicle_t::start_open_t, and update_stops().

Referenced by do_optimize().

◆ update_stops()

void anonymous_namespace{optimize_driver.cpp}::update_stops ( std::vector< Short_vehicle > &  the_stops,
const std::vector< Short_vehicle new_values 
)

Update the vehicle stops to the new values.

Parameters
[in,out]the_stopsset of stops that are to be updated
[in]new_valuessubset of stops that are to be used for the update

Definition at line 187 of file optimize_driver.cpp.

188  {
189  for (const auto &v : new_values) {
190  auto v_id = v.id;
191  auto v_to_modify = std::find_if(
192  the_stops.begin(), the_stops.end(), [v_id]
193  (const Short_vehicle& v) -> bool {return v.id == v_id;});
194  pgassert(v_to_modify != the_stops.end());
195  v_to_modify->stops = v.stops;
196  }
197 }

References pgassert, and Short_vehicle::stops.

Referenced by subdivide_processing().

anonymous_namespace{optimize_driver.cpp}::processing_times_by_vehicle
Identifiers< TTimestamp > processing_times_by_vehicle(Vehicle_t *vehicles_arr, size_t total_vehicles)
: extract the times where the vehicle opens or closes
Definition: optimize_driver.cpp:144
vrprouting::problem::Solution
Definition: solution.h:50
anonymous_namespace{optimize_driver.cpp}::processing_times_by_shipment
Identifiers< TTimestamp > processing_times_by_shipment(PickDeliveryOrders_t *shipments_arr, size_t total_shipments)
: extract the times where the shipments opens or closes
Definition: optimize_driver.cpp:123
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
vrprouting::problem::PickDeliver
the pick deliver problem
Definition: pickDeliver.h:50
vrprouting::optimizers::tabu::Optimize
Class that optimizes a solution.
Definition: optimizers/tabu.h:51
anonymous_namespace{optimize_driver.cpp}::one_processing
std::vector< Short_vehicle > one_processing(PickDeliveryOrders_t *shipments_arr, size_t total_shipments, Vehicle_t *vehicles_arr, size_t total_vehicles, std::vector< Short_vehicle > new_stops, const vrprouting::problem::Matrix &time_matrix, int max_cycles, int64_t execution_date)
Executes an optimization with the input data.
Definition: optimize_driver.cpp:69
PickDeliveryOrders_t::deliver_close_t
TTimestamp deliver_close_t
Deliver open time.
Definition: pickDeliveryOrders_t.h:75
Vehicle_t
vehicles's attributes
Definition: vehicle_t.h:50
vrprouting::initialsol::tabu::Initial_solution
Definition: initialsol/tabu.h:45
Vehicle_t::start_open_t
TTimestamp start_open_t
Start node's identifier.
Definition: vehicle_t.h:59
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
Vehicle_t::end_open_t
TTimestamp end_open_t
End node's identifier.
Definition: vehicle_t.h:66
Identifiers::size
size_t size() const
Definition: identifiers.hpp:77
PickDeliveryOrders_t::id
Id id
Definition: pickDeliveryOrders_t.h:59
Short_vehicle
short_vehicle
Definition: short_vehicle.h:41
PickDeliveryOrders_t::pick_open_t
TTimestamp pick_open_t
Pickup node identifier.
Definition: pickDeliveryOrders_t.h:66
Vehicle_t::start_close_t
TTimestamp start_close_t
Start open time.
Definition: vehicle_t.h:60
anonymous_namespace{optimize_driver.cpp}::update_stops
void update_stops(std::vector< Short_vehicle > &the_stops, const std::vector< Short_vehicle > new_values)
Update the vehicle stops to the new values.
Definition: optimize_driver.cpp:187
anonymous_namespace{optimize_driver.cpp}::get_initial_stops
std::vector< Short_vehicle > get_initial_stops(Vehicle_t *vehicles_arr, size_t total_vehicles)
get the original stops
Definition: optimize_driver.cpp:165
PickDeliveryOrders_t::deliver_open_t
TTimestamp deliver_open_t
Deliver node identifier.
Definition: pickDeliveryOrders_t.h:74
CHECK_FOR_INTERRUPTS
#define CHECK_FOR_INTERRUPTS()
Definition: interruption.h:79
Identifiers::has
bool has(const T other) const
true ids() has element
Definition: identifiers.hpp:100
Vehicle_t::end_close_t
TTimestamp end_close_t
End open time.
Definition: vehicle_t.h:67
PickDeliveryOrders_t::pick_close_t
TTimestamp pick_close_t
Pickup open time.
Definition: pickDeliveryOrders_t.h:67
Vehicle_t::stops_size
size_t stops_size
Stops.
Definition: vehicle_t.h:56
Vehicle_t::id
Id id
Definition: vehicle_t.h:51
Identifiers
Definition: identifiers.hpp:51