Commit 140a6240 authored by Dmitry Bagaev's avatar Dmitry Bagaev
Browse files

WIP: 2prev

parent 01434b75
......@@ -166,6 +166,7 @@ int main(int argc, char **argv) {
Solver solver = Solver(solverName, "test");
solver.SetVerbosityLevel(SolverVerbosityLevel::Level0);
solver.SetParameter("eps", "1e-12");
if (rank == 0) std::cout << "Solving with " << solverName << std::endl;
......@@ -247,18 +248,17 @@ int main(int argc, char **argv) {
return std::make_pair(is_solved, time);
};
const TTSP::OptimizationParameterPoints &before = optimizer->GetSpace().GetPoints();
const TTSP::OptimizationParametersSuggestion &suggestion = optimizer->Suggest(invoke, nullptr);
const TTSP::OptimizationFunctionInvokeResult &result = invoke(before, suggestion.second, nullptr);
const TTSP::OptimizationFunctionInvokeResult &result = invoke(suggestion.GetPointsBefore(), suggestion.GetPointsAfter(), nullptr);
bool is_good = result.first;
double metrics = result.second;
optimizer->SaveResult(suggestion.first, before, suggestion.second, metrics, is_good);
optimizer->SaveResult(suggestion.GetChangedParameter(), suggestion.GetPointsBefore(), suggestion.GetPointsAfter(), metrics, is_good);
if (is_good) {
optimizer->UpdateSpacePoints(suggestion.second);
optimizer->UpdateSpacePoints(suggestion.GetPointsAfter());
}
TTSP::OptimizerVerbosityLevel verbosity = TTSP::OptimizerVerbosityLevel::Level3;
......@@ -266,7 +266,7 @@ int main(int argc, char **argv) {
// 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.second.begin(), suggestion.second.end(), [&metadata](const TTSP::OptimizationParameterPoint &p) {
std::for_each(suggestion.GetPointsAfter().begin(), suggestion.GetPointsAfter().end(), [&metadata](const TTSP::OptimizationParameterPoint &p) {
metadata += ("\t" + INMOST::to_string(p.second));
});
std::cout << metadata << std::endl;
......@@ -275,7 +275,7 @@ int main(int argc, char **argv) {
// 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->GetSpace().GetPoints();
const TTSP::OptimizationParameterPoints &points = optimizer->GetCurrentPoints();
std::for_each(points.begin(), points.end(), [](const TTSP::OptimizationParameterPoint &p) {
std::cout << "\t" << p.first << " = " << p.second << std::endl;
});
......@@ -290,19 +290,19 @@ int main(int argc, char **argv) {
std::for_each(results.begin(), results.end(), [&index](const TTSP::OptimizationParameterResult &result) {
std::cout << "\t" << index++ << "\t" << " [";
const TTSP::OptimizationParameterPoints &before = result.GetPointsBefore();
std::for_each(before.begin(), before.end(), [](const TTSP::OptimizationParameterPoint &point) {
const TTSP::OptimizationParameterPoints &pbefore = result.GetPointsBefore();
std::for_each(pbefore.begin(), pbefore.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.first << "=" << point.second << " ";
});
std::cout << "] >> [";
std::cout << "] -> [";
const TTSP::OptimizationParameterPoints &after = result.GetPointsAfter();
std::for_each(after.begin(), after.end(), [](const TTSP::OptimizationParameterPoint &point) {
const TTSP::OptimizationParameterPoints &pafter = result.GetPointsAfter();
std::for_each(pafter.begin(), pafter.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.first << "=" << point.second << " ";
});
std::cout << "] " << result.GetMetrics() << std::endl;
std::cout << "]\t\t(" << result.GetMetricsBefore() << " -> " << result.GetMetricsAfter() << ")" << std::endl;
});
}
......
......@@ -98,7 +98,7 @@ namespace TTSP {
const OptimizationParameterResult &last = results.at(0);
const OptimizationParameterResult &before_last = results.at(1);
if (last.IsGood() && (last.GetMetrics() < before_last.GetMetrics())) {
if (last.IsGood() && (last.GetMetricsAfter() < before_last.GetMetricsAfter())) {
current.UpdateIndex(current.NextIndex());
} else {
current.NextDirection();
......@@ -115,7 +115,15 @@ namespace TTSP {
});
}
return std::make_pair(handlers.at(current_handler_index).GetParameter(), points);
return OptimizationParametersSuggestion(handlers.at(current_handler_index).GetParameter(), GetCurrentPoints(), points);
}
const OptimizationParameterPoints AlternatingOptimizer::GetCurrentPoints() const noexcept {
OptimizationParameterPoints points(space.GetParameters().size());
std::transform(handlers.cbegin(), handlers.cend(), points.begin(), [](const AlternatingParameterHandler &h) {
return std::make_pair(h.GetParameter().GetName(), h.GetParameter().GetValues().at(h.GetCurrentIndex()));
});
return points;
}
AlternatingOptimizer::~AlternatingOptimizer() {}
......
......@@ -50,6 +50,8 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) override;
const OptimizationParameterPoints GetCurrentPoints() const noexcept override;
virtual ~AlternatingOptimizer();
};
......
......@@ -240,7 +240,7 @@ namespace TTSP {
const OptimizationParameterResult &before_last = results.at(1);
double temp = h.GetCurrentTemp();
double delta_e = last.GetMetrics() - before_last.GetMetrics();
double delta_e = last.GetMetricsAfter() - before_last.GetMetricsAfter();
double et = 1.0 / (1.0 + std::exp(delta_e / temp));
//double h = std::exp(-delta_e / temp);
double alpha = h.GetRandom();
......@@ -259,7 +259,15 @@ namespace TTSP {
});
}
return std::make_pair(handlers.at(current_handler_index).GetParameter(), points);
return OptimizationParametersSuggestion(handlers.at(current_handler_index).GetParameter(), GetCurrentPoints(), points);
}
const OptimizationParameterPoints AnnealingOptimizer::GetCurrentPoints() const noexcept {
OptimizationParameterPoints points(space.GetParameters().size());
std::transform(handlers.cbegin(), handlers.cend(), points.begin(), [](const AnnealingParameterHandler &handler) {
return std::make_pair(handler.GetParameter().GetName(), handler.GetCurrentValue());
});
return points;
}
AnnealingOptimizer::~AnnealingOptimizer() {}
......
......@@ -92,6 +92,8 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) override;
const OptimizationParameterPoints GetCurrentPoints() const noexcept override;
virtual ~AnnealingOptimizer();
};
......
......@@ -46,7 +46,11 @@ namespace TTSP {
return std::make_pair(entry.first.GetName(), best_value);
});
return std::make_pair(parameters.at(0).first, output);
return OptimizationParametersSuggestion(parameters.at(0).first, GetCurrentPoints(), output);
}
const OptimizationParameterPoints BruteforceOptimizer::GetCurrentPoints() const noexcept {
return space.GetPoints();
}
BruteforceOptimizer::~BruteforceOptimizer() {}
......
......@@ -17,6 +17,8 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) override;
const OptimizationParameterPoints GetCurrentPoints() const noexcept override;
virtual ~BruteforceOptimizer();
};
......
......@@ -20,7 +20,11 @@ namespace TTSP {
return std::make_pair(entry.first.GetName(), entry.first.GetDefaultValue());
});
return std::make_pair(parameters.at(0).first, output);
return OptimizationParametersSuggestion(parameters.at(0).first, GetCurrentPoints(), output);
}
const OptimizationParameterPoints NoopOptimizer::GetCurrentPoints() const noexcept {
return space.GetPoints();
}
NoopOptimizer::~NoopOptimizer() {}
......
......@@ -17,6 +17,8 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) override;
const OptimizationParameterPoints GetCurrentPoints() const noexcept override;
virtual ~NoopOptimizer();
};
......
......@@ -78,6 +78,22 @@ namespace TTSP {
return default_value;
}
OptimizationParametersSuggestion::OptimizationParametersSuggestion(const OptimizationParameter &changed, const OptimizationParameterPoints &before,
const OptimizationParameterPoints &after) :
changed(changed), before(before), after(after) {}
const OptimizationParameter &OptimizationParametersSuggestion::GetChangedParameter() const noexcept {
return changed;
}
const OptimizationParameterPoints &OptimizationParametersSuggestion::GetPointsBefore() const noexcept {
return before;
}
const OptimizationParameterPoints &OptimizationParametersSuggestion::GetPointsAfter() const noexcept {
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);
......@@ -147,18 +163,20 @@ namespace TTSP {
void OptimizationParameterResult::swap(OptimizationParameterResult &left, OptimizationParameterResult &right) {
std::swap(left.before, right.before);
std::swap(left.metrics_before, right.metrics_before);
std::swap(left.after, right.after);
std::swap(left.metrics, right.metrics);
std::swap(left.metrics_after, right.metrics_after);
std::swap(left.is_good, right.is_good);
}
OptimizationParameterResult::OptimizationParameterResult(const OptimizationParameter &changed, const OptimizationParameterPoints &before,
const OptimizationParameterPoints &after,
double metrics, bool is_good) :
changed(changed), before(before), after(after), metrics(metrics), is_good(is_good) {}
double metrics_before, double metrics_after, bool is_good) :
changed(changed), before(before), after(after), metrics_before(metrics_before), metrics_after(metrics_after), is_good(is_good) {}
OptimizationParameterResult::OptimizationParameterResult(const OptimizationParameterResult &other) :
changed(other.changed), before(other.before), after(other.after), metrics(other.metrics), is_good(other.is_good) {}
changed(other.changed), before(other.before), after(other.after),
metrics_before(other.metrics_before), metrics_after(other.metrics_after), is_good(other.is_good) {}
OptimizationParameterResult::OptimizationParameterResult(OptimizationParameterResult &&other) noexcept : changed(std::move(other).changed) {
OptimizationParameterResult::swap(*this, other);
......@@ -178,8 +196,12 @@ namespace TTSP {
return after;
}
double OptimizationParameterResult::GetMetrics() const noexcept {
return metrics;
double OptimizationParameterResult::GetMetricsBefore() const noexcept {
return metrics_before;
}
double OptimizationParameterResult::GetMetricsAfter() const noexcept {
return metrics_after;
}
bool OptimizationParameterResult::IsGood() const noexcept {
......@@ -252,12 +274,18 @@ namespace TTSP {
}
void OptimizerInterface::SaveResult(const OptimizationParameter &changed,
const OptimizationParameterPoints &before, const OptimizationParameterPoints &after, double metrics, bool is_good) {
results.push(OptimizationParameterResult(changed, before, after, metrics, is_good));
const OptimizationParameterPoints &before, double metrics_before,
const OptimizationParameterPoints &after, double metrics_after, bool is_good) {
results.push(OptimizationParameterResult(changed, before, after, metrics_before, metrics_after, is_good));
}
const OptimizationParametersSpace &OptimizerInterface::GetSpace() const noexcept {
return space;
void OptimizerInterface::SaveResult(const OptimizationParameter &changed,
const OptimizationParameterPoints &before,
const OptimizationParameterPoints &after,
double metrics_after, bool is_good) {
results.push(
OptimizationParameterResult(changed, before, after, results.IsEmpty() ? -1.0 : (*results.begin()).GetMetricsAfter(), metrics_after, is_good)
);
}
const OptimizationParameterResultsBuffer &OptimizerInterface::GetResults() const noexcept {
......
......@@ -106,7 +106,21 @@ namespace TTSP {
/// @see OptimizationParameterPoint
/// @see OptimizationParameterPoints
/// @see OptimizationParametersSpace
typedef std::pair<const OptimizationParameter &, OptimizationParameterPoints> OptimizationParametersSuggestion;
class OptimizationParametersSuggestion {
private:
const OptimizationParameter &changed;
OptimizationParameterPoints before;
OptimizationParameterPoints after;
public:
OptimizationParametersSuggestion(const OptimizationParameter &changed,
const OptimizationParameterPoints &before, const OptimizationParameterPoints &after);
const OptimizationParameter &GetChangedParameter() const noexcept;
const OptimizationParameterPoints &GetPointsBefore() const noexcept;
const OptimizationParameterPoints &GetPointsAfter() const noexcept;
};
/// This class is used to define an Optimization parameters space
/// Usage: OptimizationParametersSpace space("fcbiilu2", "prefix", parameters);
......@@ -169,8 +183,9 @@ namespace TTSP {
private:
const OptimizationParameter &changed; /// Changed optimization parameter reference
OptimizationParameterPoints before; /// Optimization parameter points before changing
double metrics_before; /// Optimization metrics before changing
OptimizationParameterPoints after; /// Optimization parameter points after changing
double metrics; /// Optimization metrics
double metrics_after; /// Optimization metrics after changing
bool is_good; /// Is problem solved or not with new parameters
static void swap(OptimizationParameterResult &left, OptimizationParameterResult &right);
......@@ -182,7 +197,7 @@ namespace TTSP {
/// @param solve_time - Solve timings
/// @param is_solved - Is problem solved or not
OptimizationParameterResult(const OptimizationParameter &changed, const OptimizationParameterPoints &before, const OptimizationParameterPoints &after,
double metrics, bool is_good);
double metrics_before, double metrics_after, bool is_good);
/// Copy constructor
/// @param other - OptimizationParameterResult to make copy of
......@@ -202,8 +217,11 @@ namespace TTSP {
/// Getter for points of this result after changing
const OptimizationParameterPoints &GetPointsAfter() const noexcept;
/// Getter for metrics
double GetMetrics() const noexcept;
/// Getter for metrics before changing
double GetMetricsBefore() const noexcept;
/// Getter for metrics after changing
double GetMetricsAfter() const noexcept;
/// Getter for is solved status
bool IsGood() const noexcept;
......@@ -289,10 +307,15 @@ namespace TTSP {
const OptimizationParameterPoints &,
void *)> &invoke, void *data) = 0;
virtual const OptimizationParameterPoints GetCurrentPoints() const noexcept = 0;
void SaveResult(const OptimizationParameter &changed,
const OptimizationParameterPoints &before, const OptimizationParameterPoints &after, double metrics, bool is_solved);
const OptimizationParameterPoints &before, double metrics_before,
const OptimizationParameterPoints &after, double metrics_after, bool is_good);
const OptimizationParametersSpace &GetSpace() const noexcept;
void SaveResult(const OptimizationParameter &changed,
const OptimizationParameterPoints &before,
const OptimizationParameterPoints &after, double metrics_after, bool is_good);
const OptimizationParameterResultsBuffer &GetResults() const noexcept;
......
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