Commit 1e60f432 authored by Dmitry Bagaev's avatar Dmitry Bagaev
Browse files

WIP: Minor refactoring

parent 892ba96c
......@@ -170,16 +170,16 @@ int main(int argc, char **argv) {
if (rank == 0) std::cout << "Solving with " << solverName << std::endl;
TTSP::OptimizationParameter tau("tau", {3e-2, 5e-2, 6e-2, 7e-2, 8e-2, 9e-2, 1e-1, 2e-1, 3e-1, 5e-1, 7e-1, 9e-1}, 1e-3);
TTSP::OptimizationParameters parameters;
parameters.push_back(std::make_pair(tau, 1e-3));
TTSP::OptimizationParameter tau("tau", {3e-2, 5e-2, 6e-2, 7e-2, 8e-2, 9e-2, 1e-1, 2e-1, 3e-1, 5e-1, 7e-1, 9e-1}, 1e-3);
TTSP::OptimizationParameterEntries entries;
entries.push_back(std::make_pair(tau, 1e-3));
TTSP::OptimizerProperties properties;
properties["tau:use_closest"] = "false";
properties["tau:strict_bound"] = "false";
TTSP::OptimizationParametersSpace space(solverName, "test", parameters, -1.0);
TTSP::OptimizationParameters parameters(entries, -1.0);
if (!TTSP::OptimizerInterface::IsOptimizerAvailable(optimizerType)) {
if (rank == 0) {
......@@ -193,7 +193,7 @@ int main(int argc, char **argv) {
std::exit(0);
}
TTSP::OptimizerInterface *optimizer = TTSP::OptimizerInterface::GetOptimizer(optimizerType, space, properties, 50);
TTSP::OptimizerInterface *optimizer = TTSP::OptimizerInterface::GetOptimizer(optimizerType, parameters, properties, 50);
optimizer->SetVerbosityLevel(TTSP::OptimizerVerbosityLevel::Level1);
......
......@@ -64,9 +64,9 @@ namespace TTSP {
current_index = index;
}
AlternatingOptimizer::AlternatingOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
AlternatingOptimizer::AlternatingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity), current_handler_index(0) {
const OptimizationParameters &parameters = space.GetParameters();
const OptimizationParameterEntries &parameters = space.GetParameterEntries();
handlers.reserve(parameters.size());
std::for_each(parameters.begin(), parameters.end(), [this](const OptimizationParametersEntry &entry) {
handlers.emplace_back(AlternatingParameterHandler(entry.first));
......@@ -77,7 +77,7 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
OptimizationParameterPoints points(space.GetParameters().size());
OptimizationParameterPoints points(parameters.GetParameterEntries().size());
int i = 0;
std::transform(handlers.begin(), handlers.end(), points.begin(), [this, &i](const AlternatingParameterHandler &h) {
......@@ -87,7 +87,7 @@ namespace TTSP {
);
});
return OptimizationParametersSuggestion(handlers.at(current_handler_index).GetParameter(), space.GetPoints(), space.GetMetrics(), points);
return OptimizationParametersSuggestion(handlers.at(current_handler_index).GetParameter(), parameters.GetPoints(), parameters.GetMetrics(), points);
}
void AlternatingOptimizer::UpdateSpaceWithLatestResults() {
......@@ -96,7 +96,7 @@ namespace TTSP {
if (last.IsGood() && (last.GetMetricsBefore() < 0.0 || last.GetMetricsAfter() < last.GetMetricsBefore())) {
current.UpdateIndex(current.NextIndex());
space.Update(current_handler_index, space.GetParameter(current_handler_index).GetValues().at(current.GetCurrentIndex()), last.GetMetricsAfter());
parameters.Update(current_handler_index, parameters.GetParameter(current_handler_index).GetValues().at(current.GetCurrentIndex()), last.GetMetricsAfter());
} else {
current.NextDirection();
}
......
......@@ -47,7 +47,7 @@ namespace TTSP {
void UpdateSpaceWithLatestResults() override;
public:
AlternatingOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
AlternatingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......
......@@ -205,9 +205,9 @@ namespace TTSP {
return current_value;
}
AnnealingOptimizer::AnnealingOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity), current_handler_index(0), values(space.GetParameters().size()) {
const OptimizationParameters &parameters = space.GetParameters();
AnnealingOptimizer::AnnealingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity), current_handler_index(0), values(space.GetParameterEntries().size()) {
const OptimizationParameterEntries &parameters = space.GetParameterEntries();
handlers.reserve(parameters.size());
std::for_each(parameters.begin(), parameters.end(), [this](const OptimizationParametersEntry &entry) {
handlers.emplace_back(AnnealingParameterHandler(entry.first, *this));
......@@ -221,14 +221,14 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
OptimizationParameterPoints points(space.GetParameters().size());
OptimizationParameterPoints points(parameters.GetParameterEntries().size());
int i = 0;
std::transform(handlers.begin(), handlers.end(), points.begin(), [this, &i](const AnnealingParameterHandler &h) {
return std::make_pair(h.GetParameter().GetName(), i++ == current_handler_index ? h.GetNextValue() : h.GetCurrentValue());
});
return OptimizationParametersSuggestion(handlers.at(current_handler_index).GetParameter(), space.GetPoints(), space.GetMetrics(), points);
return OptimizationParametersSuggestion(handlers.at(current_handler_index).GetParameter(), parameters.GetPoints(), parameters.GetMetrics(), points);
}
void AnnealingOptimizer::UpdateSpaceWithLatestResults() {
......@@ -243,7 +243,7 @@ namespace TTSP {
if (last.IsGood() && (last.GetMetricsBefore() < 0.0 || ((delta_e < 0.0) || alpha < et))) {
double update_value = last.GetPointsAfter().at(current_handler_index).second;
h.SetValue(update_value);
space.Update(current_handler_index, update_value, last.GetMetricsAfter());
parameters.Update(current_handler_index, update_value, last.GetMetricsAfter());
}
current_handler_index = (current_handler_index + 1) % (handlers.size());
......
......@@ -88,7 +88,7 @@ namespace TTSP {
protected:
void UpdateSpaceWithLatestResults() override;
public:
AnnealingOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
AnnealingOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......
......@@ -7,19 +7,19 @@
namespace TTSP {
BruteforceOptimizer::BruteforceOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
BruteforceOptimizer::BruteforceOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity) {}
OptimizationParametersSuggestion BruteforceOptimizer::Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
const OptimizationParameters &parameters = space.GetParameters();
const OptimizationParameterPoints &before = space.GetPoints();
const OptimizationParameterEntries &entries = parameters.GetParameterEntries();
const OptimizationParameterPoints &before = parameters.GetPoints();
OptimizationParameterPoints output(parameters.size());
OptimizationParameterPoints output(entries.size());
std::transform(parameters.begin(), parameters.end(), output.begin(), [&](const OptimizationParametersEntry &entry) {
std::transform(entries.begin(), entries.end(), output.begin(), [&](const OptimizationParametersEntry &entry) {
const std::vector<double> &values = entry.first.GetValues();
double best_metrics = -1.0;
......@@ -28,7 +28,7 @@ namespace TTSP {
std::for_each(values.begin(), values.end(), [&](double value) {
std::cout << "[TTSP] [Bruteforce] Solving with " << entry.first.GetName() << " = " << value << "\t\t";
const OptimizationParameterPoints &after = space.GetPointsWithChangedParameter(entry.first, value);
const OptimizationParameterPoints &after = parameters.GetPointsWithChangedParameter(entry.first, value);
OptimizationFunctionInvokeResult result = invoke(before, after, data);
......@@ -46,7 +46,7 @@ namespace TTSP {
return std::make_pair(entry.first.GetName(), best_value);
});
return OptimizationParametersSuggestion(parameters.at(0).first, space.GetPoints(), space.GetMetrics(), output);
return OptimizationParametersSuggestion(entries.at(0).first, parameters.GetPoints(), parameters.GetMetrics(), output);
}
BruteforceOptimizer::~BruteforceOptimizer() {}
......
......@@ -11,7 +11,7 @@ namespace TTSP {
class BruteforceOptimizer : public OptimizerInterface {
public:
BruteforceOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
BruteforceOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......
......@@ -6,21 +6,21 @@
namespace TTSP {
NoopOptimizer::NoopOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
NoopOptimizer::NoopOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
OptimizerInterface(space, properties, buffer_capacity) {}
OptimizationParametersSuggestion NoopOptimizer::Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
void *)> &invoke, void *data) const {
const OptimizationParameters &parameters = space.GetParameters();
const OptimizationParameterEntries &entries = parameters.GetParameterEntries();
OptimizationParameterPoints output(parameters.size());
OptimizationParameterPoints output(entries.size());
std::transform(parameters.begin(), parameters.end(), output.begin(), [&](const OptimizationParametersEntry &entry) {
std::transform(entries.begin(), entries.end(), output.begin(), [&](const OptimizationParametersEntry &entry) {
return std::make_pair(entry.first.GetName(), entry.first.GetDefaultValue());
});
return OptimizationParametersSuggestion(parameters.at(0).first, space.GetPoints(), space.GetMetrics(), output);
return OptimizationParametersSuggestion(entries.at(0).first, parameters.GetPoints(), parameters.GetMetrics(), output);
}
NoopOptimizer::~NoopOptimizer() {}
......
......@@ -11,7 +11,7 @@ namespace TTSP {
class NoopOptimizer : public OptimizerInterface {
public:
NoopOptimizer(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
NoopOptimizer(const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......
......@@ -99,92 +99,69 @@ namespace TTSP {
return after;
}
void OptimizationParametersSpace::swap(OptimizationParametersSpace &left, OptimizationParametersSpace &right) {
std::swap(left.solver_name, right.solver_name);
std::swap(left.solver_prefix, right.solver_prefix);
std::swap(left.parameters, right.parameters);
void OptimizationParameters::swap(OptimizationParameters &left, OptimizationParameters &right) {
std::swap(left.entries, right.entries);
std::swap(left.metrics, right.metrics);
}
OptimizationParametersSpace::OptimizationParametersSpace(const std::string &solver_name,
const std::string &solver_prefix,
const OptimizationParameters &parameters,
double metrics) :
solver_name(solver_name), solver_prefix(solver_prefix), parameters(parameters), metrics(metrics) {}
OptimizationParameters::OptimizationParameters(const OptimizationParameterEntries &entries, double metrics) : entries(entries), metrics(metrics) {}
OptimizationParametersSpace::OptimizationParametersSpace(const OptimizationParametersSpace &other) :
solver_name(other.solver_name), solver_prefix(other.solver_prefix), parameters(other.parameters), metrics(other.metrics) {}
OptimizationParameters::OptimizationParameters(const OptimizationParameters &other) : entries(other.entries), metrics(other.metrics) {}
OptimizationParametersSpace::OptimizationParametersSpace(OptimizationParametersSpace &&other) noexcept {
OptimizationParametersSpace::swap(*this, other);
OptimizationParameters::OptimizationParameters(OptimizationParameters &&other) noexcept {
OptimizationParameters::swap(*this, other);
}
OptimizationParametersSpace &OptimizationParametersSpace::operator=(const OptimizationParametersSpace &other) {
OptimizationParametersSpace tmp(other);
OptimizationParametersSpace::swap(*this, tmp);
OptimizationParameters &OptimizationParameters::operator=(const OptimizationParameters &other) {
OptimizationParameters tmp(other);
OptimizationParameters::swap(*this, tmp);
return *this;
}
bool OptimizationParametersSpace::isSolverNameMatch(const std::string &solver_name) const noexcept {
return this->solver_name == solver_name;
const OptimizationParameterEntries &OptimizationParameters::GetParameterEntries() const noexcept {
return entries;
}
bool OptimizationParametersSpace::isSolverPrefixMatch(const std::string &solver_prefix) const noexcept {
return this->solver_prefix == solver_prefix;
const OptimizationParametersEntry &OptimizationParameters::GetParameterEntry(std::size_t index) const {
return entries.at(index);
}
const std::string &OptimizationParametersSpace::GetSolverName() const noexcept {
return solver_name;
}
const std::string &OptimizationParametersSpace::GetSolverPrefix() const noexcept {
return solver_prefix;
}
const OptimizationParameters &OptimizationParametersSpace::GetParameters() const noexcept {
return parameters;
}
const OptimizationParametersEntry &OptimizationParametersSpace::GetParameterEntry(std::size_t index) const {
return parameters.at(index);
}
const OptimizationParameter &OptimizationParametersSpace::GetParameter(std::size_t index) const {
const OptimizationParameter &OptimizationParameters::GetParameter(std::size_t index) const {
return GetParameterEntry(index).first;
}
const OptimizationParameterPoints OptimizationParametersSpace::GetPoints() const noexcept {
OptimizationParameterPoints points(parameters.size());
std::transform(parameters.begin(), parameters.end(), points.begin(), [](const OptimizationParametersEntry &p) {
const OptimizationParameterPoints OptimizationParameters::GetPoints() const noexcept {
OptimizationParameterPoints points(entries.size());
std::transform(entries.begin(), entries.end(), points.begin(), [](const OptimizationParametersEntry &p) {
return std::make_pair(p.first.GetName(), p.second);
});
return points;
}
double OptimizationParametersSpace::GetMetrics() const noexcept {
double OptimizationParameters::GetMetrics() const noexcept {
return metrics;
}
const OptimizationParameterPoints OptimizationParametersSpace::GetPointsWithChangedParameter(const OptimizationParameter &parameter,
double value) const noexcept {
OptimizationParameterPoints points(parameters.size());
std::transform(parameters.begin(), parameters.end(), points.begin(), [&parameter, value](const OptimizationParametersEntry &p) {
const OptimizationParameterPoints OptimizationParameters::GetPointsWithChangedParameter(const OptimizationParameter &parameter,
double value) const noexcept {
OptimizationParameterPoints points(entries.size());
std::transform(entries.begin(), entries.end(), points.begin(), [&parameter, value](const OptimizationParametersEntry &p) {
return p.first.GetName() == parameter.GetName() ? std::make_pair(p.first.GetName(), value) : std::make_pair(p.first.GetName(), p.second);
});
return points;
}
void OptimizationParametersSpace::Update(const OptimizationParameterPoints &update, double metrics) {
void OptimizationParameters::Update(const OptimizationParameterPoints &update, double metrics) {
for (int i = 0; i < update.size(); ++i) {
this->parameters[i].second = update[i].second;
this->entries[i].second = update[i].second;
}
this->metrics = metrics;
}
void OptimizationParametersSpace::Update(std::size_t index, double value, double metrics) {
this->parameters[index].second = value;
this->metrics = metrics;
void OptimizationParameters::Update(std::size_t index, double value, double metrics) {
this->entries[index].second = value;
this->metrics = metrics;
}
void OptimizationParameterResult::swap(OptimizationParameterResult &left, OptimizationParameterResult &right) {
......@@ -302,7 +279,7 @@ namespace TTSP {
void OptimizerInterface::UpdateSpaceWithLatestResults() {
if (!results.IsEmpty()) {
const OptimizationParameterResult &result = (*results.begin());
space.Update(result.GetPointsAfter(), result.GetMetricsAfter());
parameters.Update(result.GetPointsAfter(), result.GetMetricsAfter());
}
}
......@@ -316,7 +293,7 @@ namespace TTSP {
}
void OptimizerInterface::SaveResult(const OptimizationParameter &changed, const OptimizationParameterPoints &after, double metrics_after, bool is_good) {
SaveResult(changed, space.GetPoints(), space.GetMetrics(), after, metrics_after, is_good);
SaveResult(changed, parameters.GetPoints(), parameters.GetMetrics(), after, metrics_after, is_good);
}
const OptimizationParameterResultsBuffer &OptimizerInterface::GetResults() const noexcept {
......@@ -324,7 +301,7 @@ namespace TTSP {
};
const OptimizationParameterPoints OptimizerInterface::GetPoints() const noexcept {
return space.GetPoints();
return parameters.GetPoints();
}
void OptimizerInterface::SetVerbosityLevel(OptimizerVerbosityLevel level) noexcept {
......@@ -360,7 +337,7 @@ namespace TTSP {
}
OptimizerInterface *OptimizerInterface::GetOptimizer(const std::string &type,
const OptimizationParametersSpace &space, const OptimizerProperties &properties,
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);
......
......@@ -88,7 +88,7 @@ namespace TTSP {
/// This class is used to store a list of OptimizationParameter and associated current value
/// @see OptimizationParameter
/// @see OptimizationParametersEntry
typedef std::vector<OptimizationParametersEntry> OptimizationParameters;
typedef std::vector<OptimizationParametersEntry> OptimizationParameterEntries;
/// This class is used to store a slice of OptimizationParametersSpace for some optimization parameter
/// @see OptimizationParameter
......@@ -126,53 +126,35 @@ namespace TTSP {
const OptimizationParameterPoints &GetPointsAfter() const noexcept;
};
/// This class is used to define an Optimization parameters space
/// This class is used to define an Optimization parameters entries and associated metrics with it
/// Usage: OptimizationParametersSpace space("fcbiilu2", "prefix", parameters);
class OptimizationParametersSpace {
class OptimizationParameters {
private:
std::string solver_name; /// A name of a solver to which parameters are belong to
std::string solver_prefix; /// Solver prefix
OptimizationParameters parameters; /// List of optimization parameters in this space
double metrics; /// Metrics value for current optimization parameters
OptimizationParameterEntries entries; /// List of optimization parameters entries
double metrics; /// Metrics value for current optimization parameters
static void swap(OptimizationParametersSpace &left, OptimizationParametersSpace &right);
static void swap(OptimizationParameters &left, OptimizationParameters &right);
public:
/// Default constructor to define an OptimizationParametersSpace
/// @param solver_name - A name of a solver to which parameters are belong to
/// @param solver_prefix - Solver prefix
/// @param parameters - List of optimization parameters in this space
OptimizationParametersSpace(const std::string &solver_name, const std::string &solver_prefix,
const OptimizationParameters &parameters, double metrics);
/// @param entries - List of optimization parameter entries
/// @param metrics - Default metrics value
OptimizationParameters(const OptimizationParameterEntries &entries, double metrics);
/// Copy constructor
/// @param other - OptimizationParametersSpace to make copy of
OptimizationParametersSpace(const OptimizationParametersSpace &other);
OptimizationParameters(const OptimizationParameters &other);
/// Move constructor
/// @param other - OptimizationParametersSpace to swap with
OptimizationParametersSpace(OptimizationParametersSpace &&other) noexcept;
OptimizationParameters(OptimizationParameters &&other) noexcept;
/// Assignment operator
/// @param other - OptimizationParametersSpace to assign
OptimizationParametersSpace &operator=(const OptimizationParametersSpace &other);
/// Utility method to check against solver name
/// @param solver_name - A name of a solver to check against
bool isSolverNameMatch(const std::string &solver_name) const noexcept;
/// Utility method to check against solver prefix
/// @param solver_prefix - Solver prefix to check against
bool isSolverPrefixMatch(const std::string &solver_prefix) const noexcept;
/// Getter for solver name
const std::string &GetSolverName() const noexcept;
/// Getter for solver prefix
const std::string &GetSolverPrefix() const noexcept;
OptimizationParameters &operator=(const OptimizationParameters &other);
/// Getter for parameters of this space
const OptimizationParameters &GetParameters() const noexcept;
const OptimizationParameterEntries &GetParameterEntries() const noexcept;
/// Getter for parameter entry of this space by index
const OptimizationParametersEntry &GetParameterEntry(std::size_t index) const;
......@@ -313,14 +295,14 @@ namespace TTSP {
OptimizerVerbosityLevel verbosity = OptimizerVerbosityLevel::Level0;
protected:
OptimizationParameterResultsBuffer results;
OptimizationParametersSpace space;
OptimizationParameters parameters;
const OptimizerProperties properties;
virtual void UpdateSpaceWithLatestResults();
public:
OptimizerInterface(const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity) :
space(space), properties(properties), results(buffer_capacity) {};
OptimizerInterface(const OptimizationParameters &parameters, const OptimizerProperties &properties, std::size_t buffer_capacity) :
parameters(parameters), properties(properties), results(buffer_capacity) {};
virtual OptimizationParametersSuggestion Suggest(const std::function<OptimizationFunctionInvokeResult(const OptimizationParameterPoints &,
const OptimizationParameterPoints &,
......@@ -351,7 +333,7 @@ namespace TTSP {
static std::vector<std::string> GetAvailableOptimizers();
static OptimizerInterface *GetOptimizer(const std::string &type,
const OptimizationParametersSpace &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
const OptimizationParameters &space, const OptimizerProperties &properties, std::size_t buffer_capacity);
};
};
......
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