-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenetic_Algorithm.py
More file actions
146 lines (99 loc) · 4.04 KB
/
Genetic_Algorithm.py
File metadata and controls
146 lines (99 loc) · 4.04 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
import random
import Chromosome as ch
from Chromosome import N_of_cities
def create_random_list(n_list):
start = n_list[0]
temp = n_list[1:]
temp = random.sample(temp, len(temp))
temp.insert(0, start)
temp.append(start)
return temp
def initialization(data, pop_size):
initial_population = []
for i in range(0, pop_size):
temp = create_random_list(data)
new_ch = ch.Chromosome(temp)
initial_population.append(new_ch)
return initial_population
def selection(population):
ticket_1, ticket_2, ticket_3, ticket_4 = random.sample(range(0, 99), 4)
candidate_1 = population[ticket_1]
candidate_2 = population[ticket_2]
candidate_3 = population[ticket_3]
candidate_4 = population[ticket_4]
if candidate_1.fitness_value > candidate_2.fitness_value:
winner = candidate_1
else:
winner = candidate_2
if candidate_3.fitness_value > winner.fitness_value:
winner = candidate_3
if candidate_4.fitness_value > winner.fitness_value:
winner = candidate_4
return winner
def crossover(p_1, p_2):
one_point = random.randint(2, 14)
child_1 = p_1.chromosome[1:one_point]
child_2 = p_2.chromosome[1:one_point]
child_1_remain = [item for item in p_2.chromosome[1:-1] if item not in child_1]
child_2_remain = [item for item in p_1.chromosome[1:-1] if item not in child_2]
child_1 += child_1_remain
child_2 += child_2_remain
child_1.insert(0, p_1.chromosome[0])
child_1.append(p_1.chromosome[0])
child_2.insert(0, p_2.chromosome[0])
child_2.append(p_2.chromosome[0])
return child_1, child_2
def crossover_two(p_1, p_2):
point_1, point_2 = random.sample(range(1, len(p_1.chromosome)-1), 2)
begin = min(point_1, point_2)
end = max(point_1, point_2)
child_1 = p_1.chromosome[begin:end+1]
child_2 = p_2.chromosome[begin:end+1]
child_1_remain = [item for item in p_2.chromosome[1:-1] if item not in child_1]
child_2_remain = [item for item in p_1.chromosome[1:-1] if item not in child_2]
child_1 += child_1_remain
child_2 += child_2_remain
child_1.insert(0, p_1.chromosome[0])
child_1.append(p_1.chromosome[0])
child_2.insert(0, p_2.chromosome[0])
child_2.append(p_2.chromosome[0])
return child_1, child_2
def crossover_mix(p_1, p_2):
point_1, point_2 = random.sample(range(1, len(p_1.chromosome)-1), 2)
begin = min(point_1, point_2)
end = max(point_1, point_2)
child_1_1 = p_1.chromosome[:begin]
child_1_2 = p_1.chromosome[end:]
child_1 = child_1_1 + child_1_2
child_2 = p_2.chromosome[begin:end+1]
child_1_remain = [item for item in p_2.chromosome[1:-1] if item not in child_1]
child_2_remain = [item for item in p_1.chromosome[1:-1] if item not in child_2]
child_1 = child_1_1 + child_1_remain + child_1_2
child_2 += child_2_remain
child_2.insert(0, p_2.chromosome[0])
child_2.append(p_2.chromosome[0])
return child_1, child_2
def mutation(chromosome):
mutation_index_1, mutation_index_2 = random.sample(range(1, (N_of_cities - 1)), 2)
chromosome[mutation_index_1], chromosome[mutation_index_2] = chromosome[mutation_index_2], chromosome[mutation_index_1]
return chromosome
def find_best(generation):
best = generation[0]
for n in range(1, len(generation)):
if generation[n].cost < best.cost:
best = generation[n]
return best
def create_new_generation(previous_generation, mutation_rate):
new_generation = [find_best(previous_generation)]
for a in range(0, int(len(previous_generation)/2)):
parent_1 = selection(previous_generation)
parent_2 = selection(previous_generation)
child_1, child_2 = crossover_mix(parent_1, parent_2)
child_1 = ch.Chromosome(child_1)
child_2 = ch.Chromosome(child_2)
if random.random() < mutation_rate:
mutated = mutation(child_1.chromosome)
child_1 = ch.Chromosome(mutated)
new_generation.append(child_1)
new_generation.append(child_2)
return new_generation