Esempio n. 1
0
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 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
Esempio n. 3
0
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 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 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
Esempio n. 7
0
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
Esempio n. 8
0
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)
        ]