From a29b980fde9af671c00166a063142378dfca5fea Mon Sep 17 00:00:00 2001 From: jcoupey Date: Mon, 2 Dec 2024 14:48:17 +0100 Subject: [PATCH 1/4] Make set_exploration_level a member function. --- src/main.cpp | 2 +- src/structures/cl_args.cpp | 10 +++++----- src/structures/cl_args.h | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bad13ec18..f3076c0dd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -164,7 +164,7 @@ int main(int argc, char** argv) { vroom::io::update_port(cl_args.servers, port); } exploration_level = std::min(exploration_level, vroom::MAX_EXPLORATION_LEVEL); - vroom::io::set_exploration_level(cl_args, exploration_level); + cl_args.set_exploration_level(exploration_level); if (debug_depth) { cl_args.depth = debug_depth.value(); } diff --git a/src/structures/cl_args.cpp b/src/structures/cl_args.cpp index 944c1d5a4..b1d534e44 100644 --- a/src/structures/cl_args.cpp +++ b/src/structures/cl_args.cpp @@ -75,17 +75,17 @@ void update_port(Servers& servers, std::string_view value) { } } -void set_exploration_level(CLArgs& cl_args, unsigned exploration_level) { - cl_args.depth = exploration_level; +void CLArgs::set_exploration_level(unsigned exploration_level) { + depth = exploration_level; assert(exploration_level <= MAX_EXPLORATION_LEVEL); - cl_args.nb_searches = 4 * (exploration_level + 1); + nb_searches = 4 * (exploration_level + 1); if (exploration_level >= 4) { - cl_args.nb_searches += 4; + nb_searches += 4; } if (exploration_level == MAX_EXPLORATION_LEVEL) { - cl_args.nb_searches += 4; + nb_searches += 4; } } diff --git a/src/structures/cl_args.h b/src/structures/cl_args.h index a20ca7ae5..71b4ef9e9 100644 --- a/src/structures/cl_args.h +++ b/src/structures/cl_args.h @@ -36,14 +36,14 @@ struct CLArgs { unsigned nb_threads; // -t unsigned nb_searches; // derived from -x unsigned depth; // derived from -x + + void set_exploration_level(unsigned exploration_level); }; void update_host(Servers& servers, std::string_view value); void update_port(Servers& servers, std::string_view value); -void set_exploration_level(CLArgs& cl_args, unsigned exploration_level); - } // namespace vroom::io #endif From 0569fb3129808c9d01575eeece44b78ef14b6aeb Mon Sep 17 00:00:00 2001 From: jcoupey Date: Mon, 2 Dec 2024 14:53:44 +0100 Subject: [PATCH 2/4] Move depth and searches calculation to reusable static functions. --- src/structures/cl_args.cpp | 16 +++++++++++++--- src/structures/cl_args.h | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/structures/cl_args.cpp b/src/structures/cl_args.cpp index b1d534e44..caf7c08d7 100644 --- a/src/structures/cl_args.cpp +++ b/src/structures/cl_args.cpp @@ -75,18 +75,28 @@ void update_port(Servers& servers, std::string_view value) { } } -void CLArgs::set_exploration_level(unsigned exploration_level) { - depth = exploration_level; +unsigned CLArgs::get_depth(unsigned exploration_level) { + return exploration_level; +} +unsigned CLArgs::get_nb_searches(unsigned exploration_level) { assert(exploration_level <= MAX_EXPLORATION_LEVEL); - nb_searches = 4 * (exploration_level + 1); + unsigned nb_searches = 4 * (exploration_level + 1); if (exploration_level >= 4) { nb_searches += 4; } if (exploration_level == MAX_EXPLORATION_LEVEL) { nb_searches += 4; } + + return nb_searches; +} + +void CLArgs::set_exploration_level(unsigned exploration_level) { + depth = get_depth(exploration_level); + + nb_searches = get_nb_searches(exploration_level); } } // namespace vroom::io diff --git a/src/structures/cl_args.h b/src/structures/cl_args.h index 71b4ef9e9..53f3c131a 100644 --- a/src/structures/cl_args.h +++ b/src/structures/cl_args.h @@ -37,6 +37,10 @@ struct CLArgs { unsigned nb_searches; // derived from -x unsigned depth; // derived from -x + static unsigned get_depth(unsigned exploration_level); + + static unsigned get_nb_searches(unsigned exploration_level); + void set_exploration_level(unsigned exploration_level); }; From 2e0bbe8cce146add5df242cdb471ef8c73e5aae4 Mon Sep 17 00:00:00 2001 From: jcoupey Date: Mon, 2 Dec 2024 14:55:11 +0100 Subject: [PATCH 3/4] Derive parameters from exploration level in Input::solve. --- src/structures/vroom/input/input.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/structures/vroom/input/input.h b/src/structures/vroom/input/input.h index e4a6358fd..7f2478ed5 100644 --- a/src/structures/vroom/input/input.h +++ b/src/structures/vroom/input/input.h @@ -16,6 +16,7 @@ All rights reserved (see LICENSE). #include #include "routing/wrapper.h" +#include "structures/cl_args.h" #include "structures/generic/matrix.h" #include "structures/typedefs.h" #include "structures/vroom/matrices.h" @@ -193,6 +194,21 @@ class Input { // Returns true iff both vehicles have common job candidates. bool vehicle_ok_with_vehicle(Index v1_index, Index v2_index) const; + Solution solve(unsigned exploration_level, + unsigned nb_thread, + const Timeout& timeout = Timeout(), + const std::vector& h_param = + std::vector()) { + // Overload designed to expose the same interface as the `-x` + // command-line flag for out-of-the-box setup of exploration + // level. + return solve(io::CLArgs::get_nb_searches(exploration_level), + io::CLArgs::get_depth(exploration_level), + nb_thread, + timeout, + h_param); + } + Solution solve(unsigned nb_searches, unsigned depth, unsigned nb_thread, From bb36ba004693b5319a740230e6449d5e7b23ec6e Mon Sep 17 00:00:00 2001 From: jcoupey Date: Mon, 2 Dec 2024 15:09:37 +0100 Subject: [PATCH 4/4] Have get_nb_searches and get_depth live in vroom::utils namespace instead. --- src/structures/cl_args.cpp | 23 +++-------------------- src/structures/cl_args.h | 4 ---- src/structures/vroom/input/input.cpp | 11 +++++++++++ src/structures/vroom/input/input.h | 20 ++++++-------------- src/utils/helpers.h | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/structures/cl_args.cpp b/src/structures/cl_args.cpp index caf7c08d7..5dfeac314 100644 --- a/src/structures/cl_args.cpp +++ b/src/structures/cl_args.cpp @@ -10,6 +10,7 @@ All rights reserved (see LICENSE). #include #include "structures/cl_args.h" +#include "utils/helpers.h" namespace vroom::io { @@ -75,28 +76,10 @@ void update_port(Servers& servers, std::string_view value) { } } -unsigned CLArgs::get_depth(unsigned exploration_level) { - return exploration_level; -} - -unsigned CLArgs::get_nb_searches(unsigned exploration_level) { - assert(exploration_level <= MAX_EXPLORATION_LEVEL); - - unsigned nb_searches = 4 * (exploration_level + 1); - if (exploration_level >= 4) { - nb_searches += 4; - } - if (exploration_level == MAX_EXPLORATION_LEVEL) { - nb_searches += 4; - } - - return nb_searches; -} - void CLArgs::set_exploration_level(unsigned exploration_level) { - depth = get_depth(exploration_level); + depth = utils::get_depth(exploration_level); - nb_searches = get_nb_searches(exploration_level); + nb_searches = utils::get_nb_searches(exploration_level); } } // namespace vroom::io diff --git a/src/structures/cl_args.h b/src/structures/cl_args.h index 53f3c131a..71b4ef9e9 100644 --- a/src/structures/cl_args.h +++ b/src/structures/cl_args.h @@ -37,10 +37,6 @@ struct CLArgs { unsigned nb_searches; // derived from -x unsigned depth; // derived from -x - static unsigned get_depth(unsigned exploration_level); - - static unsigned get_nb_searches(unsigned exploration_level); - void set_exploration_level(unsigned exploration_level); }; diff --git a/src/structures/vroom/input/input.cpp b/src/structures/vroom/input/input.cpp index 6ac64d6d8..74df05fdb 100644 --- a/src/structures/vroom/input/input.cpp +++ b/src/structures/vroom/input/input.cpp @@ -1118,6 +1118,17 @@ std::unique_ptr Input::get_problem() const { return std::make_unique(*this); } +Solution Input::solve(unsigned exploration_level, + unsigned nb_thread, + const Timeout& timeout, + const std::vector& h_param) { + return solve(utils::get_nb_searches(exploration_level), + utils::get_depth(exploration_level), + nb_thread, + timeout, + h_param); +} + Solution Input::solve(unsigned nb_searches, unsigned depth, unsigned nb_thread, diff --git a/src/structures/vroom/input/input.h b/src/structures/vroom/input/input.h index 7f2478ed5..c7122e2e8 100644 --- a/src/structures/vroom/input/input.h +++ b/src/structures/vroom/input/input.h @@ -16,7 +16,6 @@ All rights reserved (see LICENSE). #include #include "routing/wrapper.h" -#include "structures/cl_args.h" #include "structures/generic/matrix.h" #include "structures/typedefs.h" #include "structures/vroom/matrices.h" @@ -194,23 +193,16 @@ class Input { // Returns true iff both vehicles have common job candidates. bool vehicle_ok_with_vehicle(Index v1_index, Index v2_index) const; - Solution solve(unsigned exploration_level, + Solution solve(unsigned nb_searches, + unsigned depth, unsigned nb_thread, const Timeout& timeout = Timeout(), const std::vector& h_param = - std::vector()) { - // Overload designed to expose the same interface as the `-x` - // command-line flag for out-of-the-box setup of exploration - // level. - return solve(io::CLArgs::get_nb_searches(exploration_level), - io::CLArgs::get_depth(exploration_level), - nb_thread, - timeout, - h_param); - } + std::vector()); - Solution solve(unsigned nb_searches, - unsigned depth, + // Overload designed to expose the same interface as the `-x` + // command-line flag for out-of-the-box setup of exploration level. + Solution solve(unsigned exploration_level, unsigned nb_thread, const Timeout& timeout = Timeout(), const std::vector& h_param = diff --git a/src/utils/helpers.h b/src/utils/helpers.h index 8a061ff37..e09170780 100644 --- a/src/utils/helpers.h +++ b/src/utils/helpers.h @@ -38,6 +38,24 @@ inline UserCost add_without_overflow(UserCost a, UserCost b) { return a + b; } +inline unsigned get_depth(unsigned exploration_level) { + return exploration_level; +} + +inline unsigned get_nb_searches(unsigned exploration_level) { + assert(exploration_level <= MAX_EXPLORATION_LEVEL); + + unsigned nb_searches = 4 * (exploration_level + 1); + if (exploration_level >= 4) { + nb_searches += 4; + } + if (exploration_level == MAX_EXPLORATION_LEVEL) { + nb_searches += 4; + } + + return nb_searches; +} + INIT get_init(std::string_view s); SORT get_sort(std::string_view s);