Ejemplo n.º 1
0
def HJSolver(dynamics_obj,
             grid,
             multiple_value,
             tau,
             compMethod,
             plot_option,
             saveAllTimeSteps=False,
             accuracy="low"):
    print("Welcome to optimized_dp \n")
    if type(multiple_value) == list:
        init_value = multiple_value[0]
        constraint = multiple_value[1]
    else:
        init_value = multiple_value
        constraint = None

    hcl.init()
    hcl.config.init_dtype = hcl.Float(32)

    ################# INITIALIZE DATA TO BE INPUT INTO EXECUTABLE ##########################

    print("Initializing\n")

    if constraint is None:
        print("No obstacles set !")
    else:
        print("Obstacles set exists !")
        constraint_dim = constraint.ndim

        # Time-varying obstacle sets
        if constraint_dim > grid.dims:
            constraint_i = constraint[..., 0]
        else:
            # Time-invariant obstacle set
            constraint_i = constraint

    # Tensors input to our computation graph
    V_0 = hcl.asarray(init_value)
    V_1 = hcl.asarray(np.zeros(tuple(grid.pts_each_dim)))
    l0 = hcl.asarray(init_value)
    # For debugging purposes
    probe = hcl.asarray(np.zeros(tuple(grid.pts_each_dim)))

    # Array for each state values
    list_x1 = np.reshape(grid.vs[0], grid.pts_each_dim[0])
    list_x2 = np.reshape(grid.vs[1], grid.pts_each_dim[1])
    list_x3 = np.reshape(grid.vs[2], grid.pts_each_dim[2])
    if grid.dims >= 4:
        list_x4 = np.reshape(grid.vs[3], grid.pts_each_dim[3])
    if grid.dims >= 5:
        list_x5 = np.reshape(grid.vs[4], grid.pts_each_dim[4])
    if grid.dims >= 6:
        list_x6 = np.reshape(grid.vs[5], grid.pts_each_dim[5])

    # Convert state arrays to hcl array type
    list_x1 = hcl.asarray(list_x1)
    list_x2 = hcl.asarray(list_x2)
    list_x3 = hcl.asarray(list_x3)
    if grid.dims >= 4:
        list_x4 = hcl.asarray(list_x4)
    if grid.dims >= 5:
        list_x5 = hcl.asarray(list_x5)
    if grid.dims >= 6:
        list_x6 = hcl.asarray(list_x6)

    # Get executable, obstacle check intial value function
    if grid.dims == 3:
        solve_pde = graph_3D(dynamics_obj, grid, compMethod["PrevSetsMode"],
                             accuracy)

    if grid.dims == 4:
        solve_pde = graph_4D(dynamics_obj, grid, compMethod["PrevSetsMode"],
                             accuracy)

    if grid.dims == 5:
        solve_pde = graph_5D(dynamics_obj, grid, compMethod["PrevSetsMode"],
                             accuracy)

    if grid.dims == 6:
        solve_pde = graph_6D(dynamics_obj, grid, compMethod["PrevSetsMode"],
                             accuracy)
    """ Be careful, for high-dimensional array (5D or higher), saving value arrays at all the time steps may 
    cause your computer to run out of memory """
    if saveAllTimeSteps is True:
        valfuncs = np.zeros(
            np.insert(tuple(grid.pts_each_dim), grid.dims, len(tau)))
        valfuncs[..., -1] = V_0.asnumpy()
        print(valfuncs.shape)

    ################ USE THE EXECUTABLE ############
    # Variables used for timing
    execution_time = 0
    iter = 0
    tNow = tau[0]
    print("Started running\n")
    for i in range(1, len(tau)):
        #tNow = tau[i-1]
        t_minh = hcl.asarray(np.array((tNow, tau[i])))

        # taking obstacle at each timestep
        if "TargetSetMode" in compMethod and constraint_dim > grid.dims:
            constraint_i = constraint[..., i]

        while tNow <= tau[i] - 1e-4:
            tmp_arr = V_0.asnumpy()
            # Start timing
            iter += 1
            start = time.time()

            # Run the execution and pass input into graph
            if grid.dims == 3:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, t_minh, l0)
            if grid.dims == 4:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4, t_minh,
                          l0, probe)
            if grid.dims == 5:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4,
                          list_x5, t_minh, l0)
            if grid.dims == 6:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4,
                          list_x5, list_x6, t_minh, l0)

            tNow = np.asscalar((t_minh.asnumpy())[0])

            # Calculate computation time
            execution_time += time.time() - start

            # If TargetSetMode is specified by user
            if "TargetSetMode" in compMethod:
                if compMethod["TargetSetMode"] == "max":
                    tmp_val = np.maximum(V_0.asnumpy(), -constraint_i)
                elif compMethod["TargetSetMode"] == "min":
                    tmp_val = np.minimum(V_0.asnumpy(), -constraint_i)
                # Update final result
                V_1 = hcl.asarray(tmp_val)
                # Update input for next iteration
                V_0 = hcl.asarray(tmp_val)

            # Some information printing
            print(t_minh)
            print("Computational time to integrate (s): {:.5f}".format(
                time.time() - start))

        if saveAllTimeSteps is True:
            valfuncs[..., -1 - i] = V_1.asnumpy()

    # Time info printing
    print("Total kernel time (s): {:.5f}".format(execution_time))
    print("Finished solving\n")

    ##################### PLOTTING #####################
    if plot_option.do_plot:
        # Only plots last value array for now
        plot_isosurface(grid, V_1.asnumpy(), plot_option)

    if saveAllTimeSteps is True:
        return valfuncs

    return V_1.asnumpy()
