Esempio n. 1
0
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()
Esempio n. 2
0
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)
Esempio n. 3
0
def case1():
    y0 = [[5], [1], [2], [3], [0], [-1], [-2]]
    t_tuple = 4
    stepsize = 0.01
    order = 3
    start = time.time()
    t_points, y_list = simulation_ode(dydx1, 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)
    draw(t_points, y_list)
Esempio n. 4
0
def case3():
    y0 = [[1,0],[0.4,0],[0.5,0],[2.3,0]]
    t_tuple = 3
    stepsize = 0.005
    order = 2

    start = time.time()
    t_points, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0)
    end_simulation = time.time()

    final_A_mat, final_b_mat = get_coef(t_points, y_list, order, stepsize)
    end_coedf = time.time()

    # x0 = np.ones((2*final_A_mat.shape[1], final_b_mat.shape[1]))*0.1
    # x0 = np.ones((1,2*final_A_mat.shape[1]*final_b_mat.shape[1]))*0.1
    print(3*final_A_mat.shape[1]*final_b_mat.shape[1])
    x0 = np.random.uniform(-5,5,[3*final_A_mat.shape[1]*final_b_mat.shape[1]])
    # print(x0)
    x1 = np.array([0,0,0,1,0,0,0,0,0,0,0,1, -1,0,0,5,0,5,0,0,0,0,0,1, 0,0,0,-1,0,4,0,0,0,0,0,1])

    # pr = cProfile.Profile()
    # pr.enable()
    results = infer_optimization3(x0, final_A_mat, final_b_mat)
    # p = Stats(pr)
    # p.strip_dirs()
    # p.sort_stats('cumtime')
    # p.print_stats(100)
    
    end_optimization = time.time()
    # print(results)
    print(results.x)
    print(lambda_three_modes(final_A_mat,final_b_mat)(x0))
    print(lambda_three_modes(final_A_mat,final_b_mat)(results.x))
    print(lambda_three_modes(final_A_mat,final_b_mat)(x1))
    print(results.success)
    print(results.message)


    print("Simulation time: ", end_simulation-start)
    print("Optimazation time: ", end_optimization-end_coedf)
    # draw2D_dots(y_list)

    # start_svm = time.time()
    A_row = final_A_mat.shape[0]
    A_col = final_A_mat.shape[1]
    b_col = final_b_mat.shape[1]
    x1, x2, x3 = np.array_split(results.x,3)
    x1 = np.mat(x1.reshape([A_col,b_col],order='F'))
    y1 = np.matmul(final_A_mat,x1) - final_b_mat
    y1 = np.multiply(y1,y1)
    y1 = y1.sum(axis=1)
    x2 = np.mat(x2.reshape([A_col,b_col],order='F'))
    y2 = np.matmul(final_A_mat,x2) - final_b_mat
    y2 = np.multiply(y2,y2)
    y2 = y2.sum(axis=1)
    x3 = np.mat(x3.reshape([A_col,b_col],order='F'))
    y3 = np.matmul(final_A_mat,x3) - final_b_mat
    y3 = np.multiply(y3,y3)
    y3 = y3.sum(axis=1)
    modet = np.zeros(A_row)
    for i in range(0,A_row):
        if y1[i] < y2[i] and y1[i] < y3[i]:
            modet[i] = 1
        if y2[i] < y1[i] and y2[i] < y3[i]:
            modet[i] = 2
        if y3[i] < y1[i] and y3[i] < y2[i]:
            modet[i] = 3
    modett = np.array_split(modet,len(y0))
    print(A_row)
    for i in range(0,len(y0)):
        plt.plot(t_points,y_list[i])   
        plt.plot(t_points[:len(modett[i])],modett[i])
        plt.show()
Esempio n. 5
0
def case1():
    y0 = [[0,3]]
    t_tuple = 20
    stepsize = 0.01
    order = 2

    start = time.time()
    t_points, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0)
    end_simulation = time.time()

    final_A_mat, final_b_mat = get_coef(t_points, y_list, order, stepsize)
    end_coedf = time.time()

    # x0 = np.ones((2*final_A_mat.shape[1], final_b_mat.shape[1]))*0.1
    # x0 = np.ones((1,2*final_A_mat.shape[1]*final_b_mat.shape[1]))*0.1
    print(2*final_A_mat.shape[1]*final_b_mat.shape[1])
    x0 = np.random.uniform(-1,1,[2*final_A_mat.shape[1]*final_b_mat.shape[1]])
    # print(x0)
    x1 = np.array([0,0,0,-0.26,0.26,0,0,0,0, 0,0,1.0, 0,0,0,-0.26,0.26,0, 0,0,0, 0,0,-1.0])

    # pr = cProfile.Profile()
    # pr.enable()
    results = infer_optimization(x0, final_A_mat, final_b_mat)
    # p = Stats(pr)
    # p.strip_dirs()
    # p.sort_stats('cumtime')
    # p.print_stats(100)
    
    end_optimization = time.time()
    # print(results)
    print(results.x)
    print(lambda_two_modes(final_A_mat,final_b_mat)(x0))
    print(lambda_two_modes(final_A_mat,final_b_mat)(results.x))
    print(lambda_two_modes(final_A_mat,final_b_mat)(x1))
    print(results.success)
    print(results.message)


    print("Simulation time: ", end_simulation-start)
    print("Optimazation time: ", end_optimization-end_coedf)
    # draw2D_dots(y_list)

    start_svm = time.time()
    A_row = final_A_mat.shape[0]
    A_col = final_A_mat.shape[1]
    b_col = final_b_mat.shape[1]
    x1, x2 = np.array_split(results.x,2)
    x1 = np.mat(x1.reshape([A_col,b_col],order='F'))
    y1 = np.matmul(final_A_mat,x1) - final_b_mat
    y1 = np.multiply(y1,y1)
    y1 = y1.sum(axis=1)
    x2 = np.mat(x2.reshape([A_col,b_col],order='F'))
    y2 = np.matmul(final_A_mat,x2) - final_b_mat
    y2 = np.multiply(y2,y2)
    y2 = y2.sum(axis=1)
    modet = np.zeros(A_row)
    for i in range(0,A_row):
        if y1[i] < y2[i]:
            modet[i] = 1
        else:
            modet[i] = -1
    # plt.plot(t_points,y_list[0])   
    # plt.plot(t_points[:A_row],modet)
    # plt.show() 
    y = []
    x = []
    for i in range(0,A_row):
        y.append(modet[i])
        x_0 = y_list[0][i][0]
        x_1 = y_list[0][i][1]
        x.append({1:x_0, 2:x_1})
    
    prob  = svm_problem(y, x)
    param = svm_parameter('-t 0 -c 200 -b 1')
    m = svm_train(prob, param)
    svm_save_model('model_file', m)
    end_svm_training = time.time()

    nsv = m.get_nr_sv()
    y0cof = 0.0
    y1cof = 0.0
    for i in range(0,nsv):
        if m.get_SV()[i].__contains__(1):
            y0cof = y0cof + m.get_sv_coef()[i][0]*m.get_SV()[i][1]
        if m.get_SV()[i].__contains__(2):
            y1cof = y1cof + m.get_sv_coef()[i][0]*m.get_SV()[i][2]
    print("y0 coef: ", y0cof)
    print("y1 coef: ", y1cof)
    print("constant: ", -m.rho[0])

    mode1_y0 = []
    mode1_y1 = []
    mode2_y0 = []
    mode2_y1 = []
    for i in range(0,A_row):
        if modet[i] > 0 :
            mode1_y0.append(y_list[0][i][0])
            mode1_y1.append(y_list[0][i][1])
        else:
            mode2_y0.append(y_list[0][i][0])
            mode2_y1.append(y_list[0][i][1])
    p_label, p_acc, p_val = svm_predict(y,x,m)
    end_svm_predict = time.time()
    print(p_acc)
    print("SVM training time: ", end_svm_training-start_svm)
    print("SVM predict time: ", end_svm_predict-end_svm_training)
    print("total time: ", end_svm_predict-start)
    plt.scatter(mode1_y0,mode1_y1,c='r',s=0.1)
    plt.scatter(mode2_y0,mode2_y1,c='g',s=0.1)
    yy1 = np.arange(-3,3,0.001)
    yy0 = (m.rho[0] - y1cof*yy1)/y0cof
    plt.plot(yy0,yy1)
    plt.show()