|
2 | 2 |
|
3 | 3 | This directory contains data structures and algorithms for various problems. |
4 | 4 |
|
5 | | -## Set covering |
6 | | - |
7 | | -An instance of set covering is composed of two entities: elements and sets; sets |
8 | | -cover a series of elements. The problem of set covering is about finding the |
9 | | -cheapest combination of sets that cover all the elements. |
10 | | - |
11 | | -[More information on Wikipedia](https://en.wikipedia.org/wiki/Set_cover_problem). |
12 | | - |
13 | | -* Solver: [`set_cover_heuristics.h`](set_cover_heuristics.h). |
14 | | -* Instance representation: [`set_cover_model.h`](set_cover_model.h). |
15 | | -* Instance parser: [`set_cover_reader.h`](set_cover_reader.h). |
16 | | - |
17 | | -Create an instance: |
18 | | - |
19 | | -```cpp |
20 | | -// If the elements are integers, a subset can be a std::vector<int> (in a pair |
21 | | -// along its cost). |
22 | | -std::vector<std::pair<std::vector<int>, int>> subsets = ...; |
23 | | - |
24 | | -SetCoverModel model; |
25 | | -for (const auto [subset, subset_cost] : subsets) { |
26 | | - model.AddEmptySubset(subset_cost) |
27 | | - for (const int element : subset) { |
28 | | - model.AddElementToLastSubset(element); |
29 | | - } |
30 | | -} |
31 | | -SetCoverLedger ledger(&model); |
32 | | -``` |
33 | | -
|
34 | | -Solve it using a MIP solver (guarantees to yield the optimum solution if it has |
35 | | -enough time to run): |
36 | | -
|
37 | | -```cpp |
38 | | -SetCoverMip mip(&ledger); |
39 | | -mip.SetTimeLimitInSeconds(10); |
40 | | -mip.NextSolution(); |
41 | | -SubsetBoolVector best_choices = ledger.GetSolution(); |
42 | | -LOG(INFO) << "Cost: " << ledger.cost(); |
43 | | -``` |
44 | | - |
45 | | -A custom combination of heuristics (10,000 iterations of: clearing 10% of the |
46 | | -variables, running a Chvatal greedy descent, using steepest local search): |
47 | | - |
48 | | -```cpp |
49 | | -Cost best_cost = std::numeric_limits<Cost>::max(); |
50 | | -SubsetBoolVector best_choices = ledger.GetSolution(); |
51 | | -for (int i = 0; i < 10000; ++i) { |
52 | | - ledger.LoadSolution(best_choices); |
53 | | - ClearRandomSubsets(0.1 * model.num_subsets().value(), &ledger); |
54 | | - |
55 | | - GreedySolutionGenerator greedy(&ledger); |
56 | | - CHECK(greedy.NextSolution()); |
57 | | - |
58 | | - SteepestSearch steepest(&ledger); |
59 | | - CHECK(steepest.NextSolution(10000)); |
60 | | - |
61 | | - EXPECT_TRUE(ledger.CheckSolution()); |
62 | | - if (ledger.cost() < best_cost) { |
63 | | - best_cost = ledger.cost(); |
64 | | - best_choices = ledger.GetSolution(); |
65 | | - LOG(INFO) << "Better cost: " << best_cost << " at iteration = " << i; |
66 | | - } |
67 | | -} |
68 | | -ledger.LoadSolution(best_choices); |
69 | | -LOG(INFO) << "Best cost: " << ledger.cost(); |
70 | | -``` |
| 5 | +It used to contain a solver for set covering, which has moved to the |
| 6 | +[set_cover](../set_cover) |
| 7 | +folder. |
0 commit comments