Files
ortools-clone/ortools/set_cover
2025-09-22 17:28:02 +02:00
..
2025-09-16 16:25:04 +02:00
2025-07-23 15:06:59 +02:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-04-30 15:15:39 +02:00
2025-09-22 17:28:02 +02:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-05-12 12:57:50 +02:00
2025-04-30 15:15:39 +02:00
2025-05-13 14:34:49 +02:00
2025-05-13 14:34:49 +02:00
2025-04-30 15:15:39 +02:00
2025-04-30 15:15:39 +02:00
2025-03-27 08:29:46 -07:00
2025-03-04 21:06:53 +01:00
2025-03-04 21:06:53 +01:00
2025-05-13 14:34:49 +02:00

Set covering

An instance of set covering is composed of two entities: elements and sets; sets cover a series of elements. The problem of set covering is about finding the cheapest combination of sets that cover all the elements.

More information on Wikipedia.

Create an instance:

// If the elements are integers, a subset can be a std::vector<int> (in a pair
// along its cost).
std::vector<std::pair<std::vector<int>, int>> subsets = ...;

SetCoverModel model;
for (const auto [subset, subset_cost] : subsets) {
  model.AddEmptySubset(subset_cost)
  for (const int element : subset) {
    model.AddElementToLastSubset(element);
  }
}
SetCoverLedger ledger(&model);

Solve it using a MIP solver (guarantees to yield the optimum solution if it has enough time to run):

SetCoverMip mip(&ledger);
mip.SetTimeLimitInSeconds(10);
mip.NextSolution();
SubsetBoolVector best_choices = ledger.GetSolution();
LOG(INFO) << "Cost: " << ledger.cost();

A custom combination of heuristics (10,000 iterations of: clearing 10% of the variables, running a Chvatal greedy descent, using steepest local search):

Cost best_cost = std::numeric_limits<Cost>::max();
SubsetBoolVector best_choices = ledger.GetSolution();
for (int i = 0; i < 10000; ++i) {
  ledger.LoadSolution(best_choices);
  ClearRandomSubsets(0.1 * model.num_subsets().value(), &ledger);

  GreedySolutionGenerator greedy(&ledger);
  CHECK(greedy.NextSolution());

  SteepestSearch steepest(&ledger);
  CHECK(steepest.NextSolution(10000));

  EXPECT_TRUE(ledger.CheckSolution());
  if (ledger.cost() < best_cost) {
    best_cost = ledger.cost();
    best_choices = ledger.GetSolution();
    LOG(INFO) << "Better cost: " << best_cost << " at iteration = " << i;
  }
}
ledger.LoadSolution(best_choices);
LOG(INFO) << "Best cost: " << ledger.cost();