Optimize::Optimize( int kind, const Solution &old_solution) : Solution(old_solution), best_solution(old_solution) { switch (kind) { case 0: sort_by_duration(); break; case 1: decrease_truck(); break; case 2: move_duration_based(); break; case 3: move_wait_time_based(); break; case 4: inter_swap(); break; } this->fleet = best_solution.fleet; sort_by_duration(); delete_empty_truck(); }
void Optimize::move_duration_based() { auto local_limit(fleet.size()); size_t i(0); sort_by_duration(); problem->log << tau("\nmove duration based"); while (move_reduce_cost() && (++i < local_limit)) { } delete_empty_truck(); i = 0; sort_by_duration(); std::reverse(fleet.begin(), fleet.end()); problem->log << tau("\nmove duration based"); while (move_reduce_cost() && (++i < local_limit)) { } sort_by_duration(); delete_empty_truck(); this->fleet = best_solution.fleet; }
bool Optimize::inter_swap(bool reversed) { // problem->log << tau("before sort"); sort_by_duration(); delete_empty_truck(); save_if_best(); if (reversed) { std::reverse(fleet.begin(), fleet.end()); } // problem->log << tau("after sort"); auto swapped = false; size_t from_pos(fleet.size()-1); while (from_pos > 1) { for (size_t to_pos = 0; to_pos < from_pos; ++to_pos) { swapped = swap_worse(from_pos, to_pos)? true : swapped; swapped = move_reduce_cost(from_pos, to_pos)? true : swapped; } delete_empty_truck(); --from_pos; } return swapped; }
void Optimize::inter_swap() { auto local_limit(fleet.size()); size_t i(0); while (inter_swap(false) && (++i < local_limit)) { } i = 0; while (inter_swap(true) && (++i < local_limit)) { } sort_by_duration(); delete_empty_truck(); this->fleet = best_solution.fleet; }
/* * Optimize decreasing truck * * - Objective: try to remove truck with less duration * - Secundary objective, acts like a shake operation * */ void Optimize::decrease_truck() { bool decreased(false); for (size_t i = 1; i < fleet.size(); ++i) { decreased = decrease_truck(i) || decreased; } if (decreased) { delete_empty_truck(); save_if_best(); decrease_truck(); } save_if_best(); }
/* * ordering the trucks by number of orders it has * - the truck with more orders wants to: * - make less time * - do more orders * The inter_swap objective is to make the trucks with more orders * - less time consuming */ bool Optimize::inter_swap() { msg.log << "\n" <<tau("before inter swap"); delete_empty_truck(); auto swapped_f = false; /* * .. to ... from .... */ for (auto &from : fleet) { for (auto &to : fleet) { if (&from == &to) break; #if 0 msg.log << "\n to " << to.id() << "from " << from.id(); auto swapped = false; #endif swap_worse(to, from); swapped_f = swap_order() || swapped_f; move_reduce_cost(from, to); #if 0 msg.log << "++++++++" << p_swaps; #endif } } while (!p_swaps.empty()) { swapped_f = swap_order() || swapped_f; } msg.log << "\n" <<tau("after"); delete_empty_truck(); return swapped_f; }