15"""Collection of helpers to visualize cp_model solutions in colab."""
21 from IPython.display
import display
22 from IPython.display
import SVG
23 import plotly.figure_factory
as ff
26 correct_imports =
True
28 correct_imports =
False
32 if not correct_imports:
35 return __IPYTHON__
is not None
41 return "2016-01-01 6:%02i:%02i" % (v / 60, v % 60)
45 """Utility to create colors to use in visualization."""
47 def ScaledColor(self, sr, sg, sb, er, eg, eb, num_steps, step):
48 """Creates an interpolated rgb color between two rgb colors."""
49 num_intervals = num_steps - 1
50 dr = (er - sr) / num_intervals
51 dg = (eg - sg) / num_intervals
52 db = (eb - sb) / num_intervals
56 return "rgb(%i, %i, %i)" % (r, g, b)
62 return "rgb(%i,%i,%i)" % (
63 random.randint(0, 255),
64 random.randint(0, 255),
65 random.randint(0, 255),
70 """Simple function to display a jobshop solution using plotly."""
72 jobs_count = len(starts)
73 machines_count = len(starts[0])
74 all_machines = range(0, machines_count)
75 all_jobs = range(0, jobs_count)
78 for j
in all_machines:
81 Task=
"Resource%i" % machines[i][j],
82 Start=
ToDate(starts[i][j]),
83 Finish=
ToDate(starts[i][j] + durations[i][j]),
88 sorted_df = sorted(df, key=
lambda k: k[
"Task"])
94 colors[
"Job%i" % i] = cm.RandomColor()
96 fig = ff.create_gantt(
110 """Simple SVG wrapper to use in colab."""
117 self.
__dwg = svgwrite.Drawing(
125 display(SVG(self.
__dwg.tostring()))
127 def AddRectangle(self, x, y, dx, dy, fill, stroke="black", label=None):
128 """Draw a rectangle, dx and dy must be >= 0."""
131 corner = (x * s + o, (self.
__sizey - y - dy) * s + o)
132 size = (dx * s - 1, dy * s - 1)
134 self.
__dwg.rect(insert=corner, size=size, fill=fill, stroke=stroke)
136 self.
AddText(x + 0.5 * dx, y + 0.5 * dy, label)
139 text = self.
__dwg.text(
145 text_anchor=
"middle",
146 font_family=
"sans-serif",
152 """Add an scale on the x axis."""
155 y = self.
__sizey * s + o / 2.0 + o
158 self.
__dwg.line((o, y), (self.
__sizex * s + o, y), stroke=
"black")
160 for i
in range(0, int(self.
__sizex) + 1, step):
163 (o + i * s, y - dy), (o + i * s, y + dy), stroke=
"black"
168 """Add an scale on the y axis."""
174 self.
__dwg.line((x, o), (x, self.
__sizey * s + o), stroke=
"black")
176 for i
in range(0, int(self.
__sizey) + 1, step):
179 (x - dx, i * s + o), (x + dx, i * s + o), stroke=
"black"
184 """Add a title to the drawing."""
185 text = self.
__dwg.text(
191 text_anchor=
"middle",
192 font_family=
"sans-serif",