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

2prev

parent 6516342d
......@@ -6,7 +6,6 @@
#include <cstdio>
#include "Source/Solver/ttsp/ttsp.h"
#include "Source/Solver/ttsp/optimizers/bruteforce/ttsp_bruteforce.h"
using namespace INMOST;
......@@ -32,6 +31,7 @@ int main(int argc, char **argv) {
std::string vectorBFileName = "";
std::string parametersFileName = "";
std::string solverName = "fcbiilu2";
std::string optimizerType = "bruteforce";
bool matrixFound = false;
bool vectorBFound = false;
......@@ -54,10 +54,16 @@ int main(int argc, char **argv) {
std::cout << "-b, --bvector <RHS vector file name>" << std::endl;
std::cout << "-d, --database <Solver parameters file name>" << std::endl;
std::cout << "-t, --type <Solver type name>" << std::endl;
std::cout << "-o, --optt <Optimizer type name>" << std::endl;
std::cout << " Available solvers:" << std::endl;
Solver::Initialize(NULL, NULL, NULL);
std::vector<std::string> availableSolvers = Solver::getAvailableSolvers();
for (auto it = availableSolvers.begin(); it != availableSolvers.end(); it++) {
for (auto it = availableSolvers.begin(); it != availableSolvers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::OptimizerInterface::getAvailableOptimizers();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
Solver::Finalize();
......@@ -113,6 +119,15 @@ int main(int argc, char **argv) {
i++;
continue;
}
//Optimizer type found with -o ot --optt options
if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--optt") == 0) {
if (rank == 0) {
std::cout << "Optimizer type index found: " << argv[i + 1] << std::endl;
}
optimizerType = std::string(argv[i + 1]);
i++;
continue;
}
}
if (!matrixFound) {
......@@ -182,7 +197,18 @@ int main(int argc, char **argv) {
parameters.push_back(std::make_pair(tau, 1e-3));
TTSP::OptimizationParametersSpace space(solverName, "test", parameters);
TTSP::BruteforceOptimizer optimizer(space);
TTSP::OptimizerInterface *optimizer = TTSP::OptimizerInterface::getOptimizer(optimizerType, space);
if (optimizer == nullptr) {
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();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
}
std::exit(0);
}
// BARRIER;
// timer = Timer();
......@@ -200,16 +226,16 @@ int main(int argc, char **argv) {
while (test < 15) {
optimizer.Solve(solver, mat, b, x);
optimizer->Solve(solver, mat, b, x);
std::cout << std::endl << "Best optimization parameters found for current iteration:" << std::endl;
const TTSP::OptimizationParameterPoints &best = optimizer.GetSpace().GetPoints();
const TTSP::OptimizationParameterPoints &best = optimizer->GetSpace().GetPoints();
std::for_each(best.begin(), best.end(), [](const TTSP::OptimizationParameterPoint &p) {
std::cout << "\t" << p.first << " = " << p.second << std::endl;
});
std::cout << std::endl << "Optimization results buffer output:" << std::endl;
const TTSP::OptimizationParameterResultsBuffer &results = optimizer.GetResults();
const TTSP::OptimizationParameterResultsBuffer &results = optimizer->GetResults();
int index = 1;
std::for_each(results.begin(), results.end(), [&index](const TTSP::OptimizationParameterResult &result) {
......
......@@ -2,6 +2,7 @@
// Created by bvdmitri on 04.02.19.
//
#include <Source/Solver/ttsp/optimizers/bruteforce/ttsp_bruteforce.h>
#include "ttsp.h"
namespace TTSP {
......@@ -226,10 +227,19 @@ namespace TTSP {
const OptimizationParametersSpace &OptimizerInterface::GetSpace() const {
return space;
}
const OptimizationParameterResultsBuffer &OptimizerInterface::GetResults() const {
return results;
};
void OptimizerInterface::SetProperty(const std::string &name, const std::string &value) {
properties[name] = value;
}
const std::string &OptimizerInterface::GetProperty(const std::string &name) const {
return properties.at(name);
}
double OptimizerInterface::DefaultGetPreconditionerTime(const INMOST::Solver &solver) {
return atof(solver.GetParameter("time_prec").c_str());
}
......@@ -237,4 +247,22 @@ namespace TTSP {
double OptimizerInterface::DefaultGetSolveTime(const INMOST::Solver &solver) {
return atof(solver.GetParameter("time_iter").c_str());
}
bool OptimizerInterface::isOptimizerAvailable(const std::string &type) {
std::vector<std::string> available = OptimizerInterface::getAvailableOptimizers();
return std::find(available.begin(), available.end(), type) != available.end();
}
std::vector<std::string> OptimizerInterface::getAvailableOptimizers() {
std::vector<std::string> available;
available.push_back("bruteforce");
return available;
}
OptimizerInterface *OptimizerInterface::getOptimizer(const std::string &type, const OptimizationParametersSpace &space) {
if (type == "bruteforce") return new BruteforceOptimizer(space);
return nullptr;
}
}
......@@ -226,11 +226,13 @@ namespace TTSP {
typedef double (*GetPreconditionerTimeFromSolverLambda)(const INMOST::Solver &solver);
typedef double (*GetSolveTimeFromSolverLambda)(const INMOST::Solver &solver);
typedef std::map<std::string, std::string> OptimizerProperties;
class OptimizerInterface {
protected:
OptimizationParameterResultsBuffer results;
OptimizationParametersSpace space;
OptimizerProperties properties;
public:
OptimizerInterface(const OptimizationParametersSpace &space, std::size_t buffer_capacity) : space(space), results(buffer_capacity) {};
......@@ -249,11 +251,21 @@ namespace TTSP {
const OptimizationParameterResultsBuffer &GetResults() const;
void SetProperty(const std::string &name, const std::string &value);
const std::string &GetProperty(const std::string &name) const;
virtual ~OptimizerInterface() {};
static double DefaultGetPreconditionerTime(const INMOST::Solver &solver);
static double DefaultGetSolveTime(const INMOST::Solver &solver);
static bool isOptimizerAvailable(const std::string &type);
static std::vector<std::string> getAvailableOptimizers();
static OptimizerInterface *getOptimizer(const std::string &type, const OptimizationParametersSpace &space);
};
};
......
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