-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algo.py
205 lines (149 loc) · 6.49 KB
/
genetic_algo.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'''
Author: Maciej Kaczkowski
16-24.03.2021
'''
import igraph as ig
import numpy as np
import pandas as pd
import random
import time
def initiate_population(size, vertices_number=25):
population = np.random.randint(2, size=(size, vertices_number))
return population
def evaluate(population, graph):
result = population.shape[1]
solved = False
for i in range(population.shape[0]):
member = population[i, :]
j = 0
not_covered = graph.copy()
to_delete = np.array([])
for v in not_covered.vs:
if member[j] == 1:
to_delete = np.append(to_delete, v)
j += 1
not_covered.delete_vertices(to_delete)
if not_covered.ecount() == 0:
solved = True
if np.sum(member) < result:
result = np.sum(member)
return solved, result
def tournament_selection(population, graph):
# przyjmujemy założenie o turnieju 2-osobnikowym
new_population = np.empty_like(population)
for i in range(new_population.shape[0]):
random_numbers = random.sample(range(population.shape[0]), 2)
first_competitor = population[random_numbers[0]]
second_competitor = population[random_numbers[1]]
j = 0
k = 0
g1 = graph.copy()
g2 = graph.copy()
to_delete_first = np.array([], dtype=int)
to_delete_second = np.array([], dtype=int)
for v in g1.vs:
if first_competitor[j] == 1:
to_delete_first = np.append(to_delete_first, v.index)
j += 1
for v in g2.vs:
if second_competitor[k] == 1:
to_delete_second = np.append(to_delete_second, v.index)
k += 1
g1.delete_vertices(to_delete_first)
g2.delete_vertices(to_delete_second)
if g1.ecount() <= g2.ecount():
if g1.ecount() < g2.ecount():
new_population[i:] = first_competitor
else:
# if both competitors cover the same number of edges, one with fewer vertices wins
new_population[i:] = first_competitor if np.sum(first_competitor) < np.sum(second_competitor) \
else second_competitor
else:
new_population[i:] = second_competitor
return new_population
def mutate(population, proba_mutation=0.01):
new_population = population
random_numbers = np.random.rand(new_population.shape[0], 1)
if np.all(random_numbers) < proba_mutation: return new_population
for i in range(random_numbers.shape[0]):
if random_numbers[i] <= proba_mutation:
mutated_element = np.random.randint(new_population.shape[1], size=1)
new_population[i][mutated_element] = abs(new_population[i][mutated_element] - 1)
if random_numbers[i] <= proba_mutation**2:
new_population[i][mutated_element - 1] = abs(new_population[i][mutated_element - 1] - 1)
return new_population
def evolve(population, graph, max_iterations=100, proba_mutation=0.01):
t0 = time.time()
generation = 0
while generation < max_iterations:
# selekcja turniejowa
new_population = tournament_selection(population, graph)
# mutacja
new_population = mutate(new_population, proba_mutation)
# sukcesja generacyjna
population = new_population
generation += 1
solved, best_result = evaluate(population, graph)
t1 = time.time()
if solved:
return best_result, t1 - t0
else:
return population.shape[1], t1 - t0
def run_tests(population, graph, name):
proba_mutations = np.array([0.01, 0.05])
max_iterations = np.array([100, 500])
results = np.zeros((max_iterations.shape[0] + proba_mutations.shape[0], 2))
i = 0
for j in range(max_iterations.shape[0]):
for k in range(proba_mutations.shape[0]):
results[i][0], results[i][1] = evolve(population, graph,
max_iterations=max_iterations[j],
proba_mutation=proba_mutations[k])
i += 1
pd.DataFrame(results).to_csv('./results_' + name + '.csv', sep='\t')
def show_menu():
while True:
choice = input("Select option:\n"
"1 - test with full graph\n"
"2 - test with bipartite graph\n"
"3 - test with random graph\n"
"q - quit\n")
if choice == 'q':
return None
try:
choice = int(choice)
except ValueError:
print('Write a number')
vertices_number = 25
population_sizes = np.array([10, 50], dtype=int)*vertices_number
if choice == 1:
full_graph = ig.Graph.Full(vertices_number)
for j in population_sizes:
name = 'full_' + str(j)
population = initiate_population(size=j)
run_tests(population=population, graph=full_graph, name=name)
elif choice == 2:
edges_number = 2*vertices_number
n1 = np.array([5, 12])
for j in n1:
for k in population_sizes:
name = 'bipartite_' + str(j) + '_' + str(k)
population = initiate_population(size=k)
bipartite_graph = ig.Graph.Random_Bipartite(n1=j, n2=vertices_number - j, m=edges_number)
run_tests(population=population, graph=bipartite_graph, name=name)
elif choice == 3:
for j in range(2):
random_graph = ig.Graph.Full(vertices_number)
for k in range(6 * random_graph.ecount() // 10):
which_edge = np.random.randint(random_graph.ecount())
edge = random_graph.get_edgelist()[which_edge]
random_graph.delete_edges(edge)
for m in population_sizes:
name = 'random_' + str(j) + '_' + str(m)
population = initiate_population(size=m)
run_tests(population=population, graph=random_graph, name=name)
else:
print('Select 1, 2, 3 or q')
# setting random seed using best practice according to numpy docs
rs = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(123456)))
show_menu()