Exemplo n.º 1
0
def tringulation_search_bound_constantK(inter_par, xi, K, ind_min):
    '''
    This function is the core of constant-K continuous search function.
    :param inter_par: Contains interpolation information w, v.
    :param xi: The union of xE(Evaluated points) and xU(Support points)
    :param K: Tuning parameter for constant-K, K = K*K0. K0 is the range of yE.
    :param ind_min: The correspoding index of minimum of yE.
    :return: The minimizer, xc, and minimum, yc, of continuous search function.
    '''
    inf = 1e+20
    n = xi.shape[0]
    # Delaunay Triangulation
    if n == 1:
        sx = sorted(range(xi.shape[1]), key=lambda x: xi[:, x])
        tri = np.zeros((xi.shape[1] - 1, 2))
        tri[:, 0] = sx[:xi.shape[1] - 1]
        tri[:, 1] = sx[1:]
        tri = tri.astype(np.int32)
    else:
        options = 'Qt Qbb Qc' if n <= 3 else 'Qt Qbb Qc Qx'
        tri = Delaunay(xi.T, qhull_options=options).simplices
        keep = np.ones(len(tri), dtype=bool)
        for i, t in enumerate(tri):
            if abs(np.linalg.det(np.hstack(
                (xi.T[t], np.ones([1, n + 1]).T)))) < 1E-15:
                keep[i] = False  # Point is coplanar, we don't want to keep it
        tri = tri[keep]
    # Search the minimum of the synthetic quadratic model
    Sc = np.zeros([np.shape(tri)[0]])
    Scl = np.zeros([np.shape(tri)[0]])
    for ii in range(np.shape(tri)[0]):
        # R2-circumradius, xc-circumcircle center
        R2, xc = Utils.circhyp(xi[:, tri[ii, :]], n)
        # x is the center of the current simplex
        x = np.dot(xi[:, tri[ii, :]], np.ones([n + 1, 1]) / (n + 1))
        Sc[ii] = interpolation.interpolate_val(
            x, inter_par) - K * (R2 - np.linalg.norm(x - xc)**2)
        if np.sum(ind_min == tri[ii, :]):
            Scl[ii] = np.copy(Sc[ii])
        else:
            Scl[ii] = inf
    # Global one
    t = np.min(Sc)
    ind = np.argmin(Sc)
    R2, xc = Utils.circhyp(xi[:, tri[ind, :]], n)
    x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))
    xm, ym = Constant_K_Search(x, inter_par, xc, R2, K)
    # Local one
    t = np.min(Scl)
    ind = np.argmin(Scl)
    R2, xc = Utils.circhyp(xi[:, tri[ind, :]], n)
    # Notice!! ind_min may have a problem as an index
    x = np.copy(xi[:, ind_min].reshape(-1, 1))
    xml, yml = Constant_K_Search(x, inter_par, xc, R2, K)
    if yml < ym:
        xm = np.copy(xml)
        ym = np.copy(yml)
    xm = xm.reshape(-1, 1)
    ym = ym[0, 0]
    return xm, ym
Exemplo n.º 2
0
def AdaptiveK_search_cost(x, inter_par, xc, R2, y0, K0):
    pos_inf = np.array([[1e+15]])
    # neg_inf to approximate gradient, Probably has problem.
    neg_inf = -1e+5 * np.ones(x.shape)
    x = x.reshape(-1, 1)
    n = x.shape[0]
    p = interpolation.interpolate_val(x, inter_par)
    e = R2 - np.linalg.norm(x - xc)**2
    # Optimize - K0 * e(x) / (p(x) - y0); Extreme point doesnt change.
    if abs(p - y0) < 1e-6:
        M = np.array([[0]])
        DM = np.zeros((n, 1))
    #            DDM = np.zeros((n, n))
    else:
        M = -e * K0 / (p - y0)
        # M = (p - y0) / e
        gp = interpolation.interpolate_grad(x, inter_par)
        ge = -2 * (x - xc)
        #            gge = - 2 * w.T
        #            ggp = interpolation.interpolate_hessian(x, inter_par)
        DM = -ge * K0 / (p - y0) + K0 * e * gp / (p - y0)**2
    #            DDM = -gge * K0/(p-y0) + K0*ge.dot(gp.T)/(p-y0)**2 + (K0*ge.dot(gp.T)+e*K0*ggp)/(p-y0)**2 - (e*K0*2*(p-y0)*np.dot(gp, gp.T))/(p-y0)**4

    # method trust-ncg in scipy optimize can't handle constraints nor bounds.
    # if optm method is chosen as TNC, use DM.T[0]
    return M, DM.T[0]
