def optimize(latent_dim, bounds, n_eval, run_id): # Optimize in the latent space n_pre_samples = 10 bounds = np.tile(bounds, (latent_dim, 1)) kernel = gp.kernels.Matern() gp_model = gp.GaussianProcessRegressor(kernel=kernel, alpha=1e-4, n_restarts_optimizer=100, normalize_y=False) zp = [] perfs = [] opt_perfs = [0] s = 1.0 gamma = 0.99 for i in range(n_eval): if i < n_pre_samples: z = np.random.uniform(bounds[:, 0], bounds[:, 1], size=latent_dim) else: perf_normalized = normalize(perfs) gp_model.fit(np.array(zp), np.array(perf_normalized)) length_scale = gp_model.kernel_.length_scale print('Length scale = {}'.format(length_scale)) previous_optimum = perf_normalized[opt_idx] if np.all(np.array(perfs[-5:]) == -1): # in case getting stuck in infeasible region print('Back to {} ...'.format(opt_z)) previous_point = opt_z else: previous_point = z # z = sample_next_point(latent_dim, neg_expected_improvement, gp_model, previous_optimum, bounds, n_restarts=100) z = sample_next_point(latent_dim, neg_expected_improvement, gp_model, previous_optimum, bounds=None, random_search=100000, previous_point=previous_point, scale=s) s *= gamma x = synthesize(z.reshape(1, -1), model) perf = evaluate(x) z = np.squeeze(z) zp.append(z) perfs.append(perf) opt_idx = np.argmax(perfs) opt_z = zp[opt_idx] opt_perf = perfs[opt_idx] opt_perfs.append(opt_perf) # Best performance so far print('GAN-BO {}-{}: z {} CL/CD {:.2f} best-so-far {:.2f}'.format( run_id, i + 1, z, perf, opt_perf)) opt_z = opt_z.reshape(1, -1) opt_airfoil = synthesize(opt_z, model) print('Optimal: z {} CL/CD {}'.format(opt_z, opt_perfs[-1])) return opt_airfoil, opt_perfs
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, U_upper, U_lower, p, n_points) perf = evaluate(airfoil) print('NURBS-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, U_upper, U_lower, p, n_points) print('Optimal CL/CD: {}'.format(opt_perfs[-1])) return opt_airfoil, opt_perfs