-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic.py
59 lines (40 loc) · 1.85 KB
/
genetic.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
import random
from utils import weighted_sampler
def genetic_algorithm(population, fitness_fn, problem, f_thres=None, ngen=1000, pmut=0.1):
fittest_individual = population[0]
fittest_individual_score = -1
fitness_fn2 = lambda x:fitness_fn(problem,x)
for i in range(ngen):
population = [mutate(crossover_uniform(*select(2, population, fitness_fn2)), problem, pmut)
for i in range(len(population))]
fittest_individual_candidate = max(population, key=fitness_fn2)
fittest_individual_candidate_score = fitness_fn2(fittest_individual_candidate)
if fittest_individual_candidate_score > fittest_individual_score:
fittest_individual = fittest_individual_candidate
fittest_individual_score = fittest_individual_candidate_score
fitnesses = map(fitness_fn2, population)
avg_fitness = sum(fitnesses)/len(population)
print(f"[+] iter {i} - best score: {fittest_individual_score} - avg score: {avg_fitness}")
if f_thres is not None:
if fittest_individual_score >= f_thres:
return fittest_individual
return fittest_individual
def select(r, population, fitness_fn2):
fitnesses = map(fitness_fn2, population)
sampler = weighted_sampler(population, fitnesses)
return [sampler() for i in range(r)]
def crossover_uniform(x, y):
n = len(x)
result = [-1] * n
tosses = random.choices([0, 1], k=n)
for i in range(n):
ix = tosses[i]
result[i] = x[i] if ix < 0.5 else y[i]
return result
def mutate(x, problem, pmut):
if random.uniform(0, 1) >= pmut:
return x
n = len(x)
c = random.randrange(0, n)
new_gene = random.choice(problem.adjList[c+1]).opposite(c+1) # safe mutate
return x[:c] + [new_gene] + x[c + 1:]