Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Dec 19, 2024
1 parent 925af8a commit fc422b4
Show file tree
Hide file tree
Showing 26 changed files with 683 additions and 110 deletions.
26 changes: 25 additions & 1 deletion ortools/algorithms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ cc_library(
"@com_google_absl//absl/base",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/numeric:bits",
"@com_google_absl//absl/types:span",
],
)
Expand Down Expand Up @@ -294,6 +295,7 @@ cc_library(
"@com_google_absl//absl/random",
"@com_google_absl//absl/random:distributions",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
)

Expand All @@ -305,6 +307,7 @@ cc_library(
":set_cover_cc_proto",
":set_cover_model",
"//ortools/base",
"//ortools/base:mathutil",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/types:span",
Expand All @@ -320,9 +323,10 @@ cc_library(
":set_cover_invariant",
":set_cover_model",
"//ortools/base",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/base",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/numeric:bits",
"@com_google_absl//absl/random:distributions",
"@com_google_absl//absl/types:span",
],
Expand Down Expand Up @@ -355,10 +359,30 @@ cc_library(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
],
)

cc_binary(
name = "set_cover_solve",
srcs = ["set_cover_solve.cc"],
deps = [
":set_cover_heuristics",
":set_cover_invariant",
":set_cover_model",
":set_cover_reader",
#"//file/placer",
"//ortools/base",
"//ortools/base:timer",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
],
)

cc_test(
name = "set_cover_test",
size = "medium",
Expand Down
7 changes: 3 additions & 4 deletions ortools/algorithms/n_choose_k.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ absl::StatusOr<int64_t> NChooseK(int64_t n, int64_t k) {
if (k < 0) {
return absl::InvalidArgumentError(absl::StrFormat("k is negative (%d)", k));
}
if (k > n) {
return absl::InvalidArgumentError(
absl::StrFormat("k=%d is greater than n=%d", k, n));
if (k > n / 2) {
if (k > n) return 0; // No way to choose more than n elements from n.
k = n - k;
}
if (k > n / 2) k = n - k;
if (k == 0) return 1;
if (n < std::numeric_limits<uint32_t>::max() &&
!NChooseKIntermediateComputationOverflowsInt<uint32_t>(n, k)) {
Expand Down
5 changes: 3 additions & 2 deletions ortools/algorithms/n_choose_k.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ namespace operations_research {
// i.e., the binomial coefficient (n, k).
// This is like std::exp(MathUtil::LogCombinations(n, k)), but faster, with
// perfect accuracy, and returning an error iff the result would overflow an
// int64_t or if an argument is invalid (i.e., n < 0, k < 0, or k > n).
// int64_t or if an argument is invalid (i.e., n < 0, k < 0). Returns 0 for k >
// n.
//
// NOTE(user): If you need a variation of this, ask the authors: it's very easy
// to add. E.g., other int types, other behaviors (e.g., return 0 if k > n, or
// to add. E.g., other int types, other behaviors (e.g., return
// std::numeric_limits<int64_t>::max() on overflow, etc).
absl::StatusOr<int64_t> NChooseK(int64_t n, int64_t k);
} // namespace operations_research
Expand Down
23 changes: 11 additions & 12 deletions ortools/algorithms/n_choose_k_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "benchmark/benchmark.h"
#include "gtest/gtest.h"
#include "ortools/base/dump_vars.h"
//#include "ortools/base/fuzztest.h"
#include "ortools/base/gmock.h"
#include "ortools/base/mathutil.h"
#include "ortools/util/flat_matrix.h"
Expand All @@ -50,11 +51,7 @@ TEST(NChooseKTest, TrivialErrorCases) {
HasSubstr("n is negative")));
EXPECT_THAT(NChooseK(x, -1), StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("k is negative")));
if (x != kint64max) {
EXPECT_THAT(NChooseK(x, x + 1),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("greater than n")));
}
if (x != kint64max) EXPECT_THAT(NChooseK(x, x + 1), IsOkAndHolds(0));
ASSERT_FALSE(HasFailure()) << DUMP_VARS(t, x);
}
}
Expand Down Expand Up @@ -310,13 +307,15 @@ void BM_NChooseK(benchmark::State& state) {
}
state.SetItemsProcessed(state.iterations() * kNumInputs);
}
BENCHMARK(BM_NChooseK<30, operations_research::NChooseK>); // int32_t domain.
BENCHMARK(
BM_NChooseK<60, operations_research::NChooseK>); // int{32,64} domain.
BENCHMARK(
BM_NChooseK<100, operations_research::NChooseK>); // int{32,64,128} domain.
BENCHMARK(
BM_NChooseK<100, MathUtil::LogCombinations>); // int{32,64,128} domain.
// int32_t domain.
BENCHMARK(BM_NChooseK<30, operations_research::NChooseK>);

