Commit fa309f2e authored by Dmitry Bagaev's avatar Dmitry Bagaev
Browse files

Global optimizers feature

parent 30e592fd
......@@ -65,7 +65,7 @@ int main(int argc, char **argv) {
std::cout << " " << *it << std::endl;
}
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::OptimizerInterface::GetAvailableOptimizers();
std::vector<std::string> availableOptimizers = TTSP::Optimizers::GetAvailableOptimizers();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
......@@ -190,11 +190,11 @@ int main(int argc, char **argv) {
TTSP::OptimizationParameters parameters(entries, -1.0);
if (!TTSP::OptimizerInterface::IsOptimizerAvailable(optimizerType)) {
if (!TTSP::Optimizers::IsOptimizerAvailable(optimizerType)) {
if (rank == 0) {
std::cout << "Optimizer " << optimizerType << " not found" << std::endl;
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::OptimizerInterface::GetAvailableOptimizers();
std::vector<std::string> availableOptimizers = TTSP::Optimizers::GetAvailableOptimizers();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
......@@ -202,10 +202,12 @@ int main(int argc, char **argv) {
std::exit(0);
}
TTSP::OptimizerInterface *optimizer = TTSP::OptimizerInterface::GetOptimizer(optimizerType, parameters, properties, 50);
TTSP::Optimizers::SaveOptimizerOrReplace("test", optimizerType, parameters, properties, 50);
optimizer->SetVerbosityLevel(TTSP::OptimizerVerbosityLevel::Level3);
optimizer->SetRestartStrategy(TTSP::OptimizerRestartStrategy::RESTART_WITH_BEST, 10);
TTSP::OptimizerInterface *topt = TTSP::Optimizers::GetOptimizer(optimizerType, parameters, properties, 50);
topt->SetVerbosityLevel(TTSP::OptimizerVerbosityLevel::Level3);
topt->SetRestartStrategy(TTSP::OptimizerRestartStrategy::RESTART_WITH_BEST, 10);
while (!series.end()) {
......@@ -259,6 +261,8 @@ int main(int argc, char **argv) {
return std::make_pair(is_solved, time);
};
TTSP::OptimizerInterface *optimizer = TTSP::Optimizers::GetSavedOptimizer("test");
const TTSP::OptimizationParametersSuggestion &suggestion = optimizer->Suggest(invoke, nullptr);
const TTSP::OptimizationFunctionInvokeResult &result = invoke(suggestion.GetPointsBefore(), suggestion.GetPointsAfter(), nullptr);
......@@ -268,7 +272,7 @@ int main(int argc, char **argv) {
optimizer->SaveResult(suggestion, metrics, is_good);
TTSP::OptimizerVerbosityLevel verbosity = TTSP::OptimizerVerbosityLevel::Level3;
TTSP::OptimizerVerbosityLevel verbosity = TTSP::OptimizerVerbosityLevel::Level1;
// On Level1 print some metadata information about solution and used parameters
if (rank == 0 && verbosity > TTSP::OptimizerVerbosityLevel::Level0) {
......
......@@ -9,6 +9,7 @@
#include <algorithm>
#include <string>
#include <deque>
#include <unordered_map>
namespace TTSP {
......@@ -395,13 +396,25 @@ namespace TTSP {
void SetRestartStrategy(OptimizerRestartStrategy strategy, std::size_t max_fails) noexcept;
virtual ~OptimizerInterface() {};
};
typedef std::unordered_map<std::string, OptimizerInterface *> SavedOptimizersMap;
class Optimizers {
private:
static SavedOptimizersMap optimizers;
public:
static bool IsOptimizerAvailable(const std::string &type);
static std::vector<std::string> GetAvailableOptimizers();
static OptimizerInterface *GetOptimizer(const std::string &type,
const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
static void SaveOptimizerOrReplace(const std::string &name, const std::string &type,
const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
static OptimizerInterface *GetSavedOptimizer(const std::string &name);
};
};
......
......@@ -443,12 +443,14 @@ namespace TTSP {
this->max_fails = max_fails;
}
bool OptimizerInterface::IsOptimizerAvailable(const std::string &type) {
std::vector<std::string> available = OptimizerInterface::GetAvailableOptimizers();
bool Optimizers::IsOptimizerAvailable(const std::string &type) {
std::vector<std::string> available = Optimizers::GetAvailableOptimizers();
return std::find(available.cbegin(), available.cend(), type) != available.cend();
}
std::vector<std::string> OptimizerInterface::GetAvailableOptimizers() {
SavedOptimizersMap Optimizers::optimizers = SavedOptimizersMap();
std::vector<std::string> Optimizers::GetAvailableOptimizers() {
std::vector<std::string> available;
available.emplace_back("noop");
......@@ -459,13 +461,34 @@ namespace TTSP {
return available;
}
OptimizerInterface *OptimizerInterface::GetOptimizer(const std::string &type,
const OptimizationParameters &space, const OptimizerProperties &properties,
std::size_t buffer_capacity) {
OptimizerInterface *Optimizers::GetOptimizer(const std::string &type,
const OptimizationParameters &space, const OptimizerProperties &properties,
std::size_t buffer_capacity) {
if (type == "noop") return new NoopOptimizer(space, properties, buffer_capacity);
if (type == "bruteforce") return new BruteforceOptimizer(space, properties, buffer_capacity);
if (type == "alternating") return new AlternatingOptimizer(space, properties, buffer_capacity);
if (type == "annealing") return new AnnealingOptimizer(space, properties, buffer_capacity);
return nullptr;
}
void Optimizers::SaveOptimizerOrReplace(const std::string &name, const std::string &type, const TTSP::OptimizationParameters &space,
const TTSP::OptimizerProperties &properties, std::size_t buffer_capacity) {
OptimizerInterface *created = Optimizers::GetOptimizer(type, space, properties, buffer_capacity);
auto optimizer = Optimizers::optimizers.find(name);
if (optimizer != Optimizers::optimizers.end()) {
delete (*optimizer).second;
}
Optimizers::optimizers[name] = created;
}
OptimizerInterface *Optimizers::GetSavedOptimizer(const std::string &name) {
auto optimizer = Optimizers::optimizers.find(name);
if (optimizer == Optimizers::optimizers.end()) {
return nullptr;
} else {
return (*optimizer).second;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment