コード例 #1
0
def optimize_route(objects, generations, crossover_chance, mutation_chance,
                   population):
    """
    Return list of points ordered by genetic algorithm.
    """
    generator = RouteGenerator(objects)
    ga = GeneticAlgorithm(
        generator, crossover_chance, mutation_chance, population
    )

    print '\nINITIAL ROUTES'
    print ', '.join([
        str(int(route.get_length())) for route in ga.get_solutions()
    ])

    # initial = [route.get_length() for route in ga.get_solutions()]
    # best_initial = min(initial)
    # while ga.get_best_solution().get_length() > best_initial * 0.5:
    #     # step condition instead of generations number
    #     # print ga.get_best_solution().get_length(), best_initial
    #     ga.evolve()

    for _ in range(generations):
        ga.evolve()

    print '\nLAST GENERATION'
    print ', '.join([
        str(int(route.get_length())) for route in ga.get_solutions()
    ])

    best = ga.get_best_solution()
    print '\nLENGTH OF ROUTE AFTER OPTIMIZATION'
    print int(best.get_length())

    return best.objects
コード例 #2
0
def optimize_route(objects, generations, crossover_chance, mutation_chance,
                   population):
    """
    Return list of points ordered by genetic algorithm.
    """
    generator = RouteGenerator(objects)
    ga = GeneticAlgorithm(generator, crossover_chance, mutation_chance,
                          population)

    print '\nINITIAL ROUTES'
    print ', '.join(
        [str(int(route.get_length())) for route in ga.get_solutions()])

    # initial = [route.get_length() for route in ga.get_solutions()]
    # best_initial = min(initial)
    # while ga.get_best_solution().get_length() > best_initial * 0.5:
    #     # step condition instead of generations number
    #     # print ga.get_best_solution().get_length(), best_initial
    #     ga.evolve()

    for _ in range(generations):
        ga.evolve()

    print '\nLAST GENERATION'
    print ', '.join(
        [str(int(route.get_length())) for route in ga.get_solutions()])

    best = ga.get_best_solution()
    print '\nLENGTH OF ROUTE AFTER OPTIMIZATION'
    print int(best.get_length())

    return best.objects
コード例 #3
0
def main():
    # Define objective class
    objective_class = Objective_function(func1, 2, lo_bounds1, up_bounds1)

    #set PSO optimizer
    ga = GeneticAlgorithm(nb_generations=20,
                          nb_populations=10,
                          objective_class=objective_class)

    ga.evolve()
    for i in ga.populations:
        print(i.fitness)