-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBacktracking.py
64 lines (48 loc) · 2.26 KB
/
Backtracking.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
# Author: Daniel
from functions import *
from csp import *
import time
class InstruCSP(CSP):
def __init__(self, variables, domains, neighbors, constraints):
super().__init__(variables, domains, neighbors, constraints)
self.count = 0
def unassign(self, var, assignment):
super().unassign(var, assignment)
self.count += 1
def make_instru(csp):
return InstruCSP(csp.variables, csp.domains, csp.neighbors, csp.constraints)
def run_q3():
for run in range(5):
n = 30
graphs = [rand_graph(n, 0.1), rand_graph(n, 0.2), rand_graph(n, 0.3), rand_graph(n, 0.4), rand_graph(n, 0.5)]
colours = []
itr = 0
for g in graphs:
variables = list(g.keys())
colours.clear()
itr += 1
print("============================ [",run+1,"] Run / Backtracking Graph [", itr,"] ===============================")
for i in range(len(variables)):
colours.append(variables[i])
# csp = MapColoringCSP(colours, g)
csp = make_instru(MapColoringCSP(colours, g))
start = time.time()
bts = backtracking_search(csp, select_unassigned_variable=mrv, order_domain_values=lcv, inference=mac)
end = time.time()
if bts != None:
print("Result:",bts)
print("Teams:", len(set(list(bts.values()))))
total_people_count = 0
for teams in set(list(bts.values())):
members_count = list(bts.values()).count(teams)
total_people_count += members_count
print("Team [", teams,"] has ", members_count, " members")
print("Total # of people assigned to teams:", total_people_count)
print("Valid teams?:", check_teams(g,bts))
print("Variables Assigned:",csp.nassigns,", Variables Unassigned:",csp.count)
print("Total Time:", end-start)
break
else:
print("Not enough colours:", colours)
# print("Total Time:", end-start)
run_q3()