-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8123be
commit 2e8619e
Showing
6 changed files
with
184 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
#include <functional> | ||
|
||
#include "stage_2.hpp" | ||
#include "../algorithms/ant_colony.hpp" | ||
#include "../algorithms/greedy.hpp" | ||
#include "../algorithms/simulated_annealing.hpp" | ||
#include "../algorithms/tabu_search.hpp" | ||
|
||
using namespace std; | ||
|
||
template<typename T> | ||
void readOption(T& option, const char* prompt, function<T(const string&)> conversionFunc) { | ||
cout << prompt << flush; | ||
string str; | ||
getline(cin, str); | ||
|
||
if (!str.empty()) { | ||
option = conversionFunc(str); | ||
} | ||
} | ||
|
||
inline u64 convertUnsignedInt(const string& str) { | ||
return stoull(str); | ||
} | ||
|
||
inline double convertDouble(const string& str) { | ||
return stod(str); | ||
} | ||
|
||
bool convertBool(const string& str) { | ||
string lower(str); | ||
transform(str.begin(), str.end(), lower.begin(), ::tolower); | ||
return lower == "yes"; | ||
} | ||
|
||
CvrpSolution applyAntColonyOptimization(const CvrpInstance& instance, bool config) { | ||
AntColonyConfig acoConfig; | ||
|
||
readOption<double>(acoConfig.alpha, "Alpha: ", convertDouble); | ||
readOption<double>(acoConfig.beta, "Beta: ", convertDouble); | ||
readOption<u64>(acoConfig.numAnts, "Num ants: ", convertUnsignedInt); | ||
readOption<u32>(acoConfig.eliteAnts, "Elite ants: ", convertUnsignedInt); | ||
readOption<u64>(acoConfig.maxIterations, "Max. iterations: ", convertUnsignedInt); | ||
readOption<bool>(acoConfig.useSwapHeuristic, "Use swap heuristic (yes / no): ", convertBool); | ||
|
||
return antColonyOptimization(instance, acoConfig); | ||
} | ||
|
||
CvrpSolution applyClarkeWrightSavings(const CvrpInstance& instance, bool config) { | ||
return clarkeWrightSavings(instance); | ||
} | ||
|
||
CvrpSolution applyGranularTabuSearch(const CvrpInstance& instance, bool config) { | ||
size_t maxIterations = 1000; | ||
double beta = 1.5; | ||
|
||
readOption<u64>(maxIterations, "Max. iterations: ", convertUnsignedInt); | ||
readOption<double>(beta, "Beta: ", convertDouble); | ||
|
||
return granularTabuSearch(instance, maxIterations, beta); | ||
} | ||
|
||
CvrpSolution applyGreedyAlgorithm(const CvrpInstance& instance, bool config) { | ||
return greedyAlgorithm(instance); | ||
} | ||
|
||
CvrpSolution applySimulatedAnnealing(const CvrpInstance& instance, bool config) { | ||
InitialSolution initialSolution; | ||
|
||
readOption<InitialSolution>( | ||
initialSolution, | ||
"Initial solution (0 - trivial, 1 - greedy, 2 - Clarke-Wright): ", | ||
[](const string& str) { return (InitialSolution) stoi(str); } | ||
); | ||
|
||
return simulatedAnnealing(instance, initialSolution); | ||
} | ||
|
||
CvrpSolution applyCvrpAlgorithm(string algorithm, const CvrpInstance& instance, bool config) { | ||
static const unordered_map<const char*, function<CvrpSolution(const CvrpInstance&, bool)>> algorithms = { | ||
{"aco", applyAntColonyOptimization}, | ||
{"cws", applyClarkeWrightSavings}, | ||
{"gts", applyGranularTabuSearch}, | ||
{"greedy", applyGreedyAlgorithm}, | ||
{"sa", applySimulatedAnnealing} | ||
}; | ||
|
||
if (algorithms.count(algorithm.c_str())) { | ||
return algorithms.at(algorithm.c_str())(instance, config); | ||
} | ||
|
||
return { {} , 0 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
#ifndef CVRP_STAGE_2_H | ||
#define CVRP_STAGE_2_H | ||
|
||
#include <string> | ||
#include "cvrp.hpp" | ||
|
||
CvrpSolution applyCvrpAlgorithm(std::string algorithm, const CvrpInstance& instance, bool config); | ||
|
||
#endif // CVRP_STAGE_2_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters