-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenetic_Algorithm_c.pyx
More file actions
174 lines (157 loc) · 6.38 KB
/
Genetic_Algorithm_c.pyx
File metadata and controls
174 lines (157 loc) · 6.38 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
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
# -*- coding: utf-8 -*-
import random
import copy
import operator
from Entity_c import Population
MAX_GENERATION = 1
def get_unvisit_client(specimen, graph):
allVertex = []
for path in specimen:
allVertex.extend(path._vertex)
return list(set(graph._vertex) - set(allVertex))
class GeneticAlgorithm(object):
def __init__(self, mutation=0.2, elite=0.1):
self.__mutagen = mutation
self.__elite = elite
self._population = Population()
self._newGeneration = Population()
def create_population(self, workers, graph):
self._population.create_area(workers, graph)
def mutation(self, graph):
specimens = random.sample(self._newGeneration.flock.keys(), int(self._newGeneration.get_count() * self.__mutagen))
mutateType = [self.__swapIn, self.__delete, self.__insert, self.__replacement]
for specimen in specimens:
newSpecimen = random.choice(mutateType)(specimen, graph)
del(self._newGeneration.flock[specimen])
self._newGeneration.add_spiec(newSpecimen)
self._newGeneration.add_spiec(copy.deepcopy(sorted(self._population.flock.iteritems(), key=operator.itemgetter(1))[-1][0]))
for k in self._newGeneration:
self._population.add_spiec(k)
def __replacement(self, specimen, *args):
"""
Заменяет в одном из путей клиента на одного из необслуженных
@param specimen Обход графа, который необходимо мутировать
@param graph исходный граф
@return Нового члена общества
"""
unVisit = get_unvisit_client(specimen, args[0])
stop = 0
while stop < 30 and unVisit:
subject = copy.deepcopy(specimen)
replVertex = random.choice(unVisit)
changePath = random.choice(subject.route.values())
if changePath.get_len():
delVertex = random.choice(changePath._vertex[1:-1])
index = changePath._vertex.index(delVertex)
changePath.remove_vertex(delVertex)
changePath.insert_vertex(index, replVertex)
else:
changePath.insert_vertex(1, replVertex)
if subject.is_valid():
return subject
stop += 1
return specimen
def __delete(self, specimen, *args):
stop = 0
while stop < 30 and specimen.get_client_count():
experimental = copy.deepcopy(specimen)
path = random.choice(experimental.route.values())
if path.get_len():
vertex = random.choice(path._vertex[1:-1])
path.remove_vertex(vertex)
return experimental
stop += 1
return specimen
def __insert(self, specimen, *args):
"""
Мутация путем вставки в путь необслуженного клиента
@param specimen Обход графа, который необходимо мутировать
@param args Необходим для совмещения с другими функциями мутации
@return Нового члена общества
"""
unVisit = get_unvisit_client(specimen, args[0])
stop = 0
while stop < 30 and unVisit:
subject = copy.deepcopy(specimen)
insPath = random.choice(subject.route.values())
insVertex = random.choice(unVisit)
index = random.choice([x for x in xrange(1, insPath.get_len() + 1)]) if insPath.get_len() else 1
insPath.insert_vertex(index, insVertex)
if subject.is_valid():
return subject
stop += 1
return specimen
def __swapIn(self, specimen, *args):
"""
Мутация путем обмена клиентов между путями монтеров
@param specimen Обход графа, который необходимо мутировать
@param args Необходим для совмещения с другими функциями мутации
@return Нового члена общества
"""
stop = 0
while True and specimen.get_client_count():
mutateVertex = []
if stop > 30:
break
choiceRoute = copy.deepcopy(specimen.route.values())
for i in xrange(2):
path = random.choice(choiceRoute)
if path.get_len():
choiceVertex = random.choice(path._vertex[1:-1])
choiceRoute.remove(path)
mutateVertex.append(choiceVertex)
else:
mutateVertex.append(None)
if len(list(set(mutateVertex))) > 1:
experimental = copy.deepcopy(specimen)
experimental.swap(mutateVertex)
if experimental.is_valid():
return experimental
stop += 1
return specimen
def crossover(self, parents):
"""
BCRC оператор скрещивания
@param parents Список с родителями
@return список новых потомка
"""
childCnt = len(parents)/2
offspring = []
for i in xrange(childCnt):
childs = copy.deepcopy(random.sample(parents, 2))
if random.random() > 0.85:
parents = list(set(parents) - set(childs))
replacePath = [copy.deepcopy(random.choice(childs[0].route.values())),
copy.deepcopy(random.choice(childs[1].route.values()))]
for i,j in zip(replacePath[0]._vertex[1:-1], replacePath[1]._vertex[1:-1]):
childs[1].remove_vertex(i)
childs[0].remove_vertex(j)
replaceVertex = replacePath[0]._vertex[1:-1] + replacePath[1]._vertex[1:-1]
for vertex in replaceVertex:
for individ in childs:
if not individ.has_vertex(vertex):
individ.insert_vertex(vertex)
offspring.extend(childs)
return offspring
def selection(self):
delSpecimen = [x[0] for x in sorted(self._population.flock.items(), key=lambda x: x[1])][:-self._population._size]
for specimen in delSpecimen:
del(self._population.flock[specimen])
def run(self, workers, graph):
#print 'start algorithm'
self.create_population(workers, graph)
#print 'END create population'
for j in xrange(MAX_GENERATION):
#print 'Generation: ' + str(j)
parents = self._population.get_parents()
childs = self.crossover(parents)
#print 'End crossover'
for child in childs:
self._newGeneration.add_spiec(child)
#print 'Start Mutation'
self.mutation(graph)
#print 'end mutation'
self.selection()
#print 'end selection'
#print '=========================='
return sorted(self._population.flock.iteritems(), key=operator.itemgetter(1))[-1]