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

update

parent 05f84618
......@@ -30,7 +30,7 @@ double DynamicX2::GetMinimumValue(int iteration) const noexcept {
}
std::pair<double, double> DynamicX2::GetMinimumPoint(int iteration) const noexcept {
return std::make_pair(2 + std::sin(iteration / 50.0), 0.0);
return std::make_pair(2 + std::sin(iteration / 100.0), 0.0);
}
std::pair<double, double> DynamicX2::GetXRange() const noexcept {
......
......@@ -145,8 +145,9 @@ int main(int argc, char **argv) {
std::exit(0);
}
OptimizerInterface *opt = Optimizers::GetOptimizer(optimizer_type, parameters, OptimizerProperties(), 30);
OptimizerInterface *opt = Optimizers::GetOptimizer(function_type, optimizer_type, parameters, OptimizerProperties(), 30);
opt->SetVerbosityLevel(OptimizerVerbosityLevel::Level0);
opt->SetRestartStrategy(OptimizerRestartStrategy::RESTART_STRATEGY_WITH_BEST, 10);
int iteration = 0;
......
......@@ -206,7 +206,7 @@ int main(int argc, char **argv) {
TTSP::OptimizerInterface *topt = TTSP::Optimizers::GetSavedOptimizer("test");
topt->SetVerbosityLevel(TTSP::OptimizerVerbosityLevel::Level3);
topt->SetVerbosityLevel(TTSP::OptimizerVerbosityLevel::Level1);
topt->SetRestartStrategy(TTSP::OptimizerRestartStrategy::RESTART_STRATEGY_WITH_BEST, 10);
double metrics_total = 0.0;
......@@ -276,51 +276,6 @@ int main(int argc, char **argv) {
optimizer->SaveResult(suggestion, metrics, is_good);
TTSP::OptimizerVerbosityLevel verbosity = TTSP::OptimizerVerbosityLevel::Level3;
// On Level1 print some metadata information about solution and used parameters
if (rank == 0 && verbosity > TTSP::OptimizerVerbosityLevel::Level0) {
std::string metadata = solver.SolutionMetadataLine("\t");
std::for_each(suggestion.GetPointsAfter().begin(), suggestion.GetPointsAfter().end(), [&metadata](const TTSP::OptimizationParameterPoint &p) {
metadata += ("\t" + INMOST::to_string(p.GetValue()));
});
std::cout << metadata << std::endl;
}
// On Level2 also print information about next parameters
if (rank == 0 && verbosity > TTSP::OptimizerVerbosityLevel::Level1) {
std::cout << std::endl << "Next optimization parameters found for current iteration:" << std::endl;
const TTSP::OptimizationParameterPoints &points = optimizer->GetPoints();
std::for_each(points.begin(), points.end(), [](const TTSP::OptimizationParameterPoint &p) {
std::cout << "\t" << p.GetName() << " = " << p.GetValue() << std::endl;
});
}
// On Level3 also print additional information about buffer
if (rank == 0 && verbosity > TTSP::OptimizerVerbosityLevel::Level2) {
std::cout << std::endl << "Optimization results buffer output:" << std::endl;
const TTSP::OptimizationParameterResultsBuffer &results = optimizer->GetResults();
int index = 1;
std::for_each(results.cbegin(), results.cend(), [&index](const TTSP::OptimizationParameterResult &result) {
std::cout << "\t" << index++ << "\t" << " [";
const TTSP::OptimizationParameterPoints &pbefore = result.GetPointsBefore();
std::for_each(pbefore.begin(), pbefore.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.GetName() << "=" << point.GetValue() << " ";
});
std::cout << "] -> [";
const TTSP::OptimizationParameterPoints &pafter = result.GetPointsAfter();
std::for_each(pafter.begin(), pafter.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.GetName() << "=" << point.GetValue() << " ";
});
std::cout << "]\t\t(" << result.GetMetricsBefore() << " -> " << result.GetMetricsAfter() << ")" << std::endl;
});
}
if (rank == 0 && waitNext) {
std::cin.get();
}
......
......@@ -33,9 +33,6 @@ namespace TTSP {
OptimizationParameterPoint(const std::string &name, double value, OptimizationParameterType type);
static void swap(OptimizationParameterPoint &left, OptimizationParameterPoint &right);
static double convert(double value, OptimizationParameterType type);
public:
OptimizationParameterPoint(const OptimizationParameterPoint &other);
......@@ -49,6 +46,8 @@ namespace TTSP {
~OptimizationParameterPoint();
static double convert(double value, OptimizationParameterType type);
friend class OptimizationParameters;
};
......@@ -359,6 +358,9 @@ namespace TTSP {
class OptimizerInterface {
private:
int mpi_rank;
std::string name;
OptimizerVerbosityLevel verbosity = OptimizerVerbosityLevel::Level0;
OptimizerRestartStrategy restart_strategy = OptimizerRestartStrategy::RESTART_STRATEGY_NO_RESTART;
......@@ -381,8 +383,12 @@ namespace TTSP {
void *)> &invoke, void *data) const = 0;
public:
OptimizerInterface(const OptimizationParameters &parameters, const OptimizerProperties &properties, std::size_t buffer_capacity) :
parameters(parameters), properties(properties), results(buffer_capacity) {};
OptimizerInterface(const std::string &name, const OptimizationParameters &parameters, const OptimizerProperties &properties, std::size_t buffer_capacity) :
name(name), parameters(parameters), properties(properties), results(buffer_capacity), mpi_rank(0) {
#ifdef USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
};
OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &, const OptimizationParameterPoints &,
void *)> &invoke = [](const OptimizationParameterPoints &,
......@@ -415,6 +421,8 @@ namespace TTSP {
void SetRestartStrategy(OptimizerRestartStrategy strategy, std::size_t max_fails) noexcept;
const std::string &GetName() const noexcept;
virtual ~OptimizerInterface() {};
};
......@@ -428,7 +436,7 @@ namespace TTSP {
static std::vector<std::string> GetAvailableOptimizers();
static OptimizerInterface *GetOptimizer(const std::string &type,
static OptimizerInterface *GetOptimizer(const std::string &name, const std::string &type,
const OptimizationParameters &parameters, const OptimizerProperties &properties, std::size_t buffer_capacity);
static void SaveOptimizerOrReplace(const std::string &name, const std::string &type,
......
......@@ -78,8 +78,8 @@ namespace TTSP {
current_index = index;
}
AlternatingOptimizer::AlternatingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity), current_handler_index(0) {
AlternatingOptimizer::AlternatingOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(name, space, properties, buffer_capacity), current_handler_index(0) {
const OptimizationParameterEntries &parameters = space.GetParameterEntries();
handlers.reserve(parameters.size());
std::for_each(parameters.cbegin(), parameters.cend(), [this](const OptimizationParametersEntry &entry) {
......
......@@ -50,7 +50,7 @@ namespace TTSP {
void *)> &invoke, void *data) const override;
public:
AlternatingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
AlternatingOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
virtual ~AlternatingOptimizer();
};
......
......@@ -203,8 +203,8 @@ namespace TTSP {
this->value = value;
}
AnnealingOptimizer::AnnealingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity), current_handler_index(0) {
AnnealingOptimizer::AnnealingOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(name, space, properties, buffer_capacity), current_handler_index(0) {
const OptimizationParameterEntries &parameters = space.GetParameterEntries();
handlers.reserve(parameters.size());
std::for_each(parameters.cbegin(), parameters.cend(), [this](const OptimizationParametersEntry &entry) {
......
......@@ -85,7 +85,7 @@ namespace TTSP {
void *)> &invoke, void *data) const override;
public:
AnnealingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
AnnealingOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
virtual ~AnnealingOptimizer();
};
......
......@@ -61,8 +61,8 @@ namespace TTSP {
double BayesianOptimizer::DEFAULT_INITIAL_ITERATIONS_RADIUS = 0.1;
double BayesianOptimizer::DEFAULT_MAX_JUMP_BARRIER = 0.1;
BayesianOptimizer::BayesianOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity),
BayesianOptimizer::BayesianOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(name, space, properties, buffer_capacity),
unique_points_max_count(BayesianOptimizer::DEFAULT_UNIQUE_POINTS_MAX_COUNT),
unique_points_random_count(BayesianOptimizer::DEFAULT_UNIQUE_POINTS_RANDOM_COUNT),
initial_iterations_count(BayesianOptimizer::DEFAULT_INITIAL_ITERATIONS_COUNT),
......
......@@ -45,7 +45,7 @@ namespace TTSP {
void *)> &invoke, void *data) const override;
public:
BayesianOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
BayesianOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
virtual ~BayesianOptimizer();
};
......
......@@ -7,8 +7,8 @@
namespace TTSP {
BruteforceOptimizer::BruteforceOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity), current_index(0) {}
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) {}
OptimizationAlgorithmSuggestion BruteforceOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......
......@@ -20,7 +20,7 @@ namespace TTSP {
void *)> &invoke, void *data) const override;
public:
BruteforceOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
BruteforceOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
virtual ~BruteforceOptimizer();
};
......
......@@ -6,8 +6,8 @@
namespace TTSP {
NoopOptimizer::NoopOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity) {}
NoopOptimizer::NoopOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(name, space, properties, buffer_capacity) {}
OptimizationAlgorithmSuggestion NoopOptimizer::AlgorithmMakeSuggestion(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......
......@@ -16,7 +16,7 @@ namespace TTSP {
void *)> &invoke, void *data) const override;
public:
NoopOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
NoopOptimizer(const std::string &name, const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
virtual ~NoopOptimizer();
};
......
......@@ -474,6 +474,13 @@ namespace TTSP {
suggestions.push_front(suggestion);
if (verbosity >= OptimizerVerbosityLevel::Level2 && mpi_rank == 0) {
std::cout << "[Optimizer:" << name << ":Suggestion]"
<< "\t" << parameters.GetParameter(suggestion.GetChangedParameterIndex()).GetName()
<< "=" << OptimizationParameterPoint::convert(suggestion.GetChangedValue(), parameters.GetParameter(suggestion.GetChangedParameterIndex()).GetType())
<< std::endl;
}
return suggestion;
}
......@@ -482,6 +489,46 @@ namespace TTSP {
const OptimizationParameterPoints &after, double metrics_after, bool is_good) {
results.push(OptimizationParameterResult(changed_index, changed_value, before, after, metrics_before, metrics_after, is_good));
if (verbosity >= OptimizerVerbosityLevel::Level1 && mpi_rank == 0) {
std::cout << "[Optimizer:" << name << ":Result]";
std::size_t i = 0;
for (const OptimizationParametersEntry &entry : parameters.GetParameterEntries()) {
std::cout << "\t" << "before." << entry.first.GetName() << "=" << before.at(i++).GetValue();
}
std::cout << "\tbefore.metrics=" << metrics_before;
i = 0;
for (const OptimizationParametersEntry &entry : parameters.GetParameterEntries()) {
std::cout << "\t" << "after." << entry.first.GetName() << "=" << after.at(i++).GetValue();
}
std::cout << "\tafter.metrics=" << metrics_after << std::endl;
}
if (verbosity >= TTSP::OptimizerVerbosityLevel::Level3 && mpi_rank == 0) {
std::cout << "[Optimizer:" << name << ":ResultsBuffer]" << std::endl;
int index = 1;
std::for_each(results.cbegin(), results.cend(), [&index](const TTSP::OptimizationParameterResult &result) {
std::cout << "\t" << index++ << "\t" << " [";
const TTSP::OptimizationParameterPoints &pbefore = result.GetPointsBefore();
std::for_each(pbefore.begin(), pbefore.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.GetName() << "=" << point.GetValue() << " ";
});
std::cout << "] -> [";
const TTSP::OptimizationParameterPoints &pafter = result.GetPointsAfter();
std::for_each(pafter.begin(), pafter.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.GetName() << "=" << point.GetValue() << " ";
});
std::cout << "]\t(" << result.GetMetricsBefore() << " -> " << result.GetMetricsAfter() << ")" << std::endl;
});
}
bool is_updated = is_good && this->UpdateSpaceWithLatestResults();
if (is_updated) {
this->fails_count = 0;
......@@ -544,6 +591,10 @@ namespace TTSP {
this->max_fails = max_fails;
}
const std::string &OptimizerInterface::GetName() const noexcept {
return name;
}
bool Optimizers::IsOptimizerAvailable(const std::string &type) {
std::vector<std::string> available = Optimizers::GetAvailableOptimizers();
return std::find(available.cbegin(), available.cend(), type) != available.cend();
......@@ -565,15 +616,15 @@ namespace TTSP {
return available;
}
OptimizerInterface *Optimizers::GetOptimizer(const std::string &type,
OptimizerInterface *Optimizers::GetOptimizer(const std::string &name, const std::string &type,
const OptimizationParameters &parameters, const OptimizerProperties &properties,
std::size_t buffer_capacity) {
if (type == "noop") return new NoopOptimizer(parameters, properties, buffer_capacity);
if (type == "bruteforce") return new BruteforceOptimizer(parameters, properties, buffer_capacity);
if (type == "alternating") return new AlternatingOptimizer(parameters, properties, buffer_capacity);
if (type == "annealing") return new AnnealingOptimizer(parameters, properties, 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_TTSP_LIMBO)
if (type == "bayesian") return new BayesianOptimizer(parameters, properties, buffer_capacity);
if (type == "bayesian") return new BayesianOptimizer(name, parameters, properties, buffer_capacity);
#endif
return nullptr;
}
......@@ -581,7 +632,7 @@ namespace TTSP {
void Optimizers::SaveOptimizerOrReplace(const std::string &name, const std::string &type, const TTSP::OptimizationParameters &parameters,
const TTSP::OptimizerProperties &properties, std::size_t buffer_capacity) {
OptimizerInterface *created = Optimizers::GetOptimizer(type, parameters, properties, buffer_capacity);
OptimizerInterface *created = Optimizers::GetOptimizer(name, type, parameters, properties, buffer_capacity);
auto optimizer = Optimizers::optimizers.find(name);
if (optimizer != Optimizers::optimizers.end()) {
......
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