Example #1
0
 def calculate_RMSE_Rsquared(self, optimiser, nt):
     """
     this function calculates the root mean squared error of the interpola-
     ted model for a sample of nt test data
     Input:
         optimiser- optimiser to be used
         nt- the size of the sample test data
     Output:
         RMSE- the root mean squared error of nt sampling points
         Rsquared- the correlation coefficient
     """
     yi_p, yi, yi_dif, yiyi_p, yiyi, yi_pyi_p = [], [], [], [], [], []
     Sample = random.sample([i for i in range(len(self.X))], nt)
     Model = kriging(self.X, self.y, name="%s" % self.name)
     Model.train(optimiser)
     for i, j in enumerate(Sample):
         yi_p.append(Model.predict(self.X[j]))
         yi.append(self.y[j])
         yi_dif.append(yi[i] - yi_p[i])
         yiyi_p.append(yi[i] * yi_p[i])
         yiyi.append(yi[i] * yi[i])
         yi_pyi_p.append(yi_p[i] * yi_p[i])
     RMSE = np.sqrt((sum(yi_dif) ** 2.0) / float(nt))
     Rsquared = (
         (float(nt) * sum(yiyi_p) - sum(yi) * sum(yi_p))
         / (np.sqrt((float(nt) * sum(yiyi) - sum(yi) ** 2.0) * (float(nt) * sum(yi_pyi_p) - sum(yi_p) ** 2.0)))
     ) ** 2.0
     return ["RMSE = %f" % RMSE, "Rsquared = %f" % Rsquared]
Example #2
0
def krig_sp():

    # Initial random move
    norm = 2 * (np.random.rand(2) - 0.5)
    norm /= la.norm(norm)
    path = [np.array(norm) / 10]

    # Start krig model with origin and random displacement
    sp = samplingplan(len(norm))
    hc = 2 * (sp.optimallhc(2**len(norm)) - 0.5)
    krig_init = [[0.0, 0.0]]
    for p in hc:
        krig_init.append(p)
    krig_init = np.array(krig_init)
    k = kriging(krig_init, func(krig_init), testfunction=func, name='simple')
    k.train()
    k.plot()

    last_found = norm
    for n in range(0, 100):

        plt.subplot(221)
        pots.plot(k.predict)

        found = []
        weights = []
        for n in range(0, 10):
            path = spalgo.act_relax_fd(k.predict,
                                       max_range=1.2,
                                       max_step=0.01,
                                       max_iter=1000)
            plt.plot(*zip(*path))
            pt = np.array(path[-1])

            duplicate = False
            for i, p in enumerate(found):
                if la.norm(pt - p) < 0.1:
                    weights[i] += 1
                    duplicate = True
                    break
            if duplicate:
                continue

            found.append(pt)
            weights.append(1)

        imax = weights.index(max(weights))
        last_found = found[imax]
        plt.plot([last_found[0]], [last_found[1]], marker="+")

        plt.subplot(222)
        pots.plot(pots.pot)
        plt.plot(*zip(*path))

        k.addPoint(path[-1], func([last_found]))
        k.train()

        plt.draw()
        plt.pause(0.1)
        plt.clf()
Example #3
0
def test1():

    sp = samplingplan(2)
    x = []
    for n in range(0, 4):
        x.append(np.random.rand(2) * 2 - 1)
    x = np.array(x) * 2

    # Next, we define the problem we would like to solve
    testfun = func
    y = testfun(x)

    # Now that we have our initial data, we can create an instance of a Kriging model
    k = kriging(x, y, testfunction=testfun, name='simple')
    k.train()

    # Now, five infill points are added. Note that the model is re-trained after each point is added
    numiter = 10
    for i in range(numiter):
        print 'Infill iteration {0} of {1}....'.format(i + 1, numiter)
        newpoints = k.infill(1)
        for point in newpoints:
            k.addPoint(point, testfun([point])[0])
        k.train()

    # And plot the results
    k.plot()
Example #4
0
    def initial_import(self, plot=False):
        '''
        Loads LHS evaluations and initialises the kriging model.
        '''
        with open('init_lhs.p', 'rb') as fp:
            self.lhs_record = leek.load(fp)
        X = self.lhs_record['location']
        y = self.lhs_record['value']
        print(X)
        print(y)

        # Adds the LHS evaluations to other history tracking dictionaries.
        for ii in range(len(y)):
            self.eval_hist['location'].append(list(copy.deepcopy(X[ii])))
            self.eval_hist['value'].append(copy.deepcopy(y[ii]))
            self.krig_hist['location'].append(list(copy.deepcopy(X[ii])))
            self.krig_hist['value'].append(copy.deepcopy(y[ii]))

        # Creates either a kriging or regression kriging model
        print("Setting up Kriging Model")
        if self.regressionswitch == True:
            self.krig = regression_kriging(X, y, name='simple', testPoints=250)
        else:
            self.krig = kriging(X, y, name='simple', testPoints=100)

        # Train model
        self.krig.train(optimizer='ga')
        self.krig.snapshot()

        # Displays initial kriging representation of function.
        if plot == True:
            self.krig.plot()
Example #5
0
 def calculate_RMSE_Rsquared(self, optimiser, nt):
     """
     this function calculates the root mean squared error of the interpola-
     ted model for a sample of nt test data
     Input:
         optimiser- optimiser to be used
         nt- the size of the sample test data
     Output:
         RMSE- the root mean squared error of nt sampling points
         Rsquared- the correlation coefficient
     """
     yi_p, yi, yi_dif, yiyi_p, yiyi, yi_pyi_p = [], [], [], [], [], []
     Sample = random.sample([i for i in range(len(self.X))], nt)
     Model = kriging(self.X, self.y, name='%s' % self.name)
     Model.train(optimiser)
     for i, j in enumerate(Sample):
         yi_p.append(Model.predict(self.X[j]))
         yi.append(self.y[j])
         yi_dif.append(yi[i] - yi_p[i])
         yiyi_p.append(yi[i]*yi_p[i])
         yiyi.append(yi[i]*yi[i])
         yi_pyi_p.append(yi_p[i]*yi_p[i])
     RMSE = np.sqrt((sum(yi_dif)**2.) / float(nt))
     Rsquared = ((float(nt)*sum(yiyi_p) - sum(yi)*sum(yi_p)) /
                 (np.sqrt((float(nt)*sum(yiyi) - sum(yi)**2.) *
                  (float(nt)*sum(yi_pyi_p) - sum(yi_p)**2.))))**2.
     return ['RMSE = %f' % RMSE, 'Rsquared = %f' % Rsquared]
Example #6
0
    def train(self, mode='lhs', **kwargs):
        target = []

        if mode is 'lhs':
            for feature in self.features:
                params = SWANParams(drf=feature[0],
                                    cfw=feature[1],
                                    stpm=feature[2],
                                    fidelity_time=self.fidelity[0],
                                    fidelity_space=self.fidelity[1])
                target.append(
                    self.fake_model.output_from_model(
                        params=params)[self.station])

        target = np.asarray(target)

        start_time = datetime.now().strftime(DATE_FORMAT)
        print(
            f'{start_time}: starting to train kriging model '
            f'with {self.points_to_train} points for station: {self.station}')

        krig = kriging(self.features, target, name='multikrieg')
        krig.train(optimizer='ga')
        self.krig = krig

        end_time = datetime.now().strftime(DATE_FORMAT)
        print(f'{end_time}: finished to train kriging model with'
              f' {self.points_to_train} points for station: {self.station}')
Example #7
0
    def krigagemMatrix(self, colY, colX, colZ):

        self.X = list(self.matrix[colY, colX, colZ])
        testfun = testfunctions().squared
        self.y = testfun(self.X)
        self.k = kriging(self.X, self.y, testfunction=testfun, testPoints=300)

        self.k.train()
        self.k.snapshot()
