Skip to content

Commit db0c467

Browse files
committed
new example: pell equation
1 parent fd6f278 commit db0c467

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

examples/python/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ code_sample_py("maze_escape_sat")
5858

5959
code_sample_py("no_wait_baking_scheduling_sat")
6060

61+
code_sample_py("pell_equation_sat")
62+
6163
code_sample_py("pentominoes_sat")
6264

6365
code_sample_py("prize_collecting_tsp_sat")
@@ -96,6 +98,8 @@ code_sample_py("task_allocation_sat")
9698

9799
code_sample_py("tasks_and_workers_assignment_sat")
98100

101+
code_sample_py("test_scheduling_sat")
102+
99103
code_sample_py("tsp_sat")
100104

101105
code_sample_py("vendor_scheduling_sat")

examples/python/golomb8.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323
"""
2424

2525
from absl import app
26-
from absl import flags
2726
from ortools.constraint_solver import pywrapcp
2827

29-
FLAGS = flags.FLAGS
30-
3128
# We disable the following warning because it is a false positive on constraints
3229
# like: solver.Add(x == 0)
3330
# pylint: disable=g-explicit-bool-comparison

examples/python/line_balancing_sat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
)
4848

4949

50-
class SectionInfo(object):
50+
class SectionInfo:
5151
"""Store model information for each section of the input file."""
5252

5353
def __init__(self):

examples/python/no_wait_baking_scheduling_sat.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
DISPLAY = "display"
5151

5252

53-
class Task(object):
53+
class Task:
5454
"""A unit baking task.
5555
5656
- Simple baking tasks have a fixed duration. They are performed by workers.
@@ -64,7 +64,7 @@ def __init__(self, name, min_duration, max_duration):
6464
self.max_duration = max_duration
6565

6666

67-
class Skill(object):
67+
class Skill:
6868
"""The skill of a worker or the capability of a machine."""
6969

7070
def __init__(self, name, efficiency):
@@ -73,7 +73,7 @@ def __init__(self, name, efficiency):
7373
self.efficiency = efficiency
7474

7575

76-
class Recipe(object):
76+
class Recipe:
7777
"""A recipe is a sequence of cooking tasks."""
7878

7979
def __init__(self, name):
@@ -85,7 +85,7 @@ def add_task(self, resource_name, min_duration, max_duration):
8585
return self
8686

8787

88-
class Resource(object):
88+
class Resource:
8989
"""A resource is a worker, a machine, or just some space for cakes to rest.
9090
9191
- Workers have a capacity of 1 and can have variable efficiency.
@@ -106,7 +106,7 @@ def add_skill(self, skill_name, efficiency):
106106
return self
107107

108108

109-
class Order(object):
109+
class Order:
110110
"""An order is a recipe that should be delivered at a given due date."""
111111

112112
def __init__(self, unique_id, recipe_name, due_date, quantity):

examples/python/pentominoes_sat.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
2121
This problem comes from the game Katamino:
2222
http://boardgamegeek.com/boardgame/6931/katamino
23+
24+
This example also includes suggestions from
25+
https://web.ma.utexas.edu/users/smmg/archive/1997/radin.html
2326
"""
2427

2528
from collections.abc import Sequence
@@ -42,6 +45,8 @@
4245
"pieces", "FILNPTUVWXYZ", "The subset of pieces to consider."
4346
)
4447

48+
_HEIGHT = flags.DEFINE_integer("height", 5, "The height of the box.")
49+
4550

4651
def is_one(mask: List[List[int]], x: int, y: int, orientation: int) -> bool:
4752
"""Returns true if the oriented piece is 1 at position [i][j].
@@ -104,8 +109,9 @@ def orientation_is_redundant(mask: List[List[int]], orientation: int) -> bool:
104109

105110
def generate_and_solve_problem(pieces: Dict[str, List[List[int]]]) -> None:
106111
"""Solves the pentominoes problem."""
107-
box_width = len(pieces)
108-
box_height = 5
112+
box_height = _HEIGHT.value
113+
box_width = 5 * len(pieces) // box_height
114+
print(f"Box has dimension {box_height} * {box_width}")
109115

110116
model = cp_model.CpModel()
111117
position_to_variables: List[List[List[cp_model.IntVar]]] = [
@@ -142,8 +148,8 @@ def generate_and_solve_problem(pieces: Dict[str, List[List[int]]]) -> None:
142148
status = solver.solve(model)
143149

144150
print(
145-
f"Problem {_PIECES.value} solved in {solver.wall_time}s with status"
146-
f" {solver.status_name(status)}"
151+
f"Problem {_PIECES.value} box {box_height}*{box_width} solved in"
152+
f" {solver.wall_time}s with status {solver.status_name(status)}"
147153
)
148154

149155
# Print the solution.
@@ -183,6 +189,16 @@ def main(argv: Sequence[str]) -> None:
183189
print(f"Piece {p} not found in the list of pieces")
184190
return
185191
selected_pieces[p] = pieces[p]
192+
if (len(selected_pieces) * 5) % _HEIGHT.value != 0:
193+
print(
194+
f"The height {_HEIGHT.value} does not divide the total area"
195+
f" {5 * len(selected_pieces)}"
196+
)
197+
return
198+
if _HEIGHT.value < 3 or 5 * len(selected_pieces) // _HEIGHT.value < 3:
199+
print(f"The height {_HEIGHT.value} is not compatible with the pieces.")
200+
return
201+
186202
generate_and_solve_problem(selected_pieces)
187203

188204

examples/python/single_machine_scheduling_with_setup_release_due_dates_sat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828
_PARAMS = flags.DEFINE_string(
2929
"params",
30-
"num_search_workers:16,log_search_progress:false,max_time_in_seconds:45",
30+
"num_search_workers:16,log_search_progress:true,max_time_in_seconds:45",
3131
"Sat solver parameters.",
3232
)
3333
_PREPROCESS = flags.DEFINE_bool(

0 commit comments

Comments
 (0)