cleanup is_run_from_ipython()
This commit is contained in:
@@ -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",
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -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)"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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__':
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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__':
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user