Ejemplo n.º 1
0
def construct_krig(X, y, g, lb, ub):
    # Define input for constraint Kriging
    KrigConstInfo = initkriginfo()
    KrigConstInfo['X'] = X
    KrigConstInfo['y'] = g.reshape(-1,1) # should be in shape (n,1)
    KrigConstInfo['problem'] = exp_const_eval
    KrigConstInfo["nrestart"] = 5
    KrigConstInfo["ub"] = ub
    KrigConstInfo["lb"] = lb
    KrigConstInfo["optimizer"] = "lbfgsb"
    KrigConstInfo['limittype'] = '>='  # value of the expensive constraints should be more than equal 7.7
    KrigConstInfo['limit'] = 7.7

    # Define input for first objective Kriging
    KrigInfo1 = initkriginfo()
    KrigInfo1["X"] = X
    KrigInfo1["y"] = y[:,0].reshape(-1,1)
    KrigInfo1["problem"] = cust_func
    KrigInfo1["nrestart"] = 5
    KrigInfo1["ub"] = ub
    KrigInfo1["lb"] = lb
    KrigInfo1["optimizer"] = "lbfgsb"

    # Define input for second objective Kriging
    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()

    krigobj2 = Kriging(KrigInfo2, standardization=True, standtype='default', normy=False, trainvar=False)
    krigobj2.train(parallel=False)
    loocverr2, _ = krigobj2.loocvcalc()

    krigconst = Kriging(KrigConstInfo, standardization=True, standtype='default', normy=False, trainvar=False)
    krigconst.train(parallel=False)
    loocverrConst, _ = krigconst.loocvcalc()

    print('LOOCV 1: ', loocverr1)
    print('LOOCV 2: ', loocverr2)
    print('LOOCV Constraint: ', loocverrConst)

    # List of Kriging objects, objective and constraints should be separated
    kriglist = [krigobj1, krigobj2]
    expconstlist = [krigconst]

    return kriglist, expconstlist
Ejemplo n.º 2
0
def generate_krig(lb, ub, n_krigsamp, nvar, problem, n_cpu):
    init_krigsamp = mcpopgen(lb=lb, ub=ub, ndim=2, n_order=1, n_coeff=3)
    print("Evaluating Kriging Sample")
    ykrig = problem(init_krigsamp)
    print(np.count_nonzero(ykrig <= 0))

    # Set Kriging Info
    KrigInfo = initkriginfo(1)
    KrigInfo["X"] = init_krigsamp
    KrigInfo["y"] = ykrig
    KrigInfo["nvar"] = nvar
    KrigInfo["nsamp"] = n_krigsamp
    KrigInfo["nrestart"] = 5
    KrigInfo["ub"] = ub
    KrigInfo["lb"] = lb
    KrigInfo["nkernel"] = len(KrigInfo["kernel"])
    KrigInfo["optimizer"] = "lbfgsb"

    #trainkrig
    drm = None
    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 to train Kriging model: ", elapsed, "s")
    print("LOOCV error of Kriging model: ", loocverr, "%")

    return krigobj, loocverr, drm
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
def generate_kriging(n_cpu):
    # 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 = myproblem(X)

    # Initialize KrigInfo
    KrigInfo1 = initkriginfo()
    # Set KrigInfo
    KrigInfo1["X"] = X
    KrigInfo1["y"] = y[:, 0].reshape(-1, 1)
    KrigInfo1["problem"] = myproblem
    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
