Skip to content
Snippets Groups Projects
Commit bccf3077 authored by awenjb's avatar awenjb
Browse files

Add bank focus string removal

parent 1c5c678c
No related branches found
No related tags found
No related merge requests found
......@@ -42,6 +42,7 @@ add_executable(pdptw src/mains/main.cpp
src/lns/operators/destruction/clean_empty_route.cpp
src/lns/operators/destruction/random_destroy.cpp
src/lns/operators/destruction/string_removal.cpp
src/lns/operators/destruction/bank_focus_string_removal/bank_focus_string_removal.cpp
src/lns/operators/reconstruction/enumerate.cpp
src/lns/operators/reconstruction/list_heuristic_cost_oriented.cpp
src/lns/operators/selector/operator_selector.cpp
......
......@@ -13,7 +13,7 @@ int const ROUTE_PENALTY = 0;
int const NUMBER_VEHICLE = 50;
// Number of iterations for the algorithm or simulation to run.
int const NUMBER_ITERATION = 100000;
int const NUMBER_ITERATION = 1000;
// Flags
......@@ -27,7 +27,7 @@ int const RANDOM_SEED = 100;
bool const PRINT = true;
// Flag indicating whether the final solution is stored.
bool const STORE_SOLUTION = true;
bool const STORE_SOLUTION = false;
// Flag indicating whether the solution is stored with complete information or not.
bool const COMPLETE_STORE = true;
......
......@@ -82,7 +82,7 @@ output::LnsOutput lns::runLns(Solution const &initialSolution, OperatorSelector
Solution actualSolution = initialSolution;
LnsRuntimeData runtime = {actualSolution};
// temporary fixed iteration
// fixed iteration
int iterationMax = NUMBER_ITERATION;
while (iterationMax > 0)
{
......
......@@ -18,14 +18,14 @@ RemoveRoute::RemoveRoute(int routeIndex, std::vector<int> &&removedPairID)
void RemoveRoute::modifySolution(Solution &solution)
{
std::vector<Route> &routes = solution.getRoutes();
if (routes.at(routeIndex).getRoute().empty())
{
std::vector<int> const &locationIDs = routes.at(routeIndex).getRoute();
std::vector<int> const &routeIDs = routes.at(routeIndex).getRoute();
if (!(routeIDs.empty()))
{
// update removedPairID
removedPairID.reserve(routes.at(routeIndex).getSize());
removedPairID.reserve(routeIDs.size());
for (int id: locationIDs)
for (int id: routeIDs)
{
if (solution.getData().getLocation(id).getLocType() == LocType::PICKUP)
{
......
#include "bank_focus_string_removal.h"
#include "lns/modification/route/remove_route.h"
#include "lns/operators/destruction/clean_empty_route.h"
#include "utils.h"
namespace
{
void BankFocusSISRsRuin(Solution &solution, unsigned int maxStringSize, unsigned int averageNumberRemovedElement)
{
// |t∈T| average number of location by route
auto averageRouteCardinality = static_cast<unsigned int>(sisr::computeAverageCardinality(solution.getRoutes()));
unsigned int maxSizeOfString = std::min(maxStringSize, averageRouteCardinality); // (5) lmax_s
unsigned int maxNumberOfString = (4 * averageNumberRemovedElement) / (1 + maxSizeOfString) - 1;// (6) kmax_s
unsigned int numberOfString = util::getRandomInt(1, maxNumberOfString + 1); // (7) k_s
// select a random location in the bank
std::vector<int> const &bank = solution.getBank();
int locationSeed = bank.at(util::getRandomInt(1, bank.size()) - 1);
// store the index of routes where a string was removed
std::vector<int> routeIndexUsed;
routeIndexUsed.reserve(numberOfString);
// getClosestLocationsID returns the list of all location sorted from the closest to furthest
for (int neighbor: solution.getData().getClosestLocationsID(locationSeed))
{
// recover the routeIndex associated to the locationID, returns -1 if in the bank
int routeIndex = solution.getRouteIDOf(neighbor);
if (routeIndex != -1 && std::ranges::find(routeIndexUsed, routeIndex) == routeIndexUsed.end())
{
// (8) lmax_t
unsigned int maxSizeOfThisString =
std::min(static_cast<int>(maxSizeOfString), solution.getRoute(routeIndex).getSize());
// (9) l_t
unsigned int actualSizeOfThisString = util::getRandomInt(1, maxSizeOfThisString);
sisr::removeString(solution, routeIndex, actualSizeOfThisString, neighbor);
routeIndexUsed.emplace_back(routeIndex);
if (routeIndexUsed.size() >= numberOfString)
{
break;
}
}
}
}
}// namespace
void BankFocusStringRemoval::destroySolution(Solution &solution) const
{
// if the bank is empty (all requests are fullfilled), remove a route
if (solution.getBank().empty())
{
// clean empty routes
CleanEmptyRoute clean = CleanEmptyRoute();
clean.destroySolution(solution);
// maybe try a heuristic to remove a specific route (smallest, longest, etc...)
RemoveRoute remove = RemoveRoute(util::getRandomInt(1, solution.getRoutes().size()) - 1);
solution.applyDestructSolution(remove);
}
std::cout << "After preparation :" << std::endl;
solution.print();
BankFocusSISRsRuin(solution, maxCardinalityOfString, averageNumberRemovedElement);
}
\ No newline at end of file
#pragma once
#include "lns/operators/abstract_operator.h"
#include "lns/operators/destruction/string_removal.h"
/*
* String removal applied to unfullfilled requests.
* Aims to reduce the number of routes by applying SISR operators on unfullfilled requests.
* If all requests are fullfilled, remove a route and try again.
*/
class BankFocusStringRemoval : public DestructionOperator
{
public:
explicit BankFocusStringRemoval(unsigned int maxCardinalityOfString, unsigned int averageNumberRemovedElement)
: maxCardinalityOfString(maxCardinalityOfString), averageNumberRemovedElement(averageNumberRemovedElement)
{}
// Each string has a max number of requests: MAX_CARDINALITY_OF_STRING
// L_max in the paper.
unsigned int maxCardinalityOfString = 10;
// This operator will remove in average AVERAGE_CUSTOMER_REMOVAL of requests
// c with a bar on top in the paper
unsigned int averageNumberRemovedElement = 10;
/**
* This operator removes numberOfPairsToDestroy pairs randomly in the solution.
*/
void destroySolution(Solution &solution) const override;
private:
int numberOfPairsToDestroy;
};
......@@ -8,7 +8,7 @@
#include <vector>
namespace
namespace sisr
{
double computeAverageCardinality(std::vector<Route> const &routes)
......@@ -156,5 +156,5 @@ namespace
void StringRemoval::destroySolution(Solution &solution) const
{
SISRsRuin(solution, maxCardinalityOfString, averageNumberRemovedElement);
sisr::SISRsRuin(solution, maxCardinalityOfString, averageNumberRemovedElement);
}
......@@ -318,11 +318,11 @@ void Solution::print() const
++i;
}
std::cout << "Constraints:\n";
for (auto const &constraint: constraints)
{
constraint->print();
}
// std::cout << "Constraints:\n";
// for (auto const &constraint: constraints)
// {
// constraint->print();
// }
std::cout << "\n";
}
......@@ -14,6 +14,7 @@
#include "lns/modification/route/insert_route.h"
#include "lns/modification/route/remove_route.h"
#include "lns/operators/abstract_operator.h"
#include "lns/operators/destruction/bank_focus_string_removal/bank_focus_string_removal.h"
#include "lns/operators/destruction/clean_empty_route.h"
#include "lns/operators/destruction/random_destroy.h"
#include "lns/operators/destruction/string_removal.h"
......@@ -79,33 +80,17 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution)
output::exportToJson(result);
}
// Solution sol = result.getBestSolution();
// CapacityConstraint capa = CapacityConstraint(sol);
Solution sol = result.getBestSolution();
sol.print();
// try reduce the number of routes
BankFocusStringRemoval rem = BankFocusStringRemoval(10,10);
// capa.initRouteCapacities();
// capa.initMaxCapacity();
// capa.print();
rem.destroySolution(sol);
// RemovePair rem = RemovePair(0, 1, 4, sol.getData().getPair(1));
std::cout << "final print" << std::endl;
sol.print();
// sol.applyDestructSolution(rem);
// sol.print();
// capa.updateMaxCapacity(sol.getData(), sol.getRoute(0));
// capa.print();
// int pickup = 8;
// int delivery = 8;
// std::cout << std::endl<< capa.checkModif(sol.getData().getPair(1), 0, pickup, delivery) << std::endl;
// std::cout << std::endl<< capa.checkModif2(sol.getData().getPair(1), 0, pickup, delivery) << std::endl;
// InsertPair add = InsertPair(0, pickup, delivery, sol.getData().getPair(1));
// sol.applyRecreateSolution(add);
// checker::checkAll(sol, sol.getData());
// capa.updateMaxCapacity(sol.getData(), sol.getRoute(0));
// capa.print();
}
int main(int argc, char **argv)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment