Repository navigation
Expand file tree
/
Copy pathgenetic_algorithm.py
More file actions
58 lines (46 loc) · 1.56 KB
/
Copy pathgenetic_algorithm.py
File metadata and controls
58 lines (46 loc) · 1.56 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
__author__ = 'keyvan'
from utility.evolutionary import *
BIT_STRING_LENGTH = 32
ALL_BITS_ONE = 2**BIT_STRING_LENGTH - 1
class BitStringIndividualBase(IndividualBase):
def __init__(self, _binary_chromosome=0):
self.chromosome = _binary_chromosome
@staticmethod
def randomly_initialised():
instance = BitStringIndividualBase()
instance.chromosome = randrange(0,ALL_BITS_ONE)
return instance
loci = range(BIT_STRING_LENGTH)
def __fitness__(self):
result = 0
for i in range(BIT_STRING_LENGTH):
if self[i] == 1:
result += 1
return result
def __getitem__(self, index):
if self.chromosome & 2**index == 0:
return 0
return 1
def __setitem__(self, index, value):
if value == 1:
self.chromosome |= 2**index
else:
self.chromosome &= ALL_BITS_ONE ^ 2**index
def __mutate__(self):
mask = 2**randrange(0, BIT_STRING_LENGTH)
return BitStringIndividualBase(self.chromosome ^ mask)
def __repr__(self):
result = ''
for i in range(BIT_STRING_LENGTH):
result = str(self[i]) + result
return result
if __name__ == '__main__':
population = []
for i in range(32):
population.append(BitStringIndividualBase.randomly_initialised())
ga = GeneticAlgorithm(population)
for i in range(10):
fittest = ga.step(0, 1)
print fittest, fittest.fitness
print 'Solution found:'
print ga.fittest_individual_found, ga.highest_fitness_found