Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Feb 18, 2024
1 parent ca4aa6e commit 7d1dd0b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
7 changes: 7 additions & 0 deletions setcoveringsolver/setcovering/algorithm_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions setcoveringsolver/setcovering/algorithms/greedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "optimizationtools/containers/indexed_binary_heap.hpp"

#include <set>

using namespace setcoveringsolver::setcovering;

const Output setcoveringsolver::setcovering::greedy(
Expand Down Expand Up @@ -189,4 +187,3 @@ const Output setcoveringsolver::setcovering::greedy_dual(
algorithm_formatter.end();
return output;
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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;
}
};
Expand Down
60 changes: 44 additions & 16 deletions setcoveringsolver/setcovering/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
std::string json_output_path = vm["output"].as<std::string>();

std::string certificate_path;
if (vm.count("certificate"))
certificate_path = vm["certificate"].as<std::string>();

std::string json_output_path;
if (vm.count("output"))
json_output_path = vm["output"].as<std::string>();

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<bool>();
}

Output run(
Expand Down Expand Up @@ -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<int>();
parameters.maximum_number_of_iterations_without_improvement = vm["maximum-number-of-iterations-without-improvement"].as<int>();
if (vm.count("maximum-number-of-iterations")) {
parameters.maximum_number_of_iterations
= vm["maximum-number-of-iterations"].as<int>();
}
if (vm.count("maximum-number-of-iterations-without-improvement")) {
parameters.maximum_number_of_iterations_without_improvement
= vm["maximum-number-of-iterations-without-improvement"].as<int>();
}
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<int>();
if (vm.count("maximum-number-of-iterations-without-improvement"))
parameters.maximum_number_of_iterations_without_improvement = vm["maximum-number-of-iterations-without-improvement"].as<int>();
if (vm.count("maximum-number-of-iterations")) {
parameters.maximum_number_of_iterations
= vm["maximum-number-of-iterations"].as<int>();
}
if (vm.count("maximum-number-of-iterations-without-improvement")) {
parameters.maximum_number_of_iterations_without_improvement
= vm["maximum-number-of-iterations-without-improvement"].as<int>();
}
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<int>();
parameters.maximum_number_of_iterations_without_improvement = vm["maximum-number-of-iterations-without-improvement"].as<int>();
if (vm.count("maximum-number-of-iterations")) {
parameters.maximum_number_of_iterations
= vm["maximum-number-of-iterations"].as<int>();
}
if (vm.count("maximum-number-of-iterations-without-improvement")) {
parameters.maximum_number_of_iterations_without_improvement
= vm["maximum-number-of-iterations-without-improvement"].as<int>();
}
if (vm.count("goal"))
parameters.goal = vm["goal"].as<Cost>();
return large_neighborhood_search(instance, parameters);
Expand Down Expand Up @@ -126,6 +153,7 @@ int main(int argc, char *argv[])
("log,l", po::value<std::string>(), "set log file")
("log-to-stderr", "write log to stderr")

("remove-dominated,", po::value<bool>(), "set remove dominated")
("maximum-number-of-iterations,", po::value<int>(), "set the maximum number of iterations")
("maximum-number-of-iterations-without-improvement,", po::value<int>(), "set the maximum number of iterations without improvement")
;
Expand Down Expand Up @@ -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>();
std::string json_output_path = vm["output"].as<std::string>();
output.write_json_output(json_output_path);
output.solution.write(certificate_path);
if (vm.count("certificate"))
output.solution.write(vm["certificate"].as<std::string>());
if (vm.count("output"))
output.write_json_output(vm["output"].as<std::string>());

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion setcoveringsolver/setcovering/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
};
}

0 comments on commit 7d1dd0b

Please sign in to comment.