def generate_kriging(n_cpu): # Initialization KrigInfo = dict() kernel = ["gaussian"] # Sampling nsample = 40 nvar = 2 ub = np.array([5, 5]) lb = np.array([-5, -5]) nup = 3 sampoption = "halton" samplenorm, sample = sampling(sampoption, nvar, nsample, result="real", upbound=ub, lobound=lb) X = sample # Evaluate sample y1 = evaluate(X, "styblinski") # Initialize KrigInfo KrigInfo = initkriginfo() # Set KrigInfo KrigInfo["X"] = X KrigInfo["y"] = y1 KrigInfo["nvar"] = nvar KrigInfo["problem"] = "styblinski" KrigInfo["nsamp"] = nsample KrigInfo["nrestart"] = 7 KrigInfo["ub"] = ub KrigInfo["lb"] = lb KrigInfo["kernel"] = kernel KrigInfo["TrendOrder"] = 0 KrigInfo["nugget"] = -6 # KrigInfo["n_princomp"] = 1 KrigInfo["kernel"] = ["gaussian"] KrigInfo["nkernel"] = len(KrigInfo["kernel"]) KrigInfo["optimizer"] = "lbfgsb" # Run Kriging t = time.time() krigobj = Kriging(KrigInfo, standardization=True, standtype="default", normy=False, trainvar=False) krigobj.train(n_cpu=n_cpu) loocverr, _ = krigobj.loocvcalc() elapsed = time.time() - t print("elapsed time for train Kriging model: ", elapsed, "s") print("LOOCV error of Kriging model: ", loocverr, "%") return krigobj
def predictkrig(krigobj): nsample = 25 nvar = 2 ub = np.array([5, 5]) lb = np.array([-5, -5]) nup = 3 sampoption = "halton" # Test Kriging Output neval = 10000 samplenormout, sampleeval = sampling(sampoption, nvar, neval, result="real", upbound=ub, lobound=lb) xx = np.linspace(-5, 5, 100) yy = np.linspace(-5, 5, 100) Xevalx, Xevaly = np.meshgrid(xx, yy) Xeval = np.zeros(shape=[neval, 2]) Xeval[:, 0] = np.reshape(Xevalx, (neval)) Xeval[:, 1] = np.reshape(Xevaly, (neval)) # Evaluate output yeval = np.zeros(shape=[neval, 1]) yact = np.zeros(shape=[neval, 1]) yeval, ssqr = krigobj.predict(Xeval, ["pred", "ssqr"]) yact = evaluate(Xeval, "styblinski") hasil = np.hstack((yeval, yact)) # Evaluate RMSE subs = np.transpose((yact - yeval)) subs1 = np.transpose((yact - yeval) / yact) RMSE = np.sqrt(np.sum(subs**2) / neval) RMSRE = np.sqrt(np.sum(subs1**2) / neval) MAPE = 100 * np.sum(abs(subs1)) / neval print("RMSE = ", RMSE) print("RMSRE = ", RMSRE) print("MAPE = ", MAPE, "%") yeval1 = np.reshape(yeval, (100, 100)) x1eval = np.reshape(Xeval[:, 0], (100, 100)) x2eval = np.reshape(Xeval[:, 1], (100, 100)) fig = plt.figure() ax = fig.gca(projection="3d") surf = ax.plot_surface(x1eval, x2eval, yeval1, cmap=cm.coolwarm, linewidth=0, antialiased=False) plt.show()
def generate_kriging(): # Sampling nsample = 20 nvar = 2 nobj = 2 lb = -1 * np.ones(shape=[nvar]) ub = 1 * np.ones(shape=[nvar]) sampoption = "halton" samplenorm, sample = sampling(sampoption, nvar, nsample, result="real", upbound=ub, lobound=lb) X = sample # Evaluate sample global y y = evaluate(X, "schaffer") # Initialize KrigInfo KrigInfo1 = initkriginfo() # Set KrigInfo KrigInfo1["X"] = X KrigInfo1["y"] = y[:, 0].reshape(-1, 1) KrigInfo1["problem"] = "schaffer" KrigInfo1["nrestart"] = 5 KrigInfo1["ub"] = ub KrigInfo1["lb"] = lb KrigInfo1["optimizer"] = "lbfgsb" # Initialize KrigInfo KrigInfo2 = deepcopy(KrigInfo1) KrigInfo2['y'] = y[:, 1].reshape(-1, 1) # Run Kriging krigobj1 = Kriging(KrigInfo1, standardization=True, standtype='default', normy=False, trainvar=False) krigobj1.train(n_cpu=n_cpu) loocverr1, _ = krigobj1.loocvcalc() print("LOOCV error of Kriging model: ", loocverr1, "%") krigobj2 = Kriging(KrigInfo2, standardization=True, standtype='default', normy=False, trainvar=False) krigobj2.train(n_cpu=n_cpu) loocverr2, _ = krigobj2.loocvcalc() print("LOOCV error of Kriging model: ", loocverr2, "%") return krigobj1, krigobj2
def generate_kriging(n_cpu): # Sampling nsample = 10 nvar = 2 lb = np.array([-5, -5]) ub = np.array([5, 5]) sampoption = "halton" samplenorm, sample = sampling(sampoption, nvar, nsample, result="real", upbound=ub, lobound=lb) X = sample # Evaluate sample # global y y = evaluate(X, "styblinski") # Initialize KrigInfo # global KrigInfo KrigInfo = initkriginfo() # Set KrigInfo KrigInfo["X"] = X KrigInfo["y"] = y KrigInfo["problem"] = "styblinski" KrigInfo["nrestart"] = 5 KrigInfo["ub"] = ub KrigInfo["lb"] = lb KrigInfo["optimizer"] = "lbfgsb" # Run Kriging krigobj = Kriging(KrigInfo, standardization=True, standtype='default', normy=False, trainvar=False) krigobj.train(n_cpu=n_cpu) loocverr, _ = krigobj.loocvcalc() print("LOOCV error of Kriging model: ", loocverr, "%") return krigobj
def run(self, disp=True): """ Run multi objective unconstrained Bayesian optimization. Args: disp (bool): Display process or not. Defaults to True Returns: xupdate (nparray): Array of design variables updates. yupdate (nparray): Array of objectives updates """ self.nup = 0 # Number of current iteration self.Xall = self.krigobj.KrigInfo['X'] self.yall = self.krigobj.KrigInfo['y'] self.yhist = np.array([np.min(self.yall)]) self.istall = 0 print("Begin single-objective Bayesian optimization process.") while self.nup < self.soboInfo['nup']: if self.autoupdate and disp: print(f"Update no.: {self.nup + 1}, F-count: {np.size(self.Xall, 0)}, " f"Best f(x): {self.yhist[self.nup]}, Stall counter: {self.istall}") else: pass # Find next suggested point self.xnext, self.metricnext = run_single_opt(self.krigobj,self.soboInfo,self.krigconstlist,self.cheapconstlist) # Break Loop if autoupdate is False if self.autoupdate is False: break else: pass # Evaluate response for next decision variable if type(self.krigobj.KrigInfo['problem']) == str: self.ynext = evaluate(self.xnext,self.krigobj.KrigInfo['problem']) elif callable(self.krigobj.KrigInfo['problem']): self.ynext = self.krigobj.KrigInfo['problem'](self.xnext) # Treatment for failed solutions, Reference : "Forrester, A. I., Sóbester, A., & Keane, A. J. (2006). Optimization with missing data. # Proceedings of the Royal Society A: Mathematical, Physical and Engineering Sciences, 462(2067), 935-945." if np.isnan(self.ynext).any() is True: SSqr, y_hat = self.krigobj.predict(self.xnext, ['SSqr', 'pred']) self.ynext = y_hat + SSqr # Enrich experimental design self.krigobj.KrigInfo['X'] = np.vstack((self.krigobj.KrigInfo['X'], self.xnext)) self.krigobj.KrigInfo['y'] = np.vstack((self.krigobj.KrigInfo['y'], self.ynext)) # Re-train Kriging model self.krigobj.standardize() self.krigobj.train(disp=False) if self.nup == 0: self.xupdate = deepcopy(self.xnext) self.yupdate = deepcopy(self.ynext) else: self.xupdate = np.vstack((self.xupdate,self.xnext)) self.yupdate = np.vstack((self.yupdate,self.ynext)) self.nup += 1 self.yhist = np.vstack((self.yhist, np.min(self.krigobj.KrigInfo['y']))) # Check stall iteration if self.yhist[self.nup,0] == self.yhist[self.nup-1,0]: self.istall += 1 if self.istall == self.soboInfo['stalliteration']: break else: pass else: self.istall = 0 print("Optimization finished, now creating the final outputs.") y_opt = np.min(self.krigobj.KrigInfo['y']) min_pos = np.argmin(self.krigobj.KrigInfo['y']) x_opt = self.krigobj.KrigInfo['X'][min_pos,:] if self.autoupdate: return x_opt,y_opt else: return self.xnext,self.ynext
def _run(self, disp=True, pool=None): """Run multi objective unconstrained Bayesian optimization. Args: disp (bool, optional): Print progress. Defaults to True. pool (int, optional): A multiprocessing.Pool instance. Will be passed to functions for use, if specified. Defaults to None. Returns: xupdate (np.ndarray): Matrix of updated samples after optimization. yupdate (np.ndarray): Response matrix of updated sample solutions after optimization. """ self.nup = 0 # Number of current iteration self.Xall = self.krigobj.KrigInfo["X"] self.yall = self.krigobj.KrigInfo["y"] self.yhist = np.array([np.min(self.yall)]) self.istall = 0 print("Begin single-objective Bayesian optimization process.") while self.nup < self.soboInfo["nup"]: if self.autoupdate and disp: print( f"Update no.: {self.nup + 1}, F-count: {np.size(self.Xall, 0)}, " f"Best f(x): {self.yhist[self.nup]}, Stall counter: {self.istall}" ) else: pass # Find next suggested point x_n, metric_n = run_single_opt(self.krigobj, self.soboInfo, krigconstlist=self.krigconstlist, cheapconstlist=self.cheapconstlist, pool=pool) self.xnext = x_n self.metricnext = metric_n # Break Loop if autoupdate is False if self.autoupdate is False: break else: pass # Evaluate response for next decision variable obj_krig_problem = self.krigobj.KrigInfo["problem"] if isinstance(obj_krig_problem, str): self.ynext = evaluate(self.xnext, obj_krig_problem) elif callable(obj_krig_problem): self.ynext = obj_krig_problem(self.xnext) else: raise ValueError('KrigInfo["problem"] is not a string nor a ' 'callable function!') # Treatment for failed solutions, Reference : "Forrester, A. I., Sóbester, A., & Keane, A. J. (2006). Optimization with missing data. # Proceedings of the Royal Society A: Mathematical, Physical and Engineering Sciences, 462(2067), 935-945." if np.isnan(self.ynext).any(): SSqr, y_hat = self.krigobj.predict(self.xnext, ["SSqr", "pred"]) self.ynext = y_hat + SSqr # Enrich experimental design self.krigobj.KrigInfo["X"] = np.vstack( (self.krigobj.KrigInfo["X"], self.xnext)) self.krigobj.KrigInfo["y"] = np.vstack( (self.krigobj.KrigInfo["y"], self.ynext)) # Re-train Kriging model self.krigobj.standardize() self.krigobj.train(disp=False, pool=pool) if self.nup == 0: self.xupdate = deepcopy(self.xnext) self.yupdate = deepcopy(self.ynext) else: self.xupdate = np.vstack((self.xupdate, self.xnext)) self.yupdate = np.vstack((self.yupdate, self.ynext)) self.nup += 1 self.yhist = np.vstack( (self.yhist, np.min(self.krigobj.KrigInfo["y"]))) # Check stall iteration if self.yhist[self.nup, 0] == self.yhist[self.nup - 1, 0]: self.istall += 1 if self.istall == self.soboInfo["stalliteration"]: break else: self.istall = 0 print("Optimization finished, now creating the final outputs.") y_opt = np.min(self.krigobj.KrigInfo["y"]) min_pos = np.argmin(self.krigobj.KrigInfo["y"]) x_opt = self.krigobj.KrigInfo["X"][min_pos, :] if self.autoupdate: return x_opt, y_opt else: return self.xnext, self.ynext
def enrich(self, xnext): """ Evaluate and enrich experimental design. Args: xnext: Next design variable(s) to be evaluated. Returns: None """ # Evaluate new sample if type(self.kriglist[0].KrigInfo['problem']) == str: if np.ndim(xnext) == 1: ynext = evaluate(xnext, self.kriglist[0].KrigInfo['problem']) else: ynext = np.zeros(shape=[np.size(xnext, 0), len(self.kriglist)]) for ii in range(np.size(xnext, 0)): ynext[ii, :] = evaluate( xnext[ii, :], self.kriglist[0].KrigInfo['problem']) elif callable(self.kriglist[0].KrigInfo['problem']): ynext = self.kriglist[0].KrigInfo['problem'](xnext) else: raise ValueError( 'KrigInfo["problem"] is not a string nor a callable function!') if self.krigconstlist is not None: for idx, constobj in enumerate(self.krigconstlist): if type(constobj.KrigInfo['problem']) == str: ynext_const = evaluate(xnext, constobj.KrigInfo['problem']) elif callable(constobj.KrigInfo['problem']): ynext_const = constobj.KrigInfo['problem'](xnext).reshape( -1, 1) else: raise ValueError( 'KrigConstInfo["problem"] is not a string nor a callable function!' ) constobj.KrigInfo['X'] = np.vstack( (constobj.KrigInfo['X'], xnext)) constobj.KrigInfo['y'] = np.vstack( (constobj.KrigInfo['y'], ynext_const)) constobj.standardize() constobj.train(disp=False) else: pass # Treatment for failed solutions, Reference : "Forrester, A. I., Sóbester, A., & Keane, A. J. (2006). Optimization with missing data. # Proceedings of the Royal Society A: Mathematical, Physical and Engineering Sciences, 462(2067), 935-945." if np.isnan(ynext).any() is True: for jj in range(len(self.kriglist)): SSqr, y_hat = self.kriglist[jj].predict( xnext, ['SSqr', 'pred']) ynext[0, jj] = y_hat + SSqr # Enrich experimental design self.yall = np.vstack((self.yall, ynext)) self.Xall = np.vstack((self.Xall, xnext)) self.ypar, I = searchpareto.paretopoint( self.yall) # Recompute non-dominated solutions if self.moboInfo['acquifunc'] == 'ehvi': for index, krigobj in enumerate(self.kriglist): krigobj.KrigInfo['X'] = self.Xall krigobj.KrigInfo['y'] = self.yall[:, index].reshape(-1, 1) krigobj.standardize() krigobj.train(disp=False) elif self.moboInfo['acquifunc'] == 'parego': self.KrigScalarizedInfo['X'] = self.Xall self.KrigScalarizedInfo['y'] = paregopre(self.yall) self.scalkrig = Kriging(self.KrigScalarizedInfo, standardization=True, standtype='default', normy=False, trainvar=False) self.scalkrig.train(disp=False) for index, krigobj in enumerate(self.kriglist): krigobj.KrigInfo['X'] = self.Xall krigobj.KrigInfo['y'] = self.yall[:, index].reshape(-1, 1) krigobj.standardize() krigobj.train(disp=False) else: raise ValueError(self.moboInfo["acquifunc"], " is not a valid acquisition function.") # Save data if self.savedata: I = I.astype(int) Xbest = self.Xall[I, :] sio.savemat(self.moboInfo["filename"], { "xbest": Xbest, "ybest": self.ypar })