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

Atomic Modifications

- add basic modification for a solution
parent 9ef014fa
No related branches found
No related tags found
No related merge requests found
#include "insert_route.h"
InsertRoute::InsertRoute() {}
void InsertRoute::modifySolution(Solution &solution)
{
std::vector<Route> &routes = solution.getRoutes();
routes.push_back(Route());
}
double InsertRoute::evaluate(Solution const &solution) const
{
return 0;
}
Location const *InsertRoute::getAddedLocation() const
{
return nullptr;
}
#pragma once
#include "./../atomic_recreation.h"
#include "./../../solution/solution.h"
#include "./../../../input/location.h"
#include <functional>
class Route;
/**
* Insert a new empty route in the solution
* Do not cost anything
*/
class InsertRoute : public AtomicRecreation
{
public:
InsertRoute();
void modifySolution(Solution &solution) override;
double evaluate(Solution const &solution) const override;
Location const *getAddedLocation() const override;
};
\ No newline at end of file
// TO DO
\ No newline at end of file
// Insertion d'une route avec une ou plusieurs paires pickup / delivery
// TO DO
\ No newline at end of file
#include "remove_route.h"
#include "../../../input/data.h"
RemoveRoute::RemoveRoute() : routeIndex(-1), removedLocationID({}) {}
RemoveRoute::RemoveRoute(int routeIndex) : routeIndex(routeIndex), removedLocationID({}) {}
RemoveRoute::RemoveRoute(int routeIndex, std::vector<int> removedLocationID) : routeIndex(routeIndex), removedLocationID(removedLocationID) {}
void RemoveRoute::modifySolution(Solution &solution)
{
std::vector<Route> &routes = solution.getRoutes();
// update removedLocationID
removedLocationID.insert(removedLocationID.end(),
std::make_move_iterator(routes[routeIndex].getRoute().begin()),
std::make_move_iterator(routes[routeIndex].getRoute().end()));
routes.erase(routes.begin() + routeIndex);
}
double RemoveRoute::evaluate(Solution const &solution) const
{
return data::routeCost(solution.getData(), solution.getRoute(routeIndex));
}
int RemoveRoute::getRouteIndex() const
{
return routeIndex;
}
std::vector<int> const &RemoveRoute::getDeletedRequests() const
{
return removedLocationID;
}
\ No newline at end of file
#pragma once
#include "./../atomic_destruction.h"
#include "./../../solution/solution.h"
#include "./../../../input/location.h"
#include <functional>
/**
* A modification that will remove a route from the solution.
*/
class RemoveRoute : public AtomicDestruction
{
/**
* Index of the route to remove
*/
int routeIndex;
/**
* Removed Location ID (empty before ModifySolution is called)
*/
std::vector<int> removedLocationID;
public:
RemoveRoute();
RemoveRoute(int routeIndex);
RemoveRoute(int routeIndex, std::vector<int> removedLocationID);
void modifySolution(Solution &solution) override;
double evaluate(Solution const &solution) const override;
int getRouteIndex() const;
/**
* Return the location ID of location that has been deleted
*/
std::vector<int> const &getDeletedRequests() const override;
};
\ No newline at end of file
#include "route.h"
#include <iostream>
#include <spdlog/spdlog.h>
Route::Route() = default;
Route::Route(std::vector<int> route, int cost) : route(route), cost(cost) {}
......@@ -21,4 +23,40 @@ void Route::print() const
std::cout << id << ", ";
}
std::cout << "\n";
}
void Route::insertAt(int locationIndex, int position)
{
if (position < 0 || position > route.size()) {
spdlog::error("Invalid position for the insertion : {}", position);
throw std::out_of_range("Invalid position for the insertion.");
}
route.insert(route.begin() + position, locationIndex);
}
void Route::deleteAt(int position) {
// Vérification si la position est valide
if (position < 0 || position >= route.size()) {
spdlog::error("Invalid position for the suppression : {}", position);
throw std::out_of_range("Invalid position for the suppression");
}
route.erase(route.begin() + position);
}
int Route::getLocation(int index) const
{
// Vérification si la position est valide
if (index < 0 || index >= route.size()) {
spdlog::error("Invalid index when reading route: {}", index);
throw std::out_of_range("Invalid index when reading route");
}
return route[index];
}
int Route::getSize() const
{
return route.size();
}
\ No newline at end of file
......@@ -5,6 +5,7 @@
/**
* Represent a route for the PDPTW
* A route does not include the depot at the begining and the end !
*/
class Route
{
......@@ -22,9 +23,25 @@ private:
public:
Route();
Route(std::vector<int> route, int cost);
int getCost() const;
const std::vector<int>& getRoute() const;
const std::vector<int> & getRoute() const;
// get Location
int getLocation(int index) const;
void print() const;
/*
* Add a location index in the route (does not update the route cost)
*/
void insertAt(int locationIndex, int position);
/*
* Remove the element at "position"
*/
void deleteAt(int position);
int getSize() const;
};
\ No newline at end of file
#include "solution.h"
#include <iostream>
#include <spdlog/spdlog.h>
Solution::Solution(RequestBank bank, std::vector<Route> routes, int totalCost)
: bank(bank), routes(routes), totalCost(totalCost) {}
Solution::Solution(const PDPTWData &data)
: data(data), totalCost(0) {
bank = RequestBank();
routes = std::vector<Route>();
}
Solution::Solution(const PDPTWData &data, RequestBank bank, std::vector<Route> routes, int totalCost)
: data(data), bank(bank), routes(routes), totalCost(totalCost) {}
const std::vector<int> & Solution::getBank() const
{
return bank;
}
const std::vector<Route> & Solution::getRoute() const
const std::vector<Route> & Solution::getRoutes() const
{
return routes;
}
std::vector<Route> & Solution::getRoutes()
{
return routes;
}
Route & Solution::getRoute(int routeIndex)
{
if (routeIndex < 0 || routeIndex >= routes.size())
{
spdlog::error("Invalid route index: {}", routeIndex);
throw std::out_of_range("Invalid route index.");
}
return routes[routeIndex];
}
const Route & Solution::getRoute(int routeIndex) const
{
if (routeIndex < 0 || routeIndex >= routes.size())
{
spdlog::error("Invalid route index: {}", routeIndex);
throw std::out_of_range("Invalid route index.");
}
return routes[routeIndex];
}
int Solution::getCost()
{
return totalCost;
}
const PDPTWData & Solution::getData() const
{
return data;
}
void Solution::print() const
{
std::cout << "Cost :" << totalCost << "\n";
for (const Route& id : getRoute())
std::cout << "Cost :" << totalCost << "\n" << "Routes : \n";
for (const Route& id : getRoutes())
{
id.print();
}
std::cout << "Banques : \n";
for (const int id : getBank())
{
std::cout << id << ", ";
}
std::cout << "\n";
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
#include <vector>
#include "route.h"
#include "./../../input/pdptw_data.h"
/**
* Represent a solution of PDPTW
......@@ -12,14 +13,29 @@ public:
using RequestBank = std::vector<int>;
private:
PDPTWData const & data;
RequestBank bank;
std::vector<Route> routes;
int totalCost;
static PDPTWData dummy;
public:
Solution(RequestBank bank, std::vector<Route> routes, int totalCost);
const RequestBank & getBank() const;
const std::vector<Route> & getRoute() const;
Solution(const PDPTWData &data);
Solution(const PDPTWData &data, RequestBank bank, std::vector<Route> routes, int totalCost);
RequestBank const & getBank() const;
std::vector<Route> const & getRoutes() const;
Route const & getRoute(int routeIndex) const;
const PDPTWData & getData() const;
// For route modification
std::vector<Route> & getRoutes();
Route & getRoute(int routeIndex);
int getCost();
void print() const;
......
......@@ -12,6 +12,12 @@
#include "input/location.h"
#include "input/time_window.h"
#include "input/json_parser.h"
#include "input/data.h"
#include "lns/solution/solution.h"
#include "lns/modification/pair/insert_pair.h"
#include "lns/modification/route/insert_route.h"
#include "lns/modification/pair/remove_pair.h"
#include "lns/modification/route/remove_route.h"
namespace fs = std::filesystem;
using json = nlohmann::json;
......@@ -29,13 +35,61 @@ int main(int argc, char const *argv[])
PDPTWData data = parsing::parseJson(filepath);
data.checkData();
/* test */
TimeWindow tw = TimeWindow(3,4);
Location pos = Location(1, 1, 20, 3, tw, 4, 5, LocType::DEPOT);
/*
* test
*/
// Empty Solution
Solution sol = Solution(data);
InsertRoute op1 = InsertRoute();
// Ajoute une route vide
op1.modifySolution(sol);
Location r1P = data.getLocations()[0];
Location r1D = data.getLocations()[50];
Location r2P = data.getLocations()[1];
Location r2D = data.getLocations()[51];
Location r3P = data.getLocations()[2];
Location r3D = data.getLocations()[52];
// Opération : Ajoute une paire pickup delivery à la route 0 en position 0 et 0
InsertPair op2 = InsertPair(0, 0, 0, r1P, r1D);
op2.modifySolution(sol);
InsertPair op3 = InsertPair(0, 0, 2, r2P, r2D);
op3.modifySolution(sol);
std::cout << "--- \n";
data::routeCost(data, sol.getRoute(0));
InsertPair op4 = InsertPair(0, 3, 4, r3P, r3D);
std::cout << "cout d'insert : "<< op4.evaluate(sol) << "\n";
op4.modifySolution(sol);
std::cout << "--- \n";
sol.print();
data::routeCost(data, sol.getRoute(0));
// TO DO
// Test supr
std::cout << "--- \n";
RemovePair op5 = RemovePair(0, 3, 5, r3P, r3D);
std::cout << "cout de supr : "<< op5.evaluate(sol) << "\n";
op5.modifySolution(sol);
sol.print();
data::routeCost(data, sol.getRoute(0));
RemoveRoute op6 = RemoveRoute(0);
std::cout << "cout de supr : "<< op6.evaluate(sol) << "\n";
op6.modifySolution(sol);
sol.print();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment