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 compare1(id, eid, case_id, verbose=False): np.random.seed(0) if eid == 'A': case_info = experiment1.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment1.get_mode2(params) event = experiment1.get_event1(params) maxorder = 1 boundary_order = 1 num_mode = 2 ep = 0.01 mergeep = 0.01 dy = dynamics.mode2t elif eid == 'B': case_info = experiment2.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment2.get_fvdp3(params) event = experiment2.get_event1(params) maxorder = 2 boundary_order = 1 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.fvdp3_3 elif eid == 'C': case_info = experiment3.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment3.get_mode(params) event = experiment3.get_event(params) maxorder = 3 boundary_order = 2 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.modeex3 elif eid == 'D': case_info = experiment4.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment4.get_mmode(params) event = experiment4.get_event(params) maxorder = 2 boundary_order = 1 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.modeex4 elif eid == 'E': case_info = experiment5.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment5.get_modetr(params) event = experiment5.get_event(params) labeltest = experiment5.get_labeltest(params) maxorder = 2 boundary_order = 1 num_mode = 3 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.modetrt # Obtain simulated trajectory start = time.time() if num_mode == 2: t_list, y_list = simulation_ode_2(modelist, event, y0, T, stepsize) test_t_list, test_y_list = simulation_ode_2(modelist, event, y0_test, T, stepsize) elif num_mode == 3: t_list, y_list = simulation_ode_3(modelist, event, labeltest, y0, T, stepsize) test_t_list, test_y_list = simulation_ode_3(modelist, event, labeltest, y0_test, T, stepsize) else: raise NotImplementedError end = time.time() # A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize) # np.savetxt("data/CA"+str(id)+".txt",A,fmt='%8f') # np.savetxt("data/Cb"+str(id)+".txt",b,fmt='%8f') # YT, FT = diff(t_list+test_t_list, y_list+test_y_list, dy) # np.savetxt("data/CYT"+str(id)+".txt",YT,fmt='%8f') # np.savetxt("data/CFT"+str(id)+".txt",FT,fmt='%8f') for i in range(0, len(y_list)): np.savetxt("data1/YLIST" + str(id) + "_" + str(i) + ".txt", y_list[i], fmt='%8f') for i in range(0, len(test_y_list)): np.savetxt("data1/YTLIST" + str(id) + "_" + str(i) + ".txt", test_y_list[i], fmt='%8f')
def compare_opt(id, eid, case_id, verbose=False): np.random.seed(0) if eid == 'A': case_info = experiment1.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment1.get_mode2(params) event = experiment1.get_event1(params) maxorder = 1 boundary_order = 1 num_mode = 2 ep = 0.01 mergeep = 0.01 dy = dynamics.mode2t elif eid == 'B': case_info = experiment2.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment2.get_fvdp3(params) event = experiment2.get_event1(params) maxorder = 2 boundary_order = 1 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.fvdp3_3 elif eid == 'C': case_info = experiment3.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment3.get_mode(params) event = experiment3.get_event(params) maxorder = 3 boundary_order = 2 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.modeex3 elif eid == 'D': case_info = experiment4.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment4.get_mmode(params) event = experiment4.get_event(params) maxorder = 2 boundary_order = 1 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.modeex4 elif eid == 'E': case_info = experiment5.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment5.get_modetr(params) event = experiment5.get_event(params) labeltest = experiment5.get_labeltest(params) maxorder = 2 boundary_order = 1 num_mode = 3 ep = case_info['ep'] mergeep = case_info['mergeep'] dy = dynamics.modetrt # Obtain simulated trajectory print(id, eid) start = time.time() if num_mode == 2: t_list, y_list = simulation_ode_2(modelist, event, y0, T, stepsize) test_t_list, test_y_list = simulation_ode_2(modelist, event, y0_test, T, stepsize) elif num_mode == 3: t_list, y_list = simulation_ode_3(modelist, event, labeltest, y0, T, stepsize) test_t_list, test_y_list = simulation_ode_3(modelist, event, labeltest, y0_test, T, stepsize) else: raise NotImplementedError end = time.time() # start = time.time() # A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize) A, b1, b2, Y, ytuple = diff_method_backandfor(t_list, y_list, maxorder, stepsize) optA, optb1, optb2, drop = seg_droprow(A, b1, b2, ep) x0 = np.zeros(num_mode * optA.shape[1] * optb1.shape[1]) for ini in range(0, 5): print('initial', ini) for i in range(len(x0)): x0[i] = np.random.uniform(-5, 5) for optmethod in ['nelder-mead', 'COBYLA', 'Powell', 'CG']: try: infer_optimizationmtest(x0, optA, optb1, num_mode) except: print(optmethod, ' timeout')
def run_test(id, eid, case_id, methods, verbose=False): np.random.seed(0) if eid == 'A': case_info = experiment1.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment1.get_mode2(params) event = experiment1.get_event1(params) maxorder = 1 boundary_order = 1 num_mode = 2 ep = 0.01 mergeep = 0.01 elif eid == 'B': case_info = experiment2.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment2.get_fvdp3(params) event = experiment2.get_event1(params) maxorder = 2 boundary_order = 1 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] elif eid == 'C': case_info = experiment3.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment3.get_mode(params) event = experiment3.get_event(params) maxorder = 3 boundary_order = 2 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] elif eid == 'D': case_info = experiment4.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment4.get_mmode(params) event = experiment4.get_event(params) maxorder = 2 boundary_order = 1 num_mode = 2 ep = case_info['ep'] mergeep = case_info['mergeep'] elif eid == 'E': case_info = experiment5.cases[case_id] params = case_info['params'] y0 = case_info['y0'] y0_test = case_info['y0_test'] T = case_info['t_tuple'] stepsize = case_info['stepsize'] modelist = experiment5.get_modetr(params) event = experiment5.get_event(params) labeltest = experiment5.get_labeltest(params) maxorder = 2 boundary_order = 1 num_mode = 3 ep = case_info['ep'] mergeep = case_info['mergeep'] # Obtain simulated trajectory start = time.time() if num_mode == 2: t_list, y_list = simulation_ode_2(modelist, event, y0, T, stepsize) test_t_list, test_y_list = simulation_ode_2(modelist, event, y0_test, T, stepsize) elif num_mode == 3: t_list, y_list = simulation_ode_3(modelist, event, labeltest, y0, T, stepsize) test_t_list, test_y_list = simulation_ode_3(modelist, event, labeltest, y0_test, T, stepsize) else: raise NotImplementedError end = time.time() # print('eid:', eid, 'N_init:', len(y0), 't_step:', stepsize, 'ep:', ep, 'sim_time: %.3f' % (end - start)) d_avg = dict() infer_time = dict() for method in methods: start = time.time() if num_mode == 2: P, G, boundary = infer_model(t_list, y_list, stepsize=stepsize, maxorder=maxorder, boundary_order=boundary_order, num_mode=num_mode, modelist=modelist, event=event, ep=ep, mergeep=mergeep, method=method, verbose=verbose) end = time.time() d_avg[method] = test_model(P, G, boundary, num_mode, y_list + test_y_list, modelist, event, maxorder, boundary_order) infer_time[method] = end - start elif num_mode == 3: P, G, boundary = infer_model(t_list, y_list, stepsize=stepsize, maxorder=maxorder, boundary_order=boundary_order, num_mode=num_mode, modelist=modelist, event=event, ep=ep, mergeep=mergeep, method=method, verbose=verbose, labeltest=labeltest) end = time.time() d_avg[method] = test_model(P, G, boundary, num_mode, y_list + test_y_list, modelist, event, maxorder, boundary_order, labeltest=labeltest) infer_time[method] = end - start else: raise NotImplementedError # print('Method: %s, d_avg: %.6f, infer_time: %.3f' % (method, d_avg[method], infer_time[method])) # best_method, best_avg = None, 1.0 # for method, avg in d_avg.items(): # total_d_avg[method] += avg # if avg < best_avg: # best_method, best_avg = method, avg # for method, t in infer_time.items(): # total_time[method] += t # total_win[best_method] += 1 print( '%d & $%s$ & %d & %.3f & %d & %.3f & %.5f & %.5f & %.5f& & %.1f & %.1f & %.1f& & \\\\' % (id, eid, len(y0), stepsize, T, mergeep, d_avg['dbscan'], d_avg['tolmerge'], d_avg['piecelinear'], infer_time['dbscan'], infer_time['tolmerge'], infer_time['piecelinear'])) return d_avg, infer_time