Example #8
0
def train_krig(X, y, optimizer='pso'):
    """
    :param X:
    :param y:
    :param optimizer: 'pso', 'ga' (much more faster)
    :return:
    """
    krig = kriging(X, y, name='multikrieg')
    krig.train(optimizer=optimizer)
    return krig
Example #9
0
 def leave_n_out(self, q=5):
     '''
     :param q: the numer of groups to split the model data inot
     :return:
     '''
     mseArray = []
     for i in splitArrays(self.model, 5):
         testk = kriging(i[0], i[1])
         testk.train()
         for j in range(len(i[2])):
             mseArray.append(mse(i[3][j], testk.predict(i[2][j])))
         del (testk)
     return np.average(mseArray), np.std(mseArray)
Example #10
0
 def leave_n_out(self, q=5):
     """
     :param q: the numer of groups to split the model data inot
     :return:
     """
     mseArray = []
     for i in splitArrays(self.model, 5):
         testk = kriging(i[0], i[1])
         testk.train()
         for j in range(len(i[2])):
             mseArray.append(mse(i[3][j], testk.predict(i[2][j])))
         del (testk)
     return np.average(mseArray), np.std(mseArray)
def build_kriging_models(obj_values, inputs, constraints):
    """
    Uses the pyKriging package to build a surrogate formulation of an optimization problem
    
    Inputs:
    obj_values          [array]
    inputs              [array]
    constraints         [array]
    
    Outputs:
    obj_surrogate            callable function(inputs)
    constraints_surrogates   [array(callable function(inputs))]
    surrogate_function       callable function(inputs): returns the objective, constraints, and whether it succeeded as an int 
    
    """
    
    
    #now build surrogates based on these
    t1=time.time()
  
    
    obj_surrogate = kriging(inputs, obj_values , name='simple')
    obj_surrogate.train()
    constraints_surrogates = []
   
    
    
    for j in range(len(constraints[0,:])): 
        constraint_surrogate = kriging(inputs, constraints[:,j] , name='simple')
        constraint_surrogate.train()
        constraints_surrogates.append(constraint_surrogate)
    t2=time.time()
    print 'time to set up = ', t2-t1
    surrogate_function    = Surrogate_Problem()
    surrogate_function.obj_surrogate          = obj_surrogate
    surrogate_function.constraints_surrogates = constraints_surrogates
    
    return obj_surrogate, constraints_surrogates, surrogate_function    
Example #12
0
    def kriging_model_check(self, k):
        lr, lc = np.shape(k.X)[0], np.shape(k.X)[1]
        X_c, y_c = np.zeros((lr - 1, lc)), np.zeros(lr - 1)
        # time is of the essence
        if lr < 8:
            sz = 3
        elif lr > 12:
            sz = 1
        else:
            sz = 2
        test_points = np.random.randint(lr, size=sz)  # just three pts

        #   check several randomly chosen point sets
        for i in range(0, len(test_points)):
            a = test_points[i]
            test_X = k.X[a, :]
            compar_y = k.y[a]
            print 'kriging check : a : ' + str(a)
            # make comparison models
            if a != 0 or a != lr:
                X_c[0:a / 2, :] = k.X[0:a / 2, :]
                X_c[a / 2:a - 1, :] = k.X[a / 2 + 1:a, :]
                y_c[0:a / 2] = k.y[0:a / 2]
                y_c[a / 2:a - 1] = k.y[a / 2 + 1:a]

            elif a == 0:
                X_c[:, :] = k.X[1:a, :]
                y_c[:] = k.y[1:a]
            else:
                X_c[:, :] = k.X[0:a - 1, :]
                y_c[:] = k.y[0:a - 1]
            # compare
            k_c = kriging(X_c, y_c)
            k_c.train()
            test_y = k_c.predict(test_X)
            # these ifs prevent infs :/
            if compar_y != 0 and test_y != 0:
                frac_check = np.abs((compar_y - test_y) / compar_y)
            elif compar_y == 0 and test_y != 0:
                frac_check = np.abs((compar_y - test_y) / test_y)
            else:
                frac_check = 0
            # update holder numbers
            if i == 0:
                frac = frac_check
            if frac_check > frac:
                frac = frac_check
            print 'kriging check : largest percentage difference : ' + str(
                frac)
        return frac
def build_kriging_models(obj_values, inputs, constraints):
    """
    Uses the pyKriging package to build a surrogate formulation of an optimization problem
    
    Inputs:
    obj_values          [array]
    inputs              [array]
    constraints         [array]
    
    Outputs:
    obj_surrogate            callable function(inputs)
    constraints_surrogates   [array(callable function(inputs))]
    surrogate_function       callable function(inputs): returns the objective, constraints, and whether it succeeded as an int 
    
    """

    #now build surrogates based on these
    t1 = time.time()

    obj_surrogate = kriging(inputs, obj_values, name='simple')
    obj_surrogate.train()
    constraints_surrogates = []

    for j in range(len(constraints[0, :])):
        constraint_surrogate = kriging(inputs,
                                       constraints[:, j],
                                       name='simple')
        constraint_surrogate.train()
        constraints_surrogates.append(constraint_surrogate)
    t2 = time.time()
    print('time to set up = ', t2 - t1)
    surrogate_function = Surrogate_Problem()
    surrogate_function.obj_surrogate = obj_surrogate
    surrogate_function.constraints_surrogates = constraints_surrogates

    return obj_surrogate, constraints_surrogates, surrogate_function
    def add_point(self, xx, fx):
        """Add a new function evaluation

        :param xx: Point to add
        :param fx: The function value of the point to add
        """
        dim = len(xx)
        self._realloc(dim)
        self.x[self.nump, :] = xx
        self.fx[self.nump, :] = fx
        self.nump += 1

        if self.k is None:
            self.k = kriging(self.x[:self.nump+1, :],
                             self.fx[:self.nump+1, :], self.maxp)
            self.k.train()

        # add point to kriging model
        self.k.addPoint(xx, fx)
        self.updated = False
Example #15
0
    def import_hist(self, plot=False):
        '''
        Imports saved history files enabling quick restarting in the event of a crash
        or other issue.
        '''
        # Import _hist libraries
        with open('krig_hist.p', 'rb') as fp:
            self.krig_hist = leek.load(fp)
        with open('eval_hist.p', 'rb') as fp:
            self.eval_hist = leek.load(fp)

        X=[]
        y=[]
        for ii in range(len(self.krig_hist['value'])):
            X.append(list(copy.deepcopy(self.krig_hist['location'][ii])))
            if type(self.krig_hist['value'][ii]) == np.ndarray:
                y.append(self.krig_hist['value'][ii][0])
            else:
                y.append(self.krig_hist['value'][ii])
            print(type(self.krig_hist['value'][ii]))

        X = np.array(X)
        y = np.array(y)

        # Creates either a kriging or regression kriging model based on 
        # previous values used.
        print("Setting up Kriging Model")
        if self.regressionswitch == True:
            self.krig = regression_kriging(X, y, name='simple', testPoints=100)
        else:
            self.krig = kriging(X, y, name='simple', testPoints=100)

        # Train model
        self.krig.train(optimizer='ga')
        self.krig.snapshot()

        if plot==True:
            self.krig.plot()
Example #16
0
    def execute(self): 
        with open(str(self.json_string_in.getValue()), 'r') as files:
            string = files.read()

        json_dict = demjson.decode(string)
        method = json_dict['type']
        xmin, xmax, ymin, ymax = float(json_dict['bounding_box']['x_min']), float(json_dict['bounding_box']['x_max']), float(json_dict['bounding_box']['y_min']), float(json_dict['bounding_box']['y_max'])
        nx, ny = json_dict['grid_size']['n_x'], json_dict['grid_size']['n_y']
        dx = (xmax - xmin) / nx
        dy = (ymax - ymin) / ny
        X = []
        y = []
        for point in json_dict['point_values']:
            X.append([point['y'], point['x']])
            y.append(point['value'])
        
        self.grid = np.zeros((ny,nx))
        
        if method == 'kriging':
            k = kriging(np.array(X), np.array(y))
            self.status.set('training model...')
            k.train()
            self.status.set('predicting grid values...')
            for i in range(ny):
                for j in range(nx):
                    cell = np.array([ymin + dy * j + .5 * dy, xmin + dx * i + .5 * dx])
                    self.grid[i][j] = k.predict(cell)
        else:
            print 'method is not supported'
        
        output = demjson.encode({"raster":self.grid})
        print str(output)
        tmpFile=tempfile.NamedTemporaryFile(suffix='.json',prefix='tmp', delete=False)
        tmpFile.write(str(output))
        
        
        self.array_string_out.setValue(tmpFile.name)