// int{32,64} domain.
BENCHMARK(BM_NChooseK<60, operations_research::NChooseK>);

// int{32,64,128} domain.
BENCHMARK(BM_NChooseK<100, operations_research::NChooseK>);
BENCHMARK(BM_NChooseK<100, MathUtil::LogCombinations>);

} // namespace
} // namespace operations_research
20 changes: 10 additions & 10 deletions ortools/algorithms/python/set_cover.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ PYBIND11_MODULE(set_cover, m) {
})
.def("next_solution",
[](TrivialSolutionGenerator& heuristic,
const std::vector<BaseInt>& focus) -> bool {
absl::Span<const BaseInt> focus) -> bool {
return heuristic.NextSolution(VectorIntToVectorSubsetIndex(focus));
});

Expand All @@ -380,7 +380,7 @@ PYBIND11_MODULE(set_cover, m) {
})
.def("next_solution",
[](RandomSolutionGenerator& heuristic,
const std::vector<BaseInt>& focus) -> bool {
absl::Span<const BaseInt> focus) -> bool {
return heuristic.NextSolution(VectorIntToVectorSubsetIndex(focus));
});

Expand All @@ -392,13 +392,13 @@ PYBIND11_MODULE(set_cover, m) {
})
.def("next_solution",
[](GreedySolutionGenerator& heuristic,
const std::vector<BaseInt>& focus) -> bool {
absl::Span<const BaseInt> focus) -> bool {
return heuristic.NextSolution(VectorIntToVectorSubsetIndex(focus));
})
.def("next_solution",
[](GreedySolutionGenerator& heuristic,
const std::vector<BaseInt>& focus,
const std::vector<double>& costs) -> bool {
absl::Span<const BaseInt> focus,
absl::Span<const double> costs) -> bool {
return heuristic.NextSolution(
VectorIntToVectorSubsetIndex(focus),
VectorDoubleToSubsetCostVector(costs));
Expand All @@ -413,7 +413,7 @@ PYBIND11_MODULE(set_cover, m) {
})
.def("next_solution",
[](ElementDegreeSolutionGenerator& heuristic,
const std::vector<BaseInt>& focus) -> bool {
absl::Span<const BaseInt> focus) -> bool {
return heuristic.NextSolution(VectorIntToVectorSubsetIndex(focus));
})
.def("next_solution",
Expand All @@ -439,8 +439,8 @@ PYBIND11_MODULE(set_cover, m) {
})
.def("next_solution",
[](LazyElementDegreeSolutionGenerator& heuristic,
const std::vector<BaseInt>& focus,
const std::vector<double>& costs) -> bool {
absl::Span<const BaseInt> focus,
absl::Span<const double> costs) -> bool {
return heuristic.NextSolution(
VectorIntToVectorSubsetIndex(focus),
VectorDoubleToSubsetCostVector(costs));
Expand Down Expand Up @@ -474,7 +474,7 @@ PYBIND11_MODULE(set_cover, m) {
return heuristic.NextSolution(num_iterations);
})
.def("next_solution",
[](GuidedLocalSearch& heuristic, const std::vector<BaseInt>& focus,
[](GuidedLocalSearch& heuristic, absl::Span<const BaseInt> focus,
int num_iterations) -> bool {
return heuristic.NextSolution(VectorIntToVectorSubsetIndex(focus),
num_iterations);
Expand Down Expand Up @@ -552,7 +552,7 @@ PYBIND11_MODULE(set_cover, m) {
return {cleared.begin(), cleared.end()};
});
m.def("clear_most_covered_elements",
[](const std::vector<BaseInt>& focus, BaseInt num_subsets,
[](absl::Span<const BaseInt> focus, BaseInt num_subsets,
SetCoverInvariant* inv) -> std::vector<BaseInt> {
const std::vector<SubsetIndex> cleared = ClearMostCoveredElements(
VectorIntToVectorSubsetIndex(focus), num_subsets, inv);
Expand Down
1 change: 1 addition & 0 deletions ortools/algorithms/samples/knapsack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// [START program]
// [START import]
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <numeric>
Expand Down
Loading

0 comments on commit fc422b4

Please sign in to comment.