14"""Collection of helpers to visualize cp_model solutions in colab."""
20 from IPython.display
import display
21 from IPython.display
import SVG
22 import plotly.figure_factory
as ff
25 correct_imports =
True
27 correct_imports =
False
31 if not correct_imports:
34 return __IPYTHON__
is not None
40 return "2016-01-01 6:%02i:%02i" % (v / 60, v % 60)
44 """Utility to create colors to use in visualization."""
46 def ScaledColor(self, sr, sg, sb, er, eg, eb, num_steps, step):
47 """Creates an interpolated rgb color between two rgb colors."""
48 num_intervals = num_steps - 1
49 dr = (er - sr) / num_intervals
50 dg = (eg - sg) / num_intervals
51 db = (eb - sb) / num_intervals
55 return "rgb(%i, %i, %i)" % (r, g, b)
61 return "rgb(%i,%i,%i)" % (
62 random.randint(0, 255),
63 random.randint(0, 255),
64 random.randint(0, 255),
69 """Simple function to display a jobshop solution using plotly."""
71 jobs_count = len(starts)
72 machines_count = len(starts[0])
73 all_machines = range(0, machines_count)
74 all_jobs = range(0, jobs_count)
77 for j
in all_machines:
80 Task=
"Resource%i" % machines[i][j],
81 Start=
ToDate(starts[i][j]),
82 Finish=
ToDate(starts[i][j] + durations[i][j]),
87 sorted_df = sorted(df, key=
lambda k: k[
"Task"])
93 colors[
"Job%i" % i] = cm.RandomColor()
95 fig = ff.create_gantt(
109 """Simple SVG wrapper to use in colab."""
116 self.
__dwg = svgwrite.Drawing(
124 display(SVG(self.
__dwg.tostring()))
126 def AddRectangle(self, x, y, dx, dy, fill, stroke="black", label=None):
127 """Draw a rectangle, dx and dy must be >= 0."""
130 corner = (x * s + o, (self.
__sizey - y - dy) * s + o)
131 size = (dx * s - 1, dy * s - 1)
133 self.
__dwg.rect(insert=corner, size=size, fill=fill, stroke=stroke)
135 self.
AddText(x + 0.5 * dx, y + 0.5 * dy, label)
138 text = self.
__dwg.text(
144 text_anchor=
"middle",
145 font_family=
"sans-serif",
151 """Add an scale on the x axis."""
154 y = self.
__sizey * s + o / 2.0 + o
157 self.
__dwg.line((o, y), (self.
__sizex * s + o, y), stroke=
"black")
159 for i
in range(0, int(self.
__sizex) + 1, step):
162 (o + i * s, y - dy), (o + i * s, y + dy), stroke=
"black"
167 """Add an scale on the y axis."""
173 self.
__dwg.line((x, o), (x, self.
__sizey * s + o), stroke=
"black")
175 for i
in range(0, int(self.
__sizey) + 1, step):
178 (x - dx, i * s + o), (x + dx, i * s + o), stroke=
"black"
183 """Add a title to the drawing."""
184 text = self.
__dwg.text(
190 text_anchor=
"middle",
191 font_family=
"sans-serif",