Exemplo n.º 3
0
def Continuous_search_cost(x, inter_par, xc, R2, K):
    x = x.reshape(-1, 1)
    M = interpolation.interpolate_val(
        x, inter_par) - K * (R2 - np.linalg.norm(x - xc)**2)
    DM = interpolation.interpolate_grad(x, inter_par) + 2 * K * (x - xc)
    # if optm method is chosen as TNC, use DM.T[0]
    return M, DM.T[0]
Exemplo n.º 4
0
def AdaptiveK_Search_p(x0, inter_par_asm, bnds):
    costfun = lambda x: interpolation.interpolate_val(x, inter_par_asm)
    costjac = lambda x: interpolation.interpolate_grad(x, inter_par_asm)
    opt = {'disp': False}
    res = optimize.minimize(costfun,
                            x0.T[0],
                            jac=costjac,
                            method='TNC',
                            bounds=bnds,
                            options=opt)
    x = res.x
    y = res.fun
    return x, y
Exemplo n.º 5
0
def adaptivek_search_cost_snopt(x):
    x = x.reshape(-1, 1)
    folder = folder_path()
    var_opt = io.loadmat(folder + "/opt_info.mat")

    n = var_opt['n'][0, 0]
    xc = var_opt['xc']
    R2 = var_opt['R2'][0, 0]
    y0 = var_opt['y0'][0, 0]
    nF = var_opt['nF'][0, 0]
    A = var_opt['A']
    y_safe = var_opt['y_safe']
    L_safe = var_opt['L_safe'][0, 0]

    # Initialize the output F and G.
    F = np.zeros(nF)

    method = var_opt['inter_par_method'][0]
    inter_par = interpolation.Inter_par(method=method)
    inter_par.w = var_opt['inter_par_w']
    inter_par.v = var_opt['inter_par_v']
    inter_par.xi = var_opt['inter_par_xi']

    p = interpolation.interpolate_val(x, inter_par)
    e = R2 - np.linalg.norm(x - xc)**2
    gp = interpolation.interpolate_grad(x, inter_par)
    ge = -2 * (x - xc)

    de = (1e-10 if abs(p - y0) < 1e-10 else p - y0)

    F[0] = -e / de  # K0 = 0 because only at 1st iteration we only have 1 function evaluation.
    val, idx, x_nn = Utils.mindis(x, inter_par.xi)
    F[-1] = y_safe[idx] - L_safe * np.linalg.norm(x - x_nn)

    if n > 1:  # nD data has n+1 simplex bounds.
        F[1:-1] = (np.dot(
            A, x)).T[0]  # broadcast input array from (3,1) into shape (3).

    DM = -ge / de + e * gp / de**2
    G = np.hstack(
        (DM.flatten(),
         (-L_safe * (x - x_nn) / np.linalg.norm(x - x_nn)).flatten()))

    return F, G
Exemplo n.º 6
0
def adaptivek_p_cost_snopt(x):
    x = x.reshape(-1, 1)
    folder = folder_path()
    var_opt = io.loadmat(folder + "/opt_info_p.mat")

    n = var_opt['n'][0, 0]
    nF = var_opt['nF'][0, 0]
    A = var_opt['A']

    # Initialize the output F and G.
    F = np.zeros(nF)

    method = var_opt['inter_par_method'][0]
    inter_par = interpolation.Inter_par(method=method)
    inter_par.w = var_opt['inter_par_w']
    inter_par.v = var_opt['inter_par_v']
    inter_par.xi = var_opt['inter_par_xi']
    gp = interpolation.interpolate_grad(x, inter_par)
    F[0] = interpolation.interpolate_val(x, inter_par)
    F[1:] = (np.dot(
        A, x)).T[0]  # broadcast input array from (3,1) into shape (3).
    G = gp.flatten()
    return F, G
