def fit(self, X, Y): self.thetas, self.Ws, self.bs, self.sf2s = [], [], [], [] n_sample = X.shape[0] for i, gp in enumerate(self.gps): gp.fit(X, Y[:, i]) ell = np.exp(gp.kernel_.theta[1:-1]) sf2 = np.exp(2 * gp.kernel_.theta[0]) sn2 = np.exp(2 * gp.kernel_.theta[-1]) sw1, sw2 = lhs(self.n_var, self.M), lhs(self.n_var, self.M) if self.nu > 0: W = np.tile(1. / ell, (self.M, 1)) * norm.ppf(sw1) * np.sqrt(self.nu / chi2.ppf(sw2, df=self.nu)) else: W = np.random.uniform(size=(self.M, self.n_var)) * np.tile(1. / ell, (self.M, 1)) b = 2 * np.pi * lhs(1, self.M) phi = np.sqrt(2. * sf2 / self.M) * np.cos(W @ X.T + np.tile(b, (1, n_sample))) A = phi @ phi.T + sn2 * np.eye(self.M) invcholA = LA.inv(LA.cholesky(A)) invA = invcholA.T @ invcholA mu_theta = invA @ phi @ Y[:, i] if self.mean_sample: theta = mu_theta else: cov_theta = sn2 * invA cov_theta = 0.5 * (cov_theta + cov_theta.T) theta = mu_theta + LA.cholesky(cov_theta) @ np.random.standard_normal(self.M) self.thetas.append(theta.copy()) self.Ws.append(W.copy()) self.bs.append(b.copy()) self.sf2s.append(sf2)
def _get_sampling(self, X, Y): ''' Initialize population from data ''' if self.pop_init_method == 'lhs': sampling = LatinHypercubeSampling() elif self.pop_init_method == 'nds': sorted_indices = NonDominatedSorting().do(Y) pop_size = self.algo_kwargs['pop_size'] sampling = X[np.concatenate(sorted_indices)][:pop_size] # NOTE: use lhs if current samples are not enough if len(sampling) < pop_size: rest_sampling = lhs(X.shape[1], pop_size - len(sampling)) sampling = np.vstack([sampling, rest_sampling]) elif self.pop_init_method == 'random': sampling = FloatRandomSampling() else: raise NotImplementedError return sampling
def generate_initial_samples(problem, n_sample): ''' Generate feasible initial samples. Input: problem: the optimization problem n_sample: number of initial samples Output: X, Y: initial samples (design parameters, performances) ''' X_feasible = np.zeros((0, problem.n_var)) Y_feasible = np.zeros((0, problem.n_obj)) # NOTE: when it's really hard to get feasible samples, the program hangs here while len(X_feasible) < n_sample: X = lhs(problem.n_var, n_sample) X = problem.xl + X * (problem.xu - problem.xl) Y, feasible = problem.evaluate(X, return_values_of=['F', 'feasible']) feasible = feasible.flatten() X_feasible = np.vstack([X_feasible, X[feasible]]) Y_feasible = np.vstack([Y_feasible, Y[feasible]]) indices = np.random.permutation(np.arange(len(X_feasible)))[:n_sample] X, Y = X_feasible[indices], Y_feasible[indices] return X, Y
def main(): # get argument values args = get_args() # get reference point if args.ref_point is None: args.ref_point = get_ref_point(args.problem, args.n_var, args.n_obj, args.n_init_sample) t0 = time() # set seed np.random.seed(args.seed) # build problem, get initial samples problem, true_pfront, X_init, Y_init = build_problem( args.problem, args.n_var, args.n_obj, args.n_init_sample, args.n_process) args.n_var, args.n_obj, args.algo = problem.n_var, problem.n_obj, 'nsga2' # save arguments and setup logger save_args(args) logger = setup_logger(args) print(problem) # initialize data exporter exporter = DataExport(X_init, Y_init, args) # initialize population if args.pop_init_method == 'lhs': sampling = LatinHypercubeSampling() elif args.pop_init_method == 'nds': sorted_indices = NonDominatedSorting().do(Y_init) sampling = X_init[np.concatenate(sorted_indices)][:args.batch_size] if len(sampling) < args.batch_size: rest_sampling = lhs(X_init.shape[1], args.batch_size - len(sampling)) sampling = np.vstack([sampling, rest_sampling]) elif args.pop_init_method == 'random': sampling = FloatRandomSampling() else: raise NotImplementedError # initialize evolutionary algorithm ea_algorithm = NSGA2(pop_size=args.batch_size, sampling=sampling) # find Pareto front res = minimize(problem, ea_algorithm, ('n_gen', args.n_iter), save_history=True) X_history = np.array([algo.pop.get('X') for algo in res.history]) Y_history = np.array([algo.pop.get('F') for algo in res.history]) # update data exporter for X_next, Y_next in zip(X_history, Y_history): exporter.update(X_next, Y_next) # export all result to csv exporter.write_csvs() if true_pfront is not None: exporter.write_truefront_csv(true_pfront) # statistics final_hv = calc_hypervolume(exporter.Y, exporter.ref_point) print('========== Result ==========') print('Total runtime: %.2fs' % (time() - t0)) print('Total evaluations: %d, hypervolume: %.4f\n' % (args.batch_size * args.n_iter, final_hv)) # close logger if logger is not None: logger.close()