-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGA_GraphColoring.py
More file actions
179 lines (164 loc) · 5.3 KB
/
Copy pathGA_GraphColoring.py
File metadata and controls
179 lines (164 loc) · 5.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import random
import operator
################### make random adjacency matrix for problem ###################
def graph_generator (node):
adj_graph=[]
n=node
for i in range(0,n):
adj_graph.append([0])
for j in range(i+1,n):
adj_graph[i].append(random.randint(0,1))
print(adj_graph)
return(adj_graph)
# initialize population (population size =100 and chromosome_size = node_number)#
def initialisation(chromosome,color):
pop= dict()
n=chromosome
m=color
for i in range (1,101):
l=list()
for j in range(0,n):
l.append(random.randint(1,m))
pop[i]=l
l=[]
#for obj in list(pop.keys()): #for test initialisation result
# print( obj ,pop[obj])
return(pop)
################### compute fitness for each choromosome ###################
def fittnes_func(graph,node, arr):
n=node
chorom_Arr = arr
rank=0
ii = 0
while ii < n:
count = ii
color = chorom_Arr[ii]
l=ii
for j in graph[l]:
if j == 1 and color != chorom_Arr[count]:
rank +=1000
elif j == 0:
rank = rank+10
count +=1
ii += 1
return(rank)
################### selection_roulette wheel ###################
def selection(fitness):
pr=random.uniform(0,0.98)
select=0
count = 0
while select < pr :
count +=1
select += fitness[count]
if count == 100:
return(count-1)
else:
return(count)
################### one point cross over ###################
def crossover_func(l1,l2,len_,graph,clr):
n=len_
point=random.randint(0,n)
l1[point: ] , l2[point: ] = l2[point: ] , l1[point: ]
#print(l1,l2) #test new chromosome
#Mutation
mutation(l1,n,graph,clr)
mutation(l2,n,graph,clr)
return(l1,l2)
################### mutation ###################
def mutation(l1,len__ , graph,clr_):
p=random.uniform(0,0.99)
n=len__
m=clr_
if p<0.15:
j = 0
while j < n:
count = j
color = l1[j]
l=j
for j in graph[l]:
if j == 1 and color == l1[count]:
l1[count]= random.randint(1,m)
return()
j += 1
else:
return()
################### compute total fitness for graph ###################
def total_fitness(graph,node):
n=node
counter=0
total_rank=0
while counter < n:
l=counter
for j in graph[l]:
if j==1:
total_rank += 1000
else:
total_rank +=10
counter +=1
return(total_rank)
######################## main #########################
node_number=int(input('Enter the number of nodes: '))
colors= int(input('Enter the number of colors for coloring: '))
graph = graph_generator(node_number)
population = initialisation(node_number,colors)
total_fit = total_fitness(graph,node_number)
flag = 0
while_count = 1
best_fit={}
while(flag == 0 and while_count < 500):
fitness={}
fitness_backup={}
sum_fit=0
for chorom in list(population.keys()):
chorom_Arr = population[chorom]
fitness[chorom]=fittnes_func(graph,node_number,chorom_Arr)
fitness_backup[chorom]=fitness[chorom]
for chorom in list(fitness.keys()):
sum_fit += fitness[chorom]
#a=0
for chorom in list(fitness.keys()):
fitness[chorom] = fitness[chorom]/sum_fit
#a = a+ fitness[chorom]
#print (while_count ,a)
next_gen=dict()
i = 50
j = 101
while i > 0:
#selection
chorom_1=selection(fitness)
chorom_2=selection(fitness)
#print(population[chorom_1] , population [chorom_2])
################## crossover and Mutation ###################
child1 , child2 = crossover_func(population[chorom_1], population[chorom_2],node_number,graph,colors)
population[j]=child1
population[j+1]=child2
j += 2
i -= 1
for chorom in list(population.keys()):
chorom_Arr = population[chorom]
fitness_backup[chorom]=fittnes_func(graph,node_number,chorom_Arr)
if fitness_backup[chorom] == total_fit:
print('The answer is:')
print(population[chorom])
print('Number of cycle: ' , while_count)
flag =1
break
if flag==0 :
#sorted_by_value = sorted(fitness.items(), key=lambda kv: kv[1])
sorted_fit= sorted(fitness_backup.items(),key=lambda kv: kv[1])
for count in range(0,100):
del population[sorted_fit[count][0]]
#if while_count == 1:
best_fit[sorted_fit[199][1]] = population[sorted_fit[199][0]]
count = 1
for chorom in list(population.keys()):
next_gen[count] = population[chorom]
count += 1
population = next_gen
while_count += 1
sorted_best = sorted(best_fit.items(),key=operator.itemgetter(0))
if while_count == 500 and flag == 0 :
print("This graph with %i colors in 100 cycle the fitest answer is: " %(colors),)
for fit , chorom in sorted_best:
print(fit , chorom)
print (total_fit)