Files
ortools-clone/examples/notebook/contrib/bacp.ipynb
2022-06-27 15:42:26 +02:00

174 lines
5.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "google",
"metadata": {},
"source": [
"##### Copyright 2022 Google LLC."
]
},
{
"cell_type": "markdown",
"id": "apache",
"metadata": {},
"source": [
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"you may not use this file except in compliance with the License.\n",
"You may obtain a copy of the License at\n",
"\n",
" http://www.apache.org/licenses/LICENSE-2.0\n",
"\n",
"Unless required by applicable law or agreed to in writing, software\n",
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"See the License for the specific language governing permissions and\n",
"limitations under the License.\n"
]
},
{
"cell_type": "markdown",
"id": "basename",
"metadata": {},
"source": [
"# bacp"
]
},
{
"cell_type": "markdown",
"id": "link",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/bacp.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
"</td>\n",
"<td>\n",
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/bacp.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"id": "doc",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "install",
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010 Pierre Schaus pschaus@gmail.com\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# http://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License.\n",
"\n",
"import argparse\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"parser = argparse.ArgumentParser()\n",
"\n",
"parser.add_argument(\n",
" '--data', default='examples/contrib/bacp.txt', help='path to data file')\n",
"\n",
"#----------------helper for binpacking posting----------------\n",
"\n",
"\n",
"def BinPacking(solver, binvars, weights, loadvars):\n",
" \"\"\"post the load constraint on bins.\n",
"\n",
" constraints forall j: loadvars[j] == sum_i (binvars[i] == j) * weights[i])\n",
" \"\"\"\n",
" pack = solver.Pack(binvars, len(loadvars))\n",
" pack.AddWeightedSumEqualVarDimension(weights, loadvars)\n",
" solver.Add(pack)\n",
" solver.Add(solver.SumEquality(loadvars, sum(weights)))\n",
"\n",
"\n",
"#------------------------------data reading-------------------\n",
"\n",
"\n",
"def ReadData(filename):\n",
" \"\"\"Read data from <filename>.\"\"\"\n",
" f = open(filename)\n",
" nb_courses, nb_periods, min_credit, max_credit, nb_prereqs =\\\n",
" [int(nb) for nb in f.readline().split()]\n",
" credits = [int(nb) for nb in f.readline().split()]\n",
" prereq = [int(nb) for nb in f.readline().split()]\n",
" prereq = [(prereq[i * 2], prereq[i * 2 + 1]) for i in range(nb_prereqs)]\n",
" return (credits, nb_periods, prereq)\n",
"\n",
"\n",
"def main(args):\n",
" #------------------solver and variable declaration-------------\n",
"\n",
" credits, nb_periods, prereq = ReadData(args.data)\n",
" nb_courses = len(credits)\n",
"\n",
" solver = pywrapcp.Solver('Balanced Academic Curriculum Problem')\n",
"\n",
" x = [\n",
" solver.IntVar(0, nb_periods - 1, 'x' + str(i)) for i in range(nb_courses)\n",
" ]\n",
" load_vars = [\n",
" solver.IntVar(0, sum(credits), 'load_vars' + str(i))\n",
" for i in range(nb_periods)\n",
" ]\n",
"\n",
" #-------------------post of the constraints--------------\n",
"\n",
" # Bin Packing.\n",
" BinPacking(solver, x, credits, load_vars)\n",
" # Add dependencies.\n",
" for i, j in prereq:\n",
" solver.Add(x[i] < x[j])\n",
"\n",
" #----------------Objective-------------------------------\n",
"\n",
" objective_var = solver.Max(load_vars)\n",
" objective = solver.Minimize(objective_var, 1)\n",
"\n",
" #------------start the search and optimization-----------\n",
"\n",
" db = solver.Phase(x, solver.CHOOSE_MIN_SIZE_LOWEST_MIN,\n",
" solver.INT_VALUE_DEFAULT)\n",
"\n",
" search_log = solver.SearchLog(100000, objective_var)\n",
" solver.Solve(db, [objective, search_log])\n",
"\n",
"\n",
"main(parser.parse_args())\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}