-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenetic_algorithm.py
193 lines (126 loc) · 5.71 KB
/
genetic_algorithm.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
import math
from config.config_parser import parser
from random import randint
from shapely.geometry import Polygon, LineString
from utils.plotter import plot
def start(obstacles, path_points, path_validity):
population = _generate_population(path_points, obstacles, path_validity)
path_lengths = []
for chromosome in population:
path_lengths.append(_calculate_path_length(chromosome, path_points))
plot(obstacles, path_points, population, path_lengths, 1, False)
generations = int(parser['Genetic Algorithm']['max_generations'])
for gen in range(generations - 1):
new_population = []
path_lengths.clear()
fitness_list = _sort_by_fitness(population, path_points)
for chromosome in population:
while True:
parent1 = _choose_random_parent(fitness_list)
parent2 = _choose_random_parent(fitness_list)
child = _crossover(parent1, parent2)
if randint(1, 10) <= 10 * float(parser['Genetic Algorithm']['mutation_probability']):
child = _mutation(child)
if _chromosome_valid(child, obstacles, path_points):
break
path_lengths.append(_calculate_path_length(child, path_points))
new_population.append(child)
population = new_population
plot(obstacles, path_points, new_population, path_lengths, (gen+2), last_gen=True if gen == generations-2 else False )
def _mutation(chromosome):
index = randint(1, len(chromosome) - 2) # we won't mutate source and goal genes
chromosome = list(chromosome)
chromosome[index] = '1' if chromosome[index] == '0' else '0'
return ''.join(chromosome)
def _fitness(chromosome, path_points):
length = _calculate_path_length(chromosome, path_points)
fitness = 1 / length if length != 0 else 0
return fitness
def _sort_by_fitness(population, path_points):
fitness_list = []
for chromosome in population:
chromosome_to_fitness = (chromosome, _fitness(chromosome, path_points))
fitness_list.append(chromosome_to_fitness)
fitness_list.sort(reverse=True, key=lambda tuple: tuple[1])
return fitness_list
def _choose_random_parent(fitness_list):
till_index = len(fitness_list) * float(parser['Genetic Algorithm']['top_percentage'])
till_index = math.floor(till_index)
parent_to_fitness = fitness_list[randint(0, till_index)]
return parent_to_fitness[0]
def _crossover(parent1, parent2):
if parser['Genetic Algorithm'].getboolean('crossover_split_random'):
split_size = randint(0, len(parent1))
else:
fraction = float(parser['Genetic Algorithm']['crossover_split_size'])
split_size = math.floor(fraction * len(parent1))
return ''.join([parent1[:split_size], parent2[split_size:]])
def _generate_population(path_points, obstacles, path_validity):
population_size = int(parser['Genetic Algorithm']['population_size'])
population = []
print('Generating initial population, please wait ....')
for i in range(population_size):
while True:
chromosome = _generate_chromosome(path_points, path_validity)
if chromosome:
break
population.append(chromosome)
print('Successfully created initial population')
print('Simulating genetic algorithm for path planning .... (Press Ctrl+C to stop)')
return population
def _generate_chromosome(path_points, path_validity):
chromosome = '1' # source is always visited
previous_path_point = path_points[0] # keep track of the previous path point that was 1
for i in range(1, len(path_points)):
path_point = path_points[i]
if i == (len(path_points) - 1) and not path_validity[previous_path_point][i]:
return False
if path_validity[previous_path_point][i]:
if i == (len(path_points) - 1):
gene = '1'
else:
gene = '0' if randint(1, 10) > 5 else '1'
if gene == '1':
previous_path_point = path_point
chromosome += gene
else:
chromosome += '0'
return chromosome
def _chromosome_valid(chromosome, obstacles, path_points):
path_point_1, path_point_2 = (), ()
for i, gene in enumerate(chromosome):
if gene == '1':
if not path_point_1:
path_point_1 = path_points[i]
else:
path_point_2 = path_points[i]
if path_point_1 and path_point_2:
if path_overlaps_obstacle(path_point_1, path_point_2, obstacles):
return False
path_point_1 = path_point_2
path_point_2 = ()
return True
def path_overlaps_obstacle(path_point_1, path_point_2, obstacles):
path = LineString([path_point_1, path_point_2])
for obstacle in obstacles:
obstacle = Polygon(obstacle)
if path.intersects(obstacle):
return True
return False
def _calculate_path_length(chromosome, path_points):
path_point_1, path_point_2 = (), ()
length = 0
for i, gene in enumerate(chromosome):
if gene == '1':
last_path_point = path_points[i]
if not path_point_1:
path_point_1 = path_points[i]
else:
path_point_2 = path_points[i]
if path_point_1 and path_point_2:
length += _distance(path_point_1, path_point_2)
path_point_1 = path_point_2
path_point_2 = ()
return length
def _distance(path_point_1, path_point_2):
return math.sqrt( (path_point_2[0] - path_point_1[0])**2 + (path_point_2[1] - path_point_1[1])**2 )