void Optimize::decrease_truck(size_t cycle, bool &decreased) { /* end recursion */ if (cycle == 0) return; std::ostringstream err_log; err_log << " --- Cycle " << cycle << "\n"; /* the front truck move to back */ std::rotate(fleet.begin(), fleet.begin() + 1, fleet.end()); err_log << "\n after rotate" << tau(); auto orders(fleet.back().orders_in_vehicle); while (!orders.empty()) { /* Step 2: grab an order */ auto order(problem->orders()[*orders.begin()]); orders.erase(orders.begin()); err_log << "\n truck with order: " << fleet.back().tau(); err_log << "\nOrder" << order << "\n"; /* Step 3: delete the order from the back of the fleet */ pgassertwm(fleet.back().has_order(order), err_log.str()); fleet.back().erase(order); pgassertwm(!fleet.back().has_order(order), err_log.str()); /* Step 3: cycle the fleet & insert in best truck possible */ /* current truck is tried last */ err_log << " trying "; auto best_truck(fleet.size() - 1); auto current_duration(duration()); auto min_delta_duration = (std::numeric_limits<double>::max)(); size_t t_i(0); for (auto &truck : fleet) { truck.insert(order); if (!truck.is_feasable()) { err_log << "\n" << truck.tau(); } else { err_log << "\n ******* success " << truck.tau() << "\n"; auto delta_duration = duration()-current_duration; if (t_i != fleet.size() - 1 && (delta_duration < min_delta_duration)) { min_delta_duration = delta_duration; best_truck = t_i; } } truck.erase(order); ++t_i; } fleet[best_truck].insert(order); save_if_best(); } if (fleet.back().empty()) { decreased = true; fleet.pop_back(); save_if_best(); } decrease_truck(--cycle, decreased); }
void Initial_solution::do_while_foo(int kind) { invariant(); pgassert(kind > 0 && kind < 7); msg.log << "\nInitial_solution::do_while_foo\n"; Identifiers<size_t> notused; #if 0 bool out_of_trucks(true); #endif while (!unassigned.empty()) { msg.log << unassigned.size() << " unassigned: " << unassigned << "\n"; msg.log << assigned.size() << " assigned:" << assigned << "\n"; auto current = unassigned.size(); #if 0 auto truck = out_of_trucks? trucks.get_truck(unassigned.front()) : trucks.get_truck(); #else auto truck = trucks.get_truck(unassigned.front()); #endif msg.log << "got truck:" << truck.tau() << "\n"; /* * kind 1 to 7 work with the same code structure */ truck.do_while_feasable(kind, unassigned, assigned); msg.log << unassigned.size() << " unassigned: " << unassigned << "\n"; msg.log << assigned.size() << " assigned:" << assigned << "\n"; msg.log << "current" << current << " unassigned: " << unassigned.size(); pgassertwm(current > unassigned.size(), msg.get_log().c_str()); #if 0 if (truck.orders_in_vehicle().empty()) { out_of_trucks = notused.has(truck.idx()); if (out_of_trucks) { for (auto t : notused) { trucks.release_truck(t); } } notused += truck.idx(); continue; } #endif fleet.push_back(truck); invariant(); } pgassertwm(true, msg.get_log().c_str()); pgassert(is_feasable()); invariant(); }
bool Dmatrix::is_symmetric() const { for (size_t i = 0; i < costs.size(); ++i) { for (size_t j = 0; j < costs.size(); ++j) { if (0.000001 < std::fabs(costs[i][j] - costs[j][i])) { std::ostringstream log; log << "i \t" << i << "j \t" << j << "costs[i][j] \t" << costs[i][j] << "costs[j][i] \t" << costs[j][i] << "\n"; log << (*this); pgassertwm(false, log.str()); return false; } } } return true; }
void Vehicle_pickDeliver::insert(const Order &order) { invariant(); pgassert(!has_order(order)); auto pick_pos(position_limits(order.pickup())); auto deliver_pos(position_limits(order.delivery())); #ifndef NDEBUG std::ostringstream err_log; err_log << "\n\tpickup limits (low, high) = (" << pick_pos.first << ", " << pick_pos.second << ") " << "\n\tdeliver limits (low, high) = (" << deliver_pos.first << ", " << deliver_pos.second << ") " << "\noriginal" << tau(); #endif if (pick_pos.second < pick_pos.first) { /* pickup generates twv evrywhere, * so put the order as last */ push_back(order); return; } if (deliver_pos.second < deliver_pos.first) { /* delivery generates twv evrywhere, * so put the order as last */ push_back(order); return; } /* * Because delivery positions were estimated without * the pickup: * - increase the upper limit position estimation */ ++deliver_pos.second; auto d_pos_backup(deliver_pos); auto best_pick_pos = m_path.size(); auto best_deliver_pos = m_path.size() + 1; auto current_duration(duration()); auto min_delta_duration = (std::numeric_limits<double>::max)(); auto found(false); pgassertwm(!has_order(order), err_log.str()); while (pick_pos.first <= pick_pos.second) { #ifndef NDEBUG err_log << "\n\tpickup cycle limits (low, high) = (" << pick_pos.first << ", " << pick_pos.second << ") "; #endif Vehicle::insert(pick_pos.first, order.pickup()); #ifndef NDEBUG err_log << "\npickup inserted: " << tau(); #endif while (deliver_pos.first <= deliver_pos.second) { Vehicle::insert(deliver_pos.first, order.delivery()); orders_in_vehicle.insert(order.id()); pgassertwm(has_order(order), err_log.str()); #ifndef NDEBUG err_log << "\ndelivery inserted: " << tau(); #endif if (is_feasable()) { pgassert(is_feasable()); auto delta_duration = duration()-current_duration; if (delta_duration < min_delta_duration) { #ifndef NDEBUG err_log << "\nsuccess" << tau(); #endif min_delta_duration = delta_duration; best_pick_pos = pick_pos.first; best_deliver_pos = deliver_pos.first; found = true; } } Vehicle::erase(deliver_pos.first); #ifndef NDEBUG err_log << "\ndelivery erased: " << tau(); #endif ++deliver_pos.first; } Vehicle::erase(pick_pos.first); #ifndef NDEBUG err_log << "\npickup erased: " << tau(); #endif orders_in_vehicle.erase(order.id()); pgassertwm(!has_order(order), err_log.str()); deliver_pos = d_pos_backup; #ifndef NDEBUG err_log << "\n\trestoring deliver limits (low, high) = (" << deliver_pos.first << ", " << deliver_pos.second << ") "; #endif ++pick_pos.first; } pgassertwm(!has_order(order), err_log.str()); if (!found) { /* order causes twv * so put the order as last */ push_back(order); return; } Vehicle::insert(best_pick_pos, order.pickup()); Vehicle::insert(best_deliver_pos, order.delivery()); orders_in_vehicle.insert(order.id()); pgassertwm(is_feasable(), err_log.str()); pgassertwm(has_order(order), err_log.str()); pgassertwm(!has_cv(), err_log.str()); invariant(); }