216 lines
6.3 KiB
Plaintext
216 lines
6.3 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": [
|
|
"# car"
|
|
]
|
|
},
|
|
{
|
|
"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/car.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/car.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": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
" Car sequencing in Google CP Solver.\n",
|
|
"\n",
|
|
" This model is based on the car sequencing model in\n",
|
|
" Pascal Van Hentenryck\n",
|
|
" 'The OPL Optimization Programming Language', page 184ff.\n",
|
|
"\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://hakank.org/minizinc/car.mzn\n",
|
|
" * Comet: http://hakank.org/comet/car.co\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" Also see my other Google CP Solver models:\n",
|
|
" http://www.hakank.org/google_or_tools/\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(num_sol=3):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Car sequence\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" nbCars = 6\n",
|
|
" nbOptions = 5\n",
|
|
" nbSlots = 10\n",
|
|
"\n",
|
|
" Cars = list(range(nbCars))\n",
|
|
" Options = list(range(nbOptions))\n",
|
|
" Slots = list(range(nbSlots))\n",
|
|
"\n",
|
|
" # car 0 1 2 3 4 5\n",
|
|
" demand = [1, 1, 2, 2, 2, 2]\n",
|
|
"\n",
|
|
" option = [\n",
|
|
" # car 0 1 2 3 4 5\n",
|
|
" [1, 0, 0, 0, 1, 1], # option 1\n",
|
|
" [0, 0, 1, 1, 0, 1], # option 2\n",
|
|
" [1, 0, 0, 0, 1, 0], # option 3\n",
|
|
" [1, 1, 0, 1, 0, 0], # option 4\n",
|
|
" [0, 0, 1, 0, 0, 0] # option 5\n",
|
|
" ]\n",
|
|
"\n",
|
|
" capacity = [(1, 2), (2, 3), (1, 3), (2, 5), (1, 5)]\n",
|
|
"\n",
|
|
" optionDemand = [\n",
|
|
" sum([demand[j] * option[i][j] for j in Cars]) for i in Options\n",
|
|
" ]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" slot = [solver.IntVar(0, nbCars - 1, \"slot[%i]\" % i) for i in Slots]\n",
|
|
" setup = {}\n",
|
|
" for i in Options:\n",
|
|
" for j in Slots:\n",
|
|
" setup[(i, j)] = solver.IntVar(0, 1, \"setup[%i,%i]\" % (i, j))\n",
|
|
" setup_flat = [setup[i, j] for i in Options for j in Slots]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" for c in Cars:\n",
|
|
" b = [solver.IsEqualCstVar(slot[s], c) for s in Slots]\n",
|
|
" solver.Add(solver.Sum(b) == demand[c])\n",
|
|
"\n",
|
|
" for o in Options:\n",
|
|
" for s in range(0, nbSlots - capacity[o][1] + 1):\n",
|
|
" b = [setup[o, j] for j in range(s, s + capacity[o][1] - 1)]\n",
|
|
" solver.Add(solver.Sum(b) <= capacity[o][0])\n",
|
|
"\n",
|
|
" for o in Options:\n",
|
|
" for s in Slots:\n",
|
|
" solver.Add(setup[(o, s)] == solver.Element(option[o], slot[s]))\n",
|
|
"\n",
|
|
" for o in Options:\n",
|
|
" for i in range(optionDemand[o]):\n",
|
|
" s_range = list(range(0, nbSlots - (i + 1) * capacity[o][1]))\n",
|
|
" ss = [setup[o, s] for s in s_range]\n",
|
|
" cc = optionDemand[o] - (i + 1) * capacity[o][0]\n",
|
|
" if len(ss) > 0 and cc >= 0:\n",
|
|
" solver.Add(solver.Sum(ss) >= cc)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # search and result\n",
|
|
" #\n",
|
|
" db = solver.Phase(slot + setup_flat, solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(\"slot:%s\" % \",\".join([str(slot[i].Value()) for i in Slots]))\n",
|
|
" print(\"setup:\")\n",
|
|
" for o in Options:\n",
|
|
" print(\"%i/%i:\" % (capacity[o][0], capacity[o][1]), end=\" \")\n",
|
|
" for s in Slots:\n",
|
|
" print(setup[o, s].Value(), end=\" \")\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
" num_solutions += 1\n",
|
|
"\n",
|
|
" if num_solutions >= num_sol:\n",
|
|
" break\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(\"num_solutions:\", num_solutions)\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"num_sol = 3\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" num_sol = int(sys.argv[1])\n",
|
|
"main(num_sol)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|