def case1(): mode2 = experiment1.get_mode2(0) event1 = experiment1.get_event1(0) y0 = [[99.5,80],[97.5,100]] stepsize = 0.1 maxorder = 1 boundary_order = 1 num_mode = 2 T = 50 ep = 0.01 mergeep = 0.01 method='piecelinear' t_list, y_list = simulation_ode_2(mode2, event1, y0, T, stepsize) with open('f1tr.txt','ab') as f: for ypoints in y_list: np.savetxt(f, ypoints, delimiter=" ") P,G,C = infer_model( t_list, y_list, stepsize=stepsize, maxorder=maxorder, boundary_order=boundary_order, num_mode=num_mode, modelist=mode2, event=event1, ep=ep, mergeep = mergeep, method=method, verbose=False) # A, b1, b2, Y, ytuple = diff_method_backandfor(t_list, y_list, maxorder, stepsize) d_avg = test_model( P, G, C, num_mode, y_list , mode2, event1, maxorder, boundary_order) # d_avg = test_model2( # P, G, C, num_mode, A, Y , mode2, event1, maxorder, boundary_order) print(G) print(C[0]/C[0],C[1]/C[0],C[2]/C[0]) print(d_avg) @eventAttr() def eventtest(t,y): y0, y1 = y return C[0] * y0 + C[1] * y1 + C[2] ttest_list, ytest_list = simulation_ode_2([ode_test(G[0],maxorder),ode_test(G[1],maxorder)], eventtest, y0, T, 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][::5] y1_list = temp_y.T[1][::5] if i == 0: plt.plot(y0_list,y1_list,mfc='None', mec='r',label='Inferred',marker='.',linestyle='None') else: # plt.plot(y0_list,y1_list,c='r',linestyle='--',marker=',') plt.plot(y0_list,y1_list,mfc='None', mec='r',marker='.',linestyle='None') plt.xlabel('x1') plt.ylabel('x2') plt.legend() plt.show()
def case2(): fvdp3 = experiment2.get_fvdp3(0) event1 = experiment2.get_event1(0) y0 = [[5,5,5], [2,2,2]] stepsize = 0.004 maxorder = 2 boundary_order = 1 num_mode = 2 T = 5 ep = 0.01 mergeep = 0.2 method='tolmerge' t_list, y_list = simulation_ode_2(fvdp3, event1, y0, T, stepsize) # A, b1, b2, Y, ytuple = diff_method_backandfor(t_list, y_list, maxorder, stepsize) P,G,C = infer_model( t_list, y_list, stepsize=stepsize, maxorder=maxorder, boundary_order=boundary_order, num_mode=num_mode, modelist=fvdp3, event=event1, ep=ep, mergeep= mergeep,method=method, verbose=False) d_avg = test_model( P, G, C, num_mode, y_list , fvdp3, event1, maxorder, boundary_order) # d_avg = test_model2( # P, G, C, num_mode, A, Y , fvdp3, event1, maxorder, boundary_order) print(G) print(C[0]/C[0],C[1]/C[0],C[2]/C[0],C[3]/C[0]) print(d_avg) with open('f2tr.txt','ab') as f: for ypoints in y_list: np.savetxt(f, ypoints, delimiter=" ") @eventAttr() def eventtest(t,y): y0, y1, y2 = y return C[0] * y0 + C[1] * y1 + C[2]* y2 + C[3] ttest_list, ytest_list = simulation_ode_2([ode_test(G[0],maxorder),ode_test(G[1],maxorder)], eventtest, y0, T, stepsize) ax = plt.axes(projection='3d') for temp_y in y_list[0:1]: y0_list = temp_y.T[0] y1_list = temp_y.T[1] y2_list = temp_y.T[2] ax.plot3D(y0_list, y1_list, y2_list,c='b',label='Original') for temp_y in ytest_list[0:1]: y0_list = temp_y.T[0] y1_list = temp_y.T[1] y2_list = temp_y.T[2] ax.plot3D(y0_list, y1_list, y2_list,c='r',label='Inferred', linestyle='--') ax.set_xlabel('x1') ax.set_ylabel('x2') ax.set_zlabel('x3') plt.legend() plt.show()
def case2(): y0 = [[20]] t_tuple = 4 stepsize = 0.001 order = 8 start = time.time() t_points, y_list = simulation_ode(dydx2, 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() t_points, y_list_test = simulation_ode(ode_test(result_coef, order), y0, t_tuple, stepsize, eps=0) 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: plt.scatter(t_points, temp_y, s=0.1, c='r') for temp_y in y_list_test: plt.scatter(t_points, temp_y, s=0.1, c='b') plt.show()
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 case(y0, t_tuple, stepsize, maxorder, modelist, event, ep, method): t_list, y_list = simulation_ode_2(modelist, event, y0, t_tuple, stepsize) if method == "new": 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) P, D = dropclass(P, G, D, A, b, Y, 0.01, stepsize) # print(len(P)) print(G) 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], 3: Y[P[0][j], 2]}) 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], 3: Y[P[1][j], 2]}) 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 a3 = 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] a3 = a3 + svc[i][0] * 0.5 * sv[i][3] g = g + svc[i][0] print(a1 / a1, a2 / a1, a3 / a1, g / a1) def f(x): return a1 * x[0] + a2 * x[1] + a3 * x[2] + g > 0 @eventAttr() def eventtest(t, y): y0, y1, y2 = y return a1 * y0 + a2 * y1 + a3 * y2 + g ttest_list, ytest_list = simulation_ode_2( [ode_test(G[0], maxorder), ode_test(G[1], maxorder)], eventtest, y0, t_tuple, stepsize) ax = plt.axes(projection='3d') for temp_y in y_list[0:1]: y0_list = temp_y.T[0] y1_list = temp_y.T[1] y2_list = temp_y.T[2] ax.plot3D(y0_list, y1_list, y2_list, c='b') for temp_y in ytest_list[0:1]: y0_list = temp_y.T[0] y1_list = temp_y.T[1] y2_list = temp_y.T[2] ax.plot3D(y0_list, y1_list, y2_list, c='r') plt.show() sum = 0 num = 0 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
def case(y0, t_tuple, stepsize, maxorder, modelist, eventlist, labeltest, ep, method): t_list, y_list = simulation_ode_3(modelist, eventlist, labeltest, y0, t_tuple, stepsize) if method == "new": 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) P, D = dropclass(P, G, D, A, b, Y, ep, stepsize) 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() 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 -q') m = svm_train(prob, param) svm_save_model('model_file1', 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] * 1 print(a1 / a2, a2 / a2, g / a2) def f(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]) + 1) return g > 0 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 -q') n = svm_train(prob, param) svm_save_model('model_file2', n) # p_label, p_acc, p_val = svm_predict(y, x, n) nsv1 = n.get_nr_sv() svc1 = n.get_sv_coef() sv1 = n.get_SV() g1 = -n.rho[0] b1 = 0 b2 = 0 for i in range(0, nsv1): b1 = b1 + svc1[i][0] * 0.5 * sv1[i][1] b2 = b2 + svc1[i][0] * 0.5 * sv1[i][2] g1 = g1 + svc1[i][0] * 1 print(b1 / b1, b2 / b1, g1 / b1) def h(x): g = -n.rho[0] for i in range(0, nsv1): g = g + svc1[i][0] * (0.5 * (x[0] * sv1[i][1] + x[1] * sv1[i][2]) + 1) return g > 0 @eventAttr() def eventtest1(t, y): y0, y1 = y return a1 * y0 + a2 * y1 + g @eventAttr() def eventtest2(t, y): y0, y1 = y return b1 * y0 + b2 * y1 + g1 def labeltesttest(y): if eventtest1(0, y) > 0: return 2 elif eventtest2(0, y) > 0: return 1 else: return 0 ttest_list, ytest_list = simulation_ode_3([ ode_test(G[0], maxorder), ode_test(G[1], maxorder), ode_test(G[2], maxorder) ], [eventtest1, eventtest2], labeltesttest, y0, t_tuple, stepsize) for temp_y in y_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list, y1_list, c='b') for temp_y in ytest_list: y0_list = temp_y.T[0] y1_list = temp_y.T[1] plt.plot(y0_list, y1_list, c='r') plt.show() sum = 0 num = 0 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]): exact = modelist[labeltest(ypoints[i])](0, ypoints[i]) if f(ypoints[i]) == 1: predict = np.matmul(get_poly_pt(ypoints[i]), G[2].T) elif h(ypoints[i]) == 1: predict = np.matmul(get_poly_pt(ypoints[i]), G[1].T) else: predict = np.matmul(get_poly_pt(ypoints[i]), G[0].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
def case5(): modetr = experiment5.get_modetr(0) event = experiment5.get_event(0) labeltest = experiment5.get_labeltest(0) y0 = [[-1,1],[1,4],[2,-3]] T = 5 stepsize = 0.01 maxorder = 2 boundary_order = 1 num_mode = 3 ep = 0.01 mergeep=0.01 method = 'piecelinear' t_list, y_list = simulation_ode_3(modetr, event, labeltest, y0, T, stepsize) P, G, (coeff1, coeff2, [first,second,third]) = infer_model( t_list, y_list, stepsize=stepsize, maxorder=maxorder, boundary_order=boundary_order, num_mode=num_mode, modelist=modetr, event=event, ep=ep, mergeep= mergeep,method=method, verbose=False, labeltest=labeltest) boundary = (coeff1, coeff2, [first,second,third]) d_avg = test_model( P, G, boundary, num_mode, y_list, modetr, event, maxorder, boundary_order, labeltest=labeltest) print(d_avg) print(coeff1[0]/coeff1[0],coeff1[1]/coeff1[0],coeff1[2]/coeff1[0]) print(coeff1[0]/coeff1[1],coeff1[1]/coeff1[1],coeff1[2]/coeff1[1]) print(coeff2[0]/coeff2[0],coeff2[1]/coeff2[0],coeff2[2]/coeff2[0]) print(coeff2[0]/coeff2[1],coeff2[1]/coeff2[1],coeff2[2]/coeff2[1]) @eventAttr() def eventtest1(t,y): y0, y1 = y return coeff1[0] * y0 + coeff1[1] * y1 + coeff1[2] @eventAttr() def eventtest2(t,y): y0, y1 = y return coeff2[0] * y0 + coeff2[1] * y1 + coeff2[2] def labeltesttest(y): if eventtest1(0,y)>0: return first elif eventtest2(0,y)>0: return second else: return third ttest_list, ytest_list = simulation_ode_3([ode_test(G[0],maxorder),ode_test(G[1],maxorder),ode_test(G[2],maxorder)], [eventtest1,eventtest2], labeltesttest, y0, T, 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',linestyle='--') else: plt.plot(y0_list,y1_list,c='r',linestyle='--') plt.xlabel('x1') plt.ylabel('x2') plt.legend() 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