fix jobshop notebook

This commit is contained in:
Laurent Perron
2017-11-20 17:53:07 +01:00
parent 4a58e5ce38
commit c8bd25f27d

View File

@@ -8,93 +8,76 @@
},
"outputs": [],
"source": [
"{\n",
" \"cells\": [\n",
" {\n",
" \"cell_type\": \"code\",\n",
" \"execution_count\": null,\n",
" \"metadata\": {\n",
" \"collapsed\": false\n",
" },\n",
" \"outputs\": [],\n",
" \"source\": [\n",
" \"from ortools.sat.python import cp_model\\n\",\n",
" \"\\n\",\n",
" \"\\n\",\n",
" \"def main():\\n\",\n",
" \" # Creates the solver.\\n\",\n",
" \" model = cp_model.CpModel()\\n\",\n",
" \"\\n\",\n",
" \" machines_count = 6\\n\",\n",
" \" jobs_count = 6\\n\",\n",
" \" all_machines = range(0, machines_count)\\n\",\n",
" \" all_jobs = range(0, jobs_count)\\n\",\n",
" \"\\n\",\n",
" \" durations = [[1, 3, 6, 7, 3, 6],\\n\",\n",
" \" [8, 5, 10, 10, 10, 4],\\n\",\n",
" \" [5, 4, 8, 9, 1, 7],\\n\",\n",
" \" [5, 5, 5, 3, 8, 9],\\n\",\n",
" \" [9, 3, 5, 4, 3, 1],\\n\",\n",
" \" [3, 3, 9, 10, 4, 1]]\\n\",\n",
" \"\\n\",\n",
" \" machines = [[2, 0, 1, 3, 5, 4],\\n\",\n",
" \" [1, 2, 4, 5, 0, 3],\\n\",\n",
" \" [2, 3, 5, 0, 1, 4],\\n\",\n",
" \" [1, 0, 2, 3, 4, 5],\\n\",\n",
" \" [2, 1, 4, 5, 0, 3],\\n\",\n",
" \" [1, 3, 5, 0, 4, 2]]\\n\",\n",
" \"\\n\",\n",
" \" # Computes horizon dynamically.\\n\",\n",
" \" horizon = sum([sum(durations[i]) for i in all_jobs])\\n\",\n",
" \"\\n\",\n",
" \" # Creates jobs.\\n\",\n",
" \" all_tasks = {}\\n\",\n",
" \" for i in all_jobs:\\n\",\n",
" \" for j in all_machines:\\n\",\n",
" \" start = model.NewIntVar(0, horizon, 'start_%i_%i' % (i, j))\\n\",\n",
" \" duration = durations[i][j]\\n\",\n",
" \" end = model.NewIntVar(0, horizon, 'end_%i_%i' % (i, j))\\n\",\n",
" \" interval = model.NewIntervalVar(start, duration, end,\\n\",\n",
" \" 'interval_%i_%i' % (i, j))\\n\",\n",
" \" all_tasks[(i, j)] = (start, end, interval)\\n\",\n",
" \"\\n\",\n",
" \" # Create disjuctive constraints.\\n\",\n",
" \" machine_to_jobs = {}\\n\",\n",
" \" for i in all_machines:\\n\",\n",
" \" machines_jobs = []\\n\",\n",
" \" for j in all_jobs:\\n\",\n",
" \" for k in all_machines:\\n\",\n",
" \" if machines[j][k] == i:\\n\",\n",
" \" machines_jobs.append(all_tasks[(j, k)][2])\\n\",\n",
" \" machine_to_jobs[i] = machines_jobs\\n\",\n",
" \" model.AddNoOverlap(machines_jobs)\\n\",\n",
" \"\\n\",\n",
" \" # Precedences inside a job.\\n\",\n",
" \" for i in all_jobs:\\n\",\n",
" \" for j in range(0, machines_count - 1):\\n\",\n",
" \" model.Add(all_tasks[(i, j + 1)][0] >= all_tasks[(i, j)][1])\\n\",\n",
" \"\\n\",\n",
" \" # Makespan objective.\\n\",\n",
" \" obj_var = model.NewIntVar(0, horizon, 'makespan')\\n\",\n",
" \" model.AddMaxEquality(\\n\",\n",
" \" obj_var, [all_tasks[(i, machines_count - 1)][1] for i in all_jobs])\\n\",\n",
" \" model.Minimize(obj_var)\\n\",\n",
" \"\\n\",\n",
" \" # Solve model.\\n\",\n",
" \" solver = cp_model.CpSolver()\\n\",\n",
" \" response = solver.Solve(model)\\n\",\n",
" \" print(solver.ObjectiveValue())\\n\",\n",
" \"\\n\",\n",
" \"\\n\",\n",
" \"if __name__ == '__main__':\\n\",\n",
" \" main()\"\n",
" ]\n",
" }\n",
" ],\n",
" \"metadata\": {},\n",
" \"nbformat\": 4,\n",
" \"nbformat_minor\": 2\n",
"}"
"from ortools.sat.python import cp_model\n",
"\n",
"\n",
"def main():\n",
" # Creates the solver.\n",
" model = cp_model.CpModel()\n",
"\n",
" machines_count = 6\n",
" jobs_count = 6\n",
" all_machines = range(0, machines_count)\n",
" all_jobs = range(0, jobs_count)\n",
"\n",
" durations = [[1, 3, 6, 7, 3, 6],\n",
" [8, 5, 10, 10, 10, 4],\n",
" [5, 4, 8, 9, 1, 7],\n",
" [5, 5, 5, 3, 8, 9],\n",
" [9, 3, 5, 4, 3, 1],\n",
" [3, 3, 9, 10, 4, 1]]\n",
"\n",
" machines = [[2, 0, 1, 3, 5, 4],\n",
" [1, 2, 4, 5, 0, 3],\n",
" [2, 3, 5, 0, 1, 4],\n",
" [1, 0, 2, 3, 4, 5],\n",
" [2, 1, 4, 5, 0, 3],\n",
" [1, 3, 5, 0, 4, 2]]\n",
"\n",
" # Computes horizon dynamically.\n",
" horizon = sum([sum(durations[i]) for i in all_jobs])\n",
"\n",
" # Creates jobs.\n",
" all_tasks = {}\n",
" for i in all_jobs:\n",
" for j in all_machines:\n",
" start = model.NewIntVar(0, horizon, 'start_%i_%i' % (i, j))\n",
" duration = durations[i][j]\n",
" end = model.NewIntVar(0, horizon, 'end_%i_%i' % (i, j))\n",
" interval = model.NewIntervalVar(start, duration, end,\n",
" 'interval_%i_%i' % (i, j))\n",
" all_tasks[(i, j)] = (start, end, interval)\n",
"\n",
" # Create disjuctive constraints.\n",
" machine_to_jobs = {}\n",
" for i in all_machines:\n",
" machines_jobs = []\n",
" for j in all_jobs:\n",
" for k in all_machines:\n",
" if machines[j][k] == i:\n",
" machines_jobs.append(all_tasks[(j, k)][2])\n",
" machine_to_jobs[i] = machines_jobs\n",
" model.AddNoOverlap(machines_jobs)\n",
"\n",
" # Precedences inside a job.\n",
" for i in all_jobs:\n",
" for j in range(0, machines_count - 1):\n",
" model.Add(all_tasks[(i, j + 1)][0] >= all_tasks[(i, j)][1])\n",
"\n",
" # Makespan objective.\n",
" obj_var = model.NewIntVar(0, horizon, 'makespan')\n",
" model.AddMaxEquality(\n",
" obj_var, [all_tasks[(i, machines_count - 1)][1] for i in all_jobs])\n",
" model.Minimize(obj_var)\n",
"\n",
" # Solve model.\n",
" solver = cp_model.CpSolver()\n",
" response = solver.Solve(model)\n",
" print(solver.ObjectiveValue())\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
}
],