Exemple #1
0
    operators += ugp4.GenOperator(ugp4.create_random_individual, 0)
    operators += ugp4.GenOperator(ugp4.macro_pool_uniform_crossover, 2)

    # Create the object that will manage the evolution__________________________________________________________________
    mu = 10
    nu = 20
    strength = 0.5
    lambda_ = 7
    max_age = 10

    for _ in range(1):
        darwin = ugp4.Darwin(
            constraints=library,
            operators=operators,
            mu=mu,
            nu=nu,
            lambda_=lambda_,
            strength=strength,
            max_age=max_age,
        )

        # Evolve____________________________________________________________________________________________________________
        darwin.evolve()
        logging.bare("This is the final population:")
        for individual in darwin.population:
            ugp4.print_individual(individual)
            ugp4.logging.bare(individual.fitness)
            ugp4.logging.bare("")

        # Print best individuals
        logging.bare("These are the best ever individuals:")
Exemple #2
0
    # Fitness (simple)
    library.evaluator = ugp4.fitness.make_evaluator(
        evaluator=lambda s: s.count('1'), fitness_type=ugp4.fitness.Simple)

    # Create a list of operators with their aritiy
    operators = ugp4.Operators()
    # Add initialization operators
    operators += ugp4.GenOperator(ugp4.create_random_individual, 0)
    # Add mutation operators
    operators += ugp4.GenOperator(ugp4.hierarchical_mutation, 1)
    operators += ugp4.GenOperator(ugp4.flat_mutation, 1)

    # Evolution core
    darwin = ugp4.Darwin(constraints=library,
                         operators=operators,
                         mu=50,
                         nu=50,
                         lambda_=10,
                         strength=.7,
                         max_age=50,
                         max_generations=10)
    darwin.evolve()

    # That's all
    ugp4.logging.bare("Final archive:")
    for i in darwin.archive.individuals:
        ugp4.logging.bare(f"{i.id}: {i} [{i.fitness}]")

    ugp4.logging.log_cpu(ugp4.logging.INFO, "Program completed")
    sys.exit(0)