Example #17
0
def AK_MCS(S1, S2, N1, S0):
    """main function for AK-MCS: active learning method based on meta-model kriging
    with simulation of Monte Carlo
    S1: first selection population in Monte Carlo to construct the meta-model
    S2: explained before
    S0: the whole population generated by Monte Carlo
    return 
    pf_matrix: the list of all calculated probabilities in every loop
    ev_matrix: the list of all evaluation value of performance function by meta-model
    U_min: the list of all minimum learning functions U in every loop
    return them for the data treatment"""

    # INITIALIZATION
    #constraction of first model of Kriging

    def calcul_prob_kriging(S0):
        """calculate the probability of failure by evaluating the sign of evaluation value"""
        S_NEG = []
        S_POS = []
        for s in S0:
            ev_s = k.predict(s)
            if ev_s <= 0:
                S_NEG.append(s)
            else:
                S_POS.append(s)
        pf = round(float(len(S_NEG)) / float(len(S0)),
                   4)  # take 4 number behind the .
        print("failure probability= " + str(pf))

        return pf, S_POS, S_NEG

    def calcul_u(s):
        """calculate the learning function U"""
        pred_s = k.predict(s)
        sigma = math.sqrt(
            k.predict_var(s))  # k.predict_var is the variance of Kriging
        u = abs(pred_s) / sigma
        return sigma, u, pred_s

    # results of first calcultion
    t1 = S1[:, 0]  # thickness in S1
    E1 = S1[:, 1]  # modulu Young in S1
    fichier = open('result.txt', 'w')
    fichier.close()

    for ts, Es in zip(t1, E1):
        Y = lanch_calcul(ts, Es)

        result = open("result.txt",
                      'a')  # 'a' means open a file for append(write only str)
        result.write(str(Y) + '\n')
        result.close()

    result_name = main_dir + "result.txt"
    result_file = open(result_name, "r")
    Y = np.loadtxt('result.txt')  #
    result_file.close()
    # library of kriging
    k = kriging(S1, Y, name='simple')
    k.train()
    pred_matrix = [k.predict(s) for s in S1]

    #first calculation of probability
    print("\n******* Initialization********")
    # calculate failure probability
    pf, S_POS, S_NEG = calcul_prob_kriging(S0)
    plot_prob(S_POS, S_NEG, S1)

    #AK_MCS(S1, S2, N1, S0)
    #ITERATION
    u_min = 1.9  #in case of the initial u_min > 5
    iteration = 0
    pf_matrix = []
    U_min = []
    NEW_S1 = S1
    NEW_S2 = S2
    while u_min < 2:
        S1 = NEW_S1
        S2 = NEW_S2
        U = []
        G = []
        iteration += 1
        print("\n****  iteration number: " + str(iteration) + "****")

        for j in range(0, len(S2), 1):
            s2 = S2[j]
            sigma, u, ev_s = calcul_u(s2)
            U.append(u)
            G.append(ev_s)

        u_min = min(U)  # search for the min U
        ind_s2min = U.index(u_min)
        G_add = G[ind_s2min]
        pred_matrix.append(G_add)
        U_min.append(u_min)  # collect all the Umin in every interation
        print("learning function U : ", u_min)

        add_s = S2[ind_s2min]
        NEW_S1 = np.append(S1, [add_s], axis=0)
        NEW_S2 = np.delete(S2, ind_s2min, axis=0)

        t_add = add_s[0]
        E_add = add_s[1]
        add_Y = lanch_calcul(t_add, E_add)
        k.addPoint(add_s, add_Y)
        k.train()

        #  calculation failure probability
        pf, S_POS, S_NEG = calcul_prob_kriging(S0)
        if (iteration % 10) == 0:
            plot_prob(S_POS, S_NEG, S1)
        else:
            pass

        pf_matrix.append(pf)
    return pf_matrix, U_min
from pyKriging.samplingplan import samplingplan

# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(2)
X = sp.optimallhc(15)

# Next, we define the problem we would like to solve
testfun = pyKriging.testfunctions().paulson1
y = testfun(X)

# We can choose between a ga and a pso here
optimizer = 'ga'

# Now that we have our initial data, we can create an instance of a kriging model
print 'Setting up the Kriging Model'
k = kriging(X, y, testfunction=testfun, name='simple_ei', testPoints=300)
k.train(optimizer=optimizer)
k.snapshot()

# Add 10 points based on model error reduction
for i in range(5):
    newpoints = k.infill(1, method='error')
    for point in newpoints:
        print 'Adding point {}'.format(point)
        k.addPoint(point, testfun(point)[0])
    k.train(optimizer=optimizer)
    k.snapshot()

# Infill ten points based on the expected improvement criterion
for i in range(5):
    newpoints = k.infill(1, method='ei')
Example #19
0
    def calculate_SCVR(self, optimiser='pso', plot=0):
        """
        this function calculates the standardised cross-validated residual
        (SCVR)
        value for each sampling point.
        Return an nx1 array with the SCVR value of each sampling point. If plot
        is 1, then plot scvr vs doe and y_pred vs y.
        Input:
            optimiser- optimiser to be used
            plot- if 1 plots scvr vs doe and y_pred vs y
        Output:
            predict_list- list with different interpolated kriging models
            excluding
                            each time one point of the sampling plan
            predict_varr- list with the square root of the posterior variance
            scvr- the scvr as proposed by Jones et al. (Journal of global
            optimisation, 13: 455-492, 1998)
        """
        y_normalised = (self.y - np.min(self.y)) / (np.max(self.y) -
                                                    np.min(self.y))
        y_ = np.copy(self.y)
        Kriging_models_i, list_arrays, list_ys, train_list = [], [], [], []

        for i in range(self.n):
            exclude_value = [i]
            idx = list(set(range(self.n)) - set(exclude_value))
            list_arrays.append(self.X[idx])
            list_ys.append(y_[idx])
            Kriging_models_i.append(kriging(list_arrays[i], list_ys[i],
                                            name='%s' % self.name))
            train_list.append(Kriging_models_i[i].train(optimizer=optimiser))
            self.predict_list.append(Kriging_models_i[i].predict(self.X[i]))
            self.predict_varr.append(Kriging_models_i[i].predict_var(self.X[i]))
            self.scvr.append((y_normalised[i] - Kriging_models_i[i].normy(self.predict_list[i])) /self.predict_varr[i][0, 0])
        if plot == 0:
            return self.predict_list, self.predict_varr, self.scvr
        elif plot == 1:
            fig = plt.figure(figsize=(12, 8), facecolor='w', edgecolor='k',
                             linewidth= 2.0, frameon=True)
            ax1 = fig.add_subplot(1, 2, 1)
            ax1.scatter([i for i in range(1, self.n+1)], self.scvr, alpha=0.5,
                        edgecolor='black', facecolor='b', linewidth=2.)
            ax1.plot([i for i in range(0, self.n+3)], [3]*(self.n+3), 'r')
            ax1.plot([i for i in range(0, self.n+3)], [-3]*(self.n+3), 'r')
            ax1.set_xlim(0, self.n+2)
            ax1.set_ylim(-4, 4)
            ax1.set_xlabel('DoE individual')
            ax1.set_ylabel('SCVR')
            ax2 = fig.add_subplot(1, 2, 2)
            ax2.scatter(self.predict_list, self.y, alpha=0.5,
                        edgecolor='black', facecolor='b', linewidth=2.)
            if np.max(self.y) > 0:
                ax2.set_ylim(0, np.max(self.y) + 0.00001)
                ax2.set_xlim(0, max(self.predict_list) + 0.00001)
            else:
                ax2.set_ylim(0, np.min(self.y) - 0.00001)
                ax2.set_xlim(0, min(self.predict_list) - 0.00001)
            ax2.plot(ax2.get_xlim(), ax2.get_ylim(), ls="-", c=".3")
            ax2.set_xlabel('predicted y')
            ax2.set_ylabel('y')

            plt.show()
            return self.predict_list, self.predict_varr, self.scvr
        else:
            raise ValueError('value for plot should be either 0 or 1')
