-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
286 lines (253 loc) · 9.51 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from math import log2, ceil
import random
import matplotlib.pyplot as plt
#citirea fisierului de intrare
def read_file(file_path):
global no_chromosomes, domain, coefficients, precision, crossover_probability, mutation_probability, no_generations
file = open(file_path, 'r')
no_chromosomes = int(file.readline())
domain = [int(x) for x in file.readline().split()]
coefficients = [int(x) for x in file.readline().split()]
precision = int(file.readline())
crossover_probability = float(file.readline())
mutation_probability = float(file.readline())
no_generations = int(file.readline())
discretize()
def write_file(initial_generation, mean, mx, x_mx):
file = open("output.txt", "w")
encoded = encode(initial_generation)
fitness = list(map(fitness_function, initial_generation))
selection_chance = chances(initial_generation)
file.write("Populatia initiala:\n")
for i in range(len(initial_generation)):
file.write(f"Cromozomul {i+1}: {encoded[i]}, x = {initial_generation[i]}, f(x) = {fitness[i]}\n")
file.write("\nProbabilitati de selectie pentru populatia initiala:\n")
for i in range(len(selection_chance)):
file.write(f"Cromozomul {i+1}: {selection_chance[i]}\n")
intervals_selection_chance = selection_chances_intervals(selection_chance)
file.write("\nIntervale probabilitati selectie:\n")
for i in intervals_selection_chance:
file.write(str(i) + " ")
file.write("\n\n")
p1 = []
for i in range(len(selection_chance)):
u = random.random()
index = find_interval(intervals_selection_chance, u)
p1.append(initial_generation[index])
file.write(f"u = {u} => selectam cromozomul numarul {index + 1}\n")
file.write("\nDupa selectie:\n")
encoded = encode(p1)
fitness = list(map(fitness_function, p1))
for i in range(len(p1)):
file.write(f"Cromozomul {i+1}: {encoded[i]}, x = {p1[i]}, f(x) = {fitness[i]}\n")
file.write("\nCine participa la cross-over:\n")
indici = []
for i in range(len(p1)):
u = random.random()
if u < crossover_probability:
indici.append(i)
file.write(f"u = {u} < {crossover_probability} participa\n")
else:
file.write(f"u = {u}\n")
if len(indici) % 2 == 1:
indici.pop()
i = 0
while i != len(indici):
file.write(f"Cross-over intre cromozomul {indici[i]+1} si cromozomul {indici[i+1]+1}:\n")
taietura = random.randint(0, chromosome_length)
c1 = encoded[indici[i]]
c2 = encoded[indici[i + 1]]
prel_c1 = c2[:taietura] + c1[taietura:]
prel_c2 = c1[:taietura] + c2[taietura:]
encoded[indici[i]] = prel_c1
encoded[indici[i + 1]] = prel_c2
file.write(f"{prel_c1}, {prel_c2}, taietura: {taietura}\n")
i += 2
p1 = decode(encoded)
fitness = list(map(fitness_function, p1))
file.write("\nDupa cross-over:\n")
for i in range(len(p1)):
file.write(f"Cromozomul {i+1}: {encoded[i]}, x = {p1[i]}, f(x) = {fitness[i]}\n")
file.write("\nCromozomii care vor participa la mutatie: ")
for i in range(len(encoded)):
u = random.random()
print(u)
if u < mutation_probability:
print("da")
taietura = random.randint(0, chromosome_length)
file.write(f"{i+1} ")
mutagen = ""
for bit in encoded[i][:taietura]:
if bit == "0":
mutagen += "1"
else:
mutagen += "0"
mutagen += encoded[i][taietura:]
encoded[i] = mutagen
p1 = decode(encoded)
fitness = list(map(fitness_function, p1))
file.write("\nDupa mutatie:\n")
for i in range(len(p1)):
file.write(f"Cromozomul {i+1}: {encoded[i]}, x = {p1[i]}, f(x) = {fitness[i]}\n")
file.write("\nPentru restul generatiilor, media fitness vs maximul fitness:\n")
for i in range(len(mx)):
file.write(f"{mean[i]} {mx[i]}\n")
evolutie(mx, x_mx)
def maxim_functie_grad2(a, b, c, interval):
f_a = a * interval[0] ** 2 + b * interval[0] + c
f_b = a * interval[1] ** 2 + b * interval[1] + c
x_crit = -b / (2 * a)
if interval[0] <= x_crit <= interval[1]:
f_crit = a * x_crit ** 2 + b * x_crit + c
else:
f_crit = float('-inf')
maxim = max(f_a, f_b, f_crit)
if maxim == f_a:
x_maxim = interval[0]
elif maxim == f_b:
x_maxim = interval[1]
else:
x_maxim = x_crit
return x_maxim, maxim
def evolutie(mx, x_mx):
plt.figure(figsize=(8, 6))
plt.plot(x_mx, mx, marker='o', color='blue', linestyle='-', label='f(x)')
plt.xlabel('x')
plt.ylabel('f(x)')
x, y = maxim_functie_grad2(coefficients[0], coefficients[1], coefficients[2], domain)
plt.scatter([x], [y], color='red', label='maximul lui f(x) (aproximat)')
plt.title('Evolutia algoritmului genetic')
plt.legend()
plt.grid(True)
plt.show()
#calcularea numarului de biti pentru fiecare cromozom, a pasului de discretizare si a intervalelor
def discretize():
global chromosome_length, discretization_step, intervals
chromosome_length = ceil(log2((domain[1] - domain[0]) * (10 ** precision)))
discretization_step = (domain[1] - domain[0])/(2 ** chromosome_length)
intervals = [[domain[0] + i * discretization_step, domain[0] + (i + 1) * discretization_step] for i in range (2 ** chromosome_length - 1)]
#cautarea binara pentru incadrarea unui cromozom in intervalul din care face parte din lista de intervale
def binary_search_intervals(x):
left = 0
right = len(intervals) - 1
while left <= right:
mid = (left + right) // 2
interval = intervals[mid]
if interval[0] <= x < interval[1]:
return mid
elif x < interval[0]:
right = mid - 1
else:
left = mid + 1
#codificarea cromozomilor
def encode(numbers):
encoded_numbers = []
for number in numbers:
index = binary_search_intervals(number)
binary_index = format(index, 'b')
encoded_number = '0' * (chromosome_length - len(binary_index)) + binary_index
encoded_numbers.append(encoded_number)
return encoded_numbers
#decodificarea cromozomilor(capatul inferior al intervalului din care face parte)
def decode(numbers):
decoded_numbers = []
for number in numbers:
index = int(number, 2)
decoded_numbers.append(intervals[index][0])
return decoded_numbers
def fitness_function(x):
return coefficients[0] * x**2 + coefficients[1] * x + coefficients[2]
def selection_chances_intervals(selection_chance):
intervals_selection = []
partial = 0
for i in range(len(selection_chance)):
intervals_selection.append(partial)
partial += selection_chance[i]
intervals_selection.append(round(partial, 2))
return intervals_selection
def chances(generation):
p = list(map(fitness_function, generation))
total = sum(p)
p = list(map(lambda x: x/total, p))
return p
def find_interval(numbers, u):
n = len(numbers)
left = 0
right = n - 1
while left < right:
mid = (left + right) // 2
if numbers[mid] <= u:
left = mid + 1
else:
right = mid
if numbers[left] <= u:
return left
else:
return left - 1
def selection(generation):
selection_chance = chances(generation)
elitist = generation[selection_chance.index(max(selection_chance))]
selection_chances_interval = selection_chances_intervals(selection_chance)
p1 = []
for i in range(len(selection_chance)):
u = random.random()
index = find_interval(selection_chances_interval, u)
p1.append(generation[index])
if elitist not in p1:
p1.pop()
p1.append(elitist)
return p1
def cross_over(sir):
indici = []
for i in range(len(sir)):
u = random.random()
if u < crossover_probability:
indici.append(i)
if len(indici) % 2 == 1:
indici.pop()
i = 0
while i != len(indici):
taietura = random.randint(0, chromosome_length)
c1 = sir[indici[i]]
c2 = sir[indici[i + 1]]
prel_c1 = c2[:taietura] + c1[taietura:]
prel_c2 = c1[:taietura] + c2[taietura:]
sir[indici[i]] = prel_c1
sir[indici[i + 1]] = prel_c2
i += 2
return sir
def mutation(sir):
for i in range(len(sir)):
u = random.random()
if u < mutation_probability:
taietura = random.randint(0, chromosome_length)
mutagen = ""
for bit in sir[i][:taietura]:
if bit == "0":
mutagen += "1"
else:
mutagen += "0"
mutagen += sir[i][taietura:]
sir[i] = mutagen
return sir
def genetic_algorithm(file_path):
read_file(file_path)
generation = [round(random.uniform(domain[0], domain[1]), precision) for _ in range(no_chromosomes)]
initial_generation = generation
n = no_generations
mean = []
mx = []
x_mx = []
while n:
p1 = selection(generation)
p1 = encode(p1)
p2 = cross_over(p1)
generation = mutation(p2)
generation = decode(generation)
p = list(map(fitness_function, generation))
mx.append(max(p))
x_mx.append(generation[p.index(max(p))])
mean.append(sum(p)/len(p))
n -= 1
write_file(initial_generation, mean, mx, x_mx)
genetic_algorithm("input.txt")