Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11bde60

Browse files
antmarakisnorvig
authored andcommitted
Genetic Algorithm: String to List Individuals (#523)
* Update search.py * Update test_search.py
1 parent 01e4450 commit 11bde60

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

search.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def genetic_search(problem, fitness_fn, ngen=1000, pmut=0.1, n=20):
582582
return genetic_algorithm(states[:n], problem.value, ngen, pmut)
583583

584584

585-
def genetic_algorithm(population, fitness_fn, gene_pool=['0', '1'], f_thres=None, ngen=1000, pmut=0.1): # noqa
585+
def genetic_algorithm(population, fitness_fn, gene_pool=[0, 1], f_thres=None, ngen=1000, pmut=0.1): # noqa
586586
"""[Figure 4.8]"""
587587
for i in range(ngen):
588588
new_population = []
@@ -610,13 +610,11 @@ def init_population(pop_number, gene_pool, state_length):
610610
"""Initializes population for genetic algorithm
611611
pop_number : Number of individuals in population
612612
gene_pool : List of possible values for individuals
613-
(char only)
614613
state_length: The length of each individual"""
615614
g = len(gene_pool)
616615
population = []
617616
for i in range(pop_number):
618-
new_individual = ''.join([gene_pool[random.randrange(0, g)]
619-
for j in range(state_length)])
617+
new_individual = [gene_pool[random.randrange(0, g)] for j in range(state_length)]
620618
population.append(new_individual)
621619

622620
return population
@@ -635,7 +633,7 @@ def mutate(x, gene_pool):
635633
r = random.randrange(0, g)
636634

637635
new_gene = gene_pool[r]
638-
return x[:c] + new_gene + x[c+1:]
636+
return x[:c] + [new_gene] + x[c+1:]
639637

640638
# _____________________________________________________________________________
641639
# The remainder of this file implements examples for the search algorithms.

tests/test_search.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,21 @@ def test_genetic_algorithm():
9696
'D': [2, 3]
9797
}
9898

99-
population = init_population(8, ['0', '1'], 4)
100-
10199
def fitness(c):
102100
return sum(c[n1] != c[n2] for (n1, n2) in edges.values())
103101

104-
solution = genetic_algorithm(population, fitness)
105-
assert solution == "0101" or solution == "1010"
102+
solution_chars = GA_GraphColoringChars(edges, fitness)
103+
assert solution_chars == ['R', 'G', 'R', 'G'] or solution_chars == ['G', 'R', 'G', 'R']
104+
105+
solution_bools = GA_GraphColoringBools(edges, fitness)
106+
assert solution_bools == [True, False, True, False] or solution_bools == [False, True, False, True]
107+
108+
solution_ints = GA_GraphColoringInts(edges, fitness)
109+
assert solution_ints == [0, 1, 0, 1] or solution_ints == [1, 0, 1, 0]
106110

107111
# Queens Problem
108-
population = init_population(100, [str(i) for i in range(8)], 8)
112+
gene_pool = range(8)
113+
population = init_population(100, gene_pool, 8)
109114

110115
def fitness(q):
111116
non_attacking = 0
@@ -122,10 +127,31 @@ def fitness(q):
122127
return non_attacking
123128

124129

125-
solution = genetic_algorithm(population, fitness, f_thres=25)
130+
solution = genetic_algorithm(population, fitness, gene_pool=gene_pool, f_thres=25)
126131
assert fitness(solution) >= 25
127132

128133

134+
def GA_GraphColoringChars(edges, fitness):
135+
gene_pool = ['R', 'G']
136+
population = init_population(8, gene_pool, 4)
137+
138+
return genetic_algorithm(population, fitness, gene_pool=gene_pool)
139+
140+
141+
def GA_GraphColoringBools(edges, fitness):
142+
gene_pool = [True, False]
143+
population = init_population(8, gene_pool, 4)
144+
145+
return genetic_algorithm(population, fitness, gene_pool=gene_pool)
146+
147+
148+
def GA_GraphColoringInts(edges, fitness):
149+
population = init_population(8, [0, 1], 4)
150+
151+
return genetic_algorithm(population, fitness)
152+
153+
154+
129155
# TODO: for .ipynb:
130156
"""
131157
>>> compare_graph_searchers()

0 commit comments

Comments
 (0)