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

Alternating optimizer added

parent 467cbc1c
......@@ -36,6 +36,7 @@ int main(int argc, char **argv) {
bool seriesFound = false;
bool parametersFound = false;
bool waitNext = false;
//Parse argv parameters
if (argc == 1) goto helpMessage;
......@@ -55,6 +56,7 @@ int main(int argc, char **argv) {
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 << "-w, --wait " << std::endl;
std::cout << " Available solvers:" << std::endl;
Solver::Initialize(NULL, NULL, NULL);
std::vector<std::string> availableSolvers = Solver::getAvailableSolvers();
......@@ -126,6 +128,11 @@ int main(int argc, char **argv) {
i++;
continue;
}
//Wait for each iteration
if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--wait") == 0) {
waitNext = true;
continue;
}
}
if (!seriesFound) {
......@@ -160,9 +167,7 @@ int main(int argc, char **argv) {
if (rank == 0) std::cout << "Solving with " << solverName << std::endl;
double timer = Timer();
TTSP::OptimizationParameter tau("tau", {1e-3, 3e-3, 5e-3, 7e-3, 1e-2, 3e-2, 5e-2, 7e-2}, 1e-3);
TTSP::OptimizationParameter tau("tau", {1e-3, 3e-3, 5e-3, 7e-3, 1e-2, 3e-2, 5e-2, 6e-2, 7e-2, 8e-2, 9e-2, 1e-1, 2e-1, 3e-1}, 1e-3);
TTSP::OptimizationParameters parameters;
parameters.push_back(std::make_pair(tau, 1e-3));
......@@ -206,30 +211,33 @@ int main(int argc, char **argv) {
matrix.Load(next.first);
std::cout << "Solving with A = " << next.first << " and b = " << next.second << std::endl;
if (rank == 0) std::cout << "Solving with A = " << next.first << " and b = " << next.second << std::endl;
optimizer->Solve(solver, matrix, rhs, x);
std::cout << std::endl << "Best optimization parameters found for current iteration:" << std::endl;
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;
});
if (rank == 0) {
std::cout << std::endl << "Next optimization parameters found for current iteration:" << std::endl;
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();
std::cout << std::endl << "Optimization results buffer output:" << std::endl;
const TTSP::OptimizationParameterResultsBuffer &results = optimizer->GetResults();
int index = 1;
std::for_each(results.begin(), results.end(), [&index](const TTSP::OptimizationParameterResult &result) {
std::cout << "\t" << index++ << "\t" << " [";
int index = 1;
std::for_each(results.begin(), results.end(), [&index](const TTSP::OptimizationParameterResult &result) {
std::cout << "\t" << index++ << "\t" << " [";
const TTSP::OptimizationParameterPoints &points = result.GetPoints();
std::for_each(points.begin(), points.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.first << "=" << point.second << " ";
const TTSP::OptimizationParameterPoints &points = result.GetPoints();
std::for_each(points.begin(), points.end(), [](const TTSP::OptimizationParameterPoint &point) {
std::cout << " " << point.first << "=" << point.second << " ";
});
std::cout << "] " << result.GetPreconditionerTime() << "\t" << result.GetSolveTime() << "\t" << result.GetTime() << std::endl;
});
}
std::cout << "] " << result.GetPreconditionerTime() << "\t" << result.GetSolveTime() << "\t" << result.GetTime() << std::endl;
});
if (rank == 0) {
std::cout << std::endl
......@@ -239,6 +247,11 @@ int main(int argc, char **argv) {
<< " residual. Reason: " << solver.ReturnReason()
<< std::endl;
}
if (rank == 0 && waitNext) {
std::cin.get();
}
INMOST::MPIBarrier();
}
}
......
......@@ -39,10 +39,10 @@ MatrixSeries::MatrixSeries(const std::string &file, const std::string &directory
}
line = trim(line);
std::string path = directory_prefix + "/" + line.substr(2);
std::string path = directory_prefix + line.substr(2);
if (!is_file_exist(path)) {
std::cerr << "[WARN] File" << path << " does not exist. Skipping..." << std::endl;
std::cerr << "[WARN] File " << path << " does not exist. Skipping..." << std::endl;
continue;
}
......
add_subdirectory(bruteforce)
add_subdirectory(alternating)
set(HEADER ${HEADER} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
\ No newline at end of file
set(SOURCE ${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/ttsp_alternating.cpp)
set(HEADER ${HEADER}
${CMAKE_CURRENT_SOURCE_DIR}/ttsp_alternating.h)
set(HEADER ${HEADER} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
\ No newline at end of file
//
// Created by bvdmitri on 10.02.19.
//
#include "ttsp_alternating.h"
namespace TTSP {
AlternatingParameterHandler::AlternatingParameterHandler(const OptimizationParameter &parameter) :
parameter(parameter), direction(AlternatingDirection::RIGHT), current_index(0) {}
AlternatingParameterHandler::AlternatingParameterHandler(const AlternatingParameterHandler &other) :
parameter(other.parameter), direction(other.direction), current_index(other.current_index) {}
const OptimizationParameter &AlternatingParameterHandler::GetParameter() const {
return parameter;
}
std::size_t AlternatingParameterHandler::GetDirection() const {
return direction;
}
std::size_t AlternatingParameterHandler::GetCurrentIndex() const {
return current_index;
}
std::size_t AlternatingParameterHandler::NextIndex() {
std::size_t count = parameter.GetValues().size() - 1;
std::size_t index = current_index;
switch_direction:
switch (direction) {
case RIGHT:
if (index == count) {
direction = LEFT;
goto switch_direction;
} else {
index += 1;
}
break;
case STAY1:
break;
case LEFT:
if (index == 0) {
direction = RIGHT;
goto switch_direction;
} else {
index -= 1;
}
break;
case STAY2:
break;
default:
break;
}
return index;
}
void AlternatingParameterHandler::NextDirection() {
direction = (direction + 1) % 4;
}
void AlternatingParameterHandler::UpdateIndex(std::size_t index) {
current_index = index;
}
AlternatingOptimizer::AlternatingOptimizer(const OptimizationParametersSpace &space) :
OptimizerInterface(space, 10), current_handler_index(0) {
const OptimizationParameters &parameters = space.GetParameters();
handlers.reserve(space.GetParameters().size());
for (auto parameter : space.GetParameters()) {
handlers.emplace_back(AlternatingParameterHandler(parameter.first));
}
}
OptimizationParameterPoints AlternatingOptimizer::MakeOptimizationIteration(INMOST::Solver &solver, INMOST::Sparse::Matrix &matrix,
INMOST::Sparse::Vector &RHS) {
OptimizationParameterPoints points(space.GetParameters().size());
std::cout << handlers.at(0).GetParameter().GetName() << std::endl;
if (results.size() < 2) {
if (results.size() == 0) {
std::transform(handlers.begin(), handlers.end(), points.begin(), [](const AlternatingParameterHandler &h) {
return std::make_pair(h.GetParameter().GetName(), h.GetParameter().GetValues().at(h.GetCurrentIndex()));
});
} else if (results.size() == 1) {
int i = 0;
std::transform(handlers.begin(), handlers.end(), points.begin(), [this, &i](AlternatingParameterHandler &h) {
return std::make_pair(
h.GetParameter().GetName(),
h.GetParameter().GetValues().at(i++ == current_handler_index ? h.NextIndex() : h.GetCurrentIndex())
);
});
}
} else {
AlternatingParameterHandler &current = handlers.at(current_handler_index);
const OptimizationParameterResult &last = results.at(0);
const OptimizationParameterResult &before_last = results.at(1);
if (last.IsSolved() && (last.GetTime() < before_last.GetTime())) {
current.UpdateIndex(current.NextIndex());
} else {
current.NextDirection();
}
current_handler_index = (current_handler_index + 1) % (handlers.size());
int i = 0;
std::transform(handlers.begin(), handlers.end(), points.begin(), [this, &i](AlternatingParameterHandler &h) {
return std::make_pair(
h.GetParameter().GetName(),
h.GetParameter().GetValues().at(i++ == current_handler_index ? h.NextIndex() : h.GetCurrentIndex())
);
});
}
return points;
}
AlternatingOptimizer::~AlternatingOptimizer() {}
}
//
// Created by bvdmitri on 10.02.19.
//
#ifndef INMOST_TTSP_ALTERNATING_H
#define INMOST_TTSP_ALTERNATING_H
#include <Source/Solver/ttsp/ttsp.h>
namespace TTSP {
enum AlternatingDirection : std::size_t {
RIGHT = 0,
STAY1 = 1,
LEFT = 2,
STAY2 = 3,
};
class AlternatingParameterHandler {
private:
const OptimizationParameter parameter;
std::size_t direction;
std::size_t current_index;
public:
explicit AlternatingParameterHandler(const OptimizationParameter &parameter);
AlternatingParameterHandler(const AlternatingParameterHandler &other);
const OptimizationParameter &GetParameter() const;
std::size_t GetDirection() const;
std::size_t GetCurrentIndex() const;
std::size_t NextIndex();
void NextDirection();
void UpdateIndex(std::size_t index);
};
class AlternatingOptimizer : public OptimizerInterface {
private:
std::size_t current_handler_index;
std::vector<AlternatingParameterHandler> handlers;
public:
AlternatingOptimizer(const OptimizationParametersSpace &space);
OptimizationParameterPoints MakeOptimizationIteration(INMOST::Solver &solver, INMOST::Sparse::Matrix &matrix,
INMOST::Sparse::Vector &RHS) override;
virtual ~AlternatingOptimizer();
};
}
#endif //INMOST_TTSP_ALTERNATING_H
......@@ -10,7 +10,7 @@ namespace TTSP {
BruteforceOptimizer::BruteforceOptimizer(const OptimizationParametersSpace &space) : OptimizerInterface(space, 10) {}
OptimizationParameterPoints BruteforceOptimizer::MakeOptimizationIteration(INMOST::Solver &solver, INMOST::Sparse::Matrix &matrix,
INMOST::Sparse::Vector &RHS) const {
INMOST::Sparse::Vector &RHS) {
const OptimizationParameters &parameters = space.GetParameters();
......@@ -57,7 +57,5 @@ namespace TTSP {
return output;
}
BruteforceOptimizer::~BruteforceOptimizer() {
}
BruteforceOptimizer::~BruteforceOptimizer() {}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ namespace TTSP {
BruteforceOptimizer(const OptimizationParametersSpace &space);
OptimizationParameterPoints MakeOptimizationIteration(INMOST::Solver &solver, INMOST::Sparse::Matrix &matrix,
INMOST::Sparse::Vector &RHS) const override;
INMOST::Sparse::Vector &RHS) override;
virtual ~BruteforceOptimizer();
};
......
......@@ -3,6 +3,7 @@
//
#include <Source/Solver/ttsp/optimizers/bruteforce/ttsp_bruteforce.h>
#include <Source/Solver/ttsp/optimizers/alternating/ttsp_alternating.h>
#include "ttsp.h"
namespace TTSP {
......@@ -53,6 +54,10 @@ namespace TTSP {
return values;
}
const std::size_t OptimizationParameter::GetValuesCount() const {
return values.size();
}
const double &OptimizationParameter::GetDefaultValue() const {
return default_value;
}
......@@ -184,6 +189,10 @@ namespace TTSP {
return buffer.crbegin();
}
const OptimizationParameterResult &OptimizationParameterResultsBuffer::at(std::size_t index) const {
return *(begin() + index);
}
std::deque<OptimizationParameterResult>::const_reverse_iterator OptimizationParameterResultsBuffer::end() const {
return buffer.crend();
}
......@@ -257,12 +266,14 @@ namespace TTSP {
std::vector<std::string> available;
available.push_back("bruteforce");
available.push_back("alternating");
return available;
}
OptimizerInterface *OptimizerInterface::getOptimizer(const std::string &type, const OptimizationParametersSpace &space) {
if (type == "bruteforce") return new BruteforceOptimizer(space);
if (type == "alternating") return new AlternatingOptimizer(space);
return nullptr;
}
}
......@@ -62,6 +62,9 @@ namespace TTSP {
/// Getter for values of a parameter
const std::vector<double> &GetValues() const;
/// Getter for number of values of a parameter
const std::size_t GetValuesCount() const;
/// Getter for default_value of a parameter
const double &GetDefaultValue() const;
};
......@@ -212,6 +215,8 @@ namespace TTSP {
/// Begin iterator of the buffer
std::deque<OptimizationParameterResult>::const_reverse_iterator begin() const;
const OptimizationParameterResult &at(std::size_t index) const;
/// End iterator of the buffer
std::deque<OptimizationParameterResult>::const_reverse_iterator end() const;
......@@ -245,7 +250,7 @@ namespace TTSP {
GetSolveTimeFromSolverLambda solve_time = OptimizerInterface::DefaultGetSolveTime);
virtual OptimizationParameterPoints MakeOptimizationIteration(INMOST::Solver &solver, INMOST::Sparse::Matrix &matrix,
INMOST::Sparse::Vector &RHS) const = 0;
INMOST::Sparse::Vector &RHS) = 0;
const OptimizationParametersSpace &GetSpace() const;
......
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