G11 = convertCtrReal(G1, aG, bG)
S1[:, 0] = E11
S1[:, 1] = G11

E2 = S2[:, 0]
G2 = S2[:, 1]
E22 = convertCtrReal(E2, aE, bE)
G22 = convertCtrReal(G2, aG, bG)
S2[:, 0] = E22
S2[:, 1] = G22
#print(S2)
# *********************** BUILDING INITIAL KRIGING MODEL *********************#
#
#print(len(S1))
#print(len(Y))
k = kriging(S1, Y, name='simple')

k.train()
ev_matrix = [k.predict(s) for s in S1]

# ********************************** PLOT ************************************#
print("\n****  Initialization  ****")
#
E0 = S0[:, 0]
G0 = S0[:, 1]
E00 = convertCtrReal(E0, aE, bE)
G00 = convertCtrReal(G0, aG, bG)
S0[:, 0] = E00
S0[:, 1] = G00
#
#umin = 0
Example #21
0
    def calculate_SCVR(self, optimiser="pso", plot=0):
        """
        this function calculates the standardised cross-validated residual
        (SCVR)
        value for each sampling point.
        Return an nx1 array with the SCVR value of each sampling point. If plot
        is 1, then plot scvr vs doe and y_pred vs y.
        Input:
            optimiser- optimiser to be used
            plot- if 1 plots scvr vs doe and y_pred vs y
        Output:
            predict_list- list with different interpolated kriging models
            excluding
                            each time one point of the sampling plan
            predict_varr- list with the square root of the posterior variance
            scvr- the scvr as proposed by Jones et al. (Journal of global
            optimisation, 13: 455-492, 1998)
        """
        y_normalised = (self.y - np.min(self.y)) / (np.max(self.y) - np.min(self.y))
        y_ = np.copy(self.y)
        Kriging_models_i, list_arrays, list_ys, train_list = [], [], [], []

        for i in range(self.n):
            exclude_value = [i]
            idx = list(set(range(self.n)) - set(exclude_value))
            list_arrays.append(self.X[idx])
            list_ys.append(y_[idx])
            Kriging_models_i.append(kriging(list_arrays[i], list_ys[i], name="%s" % self.name))
            train_list.append(Kriging_models_i[i].train(optimizer=optimiser))
            self.predict_list.append(Kriging_models_i[i].predict(self.X[i]))
            self.predict_varr.append(Kriging_models_i[i].predict_var(self.X[i]))
            self.scvr.append(
                (y_normalised[i] - Kriging_models_i[i].normy(self.predict_list[i])) / self.predict_varr[i][0, 0]
            )
        if plot == 0:
            return self.predict_list, self.predict_varr, self.scvr
        elif plot == 1:
            fig = plt.figure(figsize=(12, 8), facecolor="w", edgecolor="k", linewidth=2.0, frameon=True)
            ax1 = fig.add_subplot(1, 2, 1)
            ax1.scatter(
                [i for i in range(1, self.n + 1)], self.scvr, alpha=0.5, edgecolor="black", facecolor="b", linewidth=2.0
            )
            ax1.plot([i for i in range(0, self.n + 3)], [3] * (self.n + 3), "r")
            ax1.plot([i for i in range(0, self.n + 3)], [-3] * (self.n + 3), "r")
            ax1.set_xlim(0, self.n + 2)
            ax1.set_ylim(-4, 4)
            ax1.set_xlabel("DoE individual")
            ax1.set_ylabel("SCVR")
            ax2 = fig.add_subplot(1, 2, 2)
            ax2.scatter(self.predict_list, self.y, alpha=0.5, edgecolor="black", facecolor="b", linewidth=2.0)
            if np.max(self.y) > 0:
                ax2.set_ylim(0, np.max(self.y) + 0.00001)
                ax2.set_xlim(0, max(self.predict_list) + 0.00001)
            else:
                ax2.set_ylim(0, np.min(self.y) - 0.00001)
                ax2.set_xlim(0, min(self.predict_list) - 0.00001)
            ax2.plot(ax2.get_xlim(), ax2.get_ylim(), ls="-", c=".3")
            ax2.set_xlabel("predicted y")
            ax2.set_ylabel("y")

            plt.show()
            return self.predict_list, self.predict_varr, self.scvr
        else:
            raise ValueError("value for plot should be either 0 or 1")
sys.path.append("../../")

from util import io

import numpy as np

import pyKriging
from pyKriging.krige import kriging
from pyKriging.samplingplan import samplingplan
from pyKriging.testfunctions import testfunctions

dataFilePath = "../../data/samples-data.data"
labelsFilePath = "../../data/samples-data-labels.data"


def getTrainData():
    """获取训练数据"""
    X = io.getData(dataFilePath)
    Y = io.getData(labelsFilePath)

    return X, Y


X, Y = getTrainData()

k = kriging(X, Y)
k.train()
# k.snapshot()
testX = np.array([-60., 168.3, -121.16, -56.34, -77.])
pdtValue = k.predict(X)
print("pdtValue: {}, label: {}".format(pdtValue, Y[0]))
Example #23
0
import pyKriging
from pyKriging.krige import kriging
from pyKriging.samplingplan import samplingplan
from pyKriging.testfunctions import testfunctions


# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(3)
X = sp.optimallhc(30)

# Next, we define the problem we would like to solve
testfun = testfunctions().squared
y = testfun(X)

# Now that we have our initial data, we can create an instance of a kriging model
k = kriging(X, y, testfunction=testfun, testPoints=300)

# The model is then trained
k.train()
k.snapshot()

# It's typically beneficial to add additional points based on the results of the initial training
# The infill method can be  used for this
# In this example, we will add nine points in three batches. The model gets trained after each stage
for i in range(10):
    print k.history['rsquared'][-1]
    print 'Infill iteration {0}'.format(i + 1)
    infillPoints = k.infill(10)

    # Evaluate the infill points and add them back to the Kriging model
    for point in infillPoints:
 def train(self):
     self.model = kriging(self.data.values[:, :-1], self.data.values[:, -1])
     self.model.train()
Example #25
0
import pyKriging  
from pyKriging.krige import kriging  
from pyKriging.samplingplan import samplingplan

# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(2)  
X = sp.optimallhc(20)

# Next, we define the problem we would like to solve
testfun = pyKriging.testfunctions().branin  
y = testfun(X)

# Now that we have our initial data, we can create an instance of a Kriging model
k = kriging(X, y, testfunction=testfun, name='simple')  
k.train()

# Now, five infill points are added. Note that the model is re-trained after each point is added
numiter = 5  
for i in range(numiter):  
    print 'Infill iteration {0} of {1}....'.format(i + 1, numiter)
    newpoints = k.infill(1)
    for point in newpoints:
        k.addPoint(point, testfun(point)[0])
    k.train()

# And plot the results
k.plot()  
Example #26
0
from pyKriging.utilities import saveModel

# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(2)
X = sp.optimallhc(5)

# Next, we define the problem we would like to solve
testfun = pyKriging.testfunctions().branin

# We generate our observed values based on our sampling plan and the test function
y = testfun(X)