Exemplo n.º 7
0
def safe_continuous_constantK_search_1d_plot(xE, xU, fun_eval, safe_eval,
                                             L_safe, K, xc_min, Nm):
    '''
    Given evaluated points set xE, and the objective function. Plot the
    interpolation, uncertainty function and continuous search function.
    :param xE:
    :param xU:
    :param fun_eval:
    :param safe_eval:
    :param L_safe:
    :param K:
    :param xc_min:
    :param Nm:
    :return:
    '''
    N = xE.shape[1]
    yE = np.zeros(N)
    for i in range(N):
        yE[i] = fun_eval(xE[:, i])
    inter_par = interpolation.Inter_par(method='NPS')
    inter_par, _ = interpolation.interpolateparameterization(xE, yE, inter_par)

    x = np.linspace(0, 1, 1000)
    y, yp = np.zeros(x.shape), np.zeros(x.shape)

    for i in range(x.shape[0]):
        y[i] = fun_eval(x[i])
        yp[i] = interpolation.interpolate_val(x[i], inter_par)

    xi = np.hstack((xE, xU))
    sx = sorted(range(xi.shape[1]), key=lambda x: xi[:, x])
    tri = np.zeros((xi.shape[1] - 1, 2))
    tri[:, 0] = sx[:xi.shape[1] - 1]
    tri[:, 1] = sx[1:]
    tri = tri.astype(np.int32)

    xe_plot = np.zeros((tri.shape[0], 2000))
    e_plot = np.zeros((tri.shape[0], 2000))
    sc_plot = np.zeros((tri.shape[0], 2000))

    n = xE.shape[0]
    for ii in range(len(tri)):
        temp_x = np.copy(xi[:, tri[ii, :]])
        simplex = xi[:, tri[ii, :]]

        # Determine if the boundary corner exists or not in simplex
        exist = 0
        for i in range(simplex.shape[1]):
            vertice = simplex[:, i].reshape(-1, 1)
            val, _, _ = Utils.mindis(vertice, xE)
            if val == 0:
                pass
            else:
                exist = 1
                break

        x_ = np.linspace(temp_x[0, 0], temp_x[0, 1], 2000)
        temp_Sc = np.zeros(len(x_))
        temp_e = np.zeros(len(x_))
        p = np.zeros(len(x_))
        R2, xc = Utils.circhyp(xi[:, tri[ii, :]], n)
        for jj in range(len(x_)):
            p[jj] = interpolation.interpolate_val(x_[jj], inter_par)

            if exist == 0:
                temp_e[jj] = (R2 - np.linalg.norm(x_[jj] - xc)**2)
            else:
                val, _, _ = Utils.mindis(x_[jj], xE)
                temp_e[jj] = val**(2)
            # val, idx, vertex = Utils.mindis(x_[jj], xE)
            # c = 0.1
            # temp_e[jj] = (val + c) ** (1/2) - c ** (1/2)
            temp_Sc[jj] = p[jj] - K * temp_e[jj]

        e_plot[ii, :] = temp_e
        xe_plot[ii, :] = x_
        sc_plot[ii, :] = temp_Sc

    # The minimizer of continuous search must be subject to safe constraints.
    # sc_min = np.min(sc_plot, axis=1)
    # index_r = np.argmin(sc_min)
    # index_c = np.argmin(sc_plot[index_r, :])
    # sc_min_x = xe_plot[index_r, index_c]
    # sc_min = min(np.min(sc_plot, axis=1))

    safe_plot = {}
    ## plot the safe region
    for ii in range(xE.shape[1]):
        y_safe = safe_eval(xE[:, ii])

        safe_index = []
        y_safe_plot = []
        safe_eval_lip = lambda x: y_safe - L_safe * np.sqrt(
            np.dot((x - xE[:, ii]).T, x - xE[:, ii]))
        for i in range(x.shape[0]):
            safe_val = safe_eval_lip(x[i])
            y_safe_plot.append(safe_val[0])
            if safe_val > 0:
                safe_index.append(i)
        name = str(ii)
        safe_plot[name] = [safe_index, y_safe_plot]

    # ==================  First plot =================
    fig = plt.figure()
    plt.subplot(2, 1, 1)
    # plot the essentials for DeltaDOGS
    plt.plot(x, y, c='k')
    plt.plot(x, yp, c='b')

    for i in range(len(tri)):

        if i == 0 or i == len(tri) - 1:
            amplify_factor = 3
        else:
            amplify_factor = 50

        plt.plot(xe_plot[i, :], sc_plot[i, :], c='r')
        plt.plot(xe_plot[i, :], amplify_factor * e_plot[i, :] - 5.5, c='g')
    plt.scatter(xE, yE, c='b', marker='s')

    yc_min = sc_plot.flat[np.abs(xe_plot - xc_min).argmin()]
    plt.scatter(xc_min, yc_min, c='r', marker='^')

    # plot the safe region in cyan
    xlow, xupp = safe_region_plot(x, safe_plot)
    y_vertical = np.linspace(-2, 2, 100)
    xlow_y_vertical = xlow * np.ones(100)
    xupp_y_vertical = xupp * np.ones(100)
    plt.plot(xlow_y_vertical, y_vertical, color='cyan', linestyle='--')
    plt.plot(xupp_y_vertical, y_vertical, color='cyan', linestyle='--')

    plt.ylim(-6.5, 3.5)
    plt.gca().axes.get_yaxis().set_visible(False)

    # ==================  Second plot =================
    plt.subplot(2, 1, 2)

    y_safe_all = np.zeros(x.shape)
    for i in range(x.shape[0]):
        y_safe_all[i] = safe_eval(x[i])
    plt.plot(x, y_safe_all)
    zero_indicator = np.zeros(x.shape)
    plt.plot(x, zero_indicator, c='k')

    x_scatter = np.hstack((xE, xc_min))
    y_scatter = np.zeros(x_scatter.shape[1])
    for i in range(x_scatter.shape[1]):
        ind = np.argmin(np.abs(x_scatter[:, i] - x))
        y_scatter[i] = y_safe_all[ind]

    plt.scatter(x_scatter[:, :-1][0], y_scatter[:-1], c='b', marker='s')
    plt.scatter(x_scatter[:, -1], y_scatter[-1], c='r', marker='^')

    # plot the safe region
    xlow, xupp = safe_region_plot(x, safe_plot)
    low_idx = np.argmin(np.abs(xlow - x))
    upp_idx = np.argmin(np.abs(xupp - x))
    ylow_vertical = np.linspace(y_safe_all[low_idx], 2, 100)
    yupp_vertical = np.linspace(y_safe_all[upp_idx], 2, 100)
    xlow_y_vertical = xlow * np.ones(100)
    xupp_y_vertical = xupp * np.ones(100)
    plt.plot(xlow_y_vertical, ylow_vertical, color='cyan', linestyle='--')
    plt.plot(xupp_y_vertical, yupp_vertical, color='cyan', linestyle='--')
    plt.ylim(-1, 2.2)
    plt.gca().axes.get_yaxis().set_visible(False)

    # plt.show()
    current_path = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))
    plot_folder = current_path[:-5] + "/plot/DDOGS/0"

    num_iter = xE.shape[1] - 1 + math.log(Nm / 8, 2)
    plt.savefig(plot_folder + '/pic' + str(int(num_iter)) + '.png',
                format='png',
                dpi=250)
    plt.close(fig)
    return
