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

upd ttsp examples

parent d421b2b8
set(SOURCE ${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/static_sin.cpp
${CMAKE_CURRENT_SOURCE_DIR}/static_sin_r2.cpp
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_x2.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_x2.cpp
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_r2.cpp)
set(HEADER ${HEADER}
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_function.h
${CMAKE_CURRENT_SOURCE_DIR}/static_sin.h
${CMAKE_CURRENT_SOURCE_DIR}/static_sin_r2.h
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_x2.h)
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_x2.h
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_r2.h)
set(HEADER ${HEADER} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
\ No newline at end of file
......@@ -6,6 +6,7 @@
#define INMOST_DYNAMIC_FUNCTION_H
#include <utility>
#include <inmost_ttsp.h>
class DynamicFunction {
public:
......@@ -18,6 +19,14 @@ public:
virtual std::pair<double, double> GetXRange() const noexcept = 0;
virtual std::pair<double, double> GetYRange() const noexcept = 0;
virtual TTSP::OptimizationParameterType GetXParameterType() const noexcept {
return TTSP::OptimizationParameterType::PARAMETER_TYPE_DEFAULT;
}
virtual TTSP::OptimizationParameterType GetYParameterType() const noexcept {
return TTSP::OptimizationParameterType::PARAMETER_TYPE_DEFAULT;
}
};
......
//
// Created by Dmitri Bagaev on 2019-04-13.
//
#include "dynamic_r2.h"
DynamicR2::DynamicR2() : distribution(-0.04, 0.04) {
unsigned int seed = static_cast<unsigned int>(time(NULL));
generator.seed(seed);
}
double DynamicR2::invoke(double x, double y, int iteration) const noexcept {
std::pair<double, double> min_point = GetMinimumPoint(iteration);
double mx = min_point.first;
double my = min_point.second;
double tmp1 = std::log10(x / mx);
double tmp2 = (17.5 * (y - my)) / (7.5 + y - my);
double f = ((16.0 / 25.0) * (tmp1 * tmp1) + 1) * ((1.0 / 25.0) * (tmp2 * tmp2) + 1);
return f * (1 + distribution(generator));
}
double DynamicR2::GetMinimumValue(int iteration) const noexcept {
return 1.0;
}
std::pair<double, double> DynamicR2::GetMinimumPoint(int iteration) const noexcept {
double kx = 200;
double ky = 800;
// double x = std::pow(10, -2 - std::cos((2 * M_PI * iteration) / kx));
double x = 1.0 / (1.0 / (std::pow(10, -3) - std::pow(10, -1)) - ((double) iteration / 20.0)) + std::pow(10, -1);
double y = 2 + std::cos((2 * M_PI * iteration) / ky);
return std::make_pair(x, y);
}
std::pair<double, double> DynamicR2::GetXRange() const noexcept {
return std::make_pair(-3.5, -0.5);
}
std::pair<double, double> DynamicR2::GetYRange() const noexcept {
return std::make_pair(0, 4);
}
TTSP::OptimizationParameterType DynamicR2::GetXParameterType() const noexcept {
return TTSP::OptimizationParameterType::PARAMETER_TYPE_EXPONENT;
}
//
// Created by Dmitri Bagaev on 2019-04-13.
//
#ifndef INMOST_DYNAMIC_R2_H
#define INMOST_DYNAMIC_R2_H
#include "dynamic_function.h"
#include <random>
class DynamicR2 : public DynamicFunction {
private:
mutable std::mt19937 generator;
mutable std::uniform_real_distribution<> distribution;
public:
DynamicR2();
double invoke(double x, double y, int iteration) const noexcept override;
double GetMinimumValue(int iteration) const noexcept override;
std::pair<double, double> GetMinimumPoint(int iteration) const noexcept override;
std::pair<double, double> GetXRange() const noexcept override;
std::pair<double, double> GetYRange() const noexcept override;
TTSP::OptimizationParameterType GetXParameterType() const noexcept override;
};
#endif //INMOST_DYNAMIC_R2_H
......@@ -6,7 +6,10 @@
#include <cmath>
DynamicX2::DynamicX2() : distribution(-0.04, 0.04) {}
DynamicX2::DynamicX2() : distribution(-0.04, 0.04) {
unsigned int seed = static_cast<unsigned int>(time(NULL));
generator.seed(seed);
}
double DynamicX2::invoke(double x, double y, int iteration) const noexcept {
......@@ -28,7 +31,7 @@ double DynamicX2::invoke(double x, double y, int iteration) const noexcept {
f = a * x * x + b * x + c;
}
return f + 1 + 2 * distribution(generator);
return f + 1 + f * distribution(generator);
}
double DynamicX2::GetMinimumValue(int iteration) const noexcept {
......
......@@ -4,6 +4,7 @@
#include <static_sin.h>
#include <static_sin_r2.h>
#include <dynamic_x2.h>
#include <dynamic_r2.h>
#include <inmost_ttsp.h>
......@@ -11,26 +12,25 @@ using namespace TTSP;
int main(int argc, char **argv) {
int rank = 0, size = 1;
#if defined(USE_MPI)
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get the rank of the current process
MPI_Comm_size(MPI_COMM_WORLD, &size); // Get the total number of processors used
#endif
bool use_second_parameter = false;
int max_iterations = 500;
double x_default = 0.0;
double y_default = 0.0;
double x_step = 0.05;
double y_step = 0.05;
double x_default = 0.0;
double y_default = 0.0;
bool x_default_found = false;
bool y_default_found = false;
int x_steps = 40;
int y_steps = 40;
std::string optimizer_type = "noop";
std::string function_type = "sin1d";
std::string out_file = "";
std::string out_folder = "";
//Parse argv parameters
if (argc == 1) goto help_message;
......@@ -39,27 +39,25 @@ int main(int argc, char **argv) {
//Print help message and exit
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
help_message:
if (rank == 0) {
std::cout << "Help message: " << std::endl;
std::cout << "Command line options: " << std::endl;
std::cout << "Required: " << std::endl;
std::cout << "-o, --optimizer <Optimizer type>" << std::endl;
std::cout << "Optional: " << std::endl;
std::cout << "-f, --function <function type>" << std::endl;
std::cout << "-usp, -y, --use-second-parameter" << std::endl;
std::cout << "-xd, --x-default <x default value>" << std::endl;
std::cout << "-yd, --y-default <y default value>" << std::endl;
std::cout << "-xs, --x-step <x step>" << std::endl;
std::cout << "-ys, --y-step <y step>" << std::endl;
std::cout << "-mi, --max-iterations <max iterations>" << std::endl;
std::cout << "-of, --out-file <out file path>" << std::endl;
std::cout << "-w, --wait" << std::endl;
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::Optimizers::GetAvailableOptimizers();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
std::cout << "Help message: " << std::endl;
std::cout << "Command line options: " << std::endl;
std::cout << "Required: " << std::endl;
std::cout << "-o, --optimizer <Optimizer type>" << std::endl;
std::cout << "Optional: " << std::endl;
std::cout << "-f, --function <function type>" << std::endl;
std::cout << "-usp, -y, --use-second-parameter" << std::endl;
std::cout << "-xd, --x-default <x default value>" << std::endl;
std::cout << "-yd, --y-default <y default value>" << std::endl;
std::cout << "-xs, --x-step <x step>" << std::endl;
std::cout << "-ys, --y-step <y step>" << std::endl;
std::cout << "-mi, --max-iterations <max iterations>" << std::endl;
std::cout << "-of, --out-folder <out folder path>" << std::endl;
std::cout << "-w, --wait" << std::endl;
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::Optimizers::GetAvailableOptimizers();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
return 0;
}
......@@ -83,27 +81,29 @@ int main(int argc, char **argv) {
continue;
}
if (strcmp(argv[i], "-xd") == 0 || strcmp(argv[i], "--x-default") == 0) {
x_default = std::atoi(argv[i + 1]);
x_default_found = true;
x_default = std::atoi(argv[i + 1]);
i++;
continue;
}
if (strcmp(argv[i], "-yd") == 0 || strcmp(argv[i], "--y-default") == 0) {
y_default = std::atoi(argv[i + 1]);
y_default_found = true;
y_default = std::atoi(argv[i + 1]);
i++;
continue;
}
if (strcmp(argv[i], "-xs") == 0 || strcmp(argv[i], "--x-step") == 0) {
x_step = std::atoi(argv[i + 1]);
x_steps = std::atoi(argv[i + 1]);
i++;
continue;
}
if (strcmp(argv[i], "-ys") == 0 || strcmp(argv[i], "--y-step") == 0) {
y_step = std::atoi(argv[i + 1]);
y_steps = std::atoi(argv[i + 1]);
i++;
continue;
}
if (strcmp(argv[i], "-of") == 0 || strcmp(argv[i], "--out-file") == 0) {
out_file = std::string(argv[i + 1]);
if (strcmp(argv[i], "-of") == 0 || strcmp(argv[i], "--out-folder") == 0) {
out_folder = std::string(argv[i + 1]);
i++;
continue;
}
......@@ -116,17 +116,22 @@ int main(int argc, char **argv) {
f = new StaticSinR2();
} else if (function_type == "x21d") {
f = new DynamicX2();
} else if (function_type == "x22d") {
f = new DynamicR2();
} else {
std::cout << "Available function types:" << std::endl;
std::cout << " sin1d - Static sin with x parameter only" << std::endl;
std::cout << " sin2d - Static sin with x and y parameters" << std::endl;
std::cout << " x21d - Dynamic x2 with x parameter only" << std::endl;
std::cout << " x22d - Dynamic x2 with x and y parameters" << std::endl;
return 0;
}
std::pair<double, double> xrange = f->GetXRange();
std::pair<double, double> yrange = f->GetYRange();
OptimizationParameter x("x", f->GetXRange(), x_step, x_default);
OptimizationParameter y("y", f->GetYRange(), y_step, y_default);
OptimizationParameter x("x", xrange, (xrange.second - xrange.first) / x_steps, x_default_found ? x_default : (xrange.first + xrange.second) / 2.0, f->GetXParameterType());
OptimizationParameter y("y", yrange, (yrange.second - yrange.first) / y_steps, y_default_found ? y_default : (yrange.first + yrange.second) / 2.0, f->GetYParameterType());
TTSP::OptimizationParameterEntries entries;
entries.emplace_back(std::make_pair(x, x.GetDefaultValue()));
......@@ -137,14 +142,12 @@ int main(int argc, char **argv) {
TTSP::OptimizationParameters parameters(entries, -1.0);
if (!TTSP::Optimizers::IsOptimizerAvailable(optimizer_type)) {
if (rank == 0) {
std::cout << "Optimizer " << optimizer_type << " not found" << std::endl;
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::Optimizers::GetAvailableOptimizers();
std::cout << "Optimizer " << optimizer_type << " not found" << std::endl;
std::cout << " Available optimizers:" << std::endl;
std::vector<std::string> availableOptimizers = TTSP::Optimizers::GetAvailableOptimizers();
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
for (auto it = availableOptimizers.begin(); it != availableOptimizers.end(); ++it) {
std::cout << " " << *it << std::endl;
}
std::exit(0);
}
......@@ -158,8 +161,8 @@ int main(int argc, char **argv) {
double best_metrics = -1.0;
FILE *output = nullptr;
if (out_file != "") {
output = std::fopen(out_file.c_str(), "w");
if (out_folder != "") {
output = std::fopen((out_folder + "/" + optimizer_type + "_" + function_type).c_str(), "w");
}
while (iteration < max_iterations) {
......@@ -174,18 +177,15 @@ int main(int argc, char **argv) {
opt->SaveResult(suggestion, metrics, true);
if (rank == 0) {
std::pair<double, double> min_point = f->GetMinimumPoint(iteration);
std::pair<double, double> min_point = f->GetMinimumPoint(iteration);
double mx = min_point.first;
double my = min_point.second;
double mx = min_point.first;
double my = min_point.second;
std::printf("%4d: f( %.4lf , %.4lf ) = %e | f_min(%.4lf, %.4lf) = %e\n", iteration, xv, yv, metrics, mx, my, f->GetMinimumValue(iteration));
std::printf("%4d: f( %.4lf , %.4lf ) = %e | f_min(%.4lf, %.4lf) = %e\n", iteration, xv, yv, metrics, mx, my, f->GetMinimumValue(iteration));
if (output != nullptr) {
std::fprintf(output, "%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", iteration, xv, yv, metrics, mx, my, f->GetMinimumValue(iteration));
}
if (output != nullptr) {
std::fprintf(output, "%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", iteration, xv, yv, metrics, mx, my, f->GetMinimumValue(iteration));
}
iteration += 1;
......
......@@ -239,12 +239,13 @@ int main(int argc, char **argv) {
INMOST::Sparse::Vector rhs("b");
INMOST::Sparse::Vector x("x");
matrix.Load(next.first);
INMOST_DATA_ENUM_TYPE mbeg, mend;
matrix.GetInterval(mbeg, mend);
x.SetInterval(mbeg, mend);
for (int k = mbeg;
k < mend; ++k) {
for (int k = mbeg; k < mend; ++k) {
x[k] = 0.0;
}
......@@ -255,8 +256,6 @@ int main(int argc, char **argv) {
for (int k = mbeg; k < mend; ++k) rhs[k] = 1.0;
}
matrix.Load(next.first);
auto invoke = [&solver, &matrix, &rhs, &x](const TTSP::OptimizationParameterPoints &before, const TTSP::OptimizationParameterPoints &after,
void *data) -> TTSP::OptimizationFunctionInvokeResult {
......
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