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

TTSP module added

parent 5bd57ad9
......@@ -284,7 +284,7 @@ int main(int argc, char **argv) {
INMOST::OptimizerInterface *optimizer = INMOST::Optimizers::GetSavedOptimizer("test");
const INMOST::OptimizationParametersSuggestion &suggestion = optimizer->Suggest(invoke, nullptr);
const INMOST::OptimizationParametersSuggestion &suggestion = optimizer->Suggest();
const INMOST::OptimizationFunctionInvokeResult &result = invoke(suggestion.GetPointsBefore(), suggestion.GetPointsAfter(), nullptr);
......
......@@ -398,9 +398,7 @@ namespace INMOST {
virtual bool UpdateSpaceWithLatestResults();
virtual SuggestionChangedParameters AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const = 0;
virtual SuggestionChangedParameters AlgorithmMakeSuggestion() const = 0;
public:
OptimizerInterface(const std::string &name, const OptimizationParameters &parameters, const OptimizerProperties &properties, std::size_t buffer_capacity) :
......@@ -410,12 +408,7 @@ namespace INMOST {
#endif
};
OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &, const OptimizationParameterPoints &,
void *)> &invoke = [](const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *) -> OptimizationFunctionInvokeResult {
return std::make_pair(false, 0);
}, void *data = nullptr);
OptimizationParametersSuggestion Suggest();
void SaveResult(const SuggestionChangedParameters &changed,
const OptimizationParameterPoints &before, double metrics_before,
......
......@@ -402,6 +402,25 @@ namespace INMOST
typedef std::vector<std::string>::iterator solvers_names_iterator_t;
typedef std::vector<SolverParameters>::iterator solver_parameters_iterator_t;
/// Helper functions for Optimizer module and Solver
namespace TTSP {
void Enable();
void Disable();
bool isEnabled();
bool isDisabled();
void SolverOptimize(const std::string &name, const std::string &type, Solver &solver);
void SolverOptimizeSaveResult(const std::string &name, const std::string &type, double metrics, bool is_good);
void DestroySavedOptimizer(const std::string &name, const std::string &type);
}
}
#endif
......
if (USE_OPTIMIZER)
set(SOURCE ${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/ttsp.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/optimizers.cpp)
add_subdirectory(optimizers)
endif ()
......
......@@ -5,7 +5,6 @@
#include <inmost_optimizer.h>
#include "Source/Optimizer/optimizers/noop/noop.h"
#include "Source/Optimizer/optimizers/bruteforce/bruteforce.h"
#include "Source/Optimizer/optimizers/alternating/alternating.h"
#include "Source/Optimizer/optimizers/annealing/annealing.h"
......@@ -496,10 +495,9 @@ namespace INMOST {
return true;
}
OptimizationParametersSuggestion OptimizerInterface::Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) {
SuggestionChangedParameters changed = this->AlgorithmMakeSuggestion(invoke, data);
OptimizationParametersSuggestion OptimizerInterface::Suggest() {
SuggestionChangedParameters changed = this->AlgorithmMakeSuggestion();
OptimizationParametersSuggestion suggestion = OptimizationParametersSuggestion(
changed,
......@@ -650,7 +648,6 @@ namespace INMOST {
std::vector<std::string> available;
available.emplace_back("noop");
available.emplace_back("bruteforce");
available.emplace_back("alternating");
available.emplace_back("annealing");
#if defined(USE_OPTIMIZER_BAYESIAN)
......@@ -664,7 +661,6 @@ namespace INMOST {
const OptimizationParameters &parameters, const OptimizerProperties &properties,
std::size_t buffer_capacity) {
if (type == "noop") return new NoopOptimizer(name, parameters, properties, buffer_capacity);
if (type == "bruteforce") return new BruteforceOptimizer(name, parameters, properties, buffer_capacity);
if (type == "alternating") return new AlternatingOptimizer(name, parameters, properties, buffer_capacity);
if (type == "annealing") return new AnnealingOptimizer(name, parameters, properties, buffer_capacity);
#if defined(USE_OPTIMIZER_BAYESIAN)
......
add_subdirectory(noop)
add_subdirectory(bruteforce)
add_subdirectory(alternating)
add_subdirectory(annealing)
......
......@@ -87,9 +87,7 @@ namespace INMOST {
});
}
SuggestionChangedParameters AlternatingOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
SuggestionChangedParameters AlternatingOptimizer::AlgorithmMakeSuggestion() const {
const AlternatingParameterHandler &handler = handlers.at(current_handler_index);
return std::vector<SuggestionChangedParameter>{SuggestionChangedParameter(current_handler_index, handler.GetParameter().GetValues().at(handler.NextIndex()))};
}
......
......@@ -45,9 +45,7 @@ namespace INMOST {
protected:
bool UpdateSpaceWithLatestResults() override;
SuggestionChangedParameters AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const override;
SuggestionChangedParameters AlgorithmMakeSuggestion() const override;
public:
AlternatingOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
......
......@@ -212,9 +212,7 @@ namespace INMOST {
});
}
SuggestionChangedParameters AnnealingOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
SuggestionChangedParameters AnnealingOptimizer::AlgorithmMakeSuggestion() const {
return std::vector<SuggestionChangedParameter>{SuggestionChangedParameter(current_handler_index, handlers.at(current_handler_index).GetNextValue())};
}
......
......@@ -80,9 +80,7 @@ namespace INMOST {
protected:
bool UpdateSpaceWithLatestResults() override;
SuggestionChangedParameters AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const override;
SuggestionChangedParameters AlgorithmMakeSuggestion() const override;
public:
AnnealingOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
......
......@@ -92,9 +92,7 @@ namespace INMOST {
}
SuggestionChangedParameters BayesianOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
SuggestionChangedParameters BayesianOptimizer::AlgorithmMakeSuggestion() const {
auto unique = results.GetLastUniqueEntries(unique_points_max_count);
auto entries = parameters.GetParameterEntries();
......
......@@ -42,9 +42,7 @@ namespace INMOST {
protected:
bool UpdateSpaceWithLatestResults() override;
SuggestionChangedParameters AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const override;
SuggestionChangedParameters AlgorithmMakeSuggestion() const override;
public:
BayesianOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
......
set(SOURCE ${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/bruteforce.cpp)
set(HEADER ${HEADER}
${CMAKE_CURRENT_SOURCE_DIR}/bruteforce.h)
set(HEADER ${HEADER} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
\ No newline at end of file
//
// Created by bvdmitri on 31.01.19.
//
#include "bruteforce.h"
namespace INMOST {
BruteforceOptimizer::BruteforceOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(name, space, properties, buffer_capacity), current_index(0) {}
SuggestionChangedParameters BruteforceOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
const OptimizationParameterPoints &before = parameters.GetPoints();
const OptimizationParameter &parameter = parameters.GetParameter(current_index);
double best_metrics = -1.0;
double best_value = 0.0;
std::for_each(parameter.GetValues().cbegin(), parameter.GetValues().cend(), [&](double value) {
std::cout << "[TTSP] [Bruteforce] Solving with " << parameter.GetName() << " = " << value << "\t\t";
const OptimizationParameterPoints &after = parameters.GetPointsWithChangedParameter(parameter, value);
OptimizationFunctionInvokeResult result = invoke(before, after, data);
bool is_solved = result.first;
double metrics = result.second;
if (is_solved && (best_metrics < 0 || metrics < best_metrics)) {
best_metrics = metrics;
best_value = value;
}
std::cout << "| Metrics = " << metrics << "\t" << is_solved << std::endl;
});
return std::vector<SuggestionChangedParameter>{SuggestionChangedParameter(current_index, best_value)};
}
bool BruteforceOptimizer::UpdateSpaceWithLatestResults() {
const OptimizationParameterResult &last = results.at(0);
if (last.IsGood()) {
parameters.Update(current_index, last.GetPointsAfter().at(current_index).GetValue(), last.GetMetricsAfter());
}
return true;
}
BruteforceOptimizer::~BruteforceOptimizer() {}
}
\ No newline at end of file
//
// Created by bvdmitri on 31.01.19.
//
#ifndef INMOST_BRUTEFORCE_H
#define INMOST_BRUTEFORCE_H
#include <inmost_optimizer.h>
namespace INMOST {
class BruteforceOptimizer : public OptimizerInterface {
private:
std::size_t current_index;
protected:
bool UpdateSpaceWithLatestResults() override;
SuggestionChangedParameters AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const override;
public:
BruteforceOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
virtual ~BruteforceOptimizer();
};
}
#endif //INMOST_BRUTEFORCE_H
......@@ -9,9 +9,7 @@ namespace INMOST {
NoopOptimizer::NoopOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(name, space, properties, buffer_capacity) {}
SuggestionChangedParameters NoopOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
SuggestionChangedParameters NoopOptimizer::AlgorithmMakeSuggestion() const {
return std::vector<SuggestionChangedParameter>{SuggestionChangedParameter(0, parameters.GetParameter(0).GetDefaultValue())};
}
......
......@@ -11,9 +11,7 @@ namespace INMOST {
class NoopOptimizer : public OptimizerInterface {
protected:
SuggestionChangedParameters AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const override;
SuggestionChangedParameters AlgorithmMakeSuggestion() const override;
public:
NoopOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
......
......@@ -34,6 +34,8 @@ if (USE_SOLVER_SUPERLU)
endif()
endif(USE_SOLVER)
add_subdirectory(ttsp)
set(HEADER ${HEADER} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
set(SOLVER_DEFINITIONS ${SOLVER_DEFINITIONS} PARENT_SCOPE)
set(SOURCE ${SOURCE} ${CMAKE_CURRENT_SOURCE_DIR}/ttsp.cpp)
set(HEADER ${HEADER} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
\ No newline at end of file
#include "inmost.h"
#include "inmost_solver.h"
#include "inmost_optimizer.h"
#include "../../Misc/utils.h"
namespace INMOST {
namespace TTSP {
#if defined(USE_OPTIMIZER)
static bool ttsp_disabled = true;
static std::map<std::string, INMOST::OptimizerInterface *> optimizers = std::map<std::string, INMOST::OptimizerInterface *>();
#endif
void Enable() {
#if defined(USE_OPTIMIZER)
ttsp_disabled = false;
#endif
}
void Disable() {
#if defined(USE_OPTIMIZER)
ttsp_disabled = true;
#endif
}
bool isEnabled() {
#if defined(USE_OPTIMIZER)
return !ttsp_disabled;
#else
return false;
#endif
}
bool isDisabled() {
#if defined(USE_OPTIMIZER)
return ttsp_disabled;
#else
return true;
#endif
}
#if defined(USE_OPTIMIZER)
OptimizerInterface *GetOrCreateOptimizer(const std::string &name, const std::string &type) {
OptimizerInterface *optimizer = nullptr;
const std::string &key = name + "_" + type;
if (optimizers.find(key) == optimizers.end()) {
// TODO Here parameters for fcbiilu2 only
OptimizationParameter tau("tau", std::make_pair(-3, -1.0), 0.05, -2.0, OptimizationParameterType::PARAMETER_TYPE_EXPONENT);
OptimizationParameterEntries entries;
entries.emplace_back(std::make_pair(tau, tau.GetDefaultValue()));
OptimizerProperties properties;
properties["tau:use_closest"] = "false";
properties["tau:strict_bound"] = "false";
OptimizationParameters parameters(entries, -1.0);
optimizer = Optimizers::GetOptimizer(name, type, parameters, properties, 15);
optimizers[key] = optimizer;
} else {
optimizer = optimizers[key];
}
return optimizer;
}
#endif
void SolverOptimize(const std::string &name, const std::string &type, Solver &solver) {
#if defined(USE_OPTIMIZER)
OptimizerInterface *optimizer = GetOrCreateOptimizer(name, type);
const OptimizationParametersSuggestion &suggestion = optimizer->Suggest();
std::for_each(suggestion.GetPointsAfter().begin(), suggestion.GetPointsAfter().end(), [&solver](const OptimizationParameterPoint &point) {
solver.SetParameter(point.GetName(), INMOST::to_string(point.GetValue()));
});
#endif
}
void SolverOptimizeSaveResult(const std::string &name, const std::string &type, double metrics, bool is_good) {
OptimizerInterface *optimizer = GetOrCreateOptimizer(name, type);
auto last_suggestion = optimizer->GetLastSuggestion();
optimizer->SaveResult(last_suggestion, metrics, is_good);
}
void DestroySavedOptimizer(const std::string &name, const std::string &type) {
#if defined(USE_OPTIMIZER)
if (optimizers.find(name) != optimizers.end()) {
delete optimizers[name];
optimizers.erase(name);
}
#endif
}
}
}
\ No newline at end of file
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