Exemplo n.º 8
0
def continuous_search_1d_plot(xE, fun_eval):
    '''
    Given evaluated points set xE, and the objective function. Plot the
    interpolation, uncertainty function and continuous search function.
    :param xE:          evaluated points set xE
    :param fun_eval:    obj function
    :return:
    '''
    N = xE.shape[1]
    yE = np.zeros(N)
    for i in range(N):
        yE[i] = fun_eval(xE[:, i])
    inter_par = interpolation.Inter_par(method='NPS')
    inter_par, _ = interpolation.interpolateparameterization(xE, yE, inter_par)

    x = np.linspace(0, 1, 1000)
    y, yp = np.zeros(x.shape), np.zeros(x.shape)

    for i in range(x.shape[0]):
        y[i] = fun_eval(x[i])
        yp[i] = interpolation.interpolate_val(x[i], inter_par)

    xi = np.copy(xE)
    sx = sorted(range(xi.shape[1]), key=lambda x: xi[:, x])
    tri = np.zeros((xi.shape[1] - 1, 2))
    tri[:, 0] = sx[:xi.shape[1] - 1]
    tri[:, 1] = sx[1:]
    tri = tri.astype(np.int32)

    xe = np.copy(xi)
    xe_plot = np.zeros((tri.shape[0], 2000))
    e_plot = np.zeros((tri.shape[0], 2000))
    sc_plot = np.zeros((tri.shape[0], 2000))

    n = xE.shape[0]
    K = 3
    for ii in range(len(tri)):
        temp_x = np.copy(xi[:, tri[ii, :]])
        x_ = np.linspace(temp_x[0, 0], temp_x[0, 1], 2000)
        temp_Sc = np.zeros(len(x_))
        temp_e = np.zeros(len(x_))
        p = np.zeros(len(x_))
        R2, xc = Utils.circhyp(xi[:, tri[ii, :]], n)
        for jj in range(len(x_)):
            p[jj] = interpolation.interpolate_val(x_[jj], inter_par)
            temp_e[jj] = (R2 - np.linalg.norm(x_[jj] - xc)**2)
            temp_Sc[jj] = p[jj] - K * temp_e[jj]

        e_plot[ii, :] = temp_e
        xe_plot[ii, :] = x_
        sc_plot[ii, :] = temp_Sc

    sc_min = np.min(sc_plot, axis=1)
    index_r = np.argmin(sc_min)
    index_c = np.argmin(sc_plot[index_r, :])
    sc_min_x = xe_plot[index_r, index_c]
    sc_min = min(np.min(sc_plot, axis=1))

    plt.figure()
    plt.plot(x, y, c='k')
    plt.plot(x, yp, c='b')
    for i in range(len(tri)):
        plt.plot(xe_plot[i, :], sc_plot[i, :], c='r')
        plt.plot(xe_plot[i, :], 3 * e_plot[i, :] - 2.3, c='g')
    plt.scatter(xE, yE, c='b', marker='s')
    plt.scatter(sc_min_x, sc_min, c='r', marker='s')
    plt.ylim(-2.5, 2)
    # plt.gca().axes.get_yaxis().set_visible(False)
    plt.show()
    return
