def kpcaopt(self,w,KPCAkernel,orig_X,out='default'): # Calculate PLS coeff if KPCAkernel != "gaussian" and KPCAkernel != "precomputed": if KPCAkernel.lower() == 'poly' or KPCAkernel.lower() == 'polynomial': _drm = drm(self.n_princomp, kernel='poly', gamma=10**w[0], coef0=10**w[1], degree=np.round(w[2])) elif KPCAkernel.lower() == 'sigmoid': _drm = drm(self.n_princomp, kernel='sigmoid', gamma=10**w[0], coef0=10**w[1]) elif KPCAkernel.lower() == 'rbf': _drm = drm(self.n_princomp, kernel='rbf', gamma=10**w[0]) elif KPCAkernel.lower() == 'linear' or KPCAkernel.lower() == 'cosine': _drm = drm(self.n_princomp) self.KrigInfo["nvar"] = self.n_princomp if self.standardization is True: self.KrigInfo["X_norm"] = deepcopy(orig_X) _drm.fit(self.KrigInfo["X_norm"].copy()) transformed = _drm.transform(self.KrigInfo["X_norm"].copy()) self.KrigInfo["lb2"] = (np.min(transformed, axis=0)) # Create lowerbound for transformed X self.KrigInfo["ub2"] = (np.max(transformed, axis=0)) # Create upperbound for transformed X self.KrigInfo["X_norm"] = standardize(transformed, self.KrigInfo['y'], type=self.standtype.lower(), range=np.vstack((self.KrigInfo["lb2"], self.KrigInfo["ub2"]))) self.KrigInfo['idx'] = polytruncation(self.KrigInfo["TrendOrder"], self.KrigInfo["nvar"], 1) else: self.KrigInfo["X"] = deepcopy(orig_X) _drm.fit(self.KrigInfo["X"].copy()) transformed = _drm.transform(self.KrigInfo["X"].copy()) self.KrigInfo["X"] = transformed self.KrigInfo['idx'] = polytruncation(self.KrigInfo["TrendOrder"], self.KrigInfo["nvar"], 1) else: n_features = np.size(orig_X,1) self.KrigInfo["nvar"] = self.n_princomp _drm = drm(self.n_princomp,kernel='precomputed') k_mat = customkernel(orig_X,orig_X,w,n_features,type='gaussian') if self.standardization is True: self.KrigInfo["X_norm"] = deepcopy(orig_X) transformed = _drm.fit_transform(k_mat) self.KrigInfo["lb2"] = (np.min(transformed, axis=0)) # Create lowerbound for transformed X self.KrigInfo["ub2"] = (np.max(transformed, axis=0)) # Create upperbound for transformed X self.KrigInfo["X_norm"] = standardize(transformed, self.KrigInfo['y'], type=self.standtype.lower(), range=np.vstack((self.KrigInfo["lb2"], self.KrigInfo["ub2"]))) self.KrigInfo['idx'] = polytruncation(self.KrigInfo["TrendOrder"], self.KrigInfo["nvar"], 1) else: pass if out == 'default': self.KrigInfo["kernel"] = ["iso_gaussian"] Kriging.train(self, disp=False) else: self.KrigInfo["kernel"] = ["gaussian"] Kriging.train(self, disp=False, pre_theta=self.KrigInfo['Theta']) loocverr, _ = Kriging.loocvcalc(self, drm=_drm) if out == 'default': return loocverr elif out == 'all': return _drm, loocverr
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 global KrigInfo1 KrigInfo1 = initkriginfo("single") # 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 global KrigInfo2 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(parallel=False) loocverr1, _ = krigobj1.loocvcalc() print("LOOCV error of Kriging model: ", loocverr1, "%") krigobj2 = Kriging(KrigInfo2, standardization=True, standtype='default', normy=False, trainvar=False) krigobj2.train(parallel=False) loocverr2, _ = krigobj2.loocvcalc() print("LOOCV error of Kriging model: ", loocverr2, "%") return krigobj1, krigobj2
def generate_kriging(): # 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("single") # 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(parallel=False) 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 simultpredparego(self): """ Perform multi updates on ParEGO MOBO by varying the weighting function. Returns: xalltemp (nparray) : Array of design variables updates. yalltemp (nparray) : Array of objectives value updates. metricall (nparray) : Array of metric of the updates. """ idxs = np.random.choice(11, self.multiupdate) scalinfotemp = deepcopy(self.KrigScalarizedInfo) xalltemp = self.Xall[:, :] yalltemp = self.yall[:, :] yprednext = np.zeros(shape=[len(self.kriglist)]) for ii, idx in enumerate(idxs): print(f"update number {ii + 1}") scalinfotemp['X'] = xalltemp scalinfotemp['y'] = paregopre(yalltemp, idx) krigtemp = Kriging(scalinfotemp, standardization=True, standtype='default', normy=False, trainvar=False) krigtemp.train(disp=False) xnext, metricnext = run_single_opt(krigtemp, self.moboInfo, self.krigconstlist, self.cheapconstlist) for jj, krigobj in enumerate(self.kriglist): yprednext[jj] = krigobj.predict(xnext, ['pred']) if ii == 0: xallnext = deepcopy(xnext) yallnext = deepcopy(yprednext) metricall = deepcopy(metricnext) else: xallnext = np.vstack((xallnext, xnext)) yallnext = np.vstack((yallnext, yprednext)) metricall = np.vstack((metricall, metricnext)) yalltemp = np.vstack((yalltemp, yprednext)) xalltemp = np.vstack((xalltemp, xnext)) return xallnext, yallnext, metricall
def generate_kriging(): # 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("single") # 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(parallel=False) loocverr, _ = krigobj.loocvcalc() print("LOOCV error of Kriging model: ", loocverr, "%") return krigobj
class Problem: def __init__(self, X, y): self.X = X self.y = y def createkrig(self): # define variables lb = np.array([0.1, 3, 3.1]) ub = np.array([0.9, 11.41, 60.95]) # Set kriging Info KrigMultiInfo1 = initkriginfo("single") KrigMultiInfo1["X"] = self.X KrigMultiInfo1["y"] = self.y[:, 0].reshape(-1, 1) KrigMultiInfo1["nrestart"] = 7 KrigMultiInfo1["ub"] = ub KrigMultiInfo1["lb"] = lb KrigMultiInfo1["optimizer"] = "lbfgsb" KrigMultiInfo2 = deepcopy(KrigMultiInfo1) KrigMultiInfo2['y'] = self.y[:, 1].reshape(-1, 1) # train kriging self.krigobj1 = Kriging(KrigMultiInfo1, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj1.train(parallel=False) loocverr1, _ = self.krigobj1.loocvcalc() print(f"LOOCV Error Kriging 1: {loocverr1}% (MAPE)") self.krigobj2 = Kriging(KrigMultiInfo2, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj2.train(parallel=False) loocverr2, _ = self.krigobj2.loocvcalc() print(f"LOOCV Error Kriging 2: {loocverr2}% (MAPE)") self.kriglist = [self.krigobj1, self.krigobj2] def update_sample(self): moboInfo = dict() moboInfo["nup"] = 1 moboInfo["nrestart"] = 10 moboInfo["acquifunc"] = "ehvi" moboInfo["acquifuncopt"] = "lbfgsb" cheapconstlist = [self.geomconst] mobo = MOBO(moboInfo, self.kriglist, autoupdate=False, multiupdate=5, savedata=False, chpconst=cheapconstlist) xupdate, yupdate, metricall = mobo.run(disp=True) return xupdate, yupdate, metricall def geomconst(self, vars): # constraint 'geomconst' should have input of the design variables vars = np.array(vars) stat = foongconst(vars) return stat
class MOBO: """ Perform multi-objective Bayesian Optimization Args: moboInfo (dict): Dictionary containing necessary information for multi-objective Bayesian optimization. kriglist (list): List of Kriging object. autoupdate (bool): True or False, depends on your decision to evaluate your function automatically or not. multiupdate (int): Number of suggested samples returned for each iteration. expconst (list): List of constraints Kriging object. chpconst (list): List of cheap constraint function. Returns: xupdate (nparray): Array of design variables updates. yupdate (nparray): Array of objectives updates metricall (nparray): Array of metric values of the updates. """ def __init__(self, moboInfo, kriglist, autoupdate=True, multiupdate=0, savedata=True, expconst=None, chpconst=None): """ Initialize MOBO class Args: moboInfo (dict): Dictionary containing necessary information for multi-objective Bayesian optimization. kriglist (list): List of Kriging object. autoupdate (bool): True or False, depends on your decision to evaluate your function automatically or not. multiupdate (int): Number of suggested samples returned for each iteration. savedata (bool): Save data for each iteration or not. Defaults to True. expconst (list): List of constraints Kriging object. chpconst (list): List of cheap constraint function. """ self.moboInfo = moboinfocheck(moboInfo, autoupdate) self.kriglist = kriglist self.krignum = len(self.kriglist) self.autoupdate = autoupdate self.multiupdate = multiupdate self.savedata = savedata self.krigconstlist = expconst self.cheapconstlist = chpconst 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 metricall (nparray): Array of metric values of the updates. """ self.nup = 0 # Number of current iteration self.Xall = self.kriglist[0].KrigInfo['X'] self.yall = np.zeros(shape=[ np.size(self.kriglist[0].KrigInfo["y"], axis=0), len(self.kriglist) ]) for ii in range(np.size(self.yall, axis=1)): self.yall[:, ii] = self.kriglist[ii].KrigInfo["y"][:, 0] self.ypar, _ = searchpareto.paretopoint(self.yall) print("Begin multi-objective Bayesian optimization process.") if self.autoupdate and disp: print( f"Update no.: {self.nup+1}, F-count: {np.size(self.Xall,0)}, " f"Maximum no. updates: {self.moboInfo['nup']+1}") else: pass # If the optimizer is ParEGO, create a scalarized Kriging if self.moboInfo['acquifunc'].lower() == 'parego': self.KrigScalarizedInfo = deepcopy(self.kriglist[0].KrigInfo) self.KrigScalarizedInfo['y'] = paregopre(self.yall) self.scalkrig = Kriging(self.KrigScalarizedInfo, standardization=True, standtype='default', normy=False, trainvar=False) self.scalkrig.train(disp=False) else: pass # Perform update on design space if self.moboInfo['acquifunc'].lower() == 'ehvi': self.ehviupdate(disp) elif self.moboInfo['acquifunc'].lower() == 'parego': self.paregoupdate(disp) else: raise ValueError(self.moboInfo["acquifunc"], " is not a valid acquisition function.") # Finish optimization and return values if disp: print("Optimization finished, now creating the final outputs.") if self.multiupdate == 0 or self.multiupdate == 1: xupdate = self.Xall[-self.moboInfo['nup']:, :] yupdate = self.yall[-self.moboInfo['nup']:, :] else: xupdate = self.Xall[(-self.moboInfo['nup'] * self.multiupdate):, :] yupdate = self.yall[(-self.moboInfo['nup'] * self.multiupdate):, :] metricall = self.metricall return xupdate, yupdate, metricall def ehviupdate(self, disp): """ Update MOBO using EHVI algorithm. Args: disp (bool): Display process or not. Returns: None """ while self.nup < self.moboInfo['nup']: # Iteratively update the reference point for hypervolume computation if EHVI is used as the acquisition function if self.moboInfo['refpointtype'].lower() == 'dynamic': self.moboInfo['refpoint'] = np.max( self.yall, 0) + (np.max(self.yall, 0) - np.min(self.yall, 0)) * 2 # Perform update(s) if self.multiupdate < 0: raise ValueError( "Number of multiple update must be greater or equal to 0") elif self.multiupdate == 0 or self.multiupdate == 1: xnext, metricnext = run_multi_opt(self.kriglist, self.moboInfo, self.ypar, self.krigconstlist, self.cheapconstlist) yprednext = np.zeros(shape=[2]) for ii, krigobj in enumerate(self.kriglist): yprednext[ii] = krigobj.predict(xnext, ['pred']) else: xnext, yprednext, metricnext = self.simultpredehvi(disp) if self.nup == 0: self.metricall = metricnext else: self.metricall = np.vstack((self.metricall, metricnext)) # Break Loop if auto is false if self.autoupdate is False: self.Xall = np.vstack((self.Xall, xnext)) self.yall = np.vstack((self.yall, yprednext)) break else: pass # Evaluate and enrich experimental design self.enrich(xnext) # Update number of iterations self.nup += 1 # Show optimization progress if disp: print( f"Update no.: {self.nup+1}, F-count: {np.size(self.Xall, 0)}, " f"Maximum no. updates: {self.moboInfo['nup']+1}") def paregoupdate(self, disp): """ Update MOBO using ParEGO algorithm. Args: disp (bool): Display process or not. Returns: None """ while self.nup < self.moboInfo['nup']: # Perform update(s) if self.multiupdate < 0: raise ValueError( "Number of multiple update must be greater or equal to 0") elif self.multiupdate == 0 or self.multiupdate == 1: xnext, metricnext = run_single_opt(self.scalkrig, self.moboInfo, self.krigconstlist, self.cheapconstlist) yprednext = np.zeros(shape=[2]) for ii, krigobj in enumerate(self.kriglist): yprednext[ii] = krigobj.predict(xnext, ['pred']) else: xnext, yprednext, metricnext = self.simultpredparego() if self.nup == 0: self.metricall = metricnext else: self.metricall = np.vstack((self.metricall, metricnext)) # Break Loop if auto is false if self.autoupdate is False: self.Xall = np.vstack((self.Xall, xnext)) self.yall = np.vstack((self.yall, yprednext)) break else: pass # Evaluate and enrich experimental design self.enrich(xnext) # Update number of iterations self.nup += 1 # Show optimization progress if disp: print( f"Update no.: {self.nup+1}, F-count: {np.size(self.Xall, 0)}, " f"Maximum no. updates: {self.moboInfo['nup']+1}") def simultpredehvi(self, disp=False): """ Perform multi updates on EHVI MOBO using Kriging believer method. Returns: xalltemp (nparray) : Array of design variables updates. yalltemp (nparray) : Array of objectives value updates. metricall (nparray) : Array of metric of the updates. """ krigtemp = [0] * len(self.kriglist) for index, obj in enumerate(self.kriglist): krigtemp[index] = deepcopy(obj) yprednext = np.zeros(shape=[len(krigtemp)]) ypartemp = self.ypar yall = self.yall for ii in range(self.multiupdate): if disp: print(f"update number {ii+1}") else: pass xnext, metrictemp = run_multi_opt(krigtemp, self.moboInfo, ypartemp, self.krigconstlist, self.cheapconstlist) bound = np.vstack( (-np.ones(shape=[1, krigtemp[0].KrigInfo["nvar"]]), np.ones(shape=[1, krigtemp[0].KrigInfo["nvar"]]))) for jj in range(len(krigtemp)): yprednext[jj] = krigtemp[jj].predict(xnext, 'pred') krigtemp[jj].KrigInfo['X'] = np.vstack( (krigtemp[jj].KrigInfo['X'], xnext)) krigtemp[jj].KrigInfo['y'] = np.vstack( (krigtemp[jj].KrigInfo['y'], yprednext[jj])) krigtemp[jj].standardize() krigtemp[jj].KrigInfo["F"] = compute_regression_mat( krigtemp[jj].KrigInfo["idx"], krigtemp[jj].KrigInfo["X_norm"], bound, np.ones(shape=[krigtemp[jj].KrigInfo["nvar"]])) krigtemp[jj].KrigInfo = likelihood( krigtemp[jj].KrigInfo['Theta'], krigtemp[jj].KrigInfo, mode='all', trainvar=krigtemp[jj].trainvar) if ii == 0: xalltemp = deepcopy(xnext) yalltemp = deepcopy(yprednext) metricall = deepcopy(metrictemp) else: xalltemp = np.vstack((xalltemp, xnext)) yalltemp = np.vstack((yalltemp, yprednext)) metricall = np.vstack((metricall, metrictemp)) yall = np.vstack((yall, yprednext)) ypartemp, _ = searchpareto.paretopoint(yall) return xalltemp, yalltemp, metricall def simultpredparego(self): """ Perform multi updates on ParEGO MOBO by varying the weighting function. Returns: xalltemp (nparray) : Array of design variables updates. yalltemp (nparray) : Array of objectives value updates. metricall (nparray) : Array of metric of the updates. """ idxs = np.random.choice(11, self.multiupdate) scalinfotemp = deepcopy(self.KrigScalarizedInfo) xalltemp = self.Xall[:, :] yalltemp = self.yall[:, :] yprednext = np.zeros(shape=[len(self.kriglist)]) for ii, idx in enumerate(idxs): print(f"update number {ii + 1}") scalinfotemp['X'] = xalltemp scalinfotemp['y'] = paregopre(yalltemp, idx) krigtemp = Kriging(scalinfotemp, standardization=True, standtype='default', normy=False, trainvar=False) krigtemp.train(disp=False) xnext, metricnext = run_single_opt(krigtemp, self.moboInfo, self.krigconstlist, self.cheapconstlist) for jj, krigobj in enumerate(self.kriglist): yprednext[jj] = krigobj.predict(xnext, ['pred']) if ii == 0: xallnext = deepcopy(xnext) yallnext = deepcopy(yprednext) metricall = deepcopy(metricnext) else: xallnext = np.vstack((xallnext, xnext)) yallnext = np.vstack((yallnext, yprednext)) metricall = np.vstack((metricall, metricnext)) yalltemp = np.vstack((yalltemp, yprednext)) xalltemp = np.vstack((xalltemp, xnext)) return xallnext, yallnext, metricall def enrich(self, xnext): """ Evaluate and enrich experimental design. Args: xnext: Next design variable(s) to be evaluated. Returns: None """ # Evaluate new sample 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']) # 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 })
class Fillgap: def __init__(self, init_samp): self.init_samp = init_samp self.nsamp = np.size(self.init_samp, axis=0) self.cd = np.zeros(shape=[self.nsamp, 1]) self.noise = np.zeros(shape=[self.nsamp, 1]) self.cl = np.zeros(shape=[self.nsamp, 1]) self.solnlist = [self.cd, self.noise, self.cl] def createkrig(self): df = pd.read_csv('../innout/opt_data(1).csv', sep=',', index_col='Name') data = df.values X = data[:, 0:6].astype(float) y = data[:, 7:9].astype(float) self.y = y cldat = data[:, 6].astype(float) # define variables lb = np.min(X, axis=0) ub = np.max(X, axis=0) # Set Const Kriging KrigConstInfo = initkriginfo("single") KrigConstInfo["X"] = X KrigConstInfo["y"] = cldat.reshape(-1, 1) KrigConstInfo["nrestart"] = 5 KrigConstInfo["ub"] = ub KrigConstInfo["lb"] = lb KrigConstInfo["optimizer"] = "lbfgsb" KrigConstInfo['limit'] = 0.15 # Set Kriging Info KrigMultiInfo1 = initkriginfo("single") KrigMultiInfo1["X"] = X KrigMultiInfo1["y"] = y[:, 0].reshape(-1, 1) KrigMultiInfo1["nrestart"] = 7 KrigMultiInfo1["ub"] = ub KrigMultiInfo1["lb"] = lb KrigMultiInfo1["optimizer"] = "slsqp" KrigMultiInfo2 = deepcopy(KrigMultiInfo1) KrigMultiInfo2['y'] = y[:, 1].reshape(-1, 1) self.krigobj1 = Kriging(KrigMultiInfo1, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj1.train(parallel=False) loocverr1, _ = self.krigobj1.loocvcalc() self.krigobj2 = Kriging(KrigMultiInfo2, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj2.train(parallel=False) loocverr2, _ = self.krigobj2.loocvcalc() self.krigconst = Kriging(KrigConstInfo, standardization=True, standtype='default', normy=False, trainvar=False) self.krigconst.train(parallel=False) loocverr3, _ = self.krigconst.loocvcalc() self.kriglist = [self.krigobj1, self.krigobj2, self.krigconst] def evaluate(self): for idx, krigobj in enumerate(self.kriglist): self.solnlist[idx] = krigobj.predict(self.init_samp, ['pred']) return self.solnlist
class Problem: def __init__(self, X, y, cldat): self.X = X self.y = y self.cldat = cldat def createkrig(self): # define variables lb = np.min(self.X, axis=0) ub = np.max(self.X, axis=0) # Set Const Kriging KrigConstInfo = initkriginfo("single") KrigConstInfo["X"] = self.X KrigConstInfo["y"] = self.cldat.reshape(-1, 1) KrigConstInfo["nrestart"] = 5 KrigConstInfo["ub"] = ub KrigConstInfo["lb"] = lb KrigConstInfo["optimizer"] = "lbfgsb" KrigConstInfo['limittype'] = '>=' KrigConstInfo['limit'] = 0.15 # Set Kriging Info KrigMultiInfo1 = initkriginfo("single") KrigMultiInfo1["X"] = self.X KrigMultiInfo1["y"] = self.y[:, 0].reshape(-1, 1) KrigMultiInfo1["nrestart"] = 7 KrigMultiInfo1["ub"] = ub KrigMultiInfo1["lb"] = lb KrigMultiInfo1["optimizer"] = "lbfgsb" KrigMultiInfo2 = deepcopy(KrigMultiInfo1) KrigMultiInfo2['y'] = self.y[:, 1].reshape(-1, 1) self.krigobj1 = Kriging(KrigMultiInfo1, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj1.train(parallel=False) loocverr1, _ = self.krigobj1.loocvcalc() self.krigobj2 = Kriging(KrigMultiInfo2, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj2.train(parallel=False) loocverr2, _ = self.krigobj2.loocvcalc() self.krigconst = Kriging(KrigConstInfo, standardization=True, standtype='default', normy=False, trainvar=False) self.krigconst.train(parallel=False) loocverr3, _ = self.krigconst.loocvcalc() self.kriglist = [self.krigobj1, self.krigobj2] self.expconst = [self.krigconst] def update_sample(self): moboInfo = dict() moboInfo["nup"] = 1 moboInfo["nrestart"] = 10 moboInfo["acquifunc"] = "ehvi" moboInfo["acquifuncopt"] = "lbfgsb" moboInfo["refpoint"] = np.array([0.06, 83]) cheapconstlist = [self.geomconst] mobo = MOBO(moboInfo, self.kriglist, autoupdate=False, multiupdate=5, savedata=False, expconst=self.expconst, chpconst=cheapconstlist) xupdate, yupdate, metricall = mobo.run(disp=True) return xupdate, yupdate, metricall def geomconst(self, vars): # constraint 'geomconst' should have input of the design variables vars = np.array(vars) tip_angle = sweepdiffcheck.sweep_diff(vars[2], vars[4], 0.00165529) stat = sweepdiffcheck.min_angle_violated(tip_angle, 7) return stat
class Hispeedplane(Problem): def __init__(self): super(Hispeedplane, self).__init__(6, 2, 2) self.types[:] = [ Real(5.04167e-02, 9.9583e-02), Real(-4.425e-03, 4.425e-03), Real(-6.3833e+01, 7.3833e+01), Real(-1.475e+01, 1.475e+01), Real(4.29167e-02, 1.094167e-01), Real(2.067e-02, 9.933e-02) ] self.constraints[:] = ">=0.15" self.krigconst = object self.krigobj1 = object self.krigobj2 = object self.createkrig() def createkrig(self): df = pd.read_csv('../innout/opt_data.csv', sep=',', index_col='Name') data = df.values X = data[:, 0:6].astype(float) y = data[:, 7:9].astype(float) cldat = data[:, 6].astype(float) # define variables lb = np.min(X, axis=0) ub = np.max(X, axis=0) # Set Const Kriging KrigConstInfo = initkriginfo("single") KrigConstInfo["X"] = X KrigConstInfo["y"] = cldat.reshape(-1, 1) KrigConstInfo["nrestart"] = 5 KrigConstInfo["ub"] = ub KrigConstInfo["lb"] = lb KrigConstInfo["optimizer"] = "lbfgsb" KrigConstInfo['limit'] = 0.15 # Set Kriging Info KrigMultiInfo1 = initkriginfo("single") KrigMultiInfo1["X"] = X KrigMultiInfo1["y"] = y[:, 0].reshape(-1, 1) KrigMultiInfo1["nrestart"] = 7 KrigMultiInfo1["ub"] = ub KrigMultiInfo1["lb"] = lb KrigMultiInfo1["optimizer"] = "slsqp" KrigMultiInfo2 = deepcopy(KrigMultiInfo1) KrigMultiInfo2['y'] = y[:, 1].reshape(-1, 1) self.krigobj1 = Kriging(KrigMultiInfo1, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj1.train(parallel=False) loocverr1, _ = self.krigobj1.loocvcalc() self.krigobj2 = Kriging(KrigMultiInfo2, standardization=True, standtype='default', normy=False, trainvar=False) self.krigobj2.train(parallel=False) loocverr2, _ = self.krigobj2.loocvcalc() self.krigconst = Kriging(KrigConstInfo, standardization=True, standtype='default', normy=False, trainvar=False) self.krigconst.train(parallel=False) loocverr3, _ = self.krigconst.loocvcalc() def geomconst(self, vars): # This constraint function should return 1 if the constraint is satisfied and 0 if not. vars = np.array(vars) tip_angle = sweepdiffcheck.sweep_diff(vars[2], vars[4], 0.00165529) stat = sweepdiffcheck.min_angle_violated(tip_angle, 7) return stat # return 1 or 0, 1 is larger than 0.15 then the constraint is satisfied def evaluate(self, solution): vars = np.array(solution.variables) solution.objectives[:] = [ self.krigobj1.predict(vars, 'pred'), self.krigobj2.predict(vars, 'pred') ] solution.constraints[:] = [ self.krigconst.predict(vars, 'pred'), self.geomconst(vars) ]