-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanity.py
74 lines (63 loc) · 1.89 KB
/
sanity.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
import itertools
""" Verifies the validity of a path.
"""
def is_valid_path(path, colors, print_path = True):
if path is None:
return False
c = []
for i in range(len(path)-3):
if path[i] == 101 or path[i+1] == 101 or path[i+2] == 101 or path[i+3] == 101:
return False
color = colors[path[i]]
c.append(color)
color2 = colors[path[i+1]]
color3 = colors[path[i+2]]
color4 = colors[path[i+3]]
if color == color2 and color2 == color3 and color3 == color4:
c.append(color2)
c.append(color3)
if print_path:
print(c)
return False
return True
""" Verifies the weight of a path.
"""
def weight(path, edges):
total = 0
for i in range(len(path)-1):
total += edges[path[i]][path[i+1]]
return total
""" Normalizes the path for scorer purposes. Adds 1 to all vertices.
"""
def normalize(assign, N):
new = [-1] * N
for i in range(N):
new[i] = assign[i] + 1
return new
""" Denorms a path so we can use old paths and continue improving on them.
Subtracts 1 from all vertices.
"""
def denormalize(assign, N):
new = [-1] * N
for i in range(N):
new[i] = assign[i] - 1
return new
""" Brutally finds solutions to small graphs.
"""
def supreme_brute_generator(edges,colors,N):
permutations = itertools.permutations([i for i in range(N)])
optimal = float("inf")
optimal_path = None
path = next(permutations)
while path != None:
total = 0
for i in range(len(path)-1):
total += edges[path[i]][path[i+1]]
if total < optimal and is_valid_path(path,colors, False):
optimal_path = path
optimal = total
try:
path = next(permutations)
except StopIteration:
path = None
return optimal, optimal_path