/* * from_truck: position of the truck where the order is * to truck: truck to put the order */ bool Optimize::swap_order( const Order from_order, Vehicle_pickDeliver &from_truck, const Order to_order, Vehicle_pickDeliver &to_truck) { if (!from_truck.has_order(from_order) || !to_truck.has_order(to_order)) { return false; } pgassert(from_truck.has_order(from_order)); pgassert(to_truck.has_order(to_order)); from_truck.erase(from_order); to_truck.erase(to_order); from_truck.insert(to_order); to_truck.insert(from_order); pgassert(from_truck.has_order(to_order)); pgassert(to_truck.has_order(from_order)); return true; }
/* * from_truck: position of the truck where the order is * to truck: truck to put the order */ void Optimize::swap_order( const Order from_order, Vehicle_pickDeliver &from_truck, const Order to_order, Vehicle_pickDeliver &to_truck) { pgassert(from_truck.has_order(from_order)); pgassert(to_truck.has_order(to_order)); from_truck.erase(from_order); to_truck.erase(to_order); from_truck.insert(to_order); to_truck.insert(from_order); pgassert(from_truck.has_order(to_order)); pgassert(to_truck.has_order(from_order)); }
/* * from_truck: position of the truck where the order is * to truck: truck to put the order */ void Optimize::move_order( Order order, Vehicle_pickDeliver &from_truck, Vehicle_pickDeliver &to_truck) { pgassert(from_truck.has_order(order)); pgassert(!to_truck.has_order(order)); from_truck.erase(order); to_truck.insert(order); pgassert(!from_truck.has_order(order)); pgassert(to_truck.has_order(order)); }
/* * from_truck trying to make from_truck's duration smaller * - maybe all orders can be moved * - if that is the case, the from_truck could be removed * * Deleting an order on the from_truck * - number of truck remains the same * - from_truk duration() can not get larger * - the overall duration can get larger * */ bool Optimize::move_reduce_cost( Vehicle_pickDeliver &from, Vehicle_pickDeliver &to) { auto from_truck = from; auto to_truck = to; /* * don't move from a real truck to a phoney truck */ if (!from_truck.is_phony() && to_truck.is_phony()) { return false; } #if 0 from.id() > to.id() ? to : from; #endif size_t from_pos = 0; size_t to_pos = 0; for (; from_pos < fleet.size() && fleet[from_pos].idx() != from_truck.idx() ; ++from_pos) { } pgassert(from_pos < fleet.size()); for (; to_pos < fleet.size() && fleet[to_pos].idx() != to_truck.idx() ; ++to_pos) { } pgassert(to_pos < fleet.size()); auto moved = false; auto from_orders = from_truck.orders_in_vehicle(); while (!from_orders.empty()) { /* * removing an order decreases the duration */ auto order = from_truck.orders()[from_orders.front()]; from_orders -= order.idx(); /* * insert it in the "to" truck */ to_truck.insert(order); if (to_truck.is_feasable()) { msg.log << "\n Move order " << order.pickup().id() << " from truck " << from_truck.idx() << " to truck " << to_truck.idx(); #ifndef NDEBUG msg.dbg_log << "\nMove before:"; msg.dbg_log << "\n" << fleet[to_pos].tau(); msg.dbg_log << "\n" << fleet[from_pos].tau(); #endif #if 1 from_truck.erase(order); #else to_truck.insert(order); move_order(order, fleet[from_pos], fleet[to_pos]); #endif moved = true; save_if_best(); #ifndef NDEBUG msg.dbg_log << "\nMove after:"; msg.dbg_log << "\n" << fleet[to_pos].tau(); msg.dbg_log << "\n" << fleet[from_pos].tau(); #endif } else { to_truck.erase(order); } } return moved; }
/* * .. to ... from .... */ bool Optimize::swap_worse(Vehicle_pickDeliver &to, Vehicle_pickDeliver &from) { #if 0 pgassert(from.orders_in_vehicle().size() <= to.orders_in_vehicle().size()); #endif auto from_truck = from; auto to_truck = to; auto swapped = false; #if 0 auto best_from_order = from_truck.orders_in_vehicle().front(); auto best_to_order = to_truck.orders_in_vehicle().front(); #endif for (auto from_orders = from_truck.orders_in_vehicle(); !from_orders.empty(); from_orders.pop_front()) { auto from_order = from_truck.orders()[from_orders.front()]; #if 0 pgassert(from_truck.has_order(from_order)); msg.log << "\n" << from_orders; msg.log << "\n from " << from_order.idx() << "," << from_order.pickup().original_id(); pgassert(from_truck.has_order(from_order)); #endif auto curr_from_duration = from_truck.duration(); pgassert(from_truck.has_order(from_order)); for (auto to_orders = to_truck.orders_in_vehicle(); !to_orders.empty(); to_orders.pop_front()) { pgassert(from_truck.has_order(from_order)); auto to_order = to.orders()[to_orders.front()]; #if 0 msg.log << "\n" << to_orders; msg.log << "\n To " << to_order.idx(); #endif auto curr_to_duration = to_truck.duration(); /* * delete from_order, and to order from their trucks */ #if 0 pgassert(from_truck.has_order(from_order)); msg.log << "\n" << from_truck.tau(); msg.log << "\n" << from_order.idx(); pgassert(from_truck.has_order(from_order)); #endif from_truck.erase(from_order); to_truck.erase(to_order); /* * insert them in the other truck */ from_truck.insert(to_order); to_truck.insert(from_order); if (from_truck.is_feasable() && to_truck.is_feasable()) { /* * Can swap but: * - only swap when the total duration is reduced * - or from_truck duration is reduced */ #if 0 msg.log << "\n Can swap"; msg.log << "\n Curr_from_duration " << curr_from_duration; msg.log << " Curr_to_duration " << curr_to_duration; msg.log << "\n new_from_duration " << from_truck.duration(); msg.log << " new_to_duration " << to_truck.duration(); #endif auto estimated_delta = - (curr_from_duration + curr_to_duration) + (to_truck.duration() + from_truck.duration()); #if 1 auto estimated_duration = duration() + estimated_delta; if (from_truck.duration() < curr_from_duration || estimated_delta < 0 || estimated_duration < best_solution.duration()) { #endif msg.log << "\n Found Swap order " << from_order.pickup().id() << " from truck " << from_truck.idx() << " with order " << to_order.pickup().id() << " of truck " << to_truck.idx(); swapped = true; #if 0 best_to_order = to_order.idx(); best_from_order = from_order.idx(); #endif p_swaps.push( Swap_info( from, to, from_order.idx(), to_order.idx(), estimated_delta)); #if 1 } #endif } to_truck = to; from_truck = from; } from_truck = from; } return false && swapped; }