def case8(): # y0 = [[1,7]] # t_tuple = [(0,30)] # stepsize = 0.001 # order = 2 # maxorder = 4 # t_list, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0.00001) y0 = [[0,0],[1,0],[0.3,0]] t_tuple = 2 stepsize = 0.01 order = 2 maxorder = 4 t_list, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') for i in range(0,temp_y.shape[0]-4): g = temp_y[i][:] - 4*temp_y[i+1][:] + 6*temp_y[i+2][:] - 4*temp_y[i+3][:] + temp_y[i+4][:] gg = np.max(g) if gg > 0.005: plt.scatter(temp_y[i+4][0], temp_y[i+4][1], s= 10, c='r') plt.show()
def case2(): y0 = [[0,0],[1,0],[0.3,0],[2.7,0]] t_tuple = 2 stepsize = 0.001 order = 2 maxorder = 2 # start = time.time() t_list, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0) # end_simulation = time.time() # result_coef, calcdiff_time, pseudoinv_time = infer_dynamic(t_list, 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) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.01) print(P) print(G) print(D)
def case11(): y0 = [[1,7],[2,6]] t_tuple = 2 stepsize = 0.01 order = 2 maxorder = 2 # start = time.time() t_list, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) print(len(tpar_list)) for i in range(0,len(tpar_list)): print(tpar_list[i][0]) print(tpar_list[i][-1]) print(ypar_list[i][0]) print(ypar_list[i][-1]) G,labels = infer_dynamic_modes_pie(tpar_list, ypar_list, stepsize, maxorder, 0.02) # modes, coefs, mdors = infer_dynamic_modes_ex(tpar_list, ypar_list, stepsize, maxorder, 0.01) # print(modes) # print(coefs) # print(mdors) print(len(labels)) print(G)
def case3(): y0 = [[9]] t_tuple = 3 stepsize = 0.01 order = 2 t_list, y_list = simulation_ode(dydx3, y0, t_tuple, stepsize, eps=0) for i in range(0,len(y_list)): plt.plot(t_list[i],y_list[i]) plt.show()
def case4(): y0 = [[5,5,5],[2,2,2]] t_tuple = 5 stepsize = 0.01 maxorder = 2 t_list, y_list = simulation_ode(fvdp3_3, y0, t_tuple, stepsize, eps=0) draw3D(y_list) tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) G,labels = infer_dynamic_modes_pie(tpar_list, ypar_list, stepsize, maxorder, 0.02) print(labels) print(G)
def case1(): y0 = [[1,7]] t_tuple = 25 stepsize = 0.01 order = 2 maxorder = 4 # start = time.time() t_list, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0) # end_simulation = time.time() # result_coef, calcdiff_time, pseudoinv_time = infer_dynamic(t_list, 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) tpar_list,ypar_list = parti(t_list,y_list,0.05) print(len(tpar_list)) for i in range(0,len(tpar_list)): print(tpar_list[i][0],tpar_list[i][-1]) print(ypar_list[i][0],ypar_list[i][-1]) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() # result_coef, calcdiff_time, pseudoinv_time = infer_dynamic([tpar_list[0],tpar_list[1]], [ypar_list[0],ypar_list[1]], stepsize, 4) # print(result_coef) # tstart = time.time() # comt,comy = simulation_ode_stiff(ode_test(result_coef,4), [ypar_list[0][0],ypar_list[1][0]], [(tpar_list[0][0], tpar_list[0][-1]),(tpar_list[1][0], tpar_list[1][-1])], stepsize,eps=0) # tend = time.time() # print(dist(comy,[ypar_list[0],ypar_list[1]])) # print(tend-tstart) # print(dist([ypar_list[0],ypar_list[2],ypar_list[4]],comy)) # result_coef = np.matrix([[ 0, 0, 0, -0.26, 0.26, 0], [0, 0, 0, 0, 0, -1]]) # comt,comy = simulation_ode(ode_test(result_coef,2), [ypar_list[0][0],ypar_list[2][0],ypar_list[4][0]], [(tpar_list[0][0], tpar_list[0][-1]),(tpar_list[2][0], tpar_list[2][-1]),(tpar_list[4][0], tpar_list[4][-1])], stepsize,eps=0) # print(dist([ypar_list[0],ypar_list[2],ypar_list[4]],comy)) # result_coef = np.matrix([[ 0, 0, 0, -0.26, 0.26, 0], [0, 0, 0, 0, 0, 1]]) # comt,comy = simulation_ode(ode_test(result_coef,2), [ypar_list[1][0],ypar_list[3][0],ypar_list[5][0]], [(tpar_list[1][0], tpar_list[1][-1]),(tpar_list[3][0], tpar_list[3][-1]),(tpar_list[5][0], tpar_list[5][-1])], stepsize,eps=0) # print(dist([ypar_list[1],ypar_list[3],ypar_list[5]],comy)) tstart = time.time() modes, coefs, mdors = infer_dynamic_modes_ex(tpar_list, ypar_list, stepsize, maxorder, 0.001) # modes, coefs, mdors = infer_dynamic_modes_exx(tpar_list, ypar_list, stepsize, maxorder, 0.01) tend = time.time() print(modes) print(coefs) print(mdors) print(tend-tstart)
def case5(): y0 = [[0,0],[1,0]] t_tuple = 2 stepsize = 0.01 order = 2 maxorder = 3 t_list, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0) tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) print(len(tpar_list)) for i in range(0,len(tpar_list)): print(tpar_list[i][0]) print(tpar_list[i][-1]) print(ypar_list[i][0]) print(ypar_list[i][-1]) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() # modes, coefs, mdors = infer_dynamic_modes_ex(tpar_list, ypar_list, stepsize, maxorder, 0.01) # modes, coefs, mdors = infer_dynamic_modes_exx(tpar_list, ypar_list, stepsize, maxorder, 0.01) # print(modes) # print(coefs) # print(mdors) ttest_list=[] ttest_list.append(tpar_list[0]) ttest_list.append(tpar_list[1]) ytest_list=[] ytest_list.append(ypar_list[0]) ytest_list.append(ypar_list[1]) A, b = diff_method(ttest_list, ytest_list, 3, stepsize) g = pinv2(A).dot(b) print(g.T) t_start = tpar_list[0][0] t_end = tpar_list[0][-1] t_start = 0 t_end = 0.01 t_points = np.arange(t_start, t_end + stepsize, stepsize) tstart = time.time() y_object = solve_ivp(ode_test(g.T,3), (t_start, t_end+stepsize), ypar_list[0][0], t_eval = t_points, rtol=1e-7, atol=1e-9) y_points = y_object.y.T tend = time.time() print(y_points) print(tend-tstart)
def case3(): y0 = [[5,5,5],[2,2,2]] t_tuple = 5 stepsize = 0.01 maxorder = 2 t_list, y_list = simulation_ode(fvdp3_3, y0, t_tuple, stepsize, eps=0) draw3D(y_list) A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize) # A, b1, b2, Y = diff_method_backandfor(t_list, y_list, maxorder, stepsize) # print(b1) # print(b2) P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.02) print(P) print(G) print(D)
def case(): y0 = [[5, 5, 5], [2, 2, 2]] stepsize = 0.01 maxorder = 2 T = 5 t_list, y_list = simulation_ode(fvdp3_1, y0, T, stepsize, eps=0.02) A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize) clf = linear_model.LinearRegression(fit_intercept=False) clf.fit(A, b) g = clf.coef_ print(g) A, b, Y = diff_method_new_6(t_list, y_list, maxorder, stepsize) clf.fit(A, b) g = clf.coef_ print(g)
def case5(): y0 = [[5,5,5], [2,2,2]] t_tuple = 5 stepsize = 0.01 maxorder = 2 t_list, y_list = simulation_ode(fvdp3_3, y0, t_tuple, stepsize, eps=0) clfs, boundary = infer_multi_ch.infer_multi_linear(t_list, y_list, stepsize, maxorder) for clf in clfs: print(clf.coef_) print(boundary) num_pt0 = y_list[0].shape[0] num_pt1 = y_list[1].shape[0] sum = 0 for i in range(100): # index = random.choice(range(num_pt0)) x = y_list[0][i] # sum += infer_multi_ch.test_classify(fvdp3_3, clfs, boundary, maxorder, x) print(infer_multi_ch.test_classify(fvdp3_3, clfs, boundary, maxorder, x))
def case2(): # y0 = [[1,3],[-1,-2],[-3,-5],[2,4],[-2,3],[-4,5],[4,7],[-2,-10],[5,8]] # t_tuple = [(0,2),(0,3),(0,4),(0,5),(0,1),(0,1),(0,5),(0,6),(0,6)] y0 = [[1,3]] t_tuple = 2 stepsize = 0.01 order = 2 maxorder = 4 t_list, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0) # modes, coefs = infer_dynamic_modes(t_list, y_list, stepsize, maxorder, 0.001) modes, coefs, mdors = infer_dynamic_modes_ex(t_list, y_list, stepsize, maxorder, 0.01) print(modes) print(coefs) print(mdors) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show()
def case9(): y0 = [[0,0]] t_tuple = 7 stepsize = 0.001 order = 2 maxorder = 4 t_list, y_list = simulation_ode(mode2_11, y0, t_tuple, stepsize, eps=0) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) print(len(tpar_list)) for i in range(0,len(tpar_list)): print(tpar_list[i][0]) print(tpar_list[i][-1]) print(ypar_list[i][0]) print(ypar_list[i][-1]) labels = infer_dynamic_modes_ex_dbs(tpar_list, ypar_list, stepsize, maxorder, 0.0001) print(labels)
def case4(): y0 = [[0,0],[1,0]] t_tuple = [(0,2.5),(0,2)] stepsize = 0.01 order = 2 maxorder = 4 # start = time.time() t_list, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0) # end_simulation = time.time() # result_coef, calcdiff_time, pseudoinv_time = infer_dynamic(t_list, 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) tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) print(len(tpar_list)) for i in range(0,len(tpar_list)): print(tpar_list[i][0]) print(tpar_list[i][-1]) print(ypar_list[i][0]) print(ypar_list[i][-1]) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() modes, coefs, mdors = infer_dynamic_modes_ex(tpar_list, ypar_list, stepsize, maxorder, 0.0001) # modes, coefs, mdors = infer_dynamic_modes_exx(tpar_list, ypar_list, stepsize, maxorder, 0.0001) print(modes) print(coefs) print(mdors)
def case10(): y0 = [[0,0],[1,0],[0.3,0],[2.7,0]] t_tuple = 2 stepsize = 0.01 order = 2 maxorder = 3 # start = time.time() t_list, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0) # end_simulation = time.time() # result_coef, calcdiff_time, pseudoinv_time = infer_dynamic(t_list, 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) tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3) print(len(tpar_list)) for i in range(0,len(tpar_list)): print(tpar_list[i][0]) print(tpar_list[i][-1]) print(ypar_list[i][0]) print(ypar_list[i][-1]) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') plt.show() G,labels = infer_dynamic_modes_pie(tpar_list, ypar_list, stepsize, maxorder, 0.15) print(labels) print(G)
def case1(): # y0 = [[1,3],[-1,-2],[-3,-5],[2,4]] # t_tuple = [(0,20),(0,10),(0,15),(0,15)] y0 = [[1,3],[-1,-2]] t_tuple = 20 stepsize = 0.01 order = 2 maxorder = 2 # start = time.time() t_list, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0) # for temp_y in y_list: # y0_list = temp_y.T[0] # y1_list = temp_y.T[1] # plt.plot(y0_list,y1_list,'b') # plt.show() 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) 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 -b 0 ') m = svm_train(prob, param) svm_save_model('model_file', m) yt = [-1,-1,1,1] xt = [{1:1, 2:1},{1:-1, 2:1},{1:1, 2:-1},{1:-1, 2:-1}] 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] print("a1",a1/a1) print("a2",a2/a1) print("g",g/a1) # ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow') # plt.show() dim = G[0].shape[0] A = generate_complete_polynomial(dim,maxorder) def odepre(t,y): # print("in") basicf = [] for i in range(0,A.shape[0]): ap = 1 for j in range(0,A.shape[1]): ap = ap*(y[j]**A[i][j]) basicf.append(ap) b = np.array(basicf) dydt = np.zeros(dim) if a1 * y[0] + a2 * y[1] + g > 0: for l in range(0,dim): dydt[l] = G[0][l].dot(b) else: for l in range(0,dim): dydt[l] = G[1][l].dot(b) # print("out") return dydt py0 = [[2,4],[-1,-3]] pt_tuple = [(0,10),(0,10)] start = time.time() print("origin") tp_list, yp_list = simulation_ode(mode2_1, py0, pt_tuple, stepsize, eps=0) end1 = time.time() print("predict") tpre_list, ypre_list = simulation_ode(odepre, py0, pt_tuple, stepsize, eps=0) end2 = time.time() print("simutime",end1-start) print("predtime",end2-end1) for temp_y in yp_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'b') for temp_y in ypre_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list,y1_list,'r') plt.show()