print 'Setting up the Kriging Model'
cvMSE = []
# Now that we have our initial data, we can create an instance of a kriging model
k = kriging(X, y, testfunction=testfun, name='simple', testPoints=300)
k.train(optimizer='ga')
k.snapshot()
# cv = Cross_Validation(k)
# cvMSE.append( cv.leave_n_out(q=5)[0] )

k.plot()
for i in range(15):
    print i
    newpoints = k.infill(1)
    for point in newpoints:
        # print 'Adding point {}'.format(point)
        k.addPoint(point, testfun(point)[0])
    k.train(optimizer='pso')
    k.snapshot()
    # cv = Cross_Validation(k)
Example #27
0
                             ]).T.astype("float32")  # shape=[M,2]
    pnum = int(np.sum(target_mask / 255))  # 1枚の画像内にある予測しないといけない点の数

    startFlag = True

    pred_imgs = []

    # 範囲ごとのMAEを保存するリスト
    maes, mae0, mae05_2, maes_sep = [], [], [], []
    for ite, img in enumerate(testImg):
        print("{} of {} images".format(ite + 1, testImg.shape[0]))
        y = img[maskImg * seaImg > 0]

        #pdb.set_trace()
        stime = time.time()
        k = kriging(X, y, name='InpaintingKriging')
        k.train()
        train_time = time.time() - stime
        print("{}sec for training".format(train_time), end=" ")
        pickle.dump(k, open("krigingmodel{}-{}".format(exi, ite + exi_s),
                            "wb"))

        #pdb.set_trace()
        Prediction = CuPyPredicter(k)

        def predict(xs):
            res = np.zeros_like(img).astype("float32")
            for iterate, p in enumerate(xs):
                print("\r predict ite:{}-point:{} ".format(ite + 1, iterate),
                      end="")
                i = int(p[0])
Example #28
0
def min_mode():

    # Initial random move
    norm = 2 * (np.random.rand(2) - 0.5)
    norm /= la.norm(norm)
    path = [np.array(norm) / 10]

    # Start krig model with origin and random displacement
    krig_init = np.array([[0, 0], path[-1]])
    k = kriging(krig_init, func(krig_init), testfunction=func, name='simple')
    k.train()
    plt.show()

    for n in range(0, 100):

        v = pots.pot(path[-1])
        g = pots.grad(path[-1])

        h = pots.hess_fd(path[-1], k.predict)

        k.addPoint(path[-1], v)
        k.train()

        plt.subplot(221)
        pots.plot(k.predict)
        plt.plot(*zip(*path))

        plt.subplot(222)
        pots.plot()
        plt.plot(*zip(*path))

        plt.draw()
        plt.pause(0.001)
        plt.clf()

        evals, evecs = la.eig(h)
        both = zip(evals, evecs)
        both.sort()
        evals, evecs = zip(*both)

        dots = [np.abs(np.dot(e, path[-1])) for e in evecs]
        imax = dots.index(max(dots))

        delta = np.zeros(len(path[-1]))
        for i in range(0, len(evecs)):

            # Change the sign of the step
            # along the minimum mode
            sign = 1
            if i == 0 or i == imax: sign = -1

            # Move according to newton raphson
            delta -= evecs[i] * np.dot(g, evecs[i]) * sign / evals[i]

    # Impose max step size
        if la.norm(delta) > 0.1:
            delta = 0.1 * delta / la.norm(delta)

        path.append(path[-1] + delta)

    return path
Example #29
0
def main():
    t1 = datetime.datetime.now()
    
    '''    Modify these values to choose fidelity level
        and modeling method    '''
    # FIDELITY LEVEL: low = 0;  med = 1;  high = 2
    fidelity_level  = 2 # 0 or 2 atm.
    # MODEL METHOD
    model_method    = 'k'  # k or ck
    # ALLOWED TIME (s) 
    hours           = 1.
    mins            = 30.
    secs            = 0.
    time            = hours*60*60 + mins*60 + secs
    # 
    fidelity_method = [fidelity_level, model_method]
    from_file = False
    
    nexus = setup(fidelity_method)
    optprob = nexus.optimization_problem
    surr = nexus.surrogate_data
#    surr.plot_from_file(nexus,filename)
#    quit()

    inpstr = optprob.inputs[:,0]
    objstr = optprob.objective[:,0]
    print "inputs" + str(inpstr)
    print 'object' + str(objstr)

    inputstring=''
    for var in inpstr:
        if not inputstring == '':
            if var == 'rcp_tip':
                contr = 'tp'
            elif var == 'dihedral':
                contr = 'do'
            else:
                contr = var[0:2]
            inputstring = inputstring+contr
        else:
            inputstring = var
    savename = model_method+str(fidelity_level)+'-' +'30km'+'2-'+ inputstring+'-'
    print 'Files will be saved under prefix : \"' + savename +'\"'

    t1b = datetime.datetime.now()

    optprob.inputs[:,1] = [20., 0.05]
    print nexus.objective()
    quit()







    if model_method == 'k' and not from_file:
#        a = nexus.objective()
        surr.sample_plan.size = 40
        surr.sample_plan.lhc_type   = 'o'
        surr.sample_plan.time       = time
#        surr.create_sample(nexus)   # DO THIS JUST FOR CORNERS
#        t1b = datetime.datetime.now()
 
        data1 = np.genfromtxt('./rawresults/kriging/k2-30km2-spantp-data.csv',delimiter=',')
#        surr.sample_plan.lhc = data1[:,0:2]
        surr.X = data1[:,0:2]
        surr.y = data1[:,2:4]

#        data1 = data1[:,0:2]
#        data2 = np.genfromtxt('./results/lfhf/k20-2km25-sptp-exp.csv',delimiter=',')
#        surr.sample_plan.lhc = data[0:5,:]
#        surr.sample_plan.lhc = np.array([[5.0, 0.1],[5.0, 1.0], [20.0,0.1],[20.0,1.0]]) # span taper
#        surr.sample_plan.lhc = np.array([[5,	0.1,	0],[5,	0.1, 45],[5,	1,	0],[5,	1,	45],[20,	0.1,	0],[20,	0.1,	45],[20,	1,	0],[20,	1,	45]])
#        surr.sample_plan.lhc = data1[24:30,:]

#        data2 = data1[30:36,:]
#        data3 = data1[36:44,:]



#        print surr.sample_plan.lhc
#        quit()
#        surr.evaluate_of(nexus)
        t2a = datetime.datetime.now()
        surr.single_fid_kriging(nexus, improve=False)
#        k0 = kriging(data1[:,0:2],data1[:,2])
#        k1 = kriging(data2[:,0:2],data2[:,2])
#        k0.train()
#        k1.train()

        k0 = surr.model0
        k1 = surr.model1
        if len(inpstr) <= 2:
            surr.get_plot(nexus,model = k0,zlabel='L/D',mapname='winter')
            surr.get_plot(nexus,model = k1,zlabel='Mass(kg)',mapname='copper')
        elif len(inpstr) == 3:
            surr.get_plot3X(nexus,model=k1,zlabel='L/D',mapname='winter')
            surr.get_plot3X(nexus,model=k2,zlabel='Mass (kg)',mapname='copper')
        saveModel(surr.model0,'./rawresults/kriging/'+savename+str(t2a)+'-surrmod0'+'.pkl')
        saveModel(surr.model1,'./rawresults/kriging/'+savename+str(t2a)+'-surrmod1'+'.pkl')
        t2b = datetime.datetime.now()
#        quit()
        with open('./rawresults/kriging/'+savename+str(t2b)+'-lhc'+str(np.shape(surr.X))+'.csv','w+b') as filec:
            wrc=csv.writer(filec)
            for i in range(0,np.shape(surr.X)[0]):
                row = []
                for item in surr.X[i,:]:
                    row.append(item)
                row.append(surr.y[i,0])
                row.append(surr.y[i,1])
                wrc.writerow(row)



#        surr.sample_plan.lhc = data2

