track_final_chromosome = []

# Must rename this to match what you are testing
for def_num_populations in param_change:

    # Start timer at current time
    start_time = time.time()

    # Create the MP GA object
    mp_ga = MPGeneticAlgorithm(
        input_data=seed,
        fitness_function=sphere,
        num_genes=50,
        num_populations=def_num_populations,
        population_size=def_population_size,
        generations=def_generations,
        crossover_probability=def_crossover_probability,
        mutation_probability=def_mutation_probability,
        migration_probability=def_migration_probability,
        migration_frequency=def_migration_frequency,
        elitism_ratio=def_elitism_ratio,
    )

    # Run the MP GA
    mp_ga.run()
    # Get the best Chromosome from the MP GA run
    best_chromosomes = mp_ga.get_best_chromosomes()

    # Print the execution time of the MP GA
    run_time = time.time() - start_time
    print("Run Time: %s seconds" % (run_time))
param_change = [10, 20, 30, 40, 50, 60, 70, 80, 90]

track_run_time = []
track_final_fit_value = []
track_max_fitness = []
track_final_chromosome = []

for param in param_change:
    # Create the MP GA object
    mp_ga = MPGeneticAlgorithm(
        input_data=[param] * num_populations,
        fitness_function=fitness_function,
        num_genes=num_genes,
        num_populations=num_populations,
        population_size=population_size,
        generations=generations,
        crossover_probability=crossover_probability,
        mutation_probability=mutation_probability,
        migration_probability=migration_probability,
        migration_frequency=migration_frequency,
        elitism_ratio=elitism_ratio,
    )

    # Run the MP GA
    mp_ga.run()
    # Get the best Chromosome from the MP GA run
    best_chromosomes = mp_ga.get_best_chromosomes()

    # Print the execution time of the MP GA
    run_time = time.time() - start_time
    print("Run Time: %s seconds\n" % run_time)
예제 #3
0
# Values refer to the PERCENTAGE of a one in a Chromosome for each population
# IMPORTANT: Each value must be between 0 and 100
seed = [10, 20, 30, 40, 50]

# Start timer at current time
start_time = time.time()

# Create the MP GA object
mp_ga = MPGeneticAlgorithm(
    input_data=seed,
    fitness_function=sphere,
    num_genes=50,
    num_populations=5,
    population_size=50,
    generations=100,
    crossover_probability=0.8,
    mutation_probability=0.01,
    migration_probability=0.1,
    migration_frequency=1,
    elitism_ratio=0.02,
)

# Run the MP GA
mp_ga.run()
# Get the best Chromosome from the MP GA run
best_chromosomes = mp_ga.get_best_chromosomes()

# Print the execution time of the MP GA
print("Run Time: %s seconds" % (time.time() - start_time))