Skip to content

Commit

Permalink
update notebooks to remove main() method
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Sep 5, 2018
1 parent 466b2ee commit c0eac15
Show file tree
Hide file tree
Showing 18 changed files with 1,258 additions and 2,320 deletions.
106 changes: 48 additions & 58 deletions examples/notebook/assignment_sat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010-2017 Google\n",
Expand All @@ -24,72 +22,64 @@
"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",
"# Instantiate a cp model.\n",
"cost = [[90, 76, 75, 70, 50, 74, 12, 68], [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], [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], [47, 85, 57, 71, 92, 77, 109, 36],\n",
" [39, 63, 97, 49, 118, 56, 92, 61], [47, 101, 71, 60, 88, 109, 52, 90]]\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",
"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",
"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",
"# 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",
"# 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 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",
"# 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",
" solver = cp_model.CpSolver()\n",
" status = solver.Solve(model)\n",
"# Total cost\n",
"model.Add(total_cost == sum(\n",
" x[i][j] * cost[i][j] for j in all_tasks for i in all_workers))\n",
"model.Minimize(total_cost)\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",
"solver = cp_model.CpSolver()\n",
"status = solver.Solve(model)\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",
"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",
"if __name__ == '__main__':\n",
" main()"
"print('Statistics')\n",
"print(' - conflicts : %i' % solver.NumConflicts())\n",
"print(' - branches : %i' % solver.NumBranches())\n",
"print(' - wall time : %f ms' % solver.WallTime())\n",
"\n"
]
}
],
Expand Down
190 changes: 94 additions & 96 deletions examples/notebook/assignment_with_constraints_sat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010-2017 Google\n",
Expand All @@ -23,102 +21,102 @@
"from __future__ import print_function\n",
"from ortools.sat.python import cp_model\n",
"\n",
"def main():\n",
" # Data.\n",
" cost = [[90, 76, 75, 70, 50, 74],\n",
" [35, 85, 55, 65, 48, 101],\n",
" [125, 95, 90, 105, 59, 120],\n",
" [45, 110, 95, 115, 104, 83],\n",
" [60, 105, 80, 75, 59, 62],\n",
" [45, 65, 110, 95, 47, 31],\n",
" [38, 51, 107, 41, 69, 99],\n",
" [47, 85, 57, 71, 92, 77],\n",
" [39, 63, 97, 49, 118, 56],\n",
" [47, 101, 71, 60, 88, 109],\n",
" [17, 39, 103, 64, 61, 92],\n",
" [101, 45, 83, 59, 92, 27]]\n",
"\n",
" group1 = [[0, 0, 1, 1], # Workers 2, 3\n",
" [0, 1, 0, 1], # Workers 1, 3\n",
" [0, 1, 1, 0], # Workers 1, 2\n",
" [1, 1, 0, 0], # Workers 0, 1\n",
" [1, 0, 1, 0]] # Workers 0, 2\n",
"\n",
" group2 = [[0, 0, 1, 1], # Workers 6, 7\n",
" [0, 1, 0, 1], # Workers 5, 7\n",
" [0, 1, 1, 0], # Workers 5, 6\n",
" [1, 1, 0, 0], # Workers 4, 5\n",
" [1, 0, 0, 1]] # Workers 4, 7\n",
"\n",
" group3 = [[0, 0, 1, 1], # Workers 10, 11\n",
" [0, 1, 0, 1], # Workers 9, 11\n",
" [0, 1, 1, 0], # Workers 9, 10\n",
" [1, 0, 1, 0], # Workers 8, 10\n",
" [1, 0, 0, 1]] # Workers 8, 11\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.\n",
"\n",
" model = cp_model.CpModel()\n",
" # Variables\n",
" total_cost = model.NewIntVar(0, 1000, 'total_cost')\n",
" x = [[model.NewBoolVar('x[%i,%i]' % (i, j)) for j in all_tasks]\n",
" for i in all_workers]\n",
" works = [model.NewBoolVar('works[%i]' % i) for i in all_workers]\n",
"\n",
" # Constraints\n",
"\n",
" # Link x and workers.\n",
" for i in range(num_workers):\n",
" model.AddMaxEquality(works[i], x[i])\n",
"\n",
" # Each task is assigned to at least one worker.\n",
" for j in all_tasks:\n",
" model.Add(sum(x[i][j] for i in all_workers) >= 1)\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",
" # Group constraints.\n",
" model.AddAllowedAssignments([works[0], works[1], works[2], works[3]], group1)\n",
" model.AddAllowedAssignments([works[4], works[5], works[6], works[7]], group2)\n",
" model.AddAllowedAssignments([works[8], works[9], works[10], works[11]], group3)\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",
" # Solve and output solution.\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",
"# Data.\n",
"cost = [[90, 76, 75, 70, 50, 74], [35, 85, 55, 65, 48,\n",
" 101], [125, 95, 90, 105, 59, 120],\n",
" [45, 110, 95, 115, 104, 83], [60, 105, 80, 75, 59, 62], [\n",
" 45, 65, 110, 95, 47, 31\n",
" ], [38, 51, 107, 41, 69, 99], [47, 85, 57, 71,\n",
" 92, 77], [39, 63, 97, 49, 118, 56],\n",
" [47, 101, 71, 60, 88, 109], [17, 39, 103, 64, 61,\n",
" 92], [101, 45, 83, 59, 92, 27]]\n",
"\n",
"group1 = [\n",
" [0, 0, 1, 1], # Workers 2, 3\n",
" [0, 1, 0, 1], # Workers 1, 3\n",
" [0, 1, 1, 0], # Workers 1, 2\n",
" [1, 1, 0, 0], # Workers 0, 1\n",
" [1, 0, 1, 0]\n",
"] # Workers 0, 2\n",
"\n",
"group2 = [\n",
" [0, 0, 1, 1], # Workers 6, 7\n",
" [0, 1, 0, 1], # Workers 5, 7\n",
" [0, 1, 1, 0], # Workers 5, 6\n",
" [1, 1, 0, 0], # Workers 4, 5\n",
" [1, 0, 0, 1]\n",
"] # Workers 4, 7\n",
"\n",
"group3 = [\n",
" [0, 0, 1, 1], # Workers 10, 11\n",
" [0, 1, 0, 1], # Workers 9, 11\n",
" [0, 1, 1, 0], # Workers 9, 10\n",
" [1, 0, 1, 0], # Workers 8, 10\n",
" [1, 0, 0, 1]\n",
"] # Workers 8, 11\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.\n",
"\n",
"model = cp_model.CpModel()\n",
"# Variables\n",
"total_cost = model.NewIntVar(0, 1000, 'total_cost')\n",
"x = [[model.NewBoolVar('x[%i,%i]' % (i, j))\n",
" for j in all_tasks]\n",
" for i in all_workers]\n",
"works = [model.NewBoolVar('works[%i]' % i) for i in all_workers]\n",
"\n",
"# Constraints\n",
"\n",
"# Link x and workers.\n",
"for i in range(num_workers):\n",
" model.AddMaxEquality(works[i], x[i])\n",
"\n",
"# Each task is assigned to at least one worker.\n",
"for j in all_tasks:\n",
" model.Add(sum(x[i][j] for i in all_workers) >= 1)\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",
"# Group constraints.\n",
"model.AddAllowedAssignments([works[0], works[1], works[2], works[3]], group1)\n",
"model.AddAllowedAssignments([works[4], works[5], works[6], works[7]], group2)\n",
"model.AddAllowedAssignments([works[8], works[9], works[10], works[11]],\n",
" group3)\n",
"\n",
"# Total cost\n",
"model.Add(total_cost == sum(\n",
" x[i][j] * cost[i][j] for j in all_tasks for i in all_workers))\n",
"model.Minimize(total_cost)\n",
"\n",
"# Solve and output solution.\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",
"if __name__ == '__main__':\n",
" main()"
"print('Statistics')\n",
"print(' - conflicts : %i' % solver.NumConflicts())\n",
"print(' - branches : %i' % solver.NumBranches())\n",
"print(' - wall time : %f ms' % solver.WallTime())\n",
"\n"
]
}
],
Expand Down
Loading

0 comments on commit c0eac15

Please sign in to comment.