-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_algorithm_engine.py
More file actions
145 lines (114 loc) · 5.53 KB
/
Copy pathgenetic_algorithm_engine.py
File metadata and controls
145 lines (114 loc) · 5.53 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
import numpy as np
import pandas as pd
from Individual import Individual
import math
import random
import matplotlib.pyplot as plt
class GA:
def __init__(self,M_G=50,P_S = 7):
self.Max_Generation = int(M_G)
self.Population_Size = int(P_S)
self.Crossover_Percentage = float(0.8)
self.Mutation_Percentage = float(0.2)
self._number_Offspringe = int(2*round((self.Crossover_Percentage*self.Population_Size)/2))
self._number_Mutants = int(round(self.Mutation_Percentage*self.Population_Size))
self.Population = np.array([Individual() for i in range(self.Population_Size)])
self.sort(len(self.Population))
self.list_fitness = [self.Population[i].fitness for i in range(self.Population_Size)]
self.information = dict(
Best_Individual = [self.Population[0].ind],
Best_fitness = [self.Population[0].fitness],
mean_fitness =list()
)
def main(self):
for i in range(self.Max_Generation):
Parent_Percentage= [math.exp(2*(k/np.max(self.list_fitness))) for k in self.list_fitness]
Parent_Percentage = Parent_Percentage / np.sum(Parent_Percentage)
pc = self.crossover(Parent_Percentage)
if self.probably_mutation():
pm = self.mutation()
self.Population = np.concatenate((self.Population, pc,pm))
del pc ,pm
else:
self.Population = np.concatenate((self.Population, pc))
del pc
self.sort(len(self.Population))
self.Population = self.Population[:self.Population_Size+1]
self.list_fitness = [self.Population[i].fitness for i in range(self.Population_Size)]
self.information['Best_Individual'].append(self.Population[0].ind)
self.information['Best_fitness'].append(self.Population[0].fitness)
self.information['mean_fitness'].append(self.mean_fitness())
self.show_Population(self.Population,7)
print()
#print(self.information['Best_Individual'][len(self.information['Best_Individual'])-1])
self.showPlot()
def sort(self,new_size):
for i in range(new_size):
for j in range(new_size-i-1):
if self.Population[j]<self.Population[j+1]:
self.Population[j],self.Population[j+1] = self.Population[j+1] , self.Population[j]
def probably_mutation(self):
return np.random.rand() <= 0.2
def mean_fitness(self):
count=0
for i in range(self.Population_Size):
count= count+ self.Population[i].fitness
count= count / self.Population_Size
return count
def show_Population(self,pop,s):
for i in range(s):
print(pop[i].ind,pop[i].fitness)
def showPlot(self):
g = [i for i in range(self.Max_Generation)]
plt.plot(g, self.information['mean_fitness'], label='My Line')
plt.title('Line Plot Example')
plt.xlabel('Generation')
plt.ylabel('Fitness')
plt.legend()
plt.show()
def _doublePointCrossover(self,p1,p2):
#nvar = len(p1) - 1 # or len(p2)
position_cut = random.sample(range(1, len(p1)), 2)
c1 = min(position_cut)
c2 = max(position_cut)
offspring1 = np.concatenate((p1[:c1],p2[c1:c2],p1[c2:]))
offspring2 = np.concatenate((p2[:c1],p1[c1:c2],p2[c2:]))
return list([offspring1,offspring2])
def _singlePointCrossover(self,p1,p2):
#nvar = len(p1) - 1 # or len(p2)
position_cut = random.randint(1,len(p1))
offspring1 = np.concatenate((p1[:position_cut],p2[position_cut:]))
offspring2 = np.concatenate((p2[:position_cut],p1[position_cut:]))
return list([offspring1,offspring2])
def RoulettewheelSelection(self,p):
random_i = np.random.rand()
cumsum = np.cumsum(p)
for i in cumsum:
if random_i<=i:
return np.where(cumsum == i)[0][0]
def crossover(self,p):
Population_crossover = np.array([Individual() for i in range(self._number_Offspringe)])
for i in range(0,self._number_Offspringe,2):
rand_Individual_1 = self.RoulettewheelSelection(p)
parent1 = self.Population[rand_Individual_1]
rand_Individual_2 = self.RoulettewheelSelection(p)
parent2 = self.Population[rand_Individual_2]
r1= self._doublePointCrossover(parent1.ind, parent2.ind)
Population_crossover[i].Set_Posision(r1[0])
Population_crossover[i].Set_fitness()
Population_crossover[i+1].Set_Posision(r1[1])
Population_crossover[i+1].Set_fitness()
return Population_crossover
def _mutants(self,p):
position_mutants = np.random.randint(0,len(p))
mutants = p.copy()
mutants[position_mutants] = 1-p[position_mutants]
return mutants
def mutation(self):
Population_mutation = np.array([Individual() for i in range(self._number_Mutants)])
for i in range(self._number_Mutants):
rand_Individual = np.random.randint(0,self.Population_Size,dtype = int)
parent = self.Population[rand_Individual]
Population_mutation[i].Set_Posision(self._mutants(parent.ind))
Population_mutation[i].Set_fitness()
return Population_mutation