Skip to content
Snippets Groups Projects
Commit fda0dc48 authored by Jakub Janák's avatar Jakub Janák
Browse files

solve methods do not take a string argument but just the number of threads

parent a58d9a6f
No related branches found
No related tags found
No related merge requests found
......@@ -69,12 +69,19 @@ int Controller::run(int argc, char *argv[]) {
// solving
if (vm.contains("solve")) {
solve("");
solve();
}
if (vm.contains("solve-parallel")) {
const int num_of_threads = vm["solve-parallel"].as<int>();
solve("p", num_of_threads);
if (num_of_threads <= 0) {
std::cerr << "Number of threads must be an integer >= 1" << std::endl;
return 1;
}
if (num_of_threads == 1) {
std::cerr << "For single threaded solving call --solve" << std::endl;
}
solve(num_of_threads);
}
// approximation
......@@ -111,7 +118,7 @@ void Controller::create_synthetic_instance(const int num_of_nodes) {
std::cout << "created synthetic instance with: " << num_of_nodes << " nodes" << std::endl;
}
void Controller::solve(const std::string &args, const int num_of_threads) {
void Controller::solve(const int num_of_threads) {
if (this->unsolvedInstances.empty()) {
std::cout << "Nothing to solve!" << std::endl;
return;
......@@ -119,7 +126,7 @@ void Controller::solve(const std::string &args, const int num_of_threads) {
while (!this->unsolvedInstances.empty()) {
std::cout << "Solving..." << std::endl;
auto &instance = this->unsolvedInstances.front();
instance->solve(args, num_of_threads);
instance->solve(num_of_threads);
instance->print_statistics();
if (!instance->is_solved()) {
this->unsolvedInstances.pop_front();
......
......@@ -26,7 +26,7 @@ public:
void create_synthetic_instance(int num_of_nodes);
void solve(const std::string& args, int num_of_threads);
void solve(int num_of_threads = 1); // not multithreaded by default
void heuristic_combo();
};
......
......@@ -41,12 +41,11 @@ std::unique_ptr<TSInstance> TSInstance::create_synthetic_instance(const int numO
return std::make_unique<TSInstance>(std::move(nodes), std::move(edges));
}
std::vector<std::vector<Node> > TSInstance::solve(const std::string &args, const int num_of_threads) {
std::vector<std::vector<Node> > TSInstance::solve(const int num_of_threads) {
const auto start = std::chrono::high_resolution_clock::now();
const std::vector visitedNodes = {this->startingNode};
this->set_min_cost(heuristic_combo());
if (args == "p") {
// safe threads for M2 max: <= 8
if (num_of_threads > 1) {
start_branch_parallel(visitedNodes, 0, this->startingNode, num_of_threads);
} else {
branch(visitedNodes, 0, this->startingNode);
......
......@@ -25,7 +25,7 @@ public:
static std::unique_ptr<TSInstance> create_synthetic_instance(int numOfNodes);
std::vector<std::vector<Node> > solve(const std::string& arg, int num_of_threads = 4);
std::vector<std::vector<Node> > solve(int num_of_threads = 1); // not multithreaded by default
void branch(std::vector<Node> visitedNodes, double cost, Node &currentNode);
......
......@@ -5,7 +5,7 @@
#include "catch.hpp"
#include "../graph/TSInstance.h"
// ------------------------------------------------ HELPER FUNCTIONS -------------------------------------
// ------------------------------------------- HELPER FUNCTIONS ------------------------------------------
std::set<std::vector<Node> > convert_to_node_set(const std::vector<std::vector<Node> > &paths) {
std::set<std::vector<Node> > nodeSet;
for (const auto &path: paths) {
......@@ -19,35 +19,35 @@ TEST_CASE("Test Solve Correctness", "[all]") {
std::unique_ptr<TSInstance> instance;
SECTION("K(5)") {
instance = TSInstance::create_synthetic_instance(5);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(""));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve());
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(6)") {
instance = TSInstance::create_synthetic_instance(6);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(""));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve());
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(7)") {
instance = TSInstance::create_synthetic_instance(7);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(""));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve());
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(8)") {
instance = TSInstance::create_synthetic_instance(8);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(""));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve());
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(9)") {
instance = TSInstance::create_synthetic_instance(9);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(""));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve());
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
......@@ -58,35 +58,35 @@ TEST_CASE("Test Solve Parallel Correctness", "[all]") {
std::unique_ptr<TSInstance> instance;
SECTION("K(5)") {
instance = TSInstance::create_synthetic_instance(5);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve("p"));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(10));
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(6)") {
instance = TSInstance::create_synthetic_instance(6);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve("p"));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(10));
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(7)") {
instance = TSInstance::create_synthetic_instance(7);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve("p"));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(10));
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(8)") {
instance = TSInstance::create_synthetic_instance(8);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve("p"));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(10));
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
}
SECTION("K(9)") {
instance = TSInstance::create_synthetic_instance(9);
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve("p"));
const std::set<std::vector<Node> > solveResult = convert_to_node_set(instance->solve(10));
const std::set<std::vector<Node> > bruteForceResult = convert_to_node_set(instance->brute_force_solve());
REQUIRE(solveResult == bruteForceResult);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment