From 7d1dd0bcd91975aa1a856833dc00c244d2fd1da5 Mon Sep 17 00:00:00 2001 From: Florian Fontan Date: Sun, 18 Feb 2024 20:46:09 +0100 Subject: [PATCH] Clean code --- .../setcovering/algorithm_formatter.hpp | 7 +++ .../setcovering/algorithms/greedy.cpp | 3 - .../algorithms/local_search_row_weighting.hpp | 12 ++-- setcoveringsolver/setcovering/main.cpp | 60 ++++++++++++++----- setcoveringsolver/setcovering/solution.cpp | 2 +- 5 files changed, 60 insertions(+), 24 deletions(-) diff --git a/setcoveringsolver/setcovering/algorithm_formatter.hpp b/setcoveringsolver/setcovering/algorithm_formatter.hpp index bbcfa0d..18f01e2 100644 --- a/setcoveringsolver/setcovering/algorithm_formatter.hpp +++ b/setcoveringsolver/setcovering/algorithm_formatter.hpp @@ -72,6 +72,13 @@ inline const AlgorithmOutput solve_reduced_instance( algorithm_formatter.print_reduced_instance(reduction.instance()); algorithm_formatter.print_header(); + algorithm_formatter.update_solution( + reduction.unreduce_solution(Solution(reduction.instance())), + ""); + algorithm_formatter.update_bound( + reduction.unreduce_bound(0), + ""); + AlgorithmParameters new_parameters = parameters; new_parameters.reduction_parameters.reduce = false; new_parameters.verbosity_level = 0; diff --git a/setcoveringsolver/setcovering/algorithms/greedy.cpp b/setcoveringsolver/setcovering/algorithms/greedy.cpp index 4c8619b..4e4bfce 100644 --- a/setcoveringsolver/setcovering/algorithms/greedy.cpp +++ b/setcoveringsolver/setcovering/algorithms/greedy.cpp @@ -4,8 +4,6 @@ #include "optimizationtools/containers/indexed_binary_heap.hpp" -#include - using namespace setcoveringsolver::setcovering; const Output setcoveringsolver::setcovering::greedy( @@ -189,4 +187,3 @@ const Output setcoveringsolver::setcovering::greedy_dual( algorithm_formatter.end(); return output; } - diff --git a/setcoveringsolver/setcovering/algorithms/local_search_row_weighting.hpp b/setcoveringsolver/setcovering/algorithms/local_search_row_weighting.hpp index 64c4516..23afe19 100644 --- a/setcoveringsolver/setcovering/algorithms/local_search_row_weighting.hpp +++ b/setcoveringsolver/setcovering/algorithms/local_search_row_weighting.hpp @@ -33,7 +33,8 @@ struct LocalSearchRowWeighting1Parameters: Parameters nlohmann::json json = Parameters::to_json(); json.merge_patch({ {"MaximumNumberOfIterations", maximum_number_of_iterations}, - {"MaximumNumberOfIterationsWithoutImprovement", maximum_number_of_iterations_without_improvement}}); + {"MaximumNumberOfIterationsWithoutImprovement", maximum_number_of_iterations_without_improvement}, + }); return json; } }; @@ -62,7 +63,8 @@ struct LocalSearchRowWeighting1Output: Output { nlohmann::json json = Output::to_json(); json.merge_patch({ - {"NumberOfIterations", number_of_iterations}}); + {"NumberOfIterations", number_of_iterations}, + }); return json; } }; @@ -141,7 +143,8 @@ struct LocalSearchRowWeighting2Parameters: Parameters nlohmann::json json = Parameters::to_json(); json.merge_patch({ {"MaximumNumberOfIterations", maximum_number_of_iterations}, - {"MaximumNumberOfIterationsWithoutImprovement", maximum_number_of_iterations_without_improvement}}); + {"MaximumNumberOfIterationsWithoutImprovement", maximum_number_of_iterations_without_improvement}, + }); return json; } }; @@ -192,7 +195,8 @@ struct LocalSearchRowWeighting2Output: Output { nlohmann::json json = Output::to_json(); json.merge_patch({ - {"NumberOfIterations", number_of_iterations}}); + {"NumberOfIterations", number_of_iterations}, + }); return json; } }; diff --git a/setcoveringsolver/setcovering/main.cpp b/setcoveringsolver/setcovering/main.cpp index 27911ac..1c34236 100644 --- a/setcoveringsolver/setcovering/main.cpp +++ b/setcoveringsolver/setcovering/main.cpp @@ -28,18 +28,29 @@ void read_args( parameters.log_to_stderr = vm.count("log-to-stderr"); bool only_write_at_the_end = vm.count("only-write-at-the-end"); if (!only_write_at_the_end) { - std::string certificate_path = vm["certificate"].as(); - std::string json_output_path = vm["output"].as(); + + std::string certificate_path; + if (vm.count("certificate")) + certificate_path = vm["certificate"].as(); + + std::string json_output_path; + if (vm.count("output")) + json_output_path = vm["output"].as(); + parameters.new_solution_callback = [ json_output_path, certificate_path]( const Output& output, const std::string&) { - output.write_json_output(json_output_path); - output.solution.write(certificate_path); + if (!json_output_path.empty()) + output.write_json_output(json_output_path); + if (!certificate_path.empty()) + output.solution.write(certificate_path); }; } + if (vm.count("remove-dominated")) + parameters.reduction_parameters.remove_domianted = vm["remove-dominated"].as(); } Output run( @@ -78,23 +89,39 @@ Output run( } else if (algorithm == "local-search-row-weighting-1") { LocalSearchRowWeighting1Parameters parameters; read_args(parameters, vm); - parameters.maximum_number_of_iterations = vm["maximum-number-of-iterations"].as(); - parameters.maximum_number_of_iterations_without_improvement = vm["maximum-number-of-iterations-without-improvement"].as(); + if (vm.count("maximum-number-of-iterations")) { + parameters.maximum_number_of_iterations + = vm["maximum-number-of-iterations"].as(); + } + if (vm.count("maximum-number-of-iterations-without-improvement")) { + parameters.maximum_number_of_iterations_without_improvement + = vm["maximum-number-of-iterations-without-improvement"].as(); + } return local_search_row_weighting_1(instance, generator, parameters); } else if (algorithm == "local-search-row-weighting-2") { LocalSearchRowWeighting2Parameters parameters; read_args(parameters, vm); - if (vm.count("maximum-number-of-iterations")) - parameters.maximum_number_of_iterations = vm["maximum-number-of-iterations"].as(); - if (vm.count("maximum-number-of-iterations-without-improvement")) - parameters.maximum_number_of_iterations_without_improvement = vm["maximum-number-of-iterations-without-improvement"].as(); + if (vm.count("maximum-number-of-iterations")) { + parameters.maximum_number_of_iterations + = vm["maximum-number-of-iterations"].as(); + } + if (vm.count("maximum-number-of-iterations-without-improvement")) { + parameters.maximum_number_of_iterations_without_improvement + = vm["maximum-number-of-iterations-without-improvement"].as(); + } return local_search_row_weighting_2(instance, generator, parameters); } else if (algorithm == "large-neighborhood-search" || algorithm == "large-neighborhood-search-2") { LargeNeighborhoodSearchParameters parameters; read_args(parameters, vm); - parameters.maximum_number_of_iterations = vm["maximum-number-of-iterations"].as(); - parameters.maximum_number_of_iterations_without_improvement = vm["maximum-number-of-iterations-without-improvement"].as(); + if (vm.count("maximum-number-of-iterations")) { + parameters.maximum_number_of_iterations + = vm["maximum-number-of-iterations"].as(); + } + if (vm.count("maximum-number-of-iterations-without-improvement")) { + parameters.maximum_number_of_iterations_without_improvement + = vm["maximum-number-of-iterations-without-improvement"].as(); + } if (vm.count("goal")) parameters.goal = vm["goal"].as(); return large_neighborhood_search(instance, parameters); @@ -126,6 +153,7 @@ int main(int argc, char *argv[]) ("log,l", po::value(), "set log file") ("log-to-stderr", "write log to stderr") + ("remove-dominated,", po::value(), "set remove dominated") ("maximum-number-of-iterations,", po::value(), "set the maximum number of iterations") ("maximum-number-of-iterations-without-improvement,", po::value(), "set the maximum number of iterations without improvement") ; @@ -155,10 +183,10 @@ int main(int argc, char *argv[]) Output output = run(instance, vm); // Write outputs. - std::string certificate_path = vm["certificate"].as(); - std::string json_output_path = vm["output"].as(); - output.write_json_output(json_output_path); - output.solution.write(certificate_path); + if (vm.count("certificate")) + output.solution.write(vm["certificate"].as()); + if (vm.count("output")) + output.write_json_output(vm["output"].as()); return 0; } diff --git a/setcoveringsolver/setcovering/solution.cpp b/setcoveringsolver/setcovering/solution.cpp index d4150ec..7249ac3 100644 --- a/setcoveringsolver/setcovering/solution.cpp +++ b/setcoveringsolver/setcovering/solution.cpp @@ -96,6 +96,6 @@ nlohmann::json Solution::to_json() const {"NumberOfSets", number_of_sets()}, {"NumberOfUncoveredElements", number_of_uncovered_elements()}, {"Feasible", feasible()}, - {"Cost", cost()} + {"Cost", cost()}, }; }