modify python sat examples to use visualization when run from IPython; change corresponding notebooks

This commit is contained in:
Laurent Perron
2017-11-21 03:54:06 +01:00
parent c8bd25f27d
commit 21f9744862
7 changed files with 1552 additions and 63 deletions

View File

@@ -27,12 +27,13 @@
"We have two parallel machines that can perform this job.\n",
"One machine can only perform one job at a time.\n",
"At any point in time, the sum of the width of the two active jobs does not\n",
"exceed a max_width.\n",
"exceed a max_length.\n",
"\n",
"The objective is to minimize the max end time of all jobs.\n",
"\"\"\"\n",
"\n",
"from ortools.sat.python import cp_model\n",
"from ortools.sat.python import visualization\n",
"\n",
"\n",
"def main():\n",
@@ -122,17 +123,39 @@
" # Solve model.\n",
" solver = cp_model.CpSolver()\n",
" solver.Solve(model)\n",
" print('Solution')\n",
" print(' - makespan = %i' % solver.ObjectiveValue())\n",
" for i in all_jobs:\n",
" performed_machine = 1 - solver.Value(performed[i])\n",
" start = solver.Value(starts[i])\n",
" print(' - Job %i starts at %i on machine %i' %\n",
" (i, start, performed_machine))\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",
" # Output solution.\n",
" if visualization.RunFromIPython():\n",
" output = visualization.SvgWrapper(solver.ObjectiveValue(), max_length, 40.0)\n",
" output.AddTitle('Makespan = %i' % solver.ObjectiveValue())\n",
" color_manager = visualization.ColorManager()\n",
" color_manager.SeedRandomColor(0)\n",
"\n",
" for i in all_jobs:\n",
" performed_machine = 1 - solver.Value(performed[i])\n",
" start = solver.Value(starts[i])\n",
" dx = jobs[i][0]\n",
" dy = jobs[i][1]\n",
" sy = performed_machine * (max_length - dy)\n",
" output.AddRectangle(start, sy, dx, dy, color_manager.RandomColor(),\n",
" 'black', 'j%i' % i)\n",
"\n",
" output.AddXScale()\n",
" output.AddYScale()\n",
" output.Display()\n",
" else:\n",
" print('Solution')\n",
" print(' - makespan = %i' % solver.ObjectiveValue())\n",
" for i in all_jobs:\n",
" performed_machine = 1 - solver.Value(performed[i])\n",
" start = solver.Value(starts[i])\n",
" print(' - Job %i starts at %i on machine %i' %\n",
" (i, start, performed_machine))\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",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long