diff --git a/search.py b/search.py index d104d7793..4177cd07e 100644 --- a/search.py +++ b/search.py @@ -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 = [] @@ -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 @@ -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. diff --git a/tests/test_search.py b/tests/test_search.py index ebc02b5ab..d07edb31e 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -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 @@ -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()