Ejemplo n.º 5
0
def generate_krig(init_samp, n_krigsamp, nvar, problem):

    # Kriging Sample
    t1 = time.time()
    init_krigsamp = mcpopgen(type="lognormal",
                             ndim=nvar,
                             n_order=1,
                             n_coeff=5,
                             stddev=0.2,
                             mean=1)
    ykrig = evaluate(init_krigsamp, type=problem)
    t2 = time.time()
    print("50 samp eval", t2 - t1)

    # Evaluate Kriging Sample and calculate PoF real
    init_samp_G = evaluate(init_samp, type=problem)
    total_samp = np.hstack((init_samp, init_samp_G)).transpose()
    positive_samp = total_samp[:, total_samp[nvar] >= 0]
    positive_samp = positive_samp.transpose()
    nsamp = np.size(init_samp, 0)
    npos = np.size(positive_samp, 0)
    Pfreal = 1 - npos / nsamp

    lb = np.floor(np.min(init_samp)) * np.ones(shape=[nvar])
    ub = np.ceil(np.max(init_samp)) * np.ones(shape=[nvar])

    # Set Kriging Info
    KrigInfo = initkriginfo("single")
    KrigInfo["X"] = init_krigsamp
    KrigInfo["y"] = ykrig
    KrigInfo["nvar"] = nvar
    KrigInfo["nsamp"] = n_krigsamp
    KrigInfo["nrestart"] = 5
    KrigInfo["ub"] = ub
    KrigInfo["lb"] = lb
    KrigInfo["nkernel"] = len(KrigInfo["kernel"])
    KrigInfo["n_princomp"] = 4
    KrigInfo["optimizer"] = "lbfgsb"

    #trainkrig
    t = time.time()
    krigobj = KPLS(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, Pfreal
Ejemplo n.º 6
0
def generate_krig(init_samp, krigsamp, nvar, problem):

    # Monte Carlo Sampling
    t1 = time.time()
    init_krigsamp = krigsamp
    n_krigsamp = np.size(krigsamp, 0)
    ykrig = evaluate(init_krigsamp, type=problem)
    t2 = time.time()

    init_samp_G = evaluate(init_samp, type=problem)
    total_samp = np.hstack((init_samp, init_samp_G)).transpose()
    positive_samp = total_samp[:, total_samp[nvar] >= 0]
    positive_samp = positive_samp.transpose()
    nsamp = np.size(init_samp, 0)
    npos = np.size(positive_samp, 0)
    Pfreal = 1 - npos / nsamp

    lb = np.floor(np.min(init_samp)) * np.ones(shape=[nvar])
    ub = np.ceil(np.max(init_samp)) * np.ones(shape=[nvar])

    # Set Kriging Info
    KrigInfo = initkriginfo("single")
    KrigInfo["X"] = init_krigsamp
    KrigInfo["y"] = ykrig
    KrigInfo["nvar"] = nvar
    KrigInfo["nsamp"] = n_krigsamp
    KrigInfo["nrestart"] = 5
    KrigInfo["ub"] = ub
    KrigInfo["lb"] = lb
    KrigInfo["nkernel"] = len(KrigInfo["kernel"])
    KrigInfo["optimizer"] = "lbfgsb"

    #trainkrig
    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, Pfreal
Ejemplo n.º 7
0
def generate_krig(init_samp, n_krigsamp, nvar, problem, n_cpu):
    init_krigsamp = mcpopgen(type="lognormal",
                             ndim=nvar,
                             n_order=1,
                             n_coeff=5,
                             stddev=0.2,
                             mean=1)
    print("Evaluating Kriging Sample")
    ykrig = evaluate(init_krigsamp, type=problem)
    print(np.count_nonzero(ykrig <= 0))

    lb = (np.min(init_samp, axis=0))
    ub = (np.max(init_samp, axis=0))

    Pfreal = None

    # Set Kriging Info
    KrigInfo = initkriginfo(1)
    KrigInfo["X"] = init_krigsamp
    KrigInfo["y"] = ykrig
    KrigInfo["nvar"] = nvar
    KrigInfo["nsamp"] = n_krigsamp
    KrigInfo["nrestart"] = 5
    KrigInfo["ub"] = ub
    KrigInfo["lb"] = lb
    KrigInfo["nkernel"] = len(KrigInfo["kernel"])
    KrigInfo["n_princomp"] = 4
    KrigInfo["optimizer"] = "lbfgsb"

    #trainkrig
    drm = None
    t = time.time()
    krigobj = KPLS(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 to train Kriging model: ", elapsed, "s")
    print("LOOCV error of Kriging model: ", loocverr, "%")

    return krigobj, loocverr, drm
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
    def create_krig(self, obj_krig_map=None, con_krig_map=None, n_cpu=1):
        """Initialise and train Kriging models.

        Default settings for the objective or constraint Krigings can
        be overidden by setting the obj_krig_map or con_krig_map dict.
        The dictionary should take the form:

            e.g.
            map = {'default': {'nrestart': 5,
                               'optimizer': 'lbfgsb',
                               },
                   'CD': {'optimizer': 'cobyla',
                          },
                   'CL': {'nrestart': 10,
                          'limittype': '>=',
                          'limit': 0.15},
                          },
                   }

        -where the dict key is used to identify the objective or
        constraint by label (int, if no explicit x_label and y_label set
        previously). The subdict key-value pairs are set in each
        surrogate_models.supports.initinfo.initkriginfo('single'). The
        'default' dict is applied first and can be overridden by the
        following dictionaries.

        Args:
            obj_krig_map (dict(dict()), optional): Map specific settings
                onto objective Kriging models via the labels.
            con_krig_map (dict(dict()), optional): Map specific settings
                onto constraint Kriging models via the labels.
            n_cpu (int, optional): If > 1, uses parallel processing.
                Defaults to 1.
        """
        def apply_krig_map(krig_info, map, label):
            """Helper func. Apply 'default' dict, then labeled dict"""
            if 'default' in map:
                for k, v in map['default'].items():
                    print(f"Setting {label} Kriging defaults '{k}': {v}")
                    krig_info[k] = v
            if label in map:
                for k, v in map[label].items():
                    print(f"Setting {label} Kriging '{k}': {v}")
                    krig_info[k] = v

        # Set up Kriging for each objective
        obj_infos = []
        for i in range(self.n_obj):
            krig_multi_info = initkriginfo()
            krig_multi_info["X"] = self.X
            krig_multi_info["y"] = self.y[:, i].reshape(-1, 1)
            krig_multi_info["ub"] = self.ub
            krig_multi_info["lb"] = self.lb

            label = self.y_labels[i]
            if obj_krig_map is not None:
                apply_krig_map(krig_multi_info, obj_krig_map, label)
            obj_infos.append(krig_multi_info)

        # Set up Kriging for each constraint
        con_infos = []
        for i in range(self.n_con):
            krig_multi_info = initkriginfo()
            krig_multi_info["X"] = self.X
            krig_multi_info["y"] = self.g[:, i].reshape(-1, 1)
            krig_multi_info["ub"] = self.ub
            krig_multi_info["lb"] = self.lb

            label = self.g_labels[i]
            if con_krig_map is not None:
                apply_krig_map(krig_multi_info, con_krig_map, label)
            con_infos.append(krig_multi_info)

        # Train Kriging models
        start_total_train = time.time()
        for i, krig_info in enumerate(obj_infos):
            krig_obj = Kriging(krig_info, standardization=True,
                               standtype='default', normy=False,
                               trainvar=False)
            start_train = time.time()
            krig_obj.train(n_cpu=n_cpu)
            t = time.time() - start_train
            print(f'{self.y_labels[i]} training time: {t:.2f} seconds')
            loocve, _ = krig_obj.loocvcalc()
            print(f'Objective {self.y_labels[i]} LOOCVE: {loocve}')
            self.obj_krig.append(krig_obj)
            self.obj_loocve.append(loocve)
            self.obj_time.append(t)

        for i, krig_info in enumerate(con_infos):
            krig_con = Kriging(krig_info, standardization=True,
                               standtype='default', normy=False,
                               trainvar=False)
            start_train = time.time()
            t = time.time() - start_train
            print(f'{self.y_labels[i]} training time: {t:.2f} seconds')
            krig_con.train(n_cpu=n_cpu)
            loocve, _ = krig_con.loocvcalc()
            print(f'Constraint {self.g_labels[i]} LOOCVE: {loocve}')
            self.con_krig.append(krig_con)
            self.con_loocve.append(loocve)
            self.con_time.append(t)

        elapsed = time.time() - start_total_train
        print(f'Total training time: {elapsed:.2f} seconds')

        # Save data for summary
        self.total_train_time = elapsed
        self.obj_krig_map = obj_krig_map
        self.con_krig_map = con_krig_map