-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoolsSmall.py
137 lines (106 loc) · 3.9 KB
/
poolsSmall.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from csp import Constraint, CSP
from typing import Dict, List, Optional
class PoolExclusionConstraint(Constraint[str, str]):
def __init__(self, p1: str, p2: str) -> None:
super().__init__([p1, p2])
self.p1: str = p1
self.p2: str = p2
def satisfied(self, assignment: Dict[str, str]) -> bool:
# If either player is not in the assignment, then it is not
# yet possible for their pools to be conflicting
if self.p1 not in assignment or self.p2 not in assignment:
return True
# check the pool assigned to place1 is not the same as the
# pool assigned to place2
return assignment[self.p1] != assignment[self.p2]
class PoolSizeConstraint(Constraint[str, int]):
def __init__(self, players: List[str], size: int) -> None:
super().__init__(players)
self.players = players
self.size = size
def satisfied(self, assignment: Dict[str, int]) -> bool:
#print(f'ASSIGNMENT VALUES: {assignment.values()}')
if len(assignment) < len(self.players):
return True
for pool in [1, 2, 3, 4]:
poolCount = sum(1 for p in assignment.values() if p == pool)
#print(poolCount)
if poolCount != self.size:
return False
return True
'''
class PoolRequirementConstraint(Constraint[str, int]):
def __init__(self, player: str, pool: int) -> None:
super().__init__([player])
self.player: str = player
self.pool: int = pool
def satisfied(self, assignment: Dict[str, int]) -> bool:
# If either place is not in the assignment, then it is not
# yet possible for their pools to be conflicting
if self.player not in assignment:
return True
# check the pool assigned to place1 is not the same as the
# pool assigned to place2
return assignment[self.player] == [self.pool]
'''
def printResult(result):
print(f"SOLUTION {solutionNumber}:")
for pool in [1, 2, 3, 4]:
poolPlayers = [player for player in result if result[player] == pool]
print(f"---Pool {pool}---")
for p in poolPlayers:
print(p)
print()
if __name__ == "__main__":
players: List[str] = [
"ALX R",
"XIFL",
"Poutine",
"Thorn",
"Mkok",
"NerdCedric",
"Kxmikaze",
"Anaconda",
"Rudulf",
"Vermillion",
"Walrus",
"Yarkster",
"Xozniath",
"Brokensink",
"5/Grim",
"UT 2"
]
pools: Dict[str, List[int]] = {}
poolRestrictions = {
"XIFL": 1,
"Thorn": 2,
"Kxmikaze": 3,
"Poutine": 4
}
for player in players:
try:
pools[player] = [poolRestrictions[player]]
except KeyError:
pools[player] = [1, 2, 3, 4]
csp: CSP[str, int] = CSP(players, pools)
csp.add_constraint(PoolExclusionConstraint("Brokensink", "UT 2"))
csp.add_constraint(PoolExclusionConstraint("Walrus", "Xozniath"))
csp.add_constraint(PoolExclusionConstraint("Xozniath", "Thorn"))
csp.add_constraint(PoolExclusionConstraint("Rudulf", "Vermillion"))
csp.add_constraint(PoolExclusionConstraint("Poutine", "Yarkster"))
csp.add_constraint(PoolExclusionConstraint("NerdCedric", "Kxmikaze"))
csp.add_constraint(PoolSizeConstraint(players, 4))
'''
csp.add_constraint(PoolRequirementConstraint("XIFL", 1))
csp.add_constraint(PoolRequirementConstraint("Thorn", 2))
csp.add_constraint(PoolRequirementConstraint("Kxmikaze", 1))
csp.add_constraint(PoolRequirementConstraint("Poutine", 1))
'''
print("Computing...")
solutionNumber = 1
solution: Optional[List[Dict[str, str]]] = csp.backtracking_search()
if solution is []:
print("No solution found!")
else:
printResult(solution)
solutionNumber += 1