Files
ortools-clone/ortools/algorithms
Corentin Le Molgat cd8fe14232 graph: rename io.h as graph_io.h
This avoid compile errors on windows since msvc already provide a io.h system header.
Which is included by googletest (gtest-port.h IIRC), so enabling graph tests will fail
2024-10-21 11:01:37 +02:00
..
2024-05-30 10:52:45 +02:00
2024-01-29 15:10:26 +01:00
2024-10-01 12:48:20 +02:00
2024-10-01 12:48:20 +02:00
2024-07-19 16:10:27 +02:00
2024-07-17 11:40:54 +02:00
2024-01-04 13:43:15 +01:00
2024-07-12 13:46:46 +02:00
2024-10-14 16:47:46 +02:00
2024-10-14 16:47:46 +02:00
2024-01-04 13:43:15 +01:00
2024-01-04 13:43:15 +01:00
2024-02-15 11:43:44 +01:00
2024-02-15 11:43:44 +01:00
2024-08-02 19:58:55 +02:00
2024-07-12 13:46:46 +02:00
2024-07-12 13:46:46 +02:00
2024-02-15 09:59:54 +01:00
2024-05-30 10:52:45 +02:00
2024-10-01 12:48:20 +02:00
2024-10-01 12:48:20 +02:00
2024-07-12 13:46:46 +02:00
2024-10-01 12:48:20 +02:00
2024-10-01 12:48:20 +02:00
2024-01-04 13:43:15 +01:00
2024-01-04 13:43:15 +01:00
2024-10-01 12:48:20 +02:00
2024-10-01 12:48:20 +02:00
2024-10-01 12:48:20 +02:00

Various algorithms

This directory contains data structures and algorithms for various problems.

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