Files
ortools-clone/examples/notebook/assignment_sat.ipynb
2017-11-23 13:20:11 +01:00

100 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Copyright 2010-2017 Google\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",
"from __future__ import print_function\n",
"from ortools.sat.python import cp_model\n",
"\n",
"def main():\n",
" # Instantiate a cp model.\n",
" cost = [[90, 76, 75, 70, 50, 74, 12, 68],\n",
" [35, 85, 55, 65, 48, 101, 70, 83],\n",
" [125, 95, 90, 105, 59, 120, 36, 73],\n",
" [45, 110, 95, 115, 104, 83, 37, 71],\n",
" [60, 105, 80, 75, 59, 62, 93, 88],\n",
" [45, 65, 110, 95, 47, 31, 81, 34],\n",
" [38, 51, 107, 41, 69, 99, 115, 48],\n",
" [47, 85, 57, 71, 92, 77, 109, 36],\n",
" [39, 63, 97, 49, 118, 56, 92, 61],\n",
" [47, 101, 71, 60, 88, 109, 52, 90]]\n",
"\n",
"\n",
" sizes = [10, 7, 3, 12, 15, 4, 11, 5]\n",
" total_size_max = 15\n",
" num_workers = len(cost)\n",
" num_tasks = len(cost[1])\n",
" all_workers = range(num_workers)\n",
" all_tasks = range(num_tasks)\n",
"\n",
" model = cp_model.CpModel()\n",
" # Variables\n",
" total_cost = model.NewIntVar(0, 1000, 'total_cost')\n",
" x = []\n",
" for i in all_workers:\n",
" t = []\n",
" for j in all_tasks:\n",
" t.append(model.NewBoolVar('x[%i,%i]' % (i, j)))\n",
" x.append(t)\n",
"\n",
" # Constraints\n",
"\n",
" # Each task is assigned to at least one worker.\n",
" [model.Add(sum(x[i][j] for i in all_workers) >= 1) for j in all_tasks]\n",
"\n",
" # Total task size for each worker is at most total_size_max\n",
" for i in all_workers:\n",
" model.Add(sum(sizes[j] * x[i][j] for j in all_tasks) <= total_size_max)\n",
"\n",
" # Total cost\n",
" model.Add(total_cost ==\n",
" sum(x[i][j] * cost[i][j] for j in all_tasks for i in all_workers))\n",
" model.Minimize(total_cost)\n",
"\n",
" solver = cp_model.CpSolver()\n",
" status = solver.Solve(model)\n",
"\n",
" if status == cp_model.OPTIMAL:\n",
" print('Total cost = %i' % solver.ObjectiveValue())\n",
" print()\n",
" for i in all_workers:\n",
" for j in all_tasks:\n",
" if solver.Value(x[i][j]) == 1:\n",
" print('Worker ', i, ' assigned to task ', j, ' Cost = ', cost[i][j])\n",
"\n",
" print()\n",
"\n",
" print('Statistics')\n",
" print(' - conflicts : %i' % solver.NumConflicts())\n",
" print(' - branches : %i' % solver.NumBranches())\n",
" print(' - wall time : %f ms' % solver.WallTime())\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}