-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion.py
78 lines (58 loc) · 1.91 KB
/
region.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from typing import List, Tuple
from pulp import LpVariable, lpSum
class Region:
def __init__(self, name: str, cells: List[Tuple[int, int]]) -> None:
self.name = name
self.cells = cells
def unique_constraints(self, values: List[int], choices: LpVariable) -> List:
return [
(
lpSum([choices[value][row][column] for row, column in self.cells]) <= 1,
f"Unique_{self.name}_{value}"
)
for value in values
]
def __repr__(self):
result = self.name + "\n"
for x, y in self.cells:
result += f" {x} {y}\n"
return result
class Line(Region):
def __init__(self, r1: int, c1: int, r2: int, c2: int):
row = range(r1, r2 + 1, 1) if r1 < r2 else range(r1, r2 - 1, -1)
col = range(c1, c2 + 1, 1) if c1 < c2 else range(c1, c2 - 1, -1)
cells = [(r, c) for r, c in zip(row, col)]
super().__init__(f"Line({self.r1},{self.c1},{self.r2},{self.c2}", cells)
self.r1 = r1
self.c1 = c1
self.r2 = r2
self.c2 = c2
def __repr__(self):
return f"({self.r1}.{self.c1}),({self.r2}.{self.c2})"
class Lines:
def __init__(self, lines: List[Line]):
self.lines = lines
def __repr__(self):
return "[" + ", ".join([str(line) for line in self.lines]) + "]"
class DiagonalLines(Lines):
def __init__(self):
super().__init__(
[
Line(1, 1, 9, 9),
Line(9, 1, 1, 9)
]
)
class ArgyleLines(Lines):
def __init__(self):
super().__init__(
[
Line(1, 5, 5, 1),
Line(5, 1, 9, 5),
Line(9, 5, 5, 9),
Line(5, 9, 1, 5),
Line(1, 2, 8, 9),
Line(2, 1, 9, 8),
Line(1, 8, 8, 1),
Line(2, 9, 9, 2)
]
)