Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jun 28, 2023
1 parent 5194c98 commit 36c570e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 41 deletions.
105 changes: 66 additions & 39 deletions ortools/sat/colab/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Collection of helpers to visualize cp_model solutions in colab."""

# pylint: disable=g-import-not-at-top
import random

try:
from IPython.display import display
from IPython.display import SVG
import plotly.figure_factory as ff
import svgwrite

correct_imports = True
except ImportError:
correct_imports = False
Expand All @@ -34,7 +37,7 @@ def RunFromIPython():


def ToDate(v):
return '2016-01-01 6:%02i:%02i' % (v / 60, v % 60)
return "2016-01-01 6:%02i:%02i" % (v / 60, v % 60)


class ColorManager(object):
Expand All @@ -49,14 +52,17 @@ def ScaledColor(self, sr, sg, sb, er, eg, eb, num_steps, step):
r = sr + dr * step
g = sg + dg * step
b = sb + db * step
return 'rgb(%i, %i, %i)' % (r, g, b)
return "rgb(%i, %i, %i)" % (r, g, b)

def SeedRandomColor(self, seed=0):
random.seed(seed)

def RandomColor(self):
return 'rgb(%i,%i,%i)' % (random.randint(0, 255), random.randint(
0, 255), random.randint(0, 255))
return "rgb(%i,%i,%i)" % (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)


def DisplayJobshop(starts, durations, machines, name):
Expand All @@ -70,27 +76,32 @@ def DisplayJobshop(starts, durations, machines, name):
for i in all_jobs:
for j in all_machines:
df.append(
dict(Task='Resource%i' % machines[i][j],
Start=ToDate(starts[i][j]),
Finish=ToDate(starts[i][j] + durations[i][j]),
Resource='Job%i' % i))
dict(
Task="Resource%i" % machines[i][j],
Start=ToDate(starts[i][j]),
Finish=ToDate(starts[i][j] + durations[i][j]),
Resource="Job%i" % i,
)
)

sorted_df = sorted(df, key=lambda k: k['Task'])
sorted_df = sorted(df, key=lambda k: k["Task"])

colors = {}
cm = ColorManager()
cm.SeedRandomColor(0)
for i in all_jobs:
colors['Job%i' % i] = cm.RandomColor()

fig = ff.create_gantt(sorted_df,
colors=colors,
index_col='Resource',
title=name,
show_colorbar=False,
showgrid_x=True,
showgrid_y=True,
group_tasks=True)
colors["Job%i" % i] = cm.RandomColor()

fig = ff.create_gantt(
sorted_df,
colors=colors,
index_col="Resource",
title=name,
show_colorbar=False,
showgrid_x=True,
showgrid_y=True,
group_tasks=True,
)
fig.show()


Expand All @@ -103,30 +114,37 @@ def __init__(self, sizex, sizey, scaling=20.0):
self.__scaling = scaling
self.__offset = scaling
self.__dwg = svgwrite.Drawing(
size=(self.__sizex * self.__scaling + self.__offset,
self.__sizey * self.__scaling + self.__offset * 2))
size=(
self.__sizex * self.__scaling + self.__offset,
self.__sizey * self.__scaling + self.__offset * 2,
)
)

def Display(self):
display(SVG(self.__dwg.tostring()))

def AddRectangle(self, x, y, dx, dy, fill, stroke='black', label=None):
def AddRectangle(self, x, y, dx, dy, fill, stroke="black", label=None):
"""Draw a rectangle, dx and dy must be >= 0."""
s = self.__scaling
o = self.__offset
corner = (x * s + o, (self.__sizey - y - dy) * s + o)
size = (dx * s - 1, dy * s - 1)
self.__dwg.add(
self.__dwg.rect(insert=corner, size=size, fill=fill, stroke=stroke))
self.__dwg.rect(insert=corner, size=size, fill=fill, stroke=stroke)
)
self.AddText(x + 0.5 * dx, y + 0.5 * dy, label)

def AddText(self, x, y, label):
text = self.__dwg.text(
label,
insert=(x * self.__scaling + self.__offset,
(self.__sizey - y) * self.__scaling + self.__offset),
text_anchor='middle',
font_family='sans-serif',
font_size='%dpx' % (self.__scaling / 2))
insert=(
x * self.__scaling + self.__offset,
(self.__sizey - y) * self.__scaling + self.__offset,
),
text_anchor="middle",
font_family="sans-serif",
font_size="%dpx" % (self.__scaling / 2),
)
self.__dwg.add(text)

def AddXScale(self, step=1):
Expand All @@ -136,11 +154,14 @@ def AddXScale(self, step=1):
y = self.__sizey * s + o / 2.0 + o
dy = self.__offset / 4.0
self.__dwg.add(
self.__dwg.line((o, y), (self.__sizex * s + o, y), stroke='black'))
self.__dwg.line((o, y), (self.__sizex * s + o, y), stroke="black")
)
for i in range(0, int(self.__sizex) + 1, step):
self.__dwg.add(
self.__dwg.line((o + i * s, y - dy), (o + i * s, y + dy),
stroke='black'))
self.__dwg.line(
(o + i * s, y - dy), (o + i * s, y + dy), stroke="black"
)
)

def AddYScale(self, step=1):
"""Add an scale on the y axis."""
Expand All @@ -149,19 +170,25 @@ def AddYScale(self, step=1):
x = o / 2.0
dx = self.__offset / 4.0
self.__dwg.add(
self.__dwg.line((x, o), (x, self.__sizey * s + o), stroke='black'))
self.__dwg.line((x, o), (x, self.__sizey * s + o), stroke="black")
)
for i in range(0, int(self.__sizey) + 1, step):
self.__dwg.add(
self.__dwg.line((x - dx, i * s + o), (x + dx, i * s + o),
stroke='black'))
self.__dwg.line(
(x - dx, i * s + o), (x + dx, i * s + o), stroke="black"
)
)

def AddTitle(self, title):
"""Add a title to the drawing."""
text = self.__dwg.text(
title,
insert=(self.__offset + self.__sizex * self.__scaling / 2.0,
self.__offset / 2),
text_anchor='middle',
font_family='sans-serif',
font_size='%dpx' % (self.__scaling / 2))
insert=(
self.__offset + self.__sizex * self.__scaling / 2.0,
self.__offset / 2,
),
text_anchor="middle",
font_family="sans-serif",
font_size="%dpx" % (self.__scaling / 2),
)
self.__dwg.add(text)
4 changes: 2 additions & 2 deletions ortools/sat/feasibility_jump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ std::function<void()> FeasibilityJumpSolver::GenerateTask(int64_t /*task_id*/) {
{
const double dtime =
evaluator_->MutableLinearEvaluator()->DeterministicTime();
const double delta = dtime - deterministic_time_;
const double delta = dtime - deterministic_time();
AddTaskDeterministicDuration(delta);
shared_time_limit_->AdvanceDeterministicTime(delta);
deterministic_time_ += delta;
}

task_generated_ = false; // Atomic.
Expand Down

0 comments on commit 36c570e

Please sign in to comment.