modify python sat examples to use visualization when run from IPython; change corresponding notebooks
This commit is contained in:
@@ -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
@@ -17,12 +17,13 @@ We have a set of jobs to perform (duration, width).
|
||||
We have two parallel machines that can perform this job.
|
||||
One machine can only perform one job at a time.
|
||||
At any point in time, the sum of the width of the two active jobs does not
|
||||
exceed a max_width.
|
||||
exceed a max_length.
|
||||
|
||||
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():
|
||||
@@ -112,17 +113,39 @@ def main():
|
||||
# Solve model.
|
||||
solver = cp_model.CpSolver()
|
||||
solver.Solve(model)
|
||||
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())
|
||||
|
||||
|
||||
# Output solution.
|
||||
if visualization.RunFromIPython():
|
||||
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__':
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from ortools.sat.python import cp_model
|
||||
from ortools.sat.python import visualization
|
||||
|
||||
|
||||
def BuildPairs(rows, cols):
|
||||
@@ -110,7 +111,7 @@ def BuildPuzzle(problem):
|
||||
return puzzle
|
||||
|
||||
|
||||
def SolveHidato(puzzle):
|
||||
def SolveHidato(puzzle, index):
|
||||
"""Solve the given hidato table."""
|
||||
# Create the model.
|
||||
model = cp_model.CpModel()
|
||||
@@ -118,8 +119,12 @@ def SolveHidato(puzzle):
|
||||
r = len(puzzle)
|
||||
c = len(puzzle[0])
|
||||
|
||||
print(('Initial game (%i x %i)' % (r, c)))
|
||||
PrintMatrix(puzzle)
|
||||
if not visualization.RunFromIPython():
|
||||
print('')
|
||||
print('----- Solving problem %i -----' % index)
|
||||
print('')
|
||||
print(('Initial game (%i x %i)' % (r, c)))
|
||||
PrintMatrix(puzzle)
|
||||
|
||||
#
|
||||
# declare variables
|
||||
@@ -154,20 +159,29 @@ def SolveHidato(puzzle):
|
||||
status = solver.Solve(model)
|
||||
|
||||
if status == cp_model.MODEL_SAT:
|
||||
PrintSolution([solver.Value(x) for x in positions], r, c,)
|
||||
if visualization.RunFromIPython():
|
||||
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))
|
||||
|
||||
print('Statistics')
|
||||
print(' - conflicts : %i' % solver.NumConflicts())
|
||||
print(' - branches : %i' % solver.NumBranches())
|
||||
print(' - wall time : %f ms' % solver.WallTime())
|
||||
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():
|
||||
for i in range(1, 7):
|
||||
print('')
|
||||
print('----- Solving problem %i -----' % i)
|
||||
print('')
|
||||
SolveHidato(BuildPuzzle(i))
|
||||
SolveHidato(BuildPuzzle(i), i)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from ortools.sat.python import cp_model
|
||||
from ortools.sat.python import visualization
|
||||
|
||||
|
||||
def main():
|
||||
@@ -63,7 +64,14 @@ def main():
|
||||
# Solve model.
|
||||
solver = cp_model.CpSolver()
|
||||
response = solver.Solve(model)
|
||||
print(solver.ObjectiveValue())
|
||||
|
||||
# Output solution.
|
||||
if visualization.RunFromIPython():
|
||||
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__':
|
||||
|
||||
@@ -20,6 +20,14 @@ import plotly.offline as pyo
|
||||
import svgwrite
|
||||
|
||||
|
||||
def RunFromIPython():
|
||||
try:
|
||||
__IPYTHON__
|
||||
return True
|
||||
except NameError:
|
||||
return False
|
||||
|
||||
|
||||
def ToDate(v):
|
||||
return '2016-01-01 6:%02i:%02i' % (v / 60, v % 60)
|
||||
|
||||
@@ -156,4 +164,3 @@ class SvgWrapper(object):
|
||||
font_family='sans-serif',
|
||||
font_size='%dpx' % (self.__scaling / 2))
|
||||
self.__dwg.add(text)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user