Ejemplo n.º 2
0
def main():
    ################### PARSING ARGUMENTS FROM USERS #####################

    parser = ArgumentParser()
    parser.add_argument("-p", "--plot", default=True, type=bool)
    # Print out LLVM option only
    parser.add_argument("-l", "--llvm", default=False, type=bool)
    args = parser.parse_args()

    hcl.init()
    hcl.config.init_dtype = hcl.Float()

    ################# INITIALIZE DATA TO BE INPUT INTO EXECUTABLE ##########################

    print("Initializing\n")

    V_0 = hcl.asarray(my_shape)
    V_1 = hcl.asarray(np.zeros(tuple(g.pts_each_dim)))
    l0 = hcl.asarray(my_shape)
    #probe = hcl.asarray(np.zeros(tuple(g.pts_each_dim)))
    #obstacle = hcl.asarray(cstraint_values)

    list_x1 = np.reshape(g.vs[0], g.pts_each_dim[0])
    list_x2 = np.reshape(g.vs[1], g.pts_each_dim[1])
    list_x3 = np.reshape(g.vs[2], g.pts_each_dim[2])
    if g.dims >= 4:
        list_x4 = np.reshape(g.vs[3], g.pts_each_dim[3])
    if g.dims >= 5:
        list_x5 = np.reshape(g.vs[4], g.pts_each_dim[4])
    if g.dims >= 6:
        list_x6 = np.reshape(g.vs[5], g.pts_each_dim[5])

    # Convert to hcl array type
    list_x1 = hcl.asarray(list_x1)
    list_x2 = hcl.asarray(list_x2)
    list_x3 = hcl.asarray(list_x3)
    if g.dims >= 4:
        list_x4 = hcl.asarray(list_x4)
    if g.dims >= 5:
        list_x5 = hcl.asarray(list_x5)
    if g.dims >= 6:
        list_x6 = hcl.asarray(list_x6)

    # Get executable
    if g.dims == 4:
        solve_pde = graph_4D()
    if g.dims == 5:
        solve_pde = graph_5D()
    if g.dims == 6:
        solve_pde = graph_6D()

    # Print out code for different backend
    #print(solve_pde)

    ################ USE THE EXECUTABLE ############
    # Variables used for timing
    execution_time = 0
    lookback_time = 0

    tNow = tau[0]
    for i in range(1, len(tau)):
        #tNow = tau[i-1]
        t_minh = hcl.asarray(np.array((tNow, tau[i])))
        while tNow <= tau[i] - 1e-4:
            # Start timing
            start = time.time()

            print("Started running\n")

            # Run the execution and pass input into graph
            if g.dims == 4:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4, t_minh,
                          l0)
            if g.dims == 5:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4,
                          list_x5, t_minh, l0)
            if g.dims == 6:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4,
                          list_x5, list_x6, t_minh, l0)

            tNow = np.asscalar((t_minh.asnumpy())[0])

            # Calculate computation time
            execution_time += time.time() - start

            # Some information printing
            print(t_minh)
            print("Computational time to integrate (s): {:.5f}".format(
                time.time() - start))
            # Saving data into disk

    # Time info printing
    print("Total kernel time (s): {:.5f}".format(execution_time))
    print("Finished solving\n")

    # V1 is the final value array, fill in anything to use it

    ##################### PLOTTING #####################
    if args.plot:
        plot_isosurface(g, V_1.asnumpy(), [0, 1, 3])
