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
def Constant_K_Search(simplex, inter_par, K, lb=[], ub=[]): n = simplex.shape[0] R2, xc = Utils.circhyp(simplex, n) x = np.dot(simplex, np.ones([n + 1, 1]) / (n + 1)) costfun = lambda x: Continuous_search_cost(x, inter_par, xc, R2, K)[0] costjac = lambda x: Continuous_search_cost(x, inter_par, xc, R2, K)[1] opt = {'disp': False} bnds = tuple([(0, 1) for i in range(int(n))]) res = optimize.minimize(costfun, x, jac=costjac, method='TNC', bounds=bnds, options=opt) x = res.x y = res.fun return x, y
def continuous_search_plot1D(self, adogs): ''' Plot the continuous search function. :return: ''' xU = Utils.bounds(adogs.lb, adogs.ub, adogs.n) if adogs.iter_type == 'scmin' and Utils.mindis(adogs.xc, xU)[0] > 1e-6: xi = np.hstack((adogs.xE[:, :-1], adogs.xU)) else: xi = np.hstack((adogs.xE, adogs.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) num_plot_points = 2000 xe_plot = np.zeros((tri.shape[0], num_plot_points)) e_plot = np.zeros((tri.shape[0], num_plot_points)) sc_plot = np.zeros((tri.shape[0], num_plot_points)) for ii in range(len(tri)): simplex_range = np.copy(xi[:, tri[ii, :]]) # Discretized mesh grid on x direction in one simplex x = np.linspace(simplex_range[0, 0], simplex_range[0, 1], num_plot_points) # Circumradius and circumcenter for current simplex R2, xc = Utils.circhyp(xi[:, tri[ii, :]], adogs.n) for jj in range(len(x)): # Interpolation p(x) p = adogs.inter_par.inter_val(x[jj]) # Uncertainty function e(x) e_plot[ii, jj] = (R2 - np.linalg.norm(x[jj] - xc)**2) # Continuous search function s(x) sc_plot[ii, jj] = p - adogs.K * adogs.K0 * e_plot[ii, jj] xe_plot[ii, :] = np.copy(x) for i in range(len(tri)): # Plot the uncertainty function e(x) # plt.plot(xe_plot[i, :], e_plot[i, :] - 5.5, c='g', label=r'$e(x)$') # Plot the continuous search function sc(x) plt.plot(xe_plot[i, :], sc_plot[i, :], 'r--', zorder=20, label=r'$S_c(x)$') yc_min = sc_plot.flat[np.abs(xe_plot - adogs.xc).argmin()] plt.scatter(adogs.xc, yc_min, c=(1, 0.769, 0.122), marker='D', zorder=15, label=r'min $S_c(x)$')
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
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
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
def constantk_search_snopt_min(simplex, inter_par, K, y_safe, L_safe): ''' The function F is composed as: 1st - objective 2nd to nth - simplex bounds n+1 th .. - safe constraints :param x0 : The mass-center of delaunay simplex :param inter_par : :param xc : :param R2: :param y0: :param K0: :param A_simplex: :param b_simplex: :param lb_simplex: :param ub_simplex: :param y_safe: :param L_safe: :return: ''' xE = inter_par.xi n = xE.shape[0] # Determine if the boundary corner exists in simplex, if boundary corner detected: # e(x) = || x - x' ||^2_2 # else, e(x) is the regular uncertainty function. exist = unevaluated_vertices_identification(xE, simplex) # ------- ADD THE FOLLOWING LINE WHEN DEBUGGING -------- # simplex = xi[:, tri[ind, :]] # ------- ADD THE FOLLOWING LINE WHEN DEBUGGING -------- # Find the minimizer of the search fucntion in a simplex using SNOPT package. R2, xc = Utils.circhyp(simplex, n) # x is the center of this simplex x = np.dot(simplex, 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(simplex) lb_simplex = np.min(simplex, axis=1) ub_simplex = np.max(simplex, axis=1) inf = 1.0e+20 m = n + 1 # The number of constraints which is determined by the number of simplex boundaries. assert m == A_simplex.shape[0], 'The No. of simplex constraints is wrong' # TODO: multiple safe constraints in future. # nF: The number of problem functions in F(x), including the objective function, linear and nonlinear constraints. # ObjRow indicates the number of objective row in F(x). ObjRow = 1 # solve for constrained minimization of safe learning within each open ball of the vertices of simplex. # Then choose the one with the minimum continuous function value. x_solver = np.empty(shape=[n, 0]) y_solver = [] for i in range(n + 1): vertex = simplex[:, i].reshape(-1, 1) # First find the y_safe[vertex]: val, idx, x_nn = Utils.mindis(vertex, xE) if val > 1e-10: # This vertex is a boundary corner point. No safe-guarantee, we do not optimize around support points. continue else: # TODO: multiple safe constraints in future. safe_bounds = y_safe[idx] if n > 1: # The first function in F(x) is the objective function, the rest are m simplex constraints. # The last part of functions in F(x) is the safe constraints. # In high dimension, A_simplex make sure that linear_derivative_A won't be all zero. nF = 1 + m + 1 # the last 1 is the safe constraint. Flow = np.hstack((-inf, b_simplex.T[0], -safe_bounds)) Fupp = inf * np.ones(nF) # The lower and upper bounds of variables x. xlow = np.copy(lb_simplex) xupp = np.copy(ub_simplex) # For the nonlinear components, enter any nonzero value in G to indicate the location # of the nonlinear derivatives (in this case, 2). # A must be properly defined with the correct derivative values. linear_derivative_A = np.vstack((np.zeros( (1, n)), A_simplex, np.zeros((1, n)))) nonlinear_derivative_G = np.vstack((2 * np.ones( (1, n)), np.zeros((m, n)), 2 * np.ones((1, n)))) else: # For 1D problem, the simplex constraint is defined in x bounds. # TODO multiple safe cons. # 2 = 1 obj + 1 safe con. Plus one redundant constraint to make matrix A suitable. nF = 2 + 1 Flow = np.array([-inf, -safe_bounds, -inf]) Fupp = np.array([inf, inf, inf]) xlow = np.min(simplex) * np.ones(n) xupp = np.max(simplex) * np.ones(n) linear_derivative_A = np.vstack((np.zeros( (1, n)), np.zeros((1, n)), np.ones((1, n)))) nonlinear_derivative_G = np.vstack((2 * np.ones( (2, n)), np.zeros((1, n)))) x0 = x.T[0] # ------- ADD THE FOLLOWING LINE WHEN DEBUGGING -------- # cd dogs # ------- ADD THE FOLLOWING LINE WHEN DEBUGGING -------- save_opt_for_snopt_ck(n, nF, inter_par, xc, R2, K, A_simplex, L_safe, vertex, exist) # Since adaptiveK using ( p(x) - f0 ) / e(x), the objective function is nonlinear. # The constraints are generated by simplex bounds, all linear. options = SNOPT_options() options.setOption('Infinite bound', inf) options.setOption('Verify level', 3) options.setOption('Verbose', False) options.setOption('Print level', -1) options.setOption('Scale option', 2) options.setOption('Print frequency', -1) options.setOption('Summary', 'No') result = snopta(dogsobj, n, nF, x0=x0, name='DeltaDOGS_snopt', xlow=xlow, xupp=xupp, Flow=Flow, Fupp=Fupp, ObjRow=ObjRow, A=linear_derivative_A, G=nonlinear_derivative_G, options=options) x_solver = np.hstack((x_solver, result.x.reshape(-1, 1))) y_solver.append(result.objective) y_solver = np.array(y_solver) y = np.min(y_solver) x = x_solver[:, np.argmin(y_solver)].reshape(-1, 1) return x, y
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
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