Files
ortools-clone/examples/notebook/contrib/combinatorial_auction2.ipynb
2020-11-18 14:28:23 +01:00

180 lines
5.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Copyright 2020 Google LLC."
]
},
{
"cell_type": "markdown",
"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",
"metadata": {},
"source": [
"# combinatorial_auction2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/combinatorial_auction2.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
"</td>\n",
"<td>\n",
"<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/combinatorial_auction2.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010 Hakan Kjellerstrand hakank@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",
"\"\"\"Combinatorial auction in Google CP Solver.\n",
"\n",
" This is a more general model for the combinatorial example\n",
" in the Numberjack Tutorial, pages 9 and 24 (slides 19/175 and\n",
" 51/175).\n",
"\n",
" The original and more talkative model is here:\n",
" http://www.hakank.org/numberjack/combinatorial_auction.py\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://hakank.org/minizinc/combinatorial_auction.mzn\n",
" * Gecode: http://hakank.org/gecode/combinatorial_auction.cpp\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",
"\"\"\"\n",
"import sys\n",
"from collections import *\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"\n",
"# Create the solver.\n",
"solver = pywrapcp.Solver(\"Problem\")\n",
"\n",
"#\n",
"# data\n",
"#\n",
"N = 5\n",
"\n",
"# the items for each bid\n",
"items = [\n",
" [0, 1], # A,B\n",
" [0, 2], # A, C\n",
" [1, 3], # B,D\n",
" [1, 2, 3], # B,C,D\n",
" [0] # A\n",
"]\n",
"# collect the bids for each item\n",
"items_t = defaultdict(list)\n",
"\n",
"# [items_t.setdefault(j,[]).append(i) for i in range(N) for j in items[i] ]\n",
"# nicer:\n",
"[items_t[j].append(i) for i in range(N) for j in items[i]]\n",
"\n",
"bid_amount = [10, 20, 30, 40, 14]\n",
"\n",
"#\n",
"# declare variables\n",
"#\n",
"X = [solver.BoolVar(\"x%i\" % i) for i in range(N)]\n",
"obj = solver.IntVar(0, 100, \"obj\")\n",
"\n",
"#\n",
"# constraints\n",
"#\n",
"solver.Add(obj == solver.ScalProd(X, bid_amount))\n",
"for item in items_t:\n",
" solver.Add(solver.Sum([X[bid] for bid in items_t[item]]) <= 1)\n",
"\n",
"# objective\n",
"objective = solver.Maximize(obj, 1)\n",
"\n",
"#\n",
"# solution and search\n",
"#\n",
"solution = solver.Assignment()\n",
"solution.Add(X)\n",
"solution.Add(obj)\n",
"\n",
"# db: DecisionBuilder\n",
"db = solver.Phase(X, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)\n",
"\n",
"solver.NewSearch(db, [objective])\n",
"num_solutions = 0\n",
"while solver.NextSolution():\n",
" print(\"X:\", [X[i].Value() for i in range(N)])\n",
" print(\"obj:\", obj.Value())\n",
" print()\n",
" num_solutions += 1\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"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}