Exemplo n.º 9
0
def triangulation_search_bound_snopt(inter_par, xi, K, ind_min, y_safe,
                                     L_safe):
    # reddir is a vector
    inf = 1e+20
    n = xi.shape[0]  # The dimension of the reduced model.
    xE = inter_par.xi
    # 0: Build up the Delaunay triangulation based on reduced subspace.
    if n == 1:
        sx = sorted(range(xi.shape[1]), key=lambda x: xi[:, x])
        tri = np.zeros((xi.shape[1] - 1, 2))
        tri[:, 0] = sx[:xi.shape[1] - 1]
        tri[:, 1] = sx[1:]
        tri = tri.astype(np.int32)
    else:
        options = 'Qt Qbb Qc' if n <= 3 else 'Qt Qbb Qc Qx'
        tri = Delaunay(xi.T, qhull_options=options).simplices
        keep = np.ones(len(tri), dtype=bool)
        for i, t in enumerate(tri):
            if abs(np.linalg.det(np.hstack(
                (xi.T[t], np.ones([1, n + 1]).T)))) < 1E-15:
                keep[i] = False  # Point is coplanar, we don't want to keep it
        tri = tri[keep]
    # Sc contains the continuous search function value of the center of each Delaunay simplex

    # 1: Identify the minimizer of adaptive K continuous search function
    Sc = np.zeros([np.shape(tri)[0]])
    Scl = np.zeros([np.shape(tri)[0]])
    for ii in range(np.shape(tri)[0]):
        R2, xc = Utils.circhyp(xi[:, tri[ii, :]], n)
        if R2 < inf:
            # initialize with body center of each simplex
            x = np.dot(xi[:, tri[ii, :]], np.ones([n + 1, 1]) / (n + 1))
            exist = unevaluated_vertices_identification(xE, xi[:, tri[ii, :]])
            if exist == 0:
                Sc[ii] = interpolation.interpolate_val(
                    x, inter_par) - K * (R2 - np.linalg.norm(x - xc)**2)
            else:
                val, idx, x_nn = Utils.mindis(x, xE)
                Sc[ii] = interpolation.interpolate_val(x,
                                                       inter_par) - K * val**2

            # discrete min
            # val, idx, vertex = Utils.mindis(x, xE)
            # c = 0.1
            # e = (val + c) ** (1/2) - c ** (1/2)
            # Sc[ii] = interpolation.interpolate_val(x, inter_par) - K * e

            if np.sum(ind_min == tri[ii, :]):
                Scl[ii] = np.copy(Sc[ii])
            else:
                Scl[ii] = inf
        else:
            Scl[ii] = inf
            Sc[ii] = inf

    # Global one, the minimum of Sc has the minimum value of all circumcenters.
    ind = np.argmin(Sc)
    xm, ym = constantk_search_snopt_min(xi[:, tri[ind, :]], inter_par, K,
                                        y_safe, L_safe)

    # Local one
    ind = np.argmin(Scl)
    xml, yml = constantk_search_snopt_min(xi[:, tri[ind, :]], inter_par, K,
                                          y_safe, L_safe)
    if yml < ym:
        xm = np.copy(xml)
        ym = np.copy(yml)
        result = 'local'
    else:
        result = 'glob'
    return xm, ym, result
Exemplo n.º 10
0
def constantk_search_cost_snopt(x):
    x = x.reshape(-1, 1)
    folder = folder_path()
    var_opt = io.loadmat(folder + "/opt_info_ck.mat")

    n = var_opt['n'][0, 0]
    xc = var_opt['xc']
    R2 = var_opt['R2'][0, 0]
    K = var_opt['K'][0, 0]
    nF = var_opt['nF'][0, 0]
    A = var_opt['A']
    L_safe = var_opt['L_safe'][0, 0]
    vertex = var_opt['vertex']
    exist = var_opt['exist'][0, 0]

    # Initialize the output F and G.
    F = np.zeros(nF)

    method = var_opt['inter_par_method'][0]
    inter_par = interpolation.Inter_par(method=method)
    inter_par.w = var_opt['inter_par_w']
    inter_par.v = var_opt['inter_par_v']
    inter_par.xi = var_opt['inter_par_xi']

    p = interpolation.interpolate_val(x, inter_par)
    gp = interpolation.interpolate_grad(x, inter_par)

    if exist == 0:
        e = R2 - np.linalg.norm(x - xc)**2
        ge = -2 * (x - xc)
    else:  # unevaluated boundary corner detected.
        e = (np.dot((x - vertex).T, x - vertex))
        ge = 2 * (x - vertex)

    # discrete min
    # c = 0.1
    # norm_ = np.linalg.norm(x - vertex)
    # norm_ = ( 1e-10 if norm_ < 1e-10 else norm_ )
    # e = (norm_ + c) ** (1/2) - c ** (1/2)
    # ge = (1/2) * (norm_ + c) ** (-1/2) * (x - vertex) / norm_

    F[0] = p - K * e  # K0 = 0 because only at 1st iteration we only have 1 function evaluation.
    # F[1] = - L_safe * np.linalg.norm(x - vertex)
    norm2_difference = np.sqrt(np.dot((x - vertex).T, x - vertex))
    norm2_difference = (1e-10
                        if norm2_difference < 1e-10 else norm2_difference)
    DM = gp - K * ge

    if n == 1:
        # TODO multiple safe con.
        # next line is the safe con
        F[1] = -L_safe * norm2_difference
        # next line is the redundant row.
        F[2] = np.sum(x)

    else:
        # nD data has n+1 simplex bounds.
        F[1:-1] = (np.dot(
            A, x)).T[0]  # broadcast input array from (3,1) into shape (3).
        F[-1] = -L_safe * norm2_difference

    G = np.hstack(
        (DM.flatten(), (-L_safe * (x - vertex) / norm2_difference).flatten()))

    return F, G
Exemplo n.º 11
0
def tringulation_search_bound(inter_par, xi, y0, K0, ind_min):
    inf = 1e+20
    n = xi.shape[0]
    xm, ym = interpolation.inter_min(xi[:, ind_min], inter_par)
    sc_min = inf
    # cse=1
    if ym > y0:
        ym = inf
    # cse =2
    # construct Deluanay tringulation
    if n == 1:
        sx = sorted(range(xi.shape[1]), key=lambda x: xi[:, x])
        tri = np.zeros((xi.shape[1] - 1, 2))
        tri[:, 0] = sx[:xi.shape[1] - 1]
        tri[:, 1] = sx[1:]
        tri = tri.astype(np.int32)
    else:
        options = 'Qt Qbb Qc' if n <= 3 else 'Qt Qbb Qc Qx'
        tri = Delaunay(xi.T, qhull_options=options).simplices
        keep = np.ones(len(tri), dtype=bool)
        for i, t in enumerate(tri):
            if abs(np.linalg.det(np.hstack(
                (xi.T[t], np.ones([1, n + 1]).T)))) < 1E-15:
                keep[i] = False  # Point is coplanar, we don't want to keep it
        tri = tri[keep]

    Sc = np.zeros([np.shape(tri)[0]])
    Scl = np.zeros([np.shape(tri)[0]])
    for ii in range(np.shape(tri)[0]):
        R2, xc = Utils.circhyp(xi[:, tri[ii, :]], n)
        # if R2 != np.inf:
        if R2 < inf:
            # initialze with body center of each simplex
            x = np.dot(xi[:, tri[ii, :]], np.ones([n + 1, 1]) / (n + 1))
            Sc[ii] = (interpolation.interpolate_val(x, inter_par) -
                      y0) / (R2 - np.linalg.norm(x - xc)**2)
            if np.sum(ind_min == tri[ii, :]):
                Scl[ii] = np.copy(Sc[ii])
            else:
                Scl[ii] = inf
        else:
            Scl[ii] = inf
            Sc[ii] = inf

    # Global one
    if np.min(Sc) < 0:
        func = 'p'
        # The minimum of Sc is negative, minimize p(x) instead.
        Scp = np.zeros(tri.shape[0])
        Scpl = np.zeros(tri.shape[0])
        for ii in range(tri.shape[0]):
            x = np.dot(xi[:, tri[ii, :]], np.ones([n + 1, 1]) / (n + 1))
            Scp[ii] = interpolation.interpolate_val(x, inter_par)
            if np.sum(ind_min == tri[ii, :]):
                Scpl[ii] = np.copy(Scp[ii])
            else:
                Scpl[ii] = inf
        else:
            Scpl[ii] = inf
            Scp[ii] = inf
        # Globally minimize p(x)
        ind = np.argmin(Scp)
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))
        simplex_bnds = Utils.search_bounds(xi[:, tri[ind, :]])
        xm, ym = AdaptiveK_Search_p(x, inter_par, simplex_bnds)
        # Locally minimize p(x)
        ind = np.argmin(Scpl)
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))
        simplex_bnds = Utils.search_bounds(xi[:, tri[ind, :]])
        xml, yml = AdaptiveK_Search_p(x, inter_par, simplex_bnds)

    else:
        func = 'sc'
        # Minimize sc(x).
        # Global one, the minimum of Sc has the minimum value of all circumcenters.
        ind = np.argmin(Sc)
        R2, xc = Utils.circhyp(xi[:, tri[ind, :]], n)
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))
        simplex_bnds = Utils.search_bounds(xi[:, tri[ind, :]])
        xm, ym = Adaptive_K_Search(x, inter_par, xc, R2, y0, K0, simplex_bnds)
        # Local one
        ind = np.argmin(Scl)
        R2, xc = Utils.circhyp(xi[:, tri[ind, :]], n)
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))
        simplex_bnds = Utils.search_bounds(xi[:, tri[ind, :]])
        xml, yml = Adaptive_K_Search(x, inter_par, xc, R2, y0, K0,
                                     simplex_bnds)
    if yml < ym:
        xm = np.copy(xml)
        ym = np.copy(yml)
        result = 'local'
    else:
        result = 'glob'
    xm = xm.reshape(-1, 1)
    ym = ym[0, 0]

    return xm, ym, result, func