Ejemplo n.º 3
0
def HJSolver(dynamics_obj, grid, init_value, tau, compMethod, plot_option):
    print("Welcome to optimized_dp \n")

    ################### PARSING ARGUMENTS FROM USERS #####################

    parser = ArgumentParser()
    parser.add_argument("-p", "--plot", default=True, type=bool)
    # # Print out LLVM option only
    # parser.add_argument("-l", "--llvm", default=False, type=bool)
    args = parser.parse_args()

    hcl.init()
    hcl.config.init_dtype = hcl.Float(32)

    ################# INITIALIZE DATA TO BE INPUT INTO EXECUTABLE ##########################

    print("Initializing\n")

    V_0 = hcl.asarray(init_value)
    V_1 = hcl.asarray(np.zeros(tuple(grid.pts_each_dim)))
    l0  = hcl.asarray(init_value)
    probe = hcl.asarray(np.zeros(tuple(grid.pts_each_dim)))
    #obstacle = hcl.asarray(cstraint_values)

    list_x1 = np.reshape(grid.vs[0], grid.pts_each_dim[0])
    list_x2 = np.reshape(grid.vs[1], grid.pts_each_dim[1])
    list_x3 = np.reshape(grid.vs[2], grid.pts_each_dim[2])
    if grid.dims >= 4:
        list_x4 = np.reshape(grid.vs[3], grid.pts_each_dim[3])
    if grid.dims >= 5:
        list_x5 = np.reshape(grid.vs[4], grid.pts_each_dim[4])
    if grid.dims >= 6:
        list_x6 = np.reshape(grid.vs[5], grid.pts_each_dim[5])


    # Convert to hcl array type
    list_x1 = hcl.asarray(list_x1)
    list_x2 = hcl.asarray(list_x2)
    list_x3 = hcl.asarray(list_x3)
    if grid.dims >= 4:
        list_x4 = hcl.asarray(list_x4)
    if grid.dims >= 5:
        list_x5 = hcl.asarray(list_x5)
    if grid.dims >= 6:
        list_x6 = hcl.asarray(list_x6)

    # Get executable
    if grid.dims == 3:
        solve_pde = graph_3D(dynamics_obj, grid, compMethod)
    if grid.dims == 4:
        solve_pde = graph_4D(dynamics_obj, grid, compMethod)
    if grid.dims == 5:
        solve_pde = graph_5D(dynamics_obj, grid, compMethod)
    if grid.dims == 6:
        solve_pde = graph_6D(dynamics_obj, grid, compMethod)

    # Print out code for different backend
    #print(solve_pde)

    ################ USE THE EXECUTABLE ############
    # Variables used for timing
    execution_time = 0
    iter = 0
    tNow = tau[0]
    print("Started running\n")
    for i in range (1, len(tau)):
        #tNow = tau[i-1]
        t_minh= hcl.asarray(np.array((tNow, tau[i])))
        while tNow <= tau[i] - 1e-4:
             tmp_arr = V_0.asnumpy()
             # Start timing
             iter += 1
             start = time.time()

             # Run the execution and pass input into graph
             if grid.dims == 3:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, t_minh, l0)
             if grid.dims == 4:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4, t_minh, l0, probe)
             if grid.dims == 5:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4, list_x5 ,t_minh, l0)
             if grid.dims == 6:
                solve_pde(V_1, V_0, list_x1, list_x2, list_x3, list_x4, list_x5, list_x6, t_minh, l0)

             tNow = np.asscalar((t_minh.asnumpy())[0])

             # Calculate computation time
             execution_time += time.time() - start

             # Some information printing
             print(t_minh)
             print("Computational time to integrate (s): {:.5f}".format(time.time() - start))


    # Time info printing
    print("Total kernel time (s): {:.5f}".format(execution_time))
    print("Finished solving\n")

    # Save into file
    np.save("new_center_final.npy", V_1.asnumpy())

    print(np.sum(V_1.asnumpy() < 0))

    ##################### PLOTTING #####################
    if args.plot:
        # plot Value table when speed is maximum
        plot_isosurface(grid, V_1.asnumpy(), plot_option)
        #plot_isosurface(g, my_V, [0, 1, 3], 10)
    return V_1.asnumpy()