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

Skip to content

Genetic Algorithm: String to List Individuals #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def genetic_search(problem, fitness_fn, ngen=1000, pmut=0.1, n=20):
return genetic_algorithm(states[:n], problem.value, ngen, pmut)


def genetic_algorithm(population, fitness_fn, gene_pool=['0', '1'], f_thres=None, ngen=1000, pmut=0.1): # noqa
def genetic_algorithm(population, fitness_fn, gene_pool=[0, 1], f_thres=None, ngen=1000, pmut=0.1): # noqa
"""[Figure 4.8]"""
for i in range(ngen):
new_population = []
Expand Down Expand Up @@ -610,13 +610,11 @@ def init_population(pop_number, gene_pool, state_length):
"""Initializes population for genetic algorithm
pop_number : Number of individuals in population
gene_pool : List of possible values for individuals
(char only)
state_length: The length of each individual"""
g = len(gene_pool)
population = []
for i in range(pop_number):
new_individual = ''.join([gene_pool[random.randrange(0, g)]
for j in range(state_length)])
new_individual = [gene_pool[random.randrange(0, g)] for j in range(state_length)]
population.append(new_individual)

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

new_gene = gene_pool[r]
return x[:c] + new_gene + x[c+1:]
return x[:c] + [new_gene] + x[c+1:]

# _____________________________________________________________________________
# The remainder of this file implements examples for the search algorithms.
Expand Down
38 changes: 32 additions & 6 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,21 @@ def test_genetic_algorithm():
'D': [2, 3]
}

population = init_population(8, ['0', '1'], 4)

def fitness(c):
return sum(c[n1] != c[n2] for (n1, n2) in edges.values())

solution = genetic_algorithm(population, fitness)
assert solution == "0101" or solution == "1010"
solution_chars = GA_GraphColoringChars(edges, fitness)
assert solution_chars == ['R', 'G', 'R', 'G'] or solution_chars == ['G', 'R', 'G', 'R']

solution_bools = GA_GraphColoringBools(edges, fitness)
assert solution_bools == [True, False, True, False] or solution_bools == [False, True, False, True]

solution_ints = GA_GraphColoringInts(edges, fitness)
assert solution_ints == [0, 1, 0, 1] or solution_ints == [1, 0, 1, 0]

# Queens Problem
population = init_population(100, [str(i) for i in range(8)], 8)
gene_pool = range(8)
population = init_population(100, gene_pool, 8)

def fitness(q):
non_attacking = 0
Expand All @@ -122,10 +127,31 @@ def fitness(q):
return non_attacking


solution = genetic_algorithm(population, fitness, f_thres=25)
solution = genetic_algorithm(population, fitness, gene_pool=gene_pool, f_thres=25)
assert fitness(solution) >= 25


def GA_GraphColoringChars(edges, fitness):
gene_pool = ['R', 'G']
population = init_population(8, gene_pool, 4)

return genetic_algorithm(population, fitness, gene_pool=gene_pool)


def GA_GraphColoringBools(edges, fitness):
gene_pool = [True, False]
population = init_population(8, gene_pool, 4)

return genetic_algorithm(population, fitness, gene_pool=gene_pool)


def GA_GraphColoringInts(edges, fitness):
population = init_population(8, [0, 1], 4)

return genetic_algorithm(population, fitness)



# TODO: for .ipynb:
"""
>>> compare_graph_searchers()
Expand Down