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

Add measurements

- add a time measurement
- add an iteration measurement
parent b52eb6e6
Branches
No related tags found
No related merge requests found
......@@ -4,4 +4,4 @@ const int EXCLUSION_PENALTY = 100;
const int RANDOM_SEED = 100;
const int NUMBER_VEHICLE = 5;
\ No newline at end of file
const int NUMBER_VEHICLE = 10;
\ No newline at end of file
......@@ -4,6 +4,8 @@
#include "lns/operators/selector/operator_selector.h"
#include "output/solution_checker.h"
#include <chrono>
namespace
{
bool isBetterSolution(Solution const &candidateSolution, Solution const &bestKnownSol)
......@@ -11,22 +13,82 @@ namespace
return candidateSolution.getCost() < bestKnownSol.getCost();
}
using lns_clock = std::chrono::high_resolution_clock;
using lns_time_point = std::chrono::time_point<lns_clock, std::chrono::nanoseconds>;
struct LnsRuntimeData
{
Solution bestSolution;
unsigned int numberOfIteration = 0;
lns_time_point start = lns_clock::now();
};
/**
* @return the number of seconds between point and now (the moment the function is called).
*/
unsigned long getTimeSinceInSec(lns_time_point point)
{
return std::chrono::duration_cast<std::chrono::seconds>(lns_clock::now() - point).count();
}
unsigned long getTimeSinceInMs(lns_time_point point)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(lns_clock::now() - point).count();
}
bool triggerLogProgress(LnsRuntimeData const &)
{
static auto lastTrigger = lns_clock::now();
if (getTimeSinceInSec(lastTrigger) >= 10)
{
lastTrigger = lns_clock::now();
return true;
}
return false;
}
void logProgress(LnsRuntimeData const &runtime, Solution const &actualSolution)
{
if (triggerLogProgress(runtime))
{
unsigned long actualTime = getTimeSinceInMs(runtime.start);
double iterPerSecond = 1000 * runtime.numberOfIteration / static_cast<double>(actualTime);
std::string speedLog;
if (iterPerSecond < 1)
{
speedLog = fmt::format("{}s/100 iterations", 100 / iterPerSecond);
}
else
{
speedLog = fmt::format("{:.1f} i/s", iterPerSecond);
}
std::string missingRequestLog;
long requestsMissing = runtime.bestSolution.missingPairCount();
//... log TO DO
}
}
}// namespace
output::LnsOutput lns::runLns(Solution const &initialSolution, OperatorSelector &opSelector,
AcceptanceFunction const &acceptFunctor)
AcceptanceFunction const &acceptFunctor)
{
/**
* The solution is the base solution used to create the neighbor solution.
* It is constant unless we accept the candidate solution.
*/
Solution actualSolution = initialSolution;
Solution bestSolution = initialSolution;
LnsRuntimeData runtime = {actualSolution};
int it = 100;
while (it > 0)
{
std::cout << "\n" << 101-it << " : ";
// temporary
int iterationMax = 200;
while (iterationMax > 0)
{
// Init iteration
++runtime.numberOfIteration;
logProgress(runtime, actualSolution);
std::cout << "\n" << runtime.numberOfIteration << " : ";
/**
* The solution on which we apply the operators.
* It is discarded at the end of each loop if it is not accepted by the Acceptance Function.
......@@ -40,24 +102,26 @@ output::LnsOutput lns::runLns(Solution const &initialSolution, OperatorSelector
destructReconstructPair.reconstructor().reconstructSolution(candidateSolution, 0.01);
candidateSolution.computeAndStoreSolutionCost();
// Update best solution
if (isBetterSolution(candidateSolution, bestSolution))
if (isBetterSolution(candidateSolution, runtime.bestSolution))
{
std::cout << "\n > new Best Solution \n";
checker::checkAll(candidateSolution, candidateSolution.getData(), false);
bestSolution = candidateSolution;
runtime.bestSolution = candidateSolution;
opSelector.betterSolutionFound();
}
// Check if we use the candidate solution as the new actual solution
// operator can force to take the new solution
if (destructReconstructPair.forceTakeSolution() ||
acceptFunctor(candidateSolution, actualSolution, bestSolution) == AcceptationStatus::ACCEPT)
acceptFunctor(candidateSolution, actualSolution, runtime.bestSolution) == AcceptationStatus::ACCEPT)
{
actualSolution = std::move(candidateSolution);
}
--it;
--iterationMax;
}
auto result = output::LnsOutput(std::move(bestSolution), it, it);
auto result = output::LnsOutput(
std::move(runtime.bestSolution), runtime.numberOfIteration, getTimeSinceInSec(runtime.start));
return result;
}
\ No newline at end of file
......@@ -231,6 +231,12 @@ int Solution::getRouteIDOf(int locationID) const
return -1;
}
unsigned int Solution::missingPairCount() const
{
return pairBank.size();
}
bool Solution::checkModification(AtomicRecreation const &modification) const
{
//std::cout << "--- Check Modification Validity : ";
......
......@@ -76,7 +76,8 @@ public:
PDPTWData const &getData() const;
double getRawCost() const;
double getCost() const;
unsigned int missingPairCount() const;
/**
* Return the route index associated to the given location ID.
* -1 if the location is not in a route.
......
......@@ -77,7 +77,10 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution)
// run lns
output::LnsOutput result = lns::runLns(startingSolution, smallLargeSelector, acceptor);
result.getBestSolution().print();
std::cout << result.getNumberOfIteration() << " " << result.getTimeSpent() << std::endl;
}
int main(int argc, char **argv)
......@@ -86,7 +89,8 @@ int main(int argc, char **argv)
///////////////////////////////////////////////////////////////////////
std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n100/bar-n100-1.json";
//std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n100/bar-n100-1.json";
std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/pdp_100/lc101.json";
//std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/Nantes_1.json";
//std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n5000/bar-n5000-1.json";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment