예제 #1
0
 def test_evolve_storage(self):
     storage_instance = SqliteStorage("test.sqlite")
     protogene = ProtoGene(IntGene, 'a', min_value=0, max_value=10)
     protogenome = ProtoGenome([protogene], mutation_probability=0.1)
     algo = GeneticAlgorithm(protogenome, fitness_evaluator, 
         termination_criteria=convergence_stop, storage_instance=storage_instance)
     algo.evolve()
예제 #2
0
def test_3():
    pop_size = ProtoGene(DiscreteGene, "pop_size", alleles=[100, 200, 300])
    mutation_probability = ProtoGene(
        DiscreteGene, "mutation_probability", alleles=[0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5]
    )
    num_parents = ProtoGene(DiscreteGene, "num_parents", alleles=[2, 4, 8, 16])
    crossover_probability = ProtoGene(
        DiscreteGene, "crossover_probability", alleles=[0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    )
    crossover_method = ProtoGene(
        DiscreteGene, "crossover_method", alleles=[single_point_crossover, genome_add, genome_choose]
    )
    protogenome = ProtoGenome([pop_size, mutation_probability, num_parents, crossover_probability, crossover_method])

    algo = GeneticAlgorithm(
        protogenome,
        genetic_fitness,
        population_size=10,
        optimization_mode="min",
        termination_criteria=convergence_stop,
        termination_criteria_options={"num_generations": 5},
    )

    algo.evolve(debug=True)
    bi = algo.best_individual()
    print "*" * 10
    print bi.dict_value()
    return bi.score
예제 #3
0
def genetic_fitness(genome):
    values = genome.dict_value()

    pg_1 = ProtoGene(FloatGene, max_value=150.0, min_value=-150.0, mutation_max_step=5)
    pg_2 = ProtoGene(FloatGene, max_value=150.0, min_value=-150.0, mutation_max_step=5)
    protogenes = [pg_1, pg_2]
    protogenome = ProtoGenome(protogenes, mutation_probability=values["mutation_probability"])
    algo = GeneticAlgorithm(
        protogenome,
        schafferF6,
        population_size=values["pop_size"],
        optimization_mode="min",
        num_parents=values["num_parents"],
        # elitism = False,
        crossover_method=values["crossover_method"],
        crossover_probability=values["crossover_probability"],
        selection_method=select_from_top,
        termination_criteria=convergence_stop,
        termination_criteria_options={"num_generations": 5},
    )

    algo.evolve()
    bi = algo.best_individual()
    print bi.score
    return bi.score
예제 #4
0
 def test_evaluate_population(self):
     protogene = ProtoGene(IntGene, 'a', min_value=0, max_value=100)
     protogenome = ProtoGenome([protogene])
     algo = GeneticAlgorithm(protogenome, fitness_evaluator, termination_criteria=convergence_stop)
     algo.initialize()
     algo.evaluate_population()
     for individual in algo.population.individuals:
         assert individual.score == fitness_evaluator(individual)
예제 #5
0
 def test_evolve(self):
     protogene = ProtoGene(IntGene, 'a', min_value=0, max_value=10)
     protogenome = ProtoGenome([protogene], mutation_probability=0.1)
     algo = GeneticAlgorithm(
         protogenome, 
         fitness_evaluator, 
         termination_criteria=[raw_score_stop,convergence_stop],
         termination_criteria_options = [{'stop_score':0}]    
         )
     algo.evolve()
예제 #6
0
def test_6():   
    #not yet converging!
    return
    
    protogenes = []    
    for square in board.data.keys():
        row = square[0]
        col = square[1]
        value = board.data[(row, col)]
        if value:
            continue
            
        name = str(row) + "_" + str(col)
        alleles = range(1,10)
        
        numbers_in_row = [x for x in board.get_row(row) if x ]
        numbers_in_col = [x for x in board.get_col(col) if x ]
        numbers_in_square  = [x for x in board.get_square(row, col) if x]        

        alleles = [x for x in alleles if (x not in numbers_in_row)]
        alleles = [x for x in alleles if (x not in numbers_in_col)]
        alleles = [x for x in alleles if (x not in numbers_in_square)]
        
        protogenes.append(ProtoGene(DiscreteGene, name, alleles=alleles))
    

    protogenome = ProtoGenome(protogenes, mutation_probability=0.01)
    
    algo = GeneticAlgorithm(protogenome, sudoku_fitness, 
        population_size = 200,
        #num_populations=1,
        #isolated_cycles=8,
        optimization_mode = 'min',
        num_parents = 2,
        #elitism = False,
        crossover_method = single_point_crossover,
        crossover_probability=.5,
        selection_method = simple_tournament_select,
        termination_criteria=[raw_score_stop], termination_criteria_options=[{'stop_score':0}])
    
    
    algo.evolve(debug=True)
    bi = algo.best_individual()
    print bi.dict_value()
예제 #7
0
def test_1():
    
    protogenes = protogene_factory(IntGene, 'x', 50, min_value=0, max_value=10)
    protogenome = ProtoGenome(protogenes, mutation_probability = 0.1) 
    
    algo = GeneticAlgorithm(protogenome, fitness_function, 
        population_size = 200,
        optimization_mode = 'min',
        num_parents = 4,
        crossover_method = single_point_crossover,
        selection_method = select_from_top,
        termination_criteria=[raw_score_stop], termination_criteria_options=[{'stop_score':0}])
    
    #main evolution cycle
    algo.evolve(debug=True)

    bi = algo.best_individual()
    print bi.score
    print bi.dict_value()
예제 #8
0
def test_5():
    num_letters = len("genepi")
    protogenes = protogene_factory(CharGene, 'x', num_letters)
    protogenome = ProtoGenome(protogenes, mutation_probability = 0.05) 
    algo = GeneticAlgorithm(protogenome, findname, 
        population_size = 200,
        optimization_mode = 'min',
        num_parents = 8,
        #elitism = False,
        crossover_method = single_point_crossover,
        crossover_probability = 0.5,
        selection_method = select_from_top,
        termination_criteria = raw_score_stop,
        termination_criteria_options={'stop_score':0}
    )
        
    algo.evolve(debug=True)
    bi = algo.best_individual()
    print bi.score
    print "".join(bi.list_value())
예제 #9
0
 def test_evolve_population(self):
     protogene = ProtoGene(IntGene, 'a', min_value=0, max_value=10)
     protogenome = ProtoGenome([protogene])
     algo = GeneticAlgorithm(protogenome, fitness_evaluator, termination_criteria=convergence_stop)
     algo.initialize()
     algo.evaluate_population()
     g1 = algo.generation
     algo.evolve_population()
     g2 = algo.generation
     assert g1 == 0 and g2 == 1
예제 #10
0
def test_2():
    pg_1 = ProtoGene(FloatGene, max_value=150.0, min_value=-150.0, mutation_max_step=10)
    pg_2 = ProtoGene(FloatGene, max_value=150.0, min_value=-150.0, mutation_max_step=10)
    protogenes = [pg_1, pg_2]
    protogenome = ProtoGenome(protogenes, mutation_probability = 0.15) 
    algo = GeneticAlgorithm(protogenome, schafferF6, 
        population_size = 200,
        optimization_mode = 'min',
        num_parents = 8,
        #elitism = False,
        crossover_method = single_point_crossover,
        crossover_probability = 0.5,
        selection_method = roulette_select,
        termination_criteria = convergence_stop, 
        termination_criteria_options={'num_generations':20}
    )
        
    algo.evolve(debug=True)
    bi = algo.best_individual()
    print bi.score
    print bi.dict_value()
예제 #11
0
    def test_best_individual(self):
        protogene = ProtoGene(IntGene, 'a', min_value=0, max_value=100)
        protogenome = ProtoGenome([protogene])
        algo = GeneticAlgorithm(protogenome, fitness_evaluator, termination_criteria=convergence_stop)
        algo.initialize()
        for i, individual in enumerate(algo.population.individuals):
            individual.set_value('a', i)
        algo.evaluate_population()

        bi = algo.best_individual()
        assert bi.score == 0
예제 #12
0
 def test_should_terminate(self):
     protogene = ProtoGene(IntGene, 'a', min_value=0, max_value=100, value=1)
     protogenome = ProtoGenome([protogene], mutation_probability=1)
     algo = GeneticAlgorithm(protogenome, fitness_evaluator, termination_criteria=convergence_stop)
     algo.initialize()
     
     for individual in algo.population.individuals:
         individual.score = 1
     
     for x in range(11):
         stats = {'min_score' : 0}
         algo.population_stats.append(stats)
     algo.generation = 10
     st = algo.should_terminate()
     assert st == True
     
     algo.generation = 1
     st = algo.should_terminate()
     assert st == False
예제 #13
0
 def test_initialize(self):
     algo = GeneticAlgorithm(self.protogenome, fitness_evaluator)
     algo.initialize()