Repository navigation
Expand file tree
/
Copy pathgraph_coloring.py
More file actions
101 lines (84 loc) · 3 KB
/
Copy pathgraph_coloring.py
File metadata and controls
101 lines (84 loc) · 3 KB
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
from collections import defaultdict, deque
from copy import deepcopy
from os import system
from random import randrange
# PS clear output
def console_clear():
system('cls')
# color first vertex, for the rest check connections and assign first available color
def greedy_coloring(matrix):
colors = list(range(len(matrix)))
result = {0: colors[0]}
for vertex in range(1, len(matrix)):
temp = colors.copy()
for edge in range(0, len(matrix[vertex])):
if matrix[vertex][edge] == 1 and edge in result and result[edge] in temp:
temp.remove(result[edge])
result[vertex] = temp[0]
return result, len(set(result.values()))
def tabu_coloring(adjacency_list, number_of_colors, previous_solution, is_first_solution, tabu_size = 7, reps = 40, max_iterations = 10000):
colors = list(range(number_of_colors))
iterations = 0
tabu_list = deque()
aspiration_dict = {}
solution = deepcopy(previous_solution)
if not is_first_solution:
for i in range(len(adjacency_list)):
if solution[i] >= number_of_colors:
solution[i] = colors[randrange(0, len(colors))]
while iterations < max_iterations:
candidates = set()
conflict_count = 0
for vertice, edges in adjacency_list.items():
for edge in edges:
if solution[vertice] == solution[edge]:
candidates.add(vertice)
candidates.add(edge)
conflict_count += 1
candidates = list(candidates)
if conflict_count == 0:
# Found a valid coloring.
break
new_solution = None
for _ in range(reps):
vertice = candidates[randrange(0, len(candidates))]
new_color = colors[randrange(0, len(colors))]
if solution[vertice] == new_color:
new_color = colors[-1]
new_solution = deepcopy(solution)
new_solution[vertice] = new_color
new_conflicts = 0
for vertice, edges in adjacency_list.items():
for edge in edges:
if vertice is not None and edge is not None and new_solution[vertice] == new_solution[edge]:
new_conflicts += 1
if new_conflicts < conflict_count:
if new_conflicts <= aspiration_dict.setdefault(conflict_count, conflict_count - 1):
aspiration_dict[conflict_count] = new_conflicts - 1
if (vertice, new_color) in tabu_list:
tabu_list.remove((vertice, new_color))
break
else:
if (vertice, new_color) in tabu_list:
continue
break
tabu_list.append((vertice, solution[vertice]))
if len(tabu_list) > tabu_size:
tabu_list.popleft()
solution = deepcopy(new_solution)
iterations += 1
if conflict_count != 0:
print("No coloring found with {} colors.".format(number_of_colors))
return False, previous_solution
else:
print("Found coloring:", len(set(solution.values())))
return True, solution
def tabu_search(adjacency_list, greedy_result_dict, greedy_result_number):
first_coloring = True
result = greedy_result_dict
for v in range(greedy_result_number, 1, -1):
status, result = tabu_coloring(adjacency_list, v, result, first_coloring)
if not status:
break
first_coloring = False
return result, len(set(result.values()))