def case3(): y0 = [[5, -3], [2, 0], [-2, 3]] t_tuple = 10 stepsize = 0.01 order = 3 start = time.time() t_points, y_list = simulation_ode(fvdp2, y0, t_tuple, stepsize, eps=0) end_simulation = time.time() result_coef, calcdiff_time, pseudoinv_time = infer_dynamic( t_points, y_list, stepsize, order) end_inference = time.time() print(result_coef) print() print("Total time: ", end_inference - start) print("Simulation time: ", end_simulation - start) print("Calc-diff time: ", calcdiff_time) print("Pseudoinv time: ", pseudoinv_time) draw2D(y_list)
def case1(): y0 = [[a, b] for a in np.arange(-0.5, 0.5 + 0.25, 0.25) for b in np.arange(-2.5, -1.5 + 0.25, 0.25)] t_tuple = 1 stepsize = 0.001 order = 2 start = time.time() t_points, y_list = simulation_ode(fvdp2_1, y0, t_tuple, stepsize, eps=0) end_simulation = time.time() result_coef, calcdiff_time, pseudoinv_time = infer_dynamic( t_points, y_list, stepsize, order) end_inference = time.time() print(result_coef) print() print("Total time: ", end_inference - start) print("Simulation time: ", end_simulation - start) print("Calc-diff time: ", calcdiff_time) print("Pseudoinv time: ", pseudoinv_time) draw2D(y_list)
def case2(): y0 = [[a, b] for a in np.arange(-0.5, 0.5 + 0.25, 0.25) for b in np.arange(-2.5, -1.5 + 0.25, 0.25)] t_tuple = 1 stepsize = 0.001 order = 2 start = time.time() y_list_20 = [] y_list_ave = [] t_points = [] for i in range(0, 20): t_points, y_list = simulation_ode(fvdp2_1, y0, t_tuple, stepsize, eps=0.01) y_list_20.append(y_list) for j in range(0, len(y_list_20[0])): y_ppoints = np.zeros( (y_list_20[0][0].shape[0], y_list_20[0][0].shape[1])) for i in range(0, 20): y_ppoints = y_ppoints + y_list_20[i][j] y_ppoints = y_ppoints / 20.0 y_list_ave.append(y_ppoints) end_simulation = time.time() result_coef, calcdiff_time, pseudoinv_time = infer_dynamic( t_points, y_list_ave, stepsize, order) end_inference = time.time() print(result_coef) print() print("Total time: ", end_inference - start) print("Simulation time: ", end_simulation - start) print("Calc-diff time: ", calcdiff_time) print("Pseudoinv time: ", pseudoinv_time) draw2D(y_list_ave)
def case10(): y0 = [[-1,1],[1,4],[2,-3]] t_tuple = [(0,5),(0,5),(0,5)] stepsize = 0.01 maxorder = 2 # start = time.time() def labeltest(y): if eventtr_1(0,y)<0 and eventtr_2(0,y)>0: return 0 elif eventtr_1(0,y)>=0 and eventtr_2(0,y)>0: return 1 else: return 2 t_list, y_list = simulation_ode_3([modetr_1, modetr_2, modetr_3], [eventtr_1,eventtr_2,eventtr_2], labeltest, y0, t_tuple, stepsize) draw2D(y_list) A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize) P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.01) print(P) print(G) print(D) # for p in P: # print(len(p)) # tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) # for temp in tpar_list: # print(temp[-1]) # labels = infer_dynamic_modes_ex_dbs(tpar_list, ypar_list, stepsize, maxorder, 0.02) # print(labels) # draw2D(ypar_list) for i in range(0,len(P)): y0_list = [] y1_list = [] for j in range(0,len(P[i])): y0_list.append(Y[P[i][j],0]) y1_list.append(Y[P[i][j],1]) plt.scatter(y0_list,y1_list,s=1) plt.show() P,G = reclass(A,b,P,0.01) for p in P: print(len(p)) print(G) for i in range(0,len(P)): y0_list = [] y1_list = [] for j in range(0,len(P[i])): y0_list.append(Y[P[i][j],0]) y1_list.append(Y[P[i][j],1]) plt.scatter(y0_list,y1_list,s=1) plt.show() P,D = dropclass(P,G,D,A,b,Y,0.01,0.01) print(D) for i in range(0,len(P)): y0_list = [] y1_list = [] for j in range(0,len(P[i])): y0_list.append(Y[P[i][j],0]) y1_list.append(Y[P[i][j],1]) plt.scatter(y0_list,y1_list,s=1) # plt.show() y=[] x=[] for j in range(0,len(P[2])): y.append(1) x.append({1:Y[P[2][j],0], 2:Y[P[2][j],1]}) for j in range(0,len(P[1])): y.append(-1) x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1]}) for j in range(0,len(P[0])): y.append(-1) x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1]}) prob = svm_problem(y, x) param = svm_parameter('-t 1 -d 1 -c 100 -r 1 -b 0') m = svm_train(prob, param) svm_save_model('model_file', m) print("pred") p_label, p_acc, p_val = svm_predict(y, x, m) # print(p_label) nsv = m.get_nr_sv() svc = m.get_sv_coef() sv = m.get_SV() # print(nsv) # print(svc) # print(sv) # def clafun(x): # g = -m.rho[0] # for i in range(0,nsv): # g = g + svc[i][0] * ((0.5 * (x[0]*sv[i][1] + x[1]*sv[i][2]))**3) # return g g = -m.rho[0] a1 = 0 a2 = 0 for i in range(0,nsv): a1 = a1 + svc[i][0] * 0.5 * sv[i][1] a2 = a2 + svc[i][0] * 0.5 * sv[i][2] g = g + svc[i][0]*1 print(a1) print(a2) print(g) def f(x,y): g = -m.rho[0] for i in range(0,nsv): g = g + svc[i][0] * (0.5*(x*sv[i][1]+y*sv[i][2])+1) return g x=[] y=[] for j in range(0,len(P[1])): y.append(1) x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1]}) for j in range(0,len(P[0])): y.append(-1) x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1]}) prob = svm_problem(y, x) param = svm_parameter('-t 1 -d 1 -c 100 -r 1 -b 0') m = svm_train(prob, param) svm_save_model('model_file', m) print("pred") p_label, p_acc, p_val = svm_predict(y, x, m) # print(p_label) nsv = m.get_nr_sv() svc = m.get_sv_coef() sv = m.get_SV() # print(nsv) # print(svc) # print(sv) # def clafun(x): # g = -m.rho[0] # for i in range(0,nsv): # g = g + svc[i][0] * ((0.5 * (x[0]*sv[i][1] + x[1]*sv[i][2]))**3) # return g g1 = -m.rho[0] b1 = 0 b2 = 0 for i in range(0,nsv): b1 = b1 + svc[i][0] * 0.5 * sv[i][1] b2 = b2 + svc[i][0] * 0.5 * sv[i][2] g1 = g1 + svc[i][0]*1 # print(a1) # print(a2) # print(g) def h(x,y): g = -m.rho[0] for i in range(0,nsv): g = g + svc[i][0] * (0.5*(x*sv[i][1]+y*sv[i][2])+1) return g x = np.linspace(-10,10,100) y = np.linspace(-10,10,100) X,Y = np.meshgrid(x,y)#将x,y指传入网格中 # plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)#8指图中的8+1根线,绘制等温线,其中cmap指颜色 C = plt.contour(X,Y,f(X,Y),[0])#colors指等高线颜色 plt.clabel(C,inline=True,fontsize=10)#inline=True指字体在等高线中 D = plt.contour(X,Y,h(X,Y),[0])#colors指等高线颜色 plt.clabel(D,inline=True,fontsize=10)#inline=True指字体在等高线中 plt.xticks(()) plt.yticks(()) plt.show()
def case(y0, t_tuple, stepsize, maxorder, modelist, event, ep, method): # print('Simulating') t_list, y_list = simulation_ode_2(modelist, event, y0, t_tuple, stepsize) draw2D(y_list) if method == "new": # print('Classifying') A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize) P, G, D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, ep) P, G = reclass(A, b, P, ep) print(G) P, D = dropclass(P, G, D, A, b, Y, ep, stepsize) # print('Number of modes:', len(P)) y = [] x = [] for j in range(0, len(P[0])): y.append(1) x.append({1: Y[P[0][j], 0], 2: Y[P[0][j], 1]}) for j in range(0, len(P[1])): y.append(-1) x.append({1: Y[P[1][j], 0], 2: Y[P[1][j], 1]}) prob = svm_problem(y, x) param = svm_parameter('-t 1 -d 1 -c 10 -r 1 -b 0 -q') m = svm_train(prob, param) svm_save_model('model_file', m) nsv = m.get_nr_sv() svc = m.get_sv_coef() sv = m.get_SV() g = -m.rho[0] a1 = 0 a2 = 0 for i in range(0, nsv): a1 = a1 + svc[i][0] * 0.5 * sv[i][1] a2 = a2 + svc[i][0] * 0.5 * sv[i][2] g = g + svc[i][0] def f(x): return a1 * x[0] + a2 * x[1] + g > 0 print(a1 / a1, a2 / a1, g / a1) sum = 0 num = 0 @eventAttr() def eventtest(t, y): y0, y1 = y return a1 * y0 + a2 * y1 + g ttest_list, ytest_list = simulation_ode_2( [ode_test(G[0], maxorder), ode_test(G[1], maxorder)], eventtest, y0, t_tuple, stepsize) for i, temp_y in enumerate(y_list): y0_list = temp_y.T[0] y1_list = temp_y.T[1] if i == 0: plt.plot(y0_list, y1_list, c='b', label='Original') else: plt.plot(y0_list, y1_list, c='b') for i, temp_y in enumerate(ytest_list): y0_list = temp_y.T[0] y1_list = temp_y.T[1] if i == 0: plt.plot(y0_list, y1_list, c='r', label='Inferred') else: plt.plot(y0_list, y1_list, c='r') plt.xlabel('x1') plt.ylabel('x2') plt.legend() plt.show() def get_poly_pt(x): gene = generate_complete_polynomial(len(x), maxorder) val = [] for i in range(gene.shape[0]): val.append(1.0) for j in range(gene.shape[1]): val[i] = val[i] * (x[j]**gene[i, j]) poly_pt = np.mat(val) return poly_pt for ypoints in y_list: num = num + ypoints.shape[0] for i in range(ypoints.shape[0]): if event(0, ypoints[i]) > 0: exact = modelist[0](0, ypoints[i]) else: exact = modelist[1](0, ypoints[i]) if f(ypoints[i]) == 1: predict = np.matmul(get_poly_pt(ypoints[i]), G[0].T) else: predict = np.matmul(get_poly_pt(ypoints[i]), G[1].T) exact = np.mat(exact) diff = exact - predict c = 0 a = 0 b = 0 for j in range(diff.shape[1]): c = c + diff[0, j]**2 a = a + exact[0, j]**2 b = b + predict[0, j]**2 f1 = np.sqrt(c) f2 = np.sqrt(a) + np.sqrt(b) sum = sum + f1 / f2 return sum / num