#        print surr.sample_plan.lhc
#        quit()
#        surr.evaluate_of(nexus)
#        t2b = datetime.datetime.now()
##        quit()
#        with open('./rawresults/kriging/'+savename+str(t2b)+'-lhc'+str(surr.sample_plan.size)+'.csv','w+b') as filec:
#            wrc=csv.writer(filec)
#            for i in range(0,np.shape(surr.X)[0]):
#                row = []
#                for item in surr.X[i,:]:
#                    row.append(item)
#                row.append(surr.y[i,0])
#                row.append(surr.y[i,1])
#                wrc.writerow(row)

#        surr.sample_plan.lhc = data3
#        surr.evaluate_of(nexus)
#        t2b = datetime.datetime.now()
#        with open('./rawresults/kriging/'+savename+str(t2b)+'-lhc'+str(surr.sample_plan.size)+'.csv','w+b') as filec:
#            wrc=csv.writer(filec)
#            for i in range(0,np.shape(surr.X)[0]):
#                row = []
#                for item in surr.X[i,:]:
#                    row.append(item)
#                row.append(surr.y[i,0])
#                row.append(surr.y[i,1])
#                wrc.writerow(row)

#        quit()


        ke = surr.model0
        km = surr.model1

#        if len(inpstr) <= 3:
#            surr.get_plot(nexus,model=ke,zlabel='L/D',mapname='winter')#surr.modelck0)
#            surr.get_plot(nexus,model=km,zlabel='Mass (kg)',mapname='copper')

#        quit()
        t2 = datetime.datetime.now()
        iw = optimizer(nexus,'k')
#        quit()
        iw.evolve()
        t3 = datetime.datetime.now()
        iw.show_results(title='Pareto-front')
        iw.show_gen_results()
        iw.save_results(name = './rawresults/kriging/'+savename+'-lhc'+str(surr.sample_plan.size)+'_opt')


        print 'For LHC size: ' + str(np.shape(surr.X))
        print 'Setup time = ' + str((t1b-t1).total_seconds()) + ' sec'
        print 'Model runs time = ' + str((t2a-t1b).total_seconds()) + ' sec'
        print 'Optimisation time = ' + str((t3-t2b).total_seconds()) + ' sec'

        print '\n\n Kriging model info:\n'
        print 'Thetas L/D : ' + str(surr.model0.theta)
        print 'Thetas mass: ' + str(surr.model1.theta)
        print 'p L/D : ' + str(surr.model0.pl)
        print 'p mass: ' + str(surr.model1.pl)

#####################################################################
    elif model_method == 'ck' and not from_file:


        surr.sample_plan.lhc_type   = 'o'
        surr.sample_plan.time   = time
        t2a = surr.hybrid_cokriging(nexus,'./rawresults/cokriging'+savename)
#        surr.save_ck('./results/'+savename+'cokriging') # should be unnecessary
        t2 = datetime.datetime.now()
        # plotting methods
        if len(inpstr) <= 2:
            try:
                surr.get_plot(nexus,model=surr.model1, model1=surr.modelck0,zlabel='L/D') # mod1 is l/d
            except:
                print 'no second l/d model?'
                try:
                    surr.get_plot(nexus,model=surr.modelck0) # just plot ck model
                except:
                    print 'no models?'
        iw = optimizer(nexus,'ck')
        iw.evolve()
        iw.show_results(title='')
        iw.show_gen_results()
        iw.save_results(name = './rawresults/cokriging/'+savename+'opt')
#    quit()


######################### FROM FILE
    elif model_method == 'ck' and from_file:

        # surr.load_ck() # no .pkl
        # surr.model999 = loadModel() # pykriging built, need .pkl
        surr.load_ck('./results/lfhf/k20ck2-2km25-spantp-cokriging-verysmall')
        datac = np.genfromtxt('./results/lfhf/k20-2km25-sptp-cheap-verysmall.csv',delimiter=',')
        datae = np.genfromtxt('./results/lfhf/k20-2km25-sptp-exp-verysmall.csv',delimiter=',')
#        datae = np.genfromtxt('./results/lfhf/k20-2km25-sptp-exp.csv',delimiter=',')
#        k1 = loadModel('./krigingres/avl/k1-2km25-spantp-surrmod0.pkl')
#        print k0.theta
#        print k1.theta
        ck0 = surr.modelck0
#        k0 = kriging(datac[:,0:2],datac[:,2])
#        k0.train()
#        km = kriging(datac[:,0:2],datac[:,3])
#        km.train()
#        saveModel(km,'./results/lfhf/k20ck2-2km25-spantp-cokriging-verysmall.pkl')
#        surr.model0 = k0
        k1 = kriging(datae[:,0:2],datae[:,2])
        k1.train()
#        surr.model1 = k1
        t2 = datetime.datetime.now()
        a = ck0.predict([7.8,0.38])
        b = k1.predict([7.8,0.38])

#        surr.get_plot(nexus,model=ck0,zlabel='L/D',mapname='winter')
        iw = optimizer(nexus,'k')
#        if len(inpstr) <= 2:
        surr.get_plot(nexus,model=k1,model1=ck0,zlabel='L/D',mapname='winter')#model1=k1,
##            surr.get_plot(nexus,model=k1,model1=ck0,zlabel='L/D')
##            surr.get_plot(nexus,model=ke,model1=surr.modelck0,zlabel='L/D')
#        elif len(inpstr) <= 3:
#            surr.get_plot3X(nexus,model=k, model1=surr.modelck0)
        quit()
        iw.evolve()
        iw.show_results(title='')
        iw.show_gen_results()
        iw.save_results(name = './rawresults/cokriging/ck2-30km2-'+inputstring)

######################### FROM FILE
    elif model_method == 'k' and from_file:

        p1  = [6., .4, 30.]
        p2  = [12., .6, 15.]
        p3  = [17., .2, 47.]
        p4  = [8., .8, 8.]


#        m10 = loadModel('../results/studies/k0-30km2-lhc10-spantpsw-surrmod0.pkl')       
#        m20 = loadModel('../results/studies/k0-30km2-spantpsw--lhc20-surrmod0.pkl')
#        m30 = loadModel('../results/studies/k0-30km2-spantpsw--lhc30-surrmod0.pkl')
#        m40 = loadModel('../results/studies/k0-30km2-spantpsw--lhc40-surrmod0.pkl')
#        m50 = loadModel('../results/studies/k0-30km2-spantpsw--lhc50-surrmod0.pkl')

        d1  = np.genfromtxt('./rawresults/kriging/k2-30km2-spantpsw-lhc22x3.csv',delimiter=',')
        d2  = np.genfromtxt('./rawresults/kriging/k2-30km2-spantpsw-lhc28x3.csv',delimiter=',')
        dx1 = d1[:,0:3]
        dy1 = d1[:,3:5]
        dx2 = d2[:,0:3]
        dy2 = d2[:,3:5]

        m10 = kriging(dx1,dy1[:,0])
        m10.train()
        k1m = kriging(dx1,dy1[:,1])
        m20 = kriging(dx2,dy2[:,0])
        m20.train()
        k2m = kriging(dx2,dy2[:,1])
#        m10 = loadModel('./rawresults/kriging/k2-30km2-spantpsw-lhc22x3.pkl')
#        k1m = loadModel('./rawresults/kriging/k2-30km2-spantpsw-lhc22x3-mass.pkl')
#        m20 = loadModel('./rawresults/kriging/k2-30km2-spantpsw-lhc28x3.pkl')
#        k2m = loadModel('./rawresults/kriging/k2-30km2-spantpsw-lhc28x3-mass.pkl')



        print '\n\n Kriging model info:\n'
        print 'For LHC size: ' + str('12,3')
        print 'Thetas L/D : ' + str(m10.theta)
        print 'Thetas mass: ' + str(k1m.theta)
        print 'p L/D : ' + str(m10.pl)
        print 'p mass: ' + str(k1m.pl)

        print 'For LHC size: ' + str('18,3')
        print 'Thetas L/D : ' + str(m20.theta)
        print 'Thetas mass: ' + str(k2m.theta)
        print 'p L/D : ' + str(m20.pl)
        print 'p mass: ' + str(k2m.pl)