Exemplo n.º 12
0
def triangulation_search_bound_snopt(inter_par, xi, y0, ind_min, y_safe,
                                     L_safe):
    # reddir is a vector
    inf = 1e+20
    n = xi.shape[0]  # The dimension of the reduced model.

    # 0: Build up the Delaunay triangulation based on reduced subspace.
    if n == 1:
        sx = sorted(range(xi.shape[1]), key=lambda x: xi[:, x])
        tri = np.zeros((xi.shape[1] - 1, 2))
        tri[:, 0] = sx[:xi.shape[1] - 1]
        tri[:, 1] = sx[1:]
        tri = tri.astype(np.int32)
    else:
        options = 'Qt Qbb Qc' if n <= 3 else 'Qt Qbb Qc Qx'
        tri = Delaunay(xi.T, qhull_options=options).simplices
        keep = np.ones(len(tri), dtype=bool)
        for i, t in enumerate(tri):
            if abs(np.linalg.det(np.hstack(
                (xi.T[t], np.ones([1, n + 1]).T)))) < 1E-15:
                keep[i] = False  # Point is coplanar, we don't want to keep it
        tri = tri[keep]
    # Sc contains the continuous search function value of the center of each Delaunay simplex

    # 1: Identify the minimizer of adaptive K continuous search function
    Sc = np.zeros([np.shape(tri)[0]])
    Scl = np.zeros([np.shape(tri)[0]])
    for ii in range(np.shape(tri)[0]):
        R2, xc = Utils.circhyp(xi[:, tri[ii, :]], n)
        if R2 < inf:
            # initialize with body center of each simplex
            x = np.dot(xi[:, tri[ii, :]], np.ones([n + 1, 1]) / (n + 1))
            Sc[ii] = (interpolation.interpolate_val(x, inter_par) -
                      y0) / (R2 - np.linalg.norm(x - xc)**2)
            if np.sum(ind_min == tri[ii, :]):
                Scl[ii] = np.copy(Sc[ii])
            else:
                Scl[ii] = inf
        else:
            Scl[ii] = inf
            Sc[ii] = inf

    if np.min(Sc) < 0:
        func = 'p'
        # The minimum of Sc is negative, minimize p(x) instead.
        Scp = np.zeros(tri.shape[0])
        for ii in range(tri.shape[0]):
            x = np.dot(xi[:, tri[ii, :]], np.ones([n + 1, 1]) / (n + 1))
            Scp[ii] = interpolation.interpolate_val(x, inter_par)
        # Globally minimize p(x)
        ind = np.argmin(Scp)
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))
        xm, ym = adaptiveK_p_snopt_min(x, inter_par, y_safe, L_safe)
        result = 'glob'
    else:
        func = 'sc'
        # Global one, the minimum of Sc has the minimum value of all circumcenters.

        ind = np.argmin(Sc)
        R2, xc = Utils.circhyp(xi[:, tri[ind, :]], n)
        # x is the center of this simplex
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))

        # First find minimizer xr on reduced model, then find the 2D point corresponding to xr. Constrained optm.
        A_simplex, b_simplex = Utils.search_simplex_bounds(xi[:, tri[ind, :]])
        lb_simplex = np.min(xi[:, tri[ind, :]], axis=1)
        ub_simplex = np.max(xi[:, tri[ind, :]], axis=1)

        xm, ym = adaptiveK_search_snopt_min(x, inter_par, xc, R2, y0, K0,
                                            A_simplex, b_simplex, lb_simplex,
                                            ub_simplex, y_safe, L_safe)
        # Local one

        ind = np.argmin(Scl)
        R2, xc = Utils.circhyp(xi[:, tri[ind, :]], n)
        x = np.dot(xi[:, tri[ind, :]], np.ones([n + 1, 1]) / (n + 1))

        A_simplex, b_simplex = Utils.search_simplex_bounds(xi[:, tri[ind, :]])
        lb_simplex = np.min(xi[:, tri[ind, :]], axis=1)
        ub_simplex = np.max(xi[:, tri[ind, :]], axis=1)
        xml, yml = adaptiveK_search_snopt_min(x, inter_par, xc, R2, y0, K0,
                                              A_simplex, b_simplex, lb_simplex,
                                              ub_simplex, y_safe, L_safe)
        if yml < ym:
            xm = np.copy(xml)
            ym = np.copy(yml)
            result = 'local'
        else:
            result = 'glob'
    return xm, ym, result, func
