-
Notifications
You must be signed in to change notification settings - Fork 5
/
bga.py
191 lines (163 loc) · 6.66 KB
/
bga.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
#!usr/bin/env python
"""
Version : 0.0.1
Date : 22th Nov. 2017
Author : Bigzhao Tan
Email : [email protected]
Affiliation : SCUT
Status : Not Under Active Development
Description :
A simple GA implement with python. It can be used to solve binary combinatorial optimization problem.
"""
import numpy as np
import pandas as pd
import random
__author__ = "Bigzhao Tan"
__email__ = "[email protected]"
__version__ = "0.0.1"
class BGA():
"""
Simple 0-1 genetic algorithm.
User Guide:
>> test = GA(pop_shape=(10, 10), method=np.sum)
>> solution, fitness = test.run()
"""
def __init__(self, pop_shape, method, p_c=0.8, p_m=0.2, max_round = 1000, early_stop_rounds=None, verbose = None, maximum=True):
"""
Args:
pop_shape: The shape of the population matrix.
method: User-defined medthod to evaluate the single individual among the population.
Example:
def method(arr): # arr is a individual array
return np.sum(arr)
p_c: The probability of crossover.
p_m: The probability of mutation.
max_round: The maximun number of evolutionary rounds.
early_stop_rounds: Default is None and must smaller than max_round.
verbose: 'None' for not printing progress messages. int type number for printing messages every n iterations.
maximum: 'True' for finding the maximum value while 'False' for finding the minimum value.
"""
if early_stop_rounds != None:
assert(max_round > early_stop_rounds)
self.pop_shape = pop_shape
self.method = method
self.pop = np.zeros(pop_shape)
self.fitness = np.zeros(pop_shape[0])
self.p_c = p_c
self.p_m = p_m
self.max_round = max_round
self.early_stop_rounds = early_stop_rounds
self.verbose = verbose
self.maximum = maximum
def evaluation(self, pop):
"""
Computing the fitness of the input popluation matrix.
Args:
p: The population matrix need to be evaluated.
"""
return np.array([self.method(i) for i in pop])
def initialization(self):
"""
Initalizing the population which shape is self.pop_shape(0-1 matrix).
"""
self.pop = np.random.randint(low=0, high=2, size=self.pop_shape)
self.fitness = self.evaluation(self.pop)
def crossover(self, ind_0, ind_1):
"""
Single point crossover.
Args:
ind_0: individual_0
ind_1: individual_1
Ret:
new_0, new_1: the individuals generatd after crossover.
"""
assert(len(ind_0) == len(ind_1))
point = np.random.randint(len(ind_0))
# new_0, new_1 = np.zeros(len(ind_0)), np.zeros(len(ind_0))
new_0 = np.hstack((ind_0[:point], ind_1[point:]))
new_1 = np.hstack((ind_1[:point], ind_0[point:]))
assert(len(new_0) == len(ind_0))
return new_0, new_1
def mutation(self, indi):
"""
Simple mutation.
Arg:
indi: individual to mutation.
"""
point = np.random.randint(len(indi))
indi[point] = 1 - indi[point]
return indi
def rws(self, size, fitness):
"""
Roulette Wheel Selection.
Args:
size: the size of individuals you want to select according to their fitness.
fitness: the fitness of population you want to apply rws to.
"""
if self.maximum:
fitness_ = fitness
else:
fitness_ = 1.0 / fitness
# fitness_ = fitness
idx = np.random.choice(np.arange(len(fitness_)), size=size, replace=True,
p=fitness_/fitness_.sum()) # p 就是选它的比例
return idx
def run(self):
"""
Run the genetic algorithm.
Ret:
global_best_ind: The best indiviudal during the evolutionary process.
global_best_fitness: The fitness of the global_best_ind.
"""
global_best = 0
self.initialization()
best_index = np.argsort(self.fitness)[0]
global_best_fitness = self.fitness[best_index]
global_best_ind = self.pop[best_index, :]
eva_times = self.pop_shape[0]
count = 0
for it in range(self.max_round):
next_gene = []
for n in range(int(self.pop_shape[0]/2)):
i, j = self.rws(2, self.fitness) # choosing 2 individuals with rws.
indi_0, indi_1 = self.pop[i, :].copy(), self.pop[j, :].copy()
if np.random.rand() < self.p_c:
indi_0, indi_1 = self.crossover(indi_0, indi_1)
if np.random.rand() < self.p_m:
indi_0 = self.mutation(indi_0)
indi_1 = self.mutation(indi_1)
next_gene.append(indi_0)
next_gene.append(indi_1)
self.pop = np.array(next_gene)
self.fitness = self.evaluation(self.pop)
eva_times += self.pop_shape[0]
if self.maximum:
if np.max(self.fitness) > global_best_fitness:
best_index = np.argsort(self.fitness)[-1]
global_best_fitness = self.fitness[best_index]
global_best_ind = self.pop[best_index, :]
count = 0
else:
count +=1
worst_index = np.argsort(self.fitness)[-1]
self.pop[worst_index, :] = global_best_ind
self.fitness[worst_index] = global_best_fitness
else:
if np.min(self.fitness) < global_best_fitness:
best_index = np.argsort(self.fitness)[0]
global_best_fitness = self.fitness[best_index]
global_best_ind = self.pop[best_index, :]
count = 0
else:
count +=1
worst_index = np.argsort(self.fitness)[-1]
self.pop[worst_index, :] = global_best_ind
self.fitness[worst_index] = global_best_fitness
if self.verbose != None and 0 == (it % self.verbose):
print('Gene {}:'.format(it))
print('Global best fitness:', global_best_fitness)
if self.early_stop_rounds != None and count > self.early_stop_rounds:
print('Did not improved within {} rounds. Break.'.format(self.early_stop_rounds))
break
print('\n Solution: {} \n Fitness: {} \n Evaluation times: {}'.format(global_best_ind, global_best_fitness, eva_times))
return global_best_ind, global_best_fitness