Example #1
0
def optimize(x0, syn_func, perturb_type, perturb, n_eval, run_id):
    # Optimize using GA
    n_best = 30
    n_random = 10
    n_children = 5
    chance_of_mutation = 0.1
    population_size = int((n_best + n_random) / 2 * n_children)
    population = generate_first_population(x0, population_size, perturb_type,
                                           perturb)
    best_inds = []
    best_perfs = []
    opt_perfs = [0]
    i = 0
    while 1:
        breeders, best_perf, best_individual = select(population, n_best,
                                                      n_random, syn_func)
        best_inds.append(best_individual)
        best_perfs.append(best_perf)
        opt_perfs += [np.max(best_perfs)
                      ] * population_size  # Best performance so far
        print('PARSEC-GA %d-%d: fittest %.2f' % (run_id, i + 1, best_perf))
        # No need to create next generation for the last generation
        if i < n_eval / population_size - 1:
            next_generation = create_children(breeders, n_children)
            population = mutate_population(next_generation, chance_of_mutation,
                                           perturb_type, perturb)
            i += 1
        else:
            break

    opt_x = best_inds[np.argmax(best_perfs)]
    opt_airfoil = synthesize(opt_x, n_points)
    print('Optimal CL/CD: {}'.format(opt_perfs[-1]))

    return opt_airfoil, opt_perfs
Example #2
0
def optimize(bounds, n_eval, run_id):
    # Optimize using BO
    n_pre_samples = 10
    kernel = gp.kernels.Matern()
    gp_model = gp.GaussianProcessRegressor(kernel=kernel,
                                           alpha=1e-4,
                                           n_restarts_optimizer=100,
                                           normalize_y=False)
    xs = []
    perfs = []
    opt_perfs = [0]
    for i in range(n_eval):
        if len(xs) < n_pre_samples:
            x = np.random.uniform(bounds[:, 0], bounds[:, 1], size=d)
        else:
            perfs_normalized = normalize(perfs)
            gp_model.fit(np.array(xs), np.array(perfs_normalized))
            print('Length scale = {}'.format(gp_model.kernel_.length_scale))
            previous_optimum = np.max(perfs_normalized)
            #            x = sample_next_point(d, neg_expected_improvement, gp_model, previous_optimum, bounds, n_restarts=100)
            x = sample_next_point(d,
                                  neg_expected_improvement,
                                  gp_model,
                                  previous_optimum,
                                  bounds,
                                  random_search=100000)
        airfoil = synthesize(x, n_points)
        perf = evaluate(airfoil)
        print('PARSEC-BO %d-%d: CL/CD %.2f' % (run_id, i + 1, perf))
        xs.append(x)
        perfs.append(perf)
        opt_perfs.append(np.max(perfs))  # Best performance so far

    opt_x = xs[np.argmax(perfs)]
    opt_airfoil = synthesize(opt_x, n_points)
    print('Optimal CL/CD: {}'.format(opt_perfs[-1]))

    return opt_airfoil, opt_perfs
Example #3
0
    args = parser.parse_args()

    n_runs = args.n_runs
    n_eval = args.n_eval

    # Airfoil parameters
    n_points = 192

    # NACA 0012 as the original airfoil
    x0 = np.array([
        0.0147, 0.2996, -0.06, 0.4406, 7.335, 0.3015, 0.0599, -0.4360, -7.335
    ])  # NACA 0012

    perturb_type = 'relative'
    perturb = 0.2
    syn_func = lambda x: synthesize(x, n_points)

    opt_airfoil_runs = []
    opt_perfs_runs = []
    time_runs = []
    for i in range(n_runs):
        start_time = time.time()
        opt_airfoil, opt_perfs = optimize(x0, syn_func, perturb_type, perturb,
                                          n_eval, i + 1)
        end_time = time.time()
        opt_airfoil_runs.append(opt_airfoil)
        opt_perfs_runs.append(opt_perfs)
        time_runs.append(end_time - start_time)

    opt_airfoil_runs = np.array(opt_airfoil_runs)
    opt_perfs_runs = np.array(opt_perfs_runs)