Exemplo n.º 13
0
            num_iter += 1

            K0 = np.ptp(yE, axis=0)  # scale the domain

            [inter_par, yp
             ] = interpolation.interpolateparameterization(xE, yE, inter_par)

            ypmin = np.amin(yp)
            ind_min = np.argmin(yp)

            # Calcuate the unevaluated function:
            yu = np.zeros([1, xU.shape[1]])
            if xU.shape[1] != 0:
                for ii in range(xU.shape[1]):
                    yu[0, ii] = (
                        interpolation.interpolate_val(xU[:, ii], inter_par) -
                        y0) / Utils.mindis(xU[:, ii], xE)[0]
            else:
                yu = np.array([[]])

            xi, ind_min = cartesian_grid.add_sup(xE, xU, ind_min)
            xc, yc, result, func = adaptiveK_snopt_safelearning.triangulation_search_bound_snopt(
                inter_par, xi, y0, ind_min, y_safe, L_safe)

            xc_grid = np.round(xc * Nm) / Nm
            success = Utils.safe_eval_estimate(xE, y_safe, L_safe, xc_grid)
            xc_eval = (np.copy(xc_grid) if success == 1 else np.copy(xc))
            # Dogsplot_sl.safe_continuous_constantK_search_1d_plot(xE, xU, func_eval, safe_eval, L_safe, K, xc_eval, Nm)

            # The following block represents inactivate step: Shahrouz phd thesis P148.
            if Utils.mindis(
Exemplo n.º 14
0
    for kk in range(MeshSize):
        for k in range(iter_max):
            num_iter += 1

            [inter_par, yp
             ] = interpolation.interpolateparameterization(xE, yE, inter_par)

            ypmin = np.amin(yp)
            ind_min = np.argmin(yp)

            # Calcuate the unevaluated function:
            yu = np.zeros([1, xU.shape[1]])
            if xU.shape[1] != 0:
                for ii in range(xU.shape[1]):
                    yu[0, ii] = interpolation.interpolate_val(
                        xU[:, ii],
                        inter_par) - K * Utils.mindis(xU[:, ii], xE)[0]
            else:
                yu = np.array([[]])

            xi, ind_min = cartesian_grid.add_sup(xE, xU, ind_min)
            xc, yc, result = constantK_snopt_safelearning.triangulation_search_bound_snopt(
                inter_par, xi, K, ind_min, y_safe, L_safe)

            xc_grid = np.round(xc * Nm) / Nm
            success = Utils.safe_eval_estimate(xE, y_safe, L_safe, xc_grid)
            xc_eval = (np.copy(xc_grid) if success == 1 else np.copy(xc))
            Dogsplot_sl.safe_continuous_constantK_search_1d_plot(
                xE, xU, func_eval, safe_eval, L_safe, K, xc_eval, Nm)

            if Utils.mindis(