From e79811e9bbda20153b1e4b47dc40d4bc57408db3 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Fri, 24 Nov 2017 13:49:39 +0100 Subject: [PATCH] cleanup is_run_from_ipython() --- examples/notebook/gate_scheduling_sat.ipynb | 2 +- examples/notebook/hidato_sat.ipynb | 126 +- examples/notebook/jobshop_ft06_sat.ipynb | 1344 +------------------ examples/notebook/nqueens_sat.ipynb | 2 + examples/notebook/steel_mill_slab_sat.ipynb | 13 + examples/python/gate_scheduling_sat.py | 42 +- examples/python/hidato_sat.py | 36 +- examples/python/jobshop_ft06_sat.py | 9 +- ortools/sat/python/visualization.py | 13 +- 9 files changed, 144 insertions(+), 1443 deletions(-) diff --git a/examples/notebook/gate_scheduling_sat.ipynb b/examples/notebook/gate_scheduling_sat.ipynb index 61dcceb04d..4bc7d77b4a 100644 --- a/examples/notebook/gate_scheduling_sat.ipynb +++ b/examples/notebook/gate_scheduling_sat.ipynb @@ -126,7 +126,7 @@ "\n", "\n", " # Output solution.\n", - " if visualization.RunFromIPython():\n", + " if visualization.run_from_ipython():\n", " output = visualization.SvgWrapper(solver.ObjectiveValue(), max_length, 40.0)\n", " output.AddTitle('Makespan = %i' % solver.ObjectiveValue())\n", " color_manager = visualization.ColorManager()\n", diff --git a/examples/notebook/hidato_sat.ipynb b/examples/notebook/hidato_sat.ipynb index e122c29335..9d0692f7d6 100644 --- a/examples/notebook/hidato_sat.ipynb +++ b/examples/notebook/hidato_sat.ipynb @@ -2,83 +2,25 @@ "cells": [ { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": [ - "123456789Puzzle 1 solved in 0.000314 s" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "image/svg+xml": [ - "12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849Puzzle 2 solved in 0.001767 s" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "image/svg+xml": [ - "12345678910111213141516171819202122232425Puzzle 3 solved in 0.003336 s" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "image/svg+xml": [ - "12345678910111213141516171819202122232425Puzzle 4 solved in 0.000984 s" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "image/svg+xml": [ - "123456789101112131415161718192021222324252627282930313233343536Puzzle 5 solved in 0.001121 s" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "image/svg+xml": [ - "12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364Puzzle 6 solved in 0.002264 s" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "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 ortools.sat.python import cp_model\n", "from ortools.sat.python import visualization\n", "\n", @@ -199,8 +141,7 @@ "\n", " r = len(puzzle)\n", " c = len(puzzle[0])\n", - "\n", - " if not visualization.RunFromIPython():\n", + " if not visualization.run_from_ipython():\n", " print('')\n", " print('----- Solving problem %i -----' % index)\n", " print('')\n", @@ -240,7 +181,7 @@ " status = solver.Solve(model)\n", "\n", " if status == cp_model.MODEL_SAT:\n", - " if visualization.RunFromIPython():\n", + " if visualization.run_from_ipython():\n", " output = visualization.SvgWrapper(10, r, 40.0)\n", " for i in range(len(positions)):\n", " val = solver.Value(positions[i])\n", @@ -268,34 +209,9 @@ "if __name__ == '__main__':\n", " main()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.1" - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 2 } diff --git a/examples/notebook/jobshop_ft06_sat.ipynb b/examples/notebook/jobshop_ft06_sat.ipynb index 14fcacae77..4775a97042 100644 --- a/examples/notebook/jobshop_ft06_sat.ipynb +++ b/examples/notebook/jobshop_ft06_sat.ipynb @@ -2,1283 +2,26 @@ "cells": [ { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ], - "text/vnd.plotly.v1+html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.plotly.v1+json": { - "data": [ - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:01", - "2016-01-01 6:00:04" - ], - "y": [ - 5, - 5 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:38", - "2016-01-01 6:00:48" - ], - "y": [ - 5, - 5 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:19", - "2016-01-01 6:00:28" - ], - "y": [ - 5, - 5 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:13", - "2016-01-01 6:00:18" - ], - "y": [ - 5, - 5 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:48", - "2016-01-01 6:00:51" - ], - "y": [ - 5, - 5 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:28", - "2016-01-01 6:00:38" - ], - "y": [ - 5, - 5 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:16", - "2016-01-01 6:00:22" - ], - "y": [ - 4, - 4 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:00", - "2016-01-01 6:00:08" - ], - "y": [ - 4, - 4 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:40", - "2016-01-01 6:00:41" - ], - "y": [ - 4, - 4 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:08", - "2016-01-01 6:00:13" - ], - "y": [ - 4, - 4 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:22", - "2016-01-01 6:00:25" - ], - "y": [ - 4, - 4 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:13", - "2016-01-01 6:00:16" - ], - "y": [ - 4, - 4 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:00", - "2016-01-01 6:00:01" - ], - "y": [ - 3, - 3 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:08", - "2016-01-01 6:00:13" - ], - "y": [ - 3, - 3 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:02", - "2016-01-01 6:00:07" - ], - "y": [ - 3, - 3 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:22", - "2016-01-01 6:00:27" - ], - "y": [ - 3, - 3 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:13", - "2016-01-01 6:00:22" - ], - "y": [ - 3, - 3 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:42", - "2016-01-01 6:00:43" - ], - "y": [ - 3, - 3 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:30", - "2016-01-01 6:00:37" - ], - "y": [ - 2, - 2 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:48", - "2016-01-01 6:00:52" - ], - "y": [ - 2, - 2 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:07", - "2016-01-01 6:00:11" - ], - "y": [ - 2, - 2 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:27", - "2016-01-01 6:00:30" - ], - "y": [ - 2, - 2 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:52", - "2016-01-01 6:00:53" - ], - "y": [ - 2, - 2 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:16", - "2016-01-01 6:00:19" - ], - "y": [ - 2, - 2 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:42", - "2016-01-01 6:00:48" - ], - "y": [ - 1, - 1 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:13", - "2016-01-01 6:00:23" - ], - "y": [ - 1, - 1 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:48", - "2016-01-01 6:00:55" - ], - "y": [ - 1, - 1 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:30", - "2016-01-01 6:00:38" - ], - "y": [ - 1, - 1 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:25", - "2016-01-01 6:00:30" - ], - "y": [ - 1, - 1 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:38", - "2016-01-01 6:00:42" - ], - "y": [ - 1, - 1 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:38", - "2016-01-01 6:00:41" - ], - "y": [ - 0, - 0 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:28", - "2016-01-01 6:00:38" - ], - "y": [ - 0, - 0 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:11", - "2016-01-01 6:00:19" - ], - "y": [ - 0, - 0 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:45", - "2016-01-01 6:00:54" - ], - "y": [ - 0, - 0 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:41", - "2016-01-01 6:00:45" - ], - "y": [ - 0, - 0 - ] - }, - { - "marker": { - "color": "white" - }, - "name": "", - "x": [ - "2016-01-01 6:00:19", - "2016-01-01 6:00:28" - ], - "y": [ - 0, - 0 - ] - } - ], - "layout": { - "height": 600, - "hovermode": "closest", - "shapes": [ - { - "fillcolor": "rgb(197, 215, 20)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:01", - "x1": "2016-01-01 6:00:04", - "xref": "x", - "y0": 4.8, - "y1": 5.2, - "yref": "y" - }, - { - "fillcolor": "rgb(132, 248, 207)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:38", - "x1": "2016-01-01 6:00:48", - "xref": "x", - "y0": 4.8, - "y1": 5.2, - "yref": "y" - }, - { - "fillcolor": "rgb(155, 244, 183)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:19", - "x1": "2016-01-01 6:00:28", - "xref": "x", - "y0": 4.8, - "y1": 5.2, - "yref": "y" - }, - { - "fillcolor": "rgb(111, 71, 144)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:13", - "x1": "2016-01-01 6:00:18", - "xref": "x", - "y0": 4.8, - "y1": 5.2, - "yref": "y" - }, - { - "fillcolor": "rgb(71, 48, 128)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:48", - "x1": "2016-01-01 6:00:51", - "xref": "x", - "y0": 4.8, - "y1": 5.2, - "yref": "y" - }, - { - "fillcolor": "rgb(75, 158, 50)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:28", - "x1": "2016-01-01 6:00:38", - "xref": "x", - "y0": 4.8, - "y1": 5.2, - "yref": "y" - }, - { - "fillcolor": "rgb(197, 215, 20)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:16", - "x1": "2016-01-01 6:00:22", - "xref": "x", - "y0": 3.8, - "y1": 4.2, - "yref": "y" - }, - { - "fillcolor": "rgb(132, 248, 207)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:00", - "x1": "2016-01-01 6:00:08", - "xref": "x", - "y0": 3.8, - "y1": 4.2, - "yref": "y" - }, - { - "fillcolor": "rgb(155, 244, 183)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:40", - "x1": "2016-01-01 6:00:41", - "xref": "x", - "y0": 3.8, - "y1": 4.2, - "yref": "y" - }, - { - "fillcolor": "rgb(111, 71, 144)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:08", - "x1": "2016-01-01 6:00:13", - "xref": "x", - "y0": 3.8, - "y1": 4.2, - "yref": "y" - }, - { - "fillcolor": "rgb(71, 48, 128)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:22", - "x1": "2016-01-01 6:00:25", - "xref": "x", - "y0": 3.8, - "y1": 4.2, - "yref": "y" - }, - { - "fillcolor": "rgb(75, 158, 50)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:13", - "x1": "2016-01-01 6:00:16", - "xref": "x", - "y0": 3.8, - "y1": 4.2, - "yref": "y" - }, - { - "fillcolor": "rgb(197, 215, 20)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:00", - "x1": "2016-01-01 6:00:01", - "xref": "x", - "y0": 2.8, - "y1": 3.2, - "yref": "y" - }, - { - "fillcolor": "rgb(132, 248, 207)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:08", - "x1": "2016-01-01 6:00:13", - "xref": "x", - "y0": 2.8, - "y1": 3.2, - "yref": "y" - }, - { - "fillcolor": "rgb(155, 244, 183)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:02", - "x1": "2016-01-01 6:00:07", - "xref": "x", - "y0": 2.8, - "y1": 3.2, - "yref": "y" - }, - { - "fillcolor": "rgb(111, 71, 144)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:22", - "x1": "2016-01-01 6:00:27", - "xref": "x", - "y0": 2.8, - "y1": 3.2, - "yref": "y" - }, - { - "fillcolor": "rgb(71, 48, 128)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:13", - "x1": "2016-01-01 6:00:22", - "xref": "x", - "y0": 2.8, - "y1": 3.2, - "yref": "y" - }, - { - "fillcolor": "rgb(75, 158, 50)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:42", - "x1": "2016-01-01 6:00:43", - "xref": "x", - "y0": 2.8, - "y1": 3.2, - "yref": "y" - }, - { - "fillcolor": "rgb(197, 215, 20)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:30", - "x1": "2016-01-01 6:00:37", - "xref": "x", - "y0": 1.8, - "y1": 2.2, - "yref": "y" - }, - { - "fillcolor": "rgb(132, 248, 207)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:48", - "x1": "2016-01-01 6:00:52", - "xref": "x", - "y0": 1.8, - "y1": 2.2, - "yref": "y" - }, - { - "fillcolor": "rgb(155, 244, 183)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:07", - "x1": "2016-01-01 6:00:11", - "xref": "x", - "y0": 1.8, - "y1": 2.2, - "yref": "y" - }, - { - "fillcolor": "rgb(111, 71, 144)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:27", - "x1": "2016-01-01 6:00:30", - "xref": "x", - "y0": 1.8, - "y1": 2.2, - "yref": "y" - }, - { - "fillcolor": "rgb(71, 48, 128)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:52", - "x1": "2016-01-01 6:00:53", - "xref": "x", - "y0": 1.8, - "y1": 2.2, - "yref": "y" - }, - { - "fillcolor": "rgb(75, 158, 50)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:16", - "x1": "2016-01-01 6:00:19", - "xref": "x", - "y0": 1.8, - "y1": 2.2, - "yref": "y" - }, - { - "fillcolor": "rgb(197, 215, 20)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:42", - "x1": "2016-01-01 6:00:48", - "xref": "x", - "y0": 0.8, - "y1": 1.2, - "yref": "y" - }, - { - "fillcolor": "rgb(132, 248, 207)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:13", - "x1": "2016-01-01 6:00:23", - "xref": "x", - "y0": 0.8, - "y1": 1.2, - "yref": "y" - }, - { - "fillcolor": "rgb(155, 244, 183)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:48", - "x1": "2016-01-01 6:00:55", - "xref": "x", - "y0": 0.8, - "y1": 1.2, - "yref": "y" - }, - { - "fillcolor": "rgb(111, 71, 144)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:30", - "x1": "2016-01-01 6:00:38", - "xref": "x", - "y0": 0.8, - "y1": 1.2, - "yref": "y" - }, - { - "fillcolor": "rgb(71, 48, 128)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:25", - "x1": "2016-01-01 6:00:30", - "xref": "x", - "y0": 0.8, - "y1": 1.2, - "yref": "y" - }, - { - "fillcolor": "rgb(75, 158, 50)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:38", - "x1": "2016-01-01 6:00:42", - "xref": "x", - "y0": 0.8, - "y1": 1.2, - "yref": "y" - }, - { - "fillcolor": "rgb(197, 215, 20)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:38", - "x1": "2016-01-01 6:00:41", - "xref": "x", - "y0": -0.2, - "y1": 0.2, - "yref": "y" - }, - { - "fillcolor": "rgb(132, 248, 207)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:28", - "x1": "2016-01-01 6:00:38", - "xref": "x", - "y0": -0.2, - "y1": 0.2, - "yref": "y" - }, - { - "fillcolor": "rgb(155, 244, 183)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:11", - "x1": "2016-01-01 6:00:19", - "xref": "x", - "y0": -0.2, - "y1": 0.2, - "yref": "y" - }, - { - "fillcolor": "rgb(111, 71, 144)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:45", - "x1": "2016-01-01 6:00:54", - "xref": "x", - "y0": -0.2, - "y1": 0.2, - "yref": "y" - }, - { - "fillcolor": "rgb(71, 48, 128)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:41", - "x1": "2016-01-01 6:00:45", - "xref": "x", - "y0": -0.2, - "y1": 0.2, - "yref": "y" - }, - { - "fillcolor": "rgb(75, 158, 50)", - "line": { - "width": 0 - }, - "opacity": 1, - "type": "rect", - "x0": "2016-01-01 6:00:19", - "x1": "2016-01-01 6:00:28", - "xref": "x", - "y0": -0.2, - "y1": 0.2, - "yref": "y" - } - ], - "showlegend": false, - "title": "FT06", - "width": 900, - "xaxis": { - "rangeselector": { - "buttons": [ - { - "count": 7, - "label": "1w", - "step": "day", - "stepmode": "backward" - }, - { - "count": 1, - "label": "1m", - "step": "month", - "stepmode": "backward" - }, - { - "count": 6, - "label": "6m", - "step": "month", - "stepmode": "backward" - }, - { - "count": 1, - "label": "YTD", - "step": "year", - "stepmode": "todate" - }, - { - "count": 1, - "label": "1y", - "step": "year", - "stepmode": "backward" - }, - { - "step": "all" - } - ] - }, - "showgrid": true, - "type": "date", - "zeroline": false - }, - "yaxis": { - "autorange": false, - "range": [ - -1, - 7 - ], - "showgrid": true, - "ticktext": [ - "Resource5", - "Resource4", - "Resource3", - "Resource2", - "Resource1", - "Resource0" - ], - "tickvals": [ - 0, - 1, - 2, - 3, - 4, - 5 - ], - "zeroline": false - } - } - }, - "text/html": [ - "
" - ], - "text/vnd.plotly.v1+html": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "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", + "import collections\n", "from ortools.sat.python import cp_model\n", "from ortools.sat.python import visualization\n", "\n", @@ -1309,16 +52,20 @@ " # Computes horizon dynamically.\n", " horizon = sum([sum(durations[i]) for i in all_jobs])\n", "\n", + " Task = collections.namedtuple('Task', 'start end interval')\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", + " start_var = 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", + " end_var = model.NewIntVar(0, horizon, 'end_%i_%i' % (i, j))\n", + " interval_var = model.NewIntervalVar(start_var, duration, end_var,\n", + " 'interval_%i_%i' % (i, j))\n", + " all_tasks[(i, j)] = Task(start=start_var,\n", + " end=end_var,\n", + " interval=interval_var)\n", "\n", " # Create disjuctive constraints.\n", " machine_to_jobs = {}\n", @@ -1327,19 +74,19 @@ " 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", + " machines_jobs.append(all_tasks[(j, k)].interval)\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", + " model.Add(all_tasks[(i, j + 1)].start >= all_tasks[(i, j)].end)\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", + " obj_var, [all_tasks[(i, machines_count - 1)].end for i in all_jobs])\n", " model.Minimize(obj_var)\n", "\n", " # Solve model.\n", @@ -1347,7 +94,7 @@ " response = solver.Solve(model)\n", "\n", " # Output solution.\n", - " if visualization.RunFromIPython():\n", + " if visualization.run_from_ipython():\n", " starts = [[solver.Value(all_tasks[(i, j)][0]) for j in all_machines]\n", " for i in all_jobs]\n", " visualization.DisplayJobshop(starts, durations, machines, 'FT06')\n", @@ -1358,34 +105,9 @@ "if __name__ == '__main__':\n", " main()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.1" - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 2 } diff --git a/examples/notebook/nqueens_sat.ipynb b/examples/notebook/nqueens_sat.ipynb index cb863c78a0..71b0c7a1d8 100644 --- a/examples/notebook/nqueens_sat.ipynb +++ b/examples/notebook/nqueens_sat.ipynb @@ -102,6 +102,8 @@ "\n", "\n", "if __name__ == '__main__':\n", + " if len(sys.argv) > 1:\n", + " board_size = int(sys.argv[1])\n", " main(board_size)" ] } diff --git a/examples/notebook/steel_mill_slab_sat.ipynb b/examples/notebook/steel_mill_slab_sat.ipynb index 4a3546e1c7..c4f645c281 100644 --- a/examples/notebook/steel_mill_slab_sat.ipynb +++ b/examples/notebook/steel_mill_slab_sat.ipynb @@ -8,6 +8,19 @@ }, "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", "\n", "import argparse\n", diff --git a/examples/python/gate_scheduling_sat.py b/examples/python/gate_scheduling_sat.py index b26ec07873..bb34c5c314 100644 --- a/examples/python/gate_scheduling_sat.py +++ b/examples/python/gate_scheduling_sat.py @@ -23,6 +23,7 @@ The objective is to minimize the max end time of all jobs. """ from ortools.sat.python import cp_model +from ortools.sat.python import visualization def main(): @@ -115,17 +116,36 @@ def main(): # Output solution. - print('Solution') - print(' - makespan = %i' % solver.ObjectiveValue()) - for i in all_jobs: - performed_machine = 1 - solver.Value(performed[i]) - start = solver.Value(starts[i]) - print(' - Job %i starts at %i on machine %i' % - (i, start, performed_machine)) - print('Statistics') - print(' - conflicts : %i' % solver.NumConflicts()) - print(' - branches : %i' % solver.NumBranches()) - print(' - wall time : %f ms' % solver.WallTime()) + if visualization.run_from_ipython(): + output = visualization.SvgWrapper(solver.ObjectiveValue(), max_length, 40.0) + output.AddTitle('Makespan = %i' % solver.ObjectiveValue()) + color_manager = visualization.ColorManager() + color_manager.SeedRandomColor(0) + + for i in all_jobs: + performed_machine = 1 - solver.Value(performed[i]) + start = solver.Value(starts[i]) + dx = jobs[i][0] + dy = jobs[i][1] + sy = performed_machine * (max_length - dy) + output.AddRectangle(start, sy, dx, dy, color_manager.RandomColor(), + 'black', 'j%i' % i) + + output.AddXScale() + output.AddYScale() + output.Display() + else: + print('Solution') + print(' - makespan = %i' % solver.ObjectiveValue()) + for i in all_jobs: + performed_machine = 1 - solver.Value(performed[i]) + start = solver.Value(starts[i]) + print(' - Job %i starts at %i on machine %i' % + (i, start, performed_machine)) + print('Statistics') + print(' - conflicts : %i' % solver.NumConflicts()) + print(' - branches : %i' % solver.NumBranches()) + print(' - wall time : %f ms' % solver.WallTime()) if __name__ == '__main__': diff --git a/examples/python/hidato_sat.py b/examples/python/hidato_sat.py index 201ead05cf..e1195c618c 100644 --- a/examples/python/hidato_sat.py +++ b/examples/python/hidato_sat.py @@ -12,6 +12,7 @@ # limitations under the License. from ortools.sat.python import cp_model +from ortools.sat.python import visualization def BuildPairs(rows, cols): @@ -130,12 +131,12 @@ def SolveHidato(puzzle, index): r = len(puzzle) c = len(puzzle[0]) - - print('') - print('----- Solving problem %i -----' % index) - print('') - print(('Initial game (%i x %i)' % (r, c))) - PrintMatrix(puzzle) + if not visualization.run_from_ipython(): + print('') + print('----- Solving problem %i -----' % index) + print('') + print(('Initial game (%i x %i)' % (r, c))) + PrintMatrix(puzzle) # # declare variables @@ -170,11 +171,24 @@ def SolveHidato(puzzle, index): status = solver.Solve(model) if status == cp_model.MODEL_SAT: - PrintSolution([solver.Value(x) for x in positions], r, c,) - print('Statistics') - print(' - conflicts : %i' % solver.NumConflicts()) - print(' - branches : %i' % solver.NumBranches()) - print(' - wall time : %f ms' % solver.WallTime()) + if visualization.run_from_ipython(): + output = visualization.SvgWrapper(10, r, 40.0) + for i in range(len(positions)): + val = solver.Value(positions[i]) + x = val % c + y = val // c + color = 'white' if puzzle[y][x] == 0 else 'lightgreen' + value = solver.Value(positions[i]) + output.AddRectangle(x, r - y - 1, 1, 1, color, 'black', str(i + 1)) + + output.AddTitle('Puzzle %i solved in %f s' % (index, solver.WallTime())) + output.Display() + else: + PrintSolution([solver.Value(x) for x in positions], r, c,) + print('Statistics') + print(' - conflicts : %i' % solver.NumConflicts()) + print(' - branches : %i' % solver.NumBranches()) + print(' - wall time : %f ms' % solver.WallTime()) def main(): diff --git a/examples/python/jobshop_ft06_sat.py b/examples/python/jobshop_ft06_sat.py index c9f481443b..2a12c06501 100644 --- a/examples/python/jobshop_ft06_sat.py +++ b/examples/python/jobshop_ft06_sat.py @@ -83,8 +83,13 @@ def main(): solver = cp_model.CpSolver() response = solver.Solve(model) - # Print out makespan. - print('Optimal makespan: %i' % solver.ObjectiveValue()) + # Output solution. + if visualization.run_from_ipython(): + starts = [[solver.Value(all_tasks[(i, j)][0]) for j in all_machines] + for i in all_jobs] + visualization.DisplayJobshop(starts, durations, machines, 'FT06') + else: + print('Optimal makespan: %i' % solver.ObjectiveValue()) if __name__ == '__main__': diff --git a/ortools/sat/python/visualization.py b/ortools/sat/python/visualization.py index 8fff09863e..3ccdaa447c 100644 --- a/ortools/sat/python/visualization.py +++ b/ortools/sat/python/visualization.py @@ -19,9 +19,18 @@ try: import plotly.figure_factory as ff import plotly.offline as pyo import svgwrite - run_from_ipython = True + correct_imports = True except ImportError: - run_from_ipython = False + correct_imports = False + + +def run_from_ipython(): + if not correct_imports: + return False + try: + return __IPYTHON__ is not None + except NameError: + return False def ToDate(v):