예제 #1
0
def main(arguments):
    trainer = Trainer(arguments.path, arguments.log)
    ga = GeneticAlgorithm({
        'model_arch': ['conv->lstm', 'lstm->conv', 'conv', 'lstm'],
        'optimizer': ['adam', 'sgd', 'RMSprop'],
        'conv_filters': [4, 8, 16, 32, 64, 128],
        'conv_layers': [1, 2, 3, 5],
        'conv_filter_size': [3, 5],
        'use_max_pooling': [False, True],
        'activation': ['relu', 'tanh', 'sigmoid'],
        'lstm_cells': [4, 8, 16, 32, 64, 128],
        'lstm_layers': [1, 2],
        'loss': ['mse', huber_loss_mean],

        'epochs': [10, 20, 30, 40, 50],
        'batch_size': [32, 64, 128, 256],
        'window_size': [5, 10, 20, 30, 40, 50],
        'num_derivatives': [0, 1, 2, 3, 4, 5],
        'scaler_class': [utils.StandardScaler, utils.MinMaxScaler, utils.NoScaler],
        'num_points_to_predict': [1, 2, 3]
    }, trainer.fitness, hall_of_fame=100)

    kwargs = ga.get_kwargs(ga.sample())

    kwargs['scaler_class'] = utils.StandardScaler
    kwargs['epochs'] = 10
    kwargs['model_arch'] = 'conv'
    kwargs['num_derivatives'] = 3
    kwargs['window_size'] = 30
    kwargs['epochs'] = 40
    kwargs['loss'] = huber_loss_mean
    kwargs['use_max_pooling'] = False
    kwargs['conv_filters'] = 64
    kwargs['conv_layers'] = 2
    kwargs['optimizer'] = 'adam'
    kwargs['batch_size'] = 128
    kwargs['num_points_to_predict'] = 1
    kwargs['activation'] = 'relu'
    kwargs['conv_filter_size'] = 5

    #trainer.fitness(**kwargs)

    ga.run(100, 100)
예제 #2
0
def fitness(params):
    try:
        traces = rocket_architectures.sim_3_boosters(**params)
    except Exception as ex:
        print(ex)
        return 0.0

    time, position, velocity, acceleration = traces
    speed = sqrt(np.sum(velocity * velocity, axis=1))
    accel = sqrt(np.sum(acceleration * acceleration, axis=1)) / 9.81  # in Gs
    max_speed = max(speed)
    max_acceleration = max(sqrt(np.sum(acceleration * acceleration, axis=1)))
    max_distance = np.max(position[:, 0])

    # print(f"Distance and speed: max height:{np.max(position[:,1]):0.1f}m, distance:{max_distance:0.0f}m, "
    #     + f"max speed:{max_speed:0.0f}m/s, {ms2kmh(max_speed):0.0f}km/h), acceleration:{max_acceleration/9.81:0.0f}g")

    return max_distance


ga = GeneticAlgorithm(params,
                      fitness,
                      population_size=40,
                      generations=30,
                      temperature_factor=0.91)
ga.run()

best_params = ga.get_best_params()
traces = rocket_architectures.sim_3_boosters(**best_params)
rocket_architectures.plot_basic(traces)
예제 #3
0
    structure = parse_raw_structure(params["raw_structure"])
    gen_alg = GeneticAlgorithm(
        structure,
        mfold_command=params["mfold_command"],
        population_size=int(params["population_size"]),
        iterations=int(params["iterations"]),
        mutation_rate=int(params["mutation_rate"]),
        boltzmann_factor=float(params["boltzmann_factor"]),
        initial_sequences=[
            Sequence(definition, structure)
            for definition in params["input_sequence_definitions"]
        ],
        fixed_regions=params['fixed_regions'])
    try:
        gen_alg.run()
    finally:
        print(len(gen_alg.diversity_history) - 1, " iterations completed")
        print("Diversity history: ", gen_alg.diversity_history)
        print("Fitness history: ", gen_alg.fitness_history)
        with open("diversity.dat", "w") as outfile:
            for diversity in gen_alg.diversity_history:
                outfile.write(str(diversity) + '\n')
        with open("fitness.dat", "w") as outfile:
            for fitness in gen_alg.fitness_history:
                outfile.write(str(fitness) + '\n')
        gen_alg.print_population()
        print("Best Sequences: \n")
        gen_alg.best_child.print()

    iterations = range(int(params["iterations"]))
import numpy as np
import time
import os
import pickle
import random

from genetic import GeneticAlgorithm
from environment import Course

import neat

# Generate obstacles
course = Course()
course_1 = course.popcornCourse(1.5)
course_2 = course.popcornCourse(11.5)
course_3 = course.avoidCourse()
courses = [course_1, course_2, course_3]

random.seed(1)
# Algorithm Paramaters
threshold = 0.  # Fitness Threshold
generation_limit = 30
population_size = 48  # Divisible by 4
mutation_variance = 1.5

# Load Algorithm Object
algorithm = GeneticAlgorithm(threshold, generation_limit, population_size,
                             mutation_variance, courses)
# Run Genetic Algorithm
algorithm.run()