|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "google", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "##### Copyright 2024 Google LLC." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "apache", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 17 | + "you may not use this file except in compliance with the License.\n", |
| 18 | + "You may obtain a copy of the License at\n", |
| 19 | + "\n", |
| 20 | + " http://www.apache.org/licenses/LICENSE-2.0\n", |
| 21 | + "\n", |
| 22 | + "Unless required by applicable law or agreed to in writing, software\n", |
| 23 | + "distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 24 | + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 25 | + "See the License for the specific language governing permissions and\n", |
| 26 | + "limitations under the License.\n" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "id": "basename", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "# set_cover" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "id": "link", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "<table align=\"left\">\n", |
| 43 | + "<td>\n", |
| 44 | + "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/algorithms/set_cover.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n", |
| 45 | + "</td>\n", |
| 46 | + "<td>\n", |
| 47 | + "<a href=\"https://github.com/google/or-tools/blob/main/ortools/algorithms/samples/set_cover.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n", |
| 48 | + "</td>\n", |
| 49 | + "</table>" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "id": "doc", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab." |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "id": "install", |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "%pip install ortools" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "id": "description", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "\n", |
| 76 | + "A simple set-covering problem.\n" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "id": "code", |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "from ortools.algorithms.python import set_cover\n", |
| 87 | + "\n", |
| 88 | + "\n", |
| 89 | + "def main():\n", |
| 90 | + " model = set_cover.SetCoverModel()\n", |
| 91 | + " model.add_empty_subset(2.0)\n", |
| 92 | + " model.add_element_to_last_subset(0)\n", |
| 93 | + " model.add_empty_subset(2.0)\n", |
| 94 | + " model.add_element_to_last_subset(1)\n", |
| 95 | + " model.add_empty_subset(1.0)\n", |
| 96 | + " model.add_element_to_last_subset(0)\n", |
| 97 | + " model.add_element_to_last_subset(1)\n", |
| 98 | + "\n", |
| 99 | + " inv = set_cover.SetCoverInvariant(model)\n", |
| 100 | + " greedy = set_cover.GreedySolutionGenerator(inv)\n", |
| 101 | + " has_found = greedy.next_solution()\n", |
| 102 | + " if not has_found:\n", |
| 103 | + " print(\"No solution found by the greedy heuristic.\")\n", |
| 104 | + " return\n", |
| 105 | + " solution = inv.export_solution_as_proto()\n", |
| 106 | + "\n", |
| 107 | + " print(f\"Total cost: {solution.cost}\") # == inv.cost()\n", |
| 108 | + " print(f\"Total number of selected subsets: {solution.num_subsets}\")\n", |
| 109 | + " print(\"Chosen subsets:\")\n", |
| 110 | + " for subset in solution.subset:\n", |
| 111 | + " print(f\" {subset}\")\n", |
| 112 | + "\n", |
| 113 | + "\n", |
| 114 | + "main()\n", |
| 115 | + "\n" |
| 116 | + ] |
| 117 | + } |
| 118 | + ], |
| 119 | + "metadata": { |
| 120 | + "language_info": { |
| 121 | + "name": "python" |
| 122 | + } |
| 123 | + }, |
| 124 | + "nbformat": 4, |
| 125 | + "nbformat_minor": 5 |
| 126 | +} |
0 commit comments