#        optprob.inputs[:,1] = p1
#        p1o = nexus.objective()

#        optprob.inputs[:,1] = p2
#        p2o = nexus.objective()
#        optprob.inputs[:,1] = p3
#        p3o = nexus.objective()

#        optprob.inputs[:,1] = p4
#        p4o = nexus.objective()
#        print p4o
#        quit()

        print '==='+str(p1)
        print 'corel: ' + str(-8.758738709620385)
        print m10.predict(p1)
        print m20.predict(p1)
#        print m30.predict(p1)
#        print m40.predict(p1)
#        print m50.predict(p1)
#        
        print '==='+str(p2)
        print 'corel: ' + str(-1.7228475920765616)
        print m10.predict(p2)
        print m20.predict(p2)
#        print m30.predict(p2)
#        print m40.predict(p2)
#        print m50.predict(p2)

        print '==='+str(p3)
        print 'corel: ' + str(-4.1441922666)
        print m10.predict(p3)
        print m20.predict(p3)
#        print m30.predict(p3)
#        print m40.predict(p3)
#        print m50.predict(p3)

        print '==='+str(p4)
        print 'corel: ' + str(-5.2278043817745)
        print m10.predict(p4)
        print m20.predict(p4)
#        print m30.predict(p4)
#        print m40.predict(p4)
#        print m50.predict(p4)





   
    return
from pyKriging.samplingplan import samplingplan

# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(2)
X = sp.optimallhc(15)

# Next, we define the problem we would like to solve
testfun = pyKriging.testfunctions().branin

# We generate our observed values based on our sampling plan and the test function
y = testfun(X)

print('Setting up the Kriging Model')

# Now that we have our initial data, we can create an instance of a kriging model
k = kriging(X, y, testfunction=testfun, name='simple', testPoints=250)
k.train(optimizer='ga')
k.snapshot()

for i in range(5):
    newpoints = k.infill(2)
    for point in newpoints:
        print(('Adding point {}'.format(point)))
        k.addPoint(point, testfun(point)[0])
    k.train()
    k.snapshot()

# #And plot the model

print('Now plotting final results...')
k.plot()
Example #31
0
    def single_fid_kriging(self, nexus, improve=False):
        print "Generating kriging model ..."
        """
            single_fidelity_kriging
            obj_choice      =   choice of objective to generate model, 0 for first/default
                                subsequent functions for others.
            
            At the moment this is only single fidelity kriging.
            Will look into allowing both co-kriging and heirarchical kriging
            Option  = 'heirarchical' or 'hk', and 'cokriging', 'co-kriging' 
                    or 'ck'
        """
        if not isinstance(self.y, np.ndarray):
            y = np.array(self.y)
        else:
            y = self.y

        if len(y[0, :]) > 1:
            two_mod = True
        else:
            two_mod = False

        #   check input types
        if not isinstance(self.X, np.ndarray):
            X = np.array(self.X)
        else:
            X = self.X

        if two_mod:
            y0 = y[:, 0]
            y1 = y[:, 1]
        else:
            y0 = y
#        print '\n ================= \n'
#        print X
#        print y

        of = self.objective_function

        tk1 = time.time()

        k0 = kriging(X, y0)
        k0.train()
        print 'First training: model 0'
        if two_mod:
            k1 = kriging(X, y1)
            k1.train()
            print 'First training: model 1'

        tk2 = time.time()
        #        print k.X
        #        print k.y
        print 'Kriging model initial setup time : ' + str(tk2 - tk1) + ' sec'

        # We need to check before we update (getting an uninvertible numpy matrix :/ )
        # Method 1: remove a point and compare its original predicted value with k_red.predict(x)
        #        check   = self.kriging_model_check(k)
        # Method 2: make a new point
        checkpt1 = self.scale_points(k0.X.mean(0), nexus)
        checkpt2 = self.scale_points(k0.X[0:np.shape(X)[0] / 2, :].mean(0),
                                     nexus)

        if improve:
            ei = np.minimum(k0.expimp(checkpt1), k0.expimp(checkpt2))
        else:
            ei = 1.e-10
        counter = 0
        gain_small = False
        print 'precheck : ' + str(ei)
        while ei >= 1.e-4 and not gain_small and counter <= 5:  # and improve:# guarantees a few updates
            print 'Kriging infill iteration ' + str(counter)
            newpoints = k0.infill(1)  # this is being a wild prick >:(
            #            print 'are these reasonable' +str(newpoints)
            ei = k0.expimp(newpoints)
            print 'Expected Improvement : EI(x) = ' + str(ei)
            if ei < 1e-4:
                gain_small = True
            else:
                for point in newpoints:
                    value = of(point, nexus, -1)
                    #                    print "\n\n value ===== " + str(value)
                    if len(value[0]) > 1:
                        obj_0 = value[0][0]
                        obj_1 = value[0][1]
                    else:
                        obj_0 = value[0]
                    if value[1] == 0:
                        k0.addPoint(point, obj_0)
                        k0.train()
                        if len(value[0]) > 1 and two_mod:
                            k1.addPoint(point, obj_1)
                            k1.train()
            counter = counter + 1
        tk3 = time.time()
        print 'Exit criteria: gain_small = ' + str(
            gain_small) + '  counter = ' + str(counter) + '/10'
        print 'Kriging model improvement time : ' + str(tk3 - tk2) + ' sec'
        print 'Total time : ' + str(tk3 - tk1) + ' sec'

        self.model0 = k0
        print 'model 0 added'
        if two_mod:
            self.model1 = k1
            print 'model 1 added'

        return  #k
Example #32
0
import unittest
import numpy as np
import pdb
from geomdl import BSpline
from geomdl import exchange
from geomdl.visualization import VisMPL as vis
import pyKriging
from pyKriging import matrixops
from pyKriging import samplingplan as sp
from geomdl import utilities
from pyKriging.krige import kriging
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter

xt = np.array([[0.], [1.], [2.], [3], [4.]])
yt = np.array([0., 1., 1.5, 0.5, 1.0])

# krig_cube = kriging(self.X, self.y, testfunction=none, reg='Cubic')
krig_cube = kriging(xt, yt)

krig_cube.train()

# And plot the results
krig_cube.plot1d()
krig_cube.plot_likelihood1d()
# krig_cube.plot_trend()
# krig_cube.plot_rad()
pdb.set_trace()
def AK_MCS(S1, S2, N1, S0):
    """main function for AK-MCS: active learning method based on meta-model kriging
    with simulation of Monte Carlo
    S1: first selection population in Monte Carlo to construct the meta-model
    S2: explained before
    S0: the whole population generated by Monte Carlo
    return 
    pf_matrix: the list of all calculated probabilities in every loop
    ev_matrix: the list of all evaluation value of performance function by meta-model
    U_min: the list of all minimum learning functions U in every loop
    return them for the data treatment"""
       
    def calcul_prob(S0):
        """calculate the probability of failure by evaluating the sign of evaluation value"""
        S_NEG = []
        S_POS = []
        for s in S0:
                ev_s = k.predict(s)
                if ev_s<=0:
                    S_NEG.append(s)
                else:
                    S_POS.append(s)
        pf = float(len(S_NEG))/float(len(S0))
        print("failure probability= "+str(pf))
        
        return pf, S_POS, S_NEG

    def calcul_u(s):
        """calculate the learning funtion U"""
        ev_s = k.predict(s)
        sigma = math.sqrt(k.predict_var(s))  # k.predict_var si the variance of kriging
        u = abs(ev_s)/sigma
        return sigma, u, ev_s
    
    Y1 = []
    for i in range(N1):
        s1 = S1[i]
        y1 = volkersen(s1)
        Y1.append(y1)
        
    
    k = kriging(S1, Y1, name='simple')
    k.train()
    k.plot()
    ev_matrix = [k.predict(s) for s in S1]
    # ********************************** PLOT ************************************#
    print("\n****  Initialization  ****")
    NEW_S1 = S1
    NEW_S2 = S2
    
    # calculate failure probability 
    pf_matrix = []
    pf, S_POS, S_NEG = calcul_prob(S0)
    pf_matrix.append(pf)
    
        
    plot_prob(S_POS,S_NEG, S1)
    
# ******************************* ITERATION **********************************# 
    u_min = 1.9  #in case of the initial u_min > 5
    iteration = 0
    
    U_min = []
    while u_min < 2.:   
        S1 = NEW_S1
        S2 = NEW_S2
        U = []
        G = []
        iteration += 1   
        print("\n****  iteration number: "+str(iteration)+ "****")
            
        for j in range(0, len(S2), 1):
            s2 = S2[j]
    
            sigma, u, ev_s = calcul_u(s2)
            U.append(u)
            G.append(ev_s)
            
        u_min = min(U)
        ind_s2min = U.index(u_min)
        G_add = G[ind_s2min]
        ev_matrix.append(G_add)
        U_min.append(u_min)
        print("learning function U : ", u_min)
       
        add_s = S2[ind_s2min]
        NEW_S1 = np.append(S1,[add_s],axis=0)
        print("thickness and moudul: ", add_s)
        print("number of construction ponits: "+str(len(NEW_S1)))
        NEW_S2 = np.delete(S2,ind_s2min,axis=0)
    #   print("add_s= "+str(add_s))
        y1 = volkersen(add_s)
        Y1.append(y1)
        k.addPoint(add_s,y1)
        k.train()
        #k.plot(show=True)
    
    # calculate failure probability
        pf, S_POS, S_NEG = calcul_prob(S0)
        # plot figures
        plot_prob(S_POS, S_NEG, NEW_S1, [add_s])    
        pf_matrix.append(pf)
        
        '''X_plot1 = np.arange(-3., 3.1, 0.1)
        X_plot2 = []
        nPlot = len(X_plot1)
            
        for i in range(nPlot):
            Y_SOL = []
            for j in range(nPlot):
                s_predict = [X_plot1[i],X_plot1[j]]
                y_sol = k.predict(s_predict)
                Y_SOL.append(y_sol)
            POLY = np.polyfit(X_plot1, Y_SOL,3)  # coefficient
            #print(POLY)
            x_sol = root(funcSolPoly, 0.)
    #       print("x_sol= "+str(x_sol.x[0]))
            X_plot2.append(x_sol.x[0])
        plotFunction(X_plot1, X_plot2, 'c-', 'Kriging model')
        if iteration >= 2 and pf_matrix[iteration-1] == pf_matrix[iteration-2]:
            break'''
#    print(ev_matrix)
    return pf_matrix, ev_matrix, U_min
Example #34
0
#!/usr/bin/env python

import pyKriging  
from pyKriging.krige import kriging  
from pyKriging.samplingplan import samplingplan

# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(2)  
X = sp.optimallhc(20)

# Next, we define the problem we would like to solve
testfun = pyKriging.testfunctions().branin  
y = testfun(X)

# Now that we have our initial data, we can create an instance of a Kriging model
k = kriging(X, y, testfunction=testfun, name='simple')  
k.train()

# Now, five infill points are added. Note that the model is re-trained after each point is added
numiter = 5  
for i in range(numiter):  
    print 'Infill iteration {0} of {1}....'.format(i + 1, numiter)
    newpoints = k.infill(1)
    for point in newpoints:
        k.addPoint(point, testfun(point)[0])
    k.train()

# And plot the results
k.plot()  
Example #35
0
    return X, Y


# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(3)
X = sp.optimallhc(30)

print("[DEBUG] X: {}".format(X))

# Next, we define the problem we would like to solve
testfun = testfunctions().squared
y = testfun(X)

# Now that we have our initial data, we can create an instance of a kriging model
k = kriging(X, y, testfunction=testfun, testPoints=300)

# The model is then trained
k.train()
k.snapshot()

# It's typically beneficial to add additional points based on the results of the initial training
# The infill method can be  used for this
# In this example, we will add nine points in three batches. The model gets trained after each stage
for i in range(10):
    print(k.history['rsquared'][-1])
    print('Infill iteration {0}'.format(i + 1))
    infillPoints = k.infill(10)

    # Evaluate the infill points and add them back to the Kriging model
    for point in infillPoints:
Example #36
-1
    def interpGrid(self):
        ptx = np.array(self.x)
        pty = np.array(self.y)
        z = np.array(self.z)
        print(len(ptx), 'length x')
        # remove duplicate x values
        dups = self.checkdups(self.x)
        ptx = np.delete(ptx, dups)
        pty = np.delete(pty, dups)
        z = np.delete(z, dups)
        print(len(ptx), 'length x')

        pts = zip(self.x, self.y)
        # gridx, gridy = np.mgrid[uprLeft[0]:lwrRight[0]:50j,uprLeft[1]:lwrRight[1]:50j]
        gridx, gridy = np.mgrid[self.ext[0]:self.ext[1]:self.ncol*1j,
                            self.ext[2]:self.ext[3]:self.nrow*1j]
        ##### using griddata #####
        if self.interptype == 'griddata':
            from scipy.interpolate import griddata
            self.grid = griddata(pts,self.z,(gridx,gridy), method='cubic',fill_value=-3e30)
        #### examples from 
        ##### http://stackoverflow.com/questions/24978052/interpolation-over-regular-grid-in-python
        ##### using radial basis function ####
        if self.interptype == 'rbf':
            import scipy.interpolate as interpolate
            f = interpolate.Rbf(pty, ptx, z, function='linear')
            self.grid = f(gridy, gridx)

        ##### using kriging ####
        if self.interptype == 'gauss':
            from sklearn.gaussian_process import GaussianProcess
            # print math.sqrt(np.var(z))
            # gp = GaussianProcess(theta0=0.1, thetaL=1.1, thetaU=10.1, nugget=0.000001)
            if np.min(z) <= 0:
                thetaL = 0.1
            else:
                thetaL = np.min(z)

            print(np.min(z), thetaL, np.max(z))
            # gp = GaussianProcess(regr='quadratic',corr='cubic',theta0=np.min(z),thetaL=thetaL,thetaU=np.max(z),nugget=0.05)
            gp = GaussianProcess(theta0=500,thetaL=100,thetaU=2000)
            gp.fit(X=np.column_stack([pty,ptx]),y=z)
            rr_cc_as_cols = np.column_stack([gridy.flatten(), gridx.flatten()])
            self.grid = gp.predict(rr_cc_as_cols).reshape((self.ncol,self.nrow))

        if self.interptype == 'krig':
            import pyKriging  
            from pyKriging.krige import kriging  
            from pyKriging.samplingplan import samplingplan
            
            # The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
            # sp = samplingplan(2)  
            # X = sp.optimallhc(20)
            # print(X)
            X = np.array(zip(self.x, self.y))
            print(X.shape)
            
            # Next, we define the problem we would like to solve
            testfun = pyKriging.testfunctions().squared  
            # y = testfun(X)
            # print(y)
            y = self.z
            
            # Now that we have our initial data, we can create an instance of a Kriging model
            k = kriging(X, y)#, testfunction=testfun, name='simple')  
            # k.train()
            
            # Now, five infill points are added. Note that the model is re-trained after each point is added
            # numiter = 5  
            # for i in range(numiter):  
                # print 'Infill iteration {0} of {1}....'.format(i + 1, numiter)
                # newpoints = k.infill(1)
                # for point in newpoints:
                    # k.addPoint(point, testfun(point)[0])
                # k.train()
            
            # And plot the results
            k.plot() 
            sys.exit()

        self.grid[self.grid < self.minval] = -2.99999989403e+030 #self.minval
        self.grid = np.flipud(self.grid.T)