-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
102 lines (69 loc) · 2.64 KB
/
search.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
Person = str
Table = int
# For selection variable
def first_unassigned_variable(csp):
return [v for v in csp.people if v not in csp.assignment][0]
def minimum_remaining_value(csp) -> Person:
"""choosing the variable with the fewest “legal” values"""
return min((v for v in csp.people if v not in csp.assignment), key=lambda kv: len(csp.domains[kv]))
# ordering value
def least_constraining_value(csp, person: Person):
"""Given a variable, choose the least constraining value:"""
list = csp.n_global_conflict(person)
list_ordered = sorted(list, key=lambda kv: kv[1], reverse=True)
return [v for v, z in list_ordered]
def unordered_values(csp, person: Person):
return csp.domains[person]
# For inference
def no_inference(csp) -> bool:
return True
def forward_checking(csp) -> bool:
for variable in csp.people:
if variable not in csp.assignment:
if not csp.domains[variable]:
return False
return True
def AC3(csp) -> bool:
queue = set()
for var1 in csp.people:
if var1 not in csp.assignment:
for var2 in csp.people:
if var2 not in csp.assignment:
if var1 != var2:
queue.add((var1, var2))
while queue:
xi, xj = queue.pop()
if revise(csp, xi, xj):
if csp.domains[xi]:
return False
for xk in csp.neighbours(xi, xj):
queue.add((xk,xi))
# for xz, xk in queue:
# if xz == xi and xk != xj:
# queue.add((xk, xi))
return True
def revise(csp, xi, xj):
revised = False
for val in csp.domains[xi]:
if not csp.assign_var(xi, val, xj):
csp.domains[xi] = csp.domains[xi] - {val}
revised = True
return revised
# Algorithm for search
def backtracking_search(csp, selected_unassigned_value=first_unassigned_variable, ordered_value=unordered_values,
inference=no_inference):
# assignment is complete if every variable is assigned
if len(csp.assignment) == len(csp.people):
return csp
person = selected_unassigned_value(csp)
for value in ordered_value(csp, person):
removed = csp.assign(person, value)
if inference(csp):
result = backtracking_search(csp)
# if we didn't find the result, we will end up backtracking
if result is not None:
return result
csp.unassign(person, removed)
return None
# def deegre_heuristic(problem):
# return [v for v in problem.constraint_for_person if v not in problem.list_table][0]