Ejemplo n.º 1
0
def central_wrapper(g, horizon, searchers, b0, M_target, gamma, timeout):
    """Add variables, constraints, objective function and solve the model
    compute all paths"""

    solver_type = 'central'

    # V^{s, t}
    start, vertices_t, times_v = cm.get_vertices_and_steps(
        g, horizon, searchers)

    # create model
    md = mf.create_model()

    # add variables
    my_vars = mf.add_variables(md, g, horizon, start, vertices_t, searchers)

    # add constraints (central algorithm)
    mf.add_constraints(md, g, my_vars, searchers, vertices_t, horizon, b0,
                       M_target)

    # objective function
    mf.set_solver_parameters(md, gamma, horizon, my_vars, timeout)

    # solve and save results
    obj_fun, time_sol, gap, x_searchers, b_target, threads = mf.solve_model(md)

    # clean things
    md.reset()
    md.terminate()
    del md
    #

    # clean things
    return obj_fun, time_sol, gap, x_searchers, b_target, threads
Ejemplo n.º 2
0
def test_run_solver_get_model_data():
    horizon, theta, deadline, solver_type = get_solver_param()
    g, v0_target, v0_searchers, target_motion, belief_distribution = parameters_sim()
    gamma = 0.99
    timeout = 60

    # initialize parameters according to inputs
    b_0 = cp.set_initial_belief(g, v0_target, belief_distribution)
    M = cp.set_motion_matrix(g, target_motion)
    searchers = cp.create_dict_searchers(g, v0_searchers)

    # solve: 1 [low level]
    start, vertices_t, times_v = cm.get_vertices_and_steps(g, horizon, searchers)
    # create model
    md = mf.create_model()
    # add variables
    my_vars = mf.add_variables(md, g, horizon, start, vertices_t, searchers)
    # add constraints (central algorithm)
    mf.add_constraints(md, g, my_vars, searchers, vertices_t, horizon, b_0, M)
    # objective function
    mf.set_solver_parameters(md, gamma, horizon, my_vars, timeout)
    # update
    md.update()
    # Optimize model
    md.optimize()
    x_s1, b_target1 = mf.query_variables(md)
    obj_fun1, time_sol1, gap1, threads1 = mf.get_model_data(md)
    pi_dict1 = core.extract_info.xs_to_path_dict(x_s1)
    path1 = core.extract_info.path_as_list(pi_dict1)

    # solve: 2
    obj_fun2, time_sol2, gap2, x_s2, b_target2, threads2 = pln.run_solver(g, horizon, searchers, b_0, M)
    pi_dict2 = core.extract_info.xs_to_path_dict(x_s2)
    path2 = core.extract_info.path_as_list(pi_dict2)

    # solve: 3
    specs = my_specs()
    # initialize instances of classes
    path3 = pln.run_planner(specs)

    assert obj_fun1 == md.objVal
    assert round(time_sol1, 2) == round(md.Runtime, 2)
    assert gap1 == md.MIPGap

    # 1 x 2
    assert x_s1 == x_s2
    assert b_target1 == b_target2

    assert obj_fun2 == obj_fun1
    assert round(time_sol2, 2) == round(time_sol1, 2)
    assert gap2 == gap1
    assert threads2 == threads1

    # paths
    assert pi_dict1 == pi_dict2
    assert path1 == path2
    # 1 x 3
    assert path1 == path3
Ejemplo n.º 3
0
def test_solver_data_class():

    horizon = 3
    n, b_0, M, searchers, g = parameters_7v_random_motion2()
    # solve
    # create model
    md = Model("my_model")

    start, vertices_t, times_v = cm.get_vertices_and_steps(
        g, horizon, searchers)
    start1, vertices_t1, times_v1 = cm.get_vertices_and_steps(
        g, horizon, searchers)

    my_vars = mf.add_variables(md, g, horizon, start, vertices_t)

    mf.add_constraints(md, g, my_vars, searchers, vertices_t, horizon, b_0, M)

    mf.set_solver_parameters(md, 0.99, horizon, my_vars)

    md.update()
    # Optimize model
    md.optimize()

    x_s, b_target = mf.query_variables(md)

    obj_fun = md.objVal
    gap = md.MIPGap
    time_sol = round(md.Runtime, 4)
    threads = md.Params.Threads
    deadline = 6
    theta = 3

    my_data = MySolverData(horizon, deadline, theta, g, 'central')
    t = 0
    my_data.store_new_data(obj_fun, time_sol, gap, threads, x_s, b_target, t,
                           horizon)

    assert all(start1) == all(start)
    assert all(vertices_t) == all(vertices_t1)
    assert all(times_v) == all(times_v1)

    assert my_data.obj_value[0] == obj_fun
    assert my_data.solve_time[0] == time_sol
    assert my_data.threads[0] == md.Params.Threads
    assert my_data.gap[0] == gap
    assert my_data.x_s[0] == x_s
    assert my_data.belief[0] == b_target
    assert my_data.solver_type == 'central'
    assert my_data.threads[0] == threads
Ejemplo n.º 4
0
def distributed_wrapper(g,
                        horizon,
                        searchers,
                        b0,
                        M_target,
                        gamma,
                        timeout=5,
                        n_inter=1,
                        pre_solver=-1):
    """Distributed version of the algorithm """

    # parameter to stop iterations
    # number of full loops s= 1..m
    n_it = n_inter

    # iterative parameters
    total_time_sol = 0
    previous_obj_fun = 0
    my_counter = 0

    # temporary path for the searchers
    temp_pi = init_temp_path(searchers, horizon)

    # get last searcher number [m]
    m = ext.get_last_info(searchers)[0]

    obj_fun_list = {}
    time_sol_list = {}
    gap_list = {}

    while True:

        for s_id in searchers.keys():
            # create model
            md = mf.create_model()

            temp_pi['current_searcher'] = s_id

            start, vertices_t, times_v = cm.get_vertices_and_steps_distributed(
                g, horizon, searchers, temp_pi)

            # add variables
            my_vars = mf.add_variables(md, g, horizon, start, vertices_t,
                                       searchers)

            mf.add_constraints(md, g, my_vars, searchers, vertices_t, horizon,
                               b0, M_target)

            # objective function
            mf.set_solver_parameters(md, gamma, horizon, my_vars, timeout,
                                     pre_solver)

            # solve and save results
            obj_fun, time_sol, gap, x_searchers, b_target, threads = mf.solve_model(
                md)

            if md.SolCount == 0:
                # problem was infeasible or other error (no solution found)
                print('Error, no solution found!')
                obj_fun, time_sol, gap, threads = -1, -1, -1, -1
                # keep previous belief
                b_target = {}
                v = 0
                for el in b0:
                    b_target[(v, 0)] = el
                    v += 1

                x_searchers = keep_all_still(temp_pi)

            # clean things
            md.reset()
            md.terminate()
            del md

            # ------------------------------------------------------
            # append to the list
            obj_fun_list[s_id] = obj_fun
            time_sol_list[s_id] = time_sol
            gap_list[s_id] = gap

            # save the current searcher's path
            temp_pi = update_temp_path(x_searchers, temp_pi, s_id)

            total_time_sol = total_time_sol + time_sol_list[s_id]

            # end of a loop through searchers
            if s_id == m:
                # compute difference between previous and current objective functions
                delta_obj = abs(previous_obj_fun - obj_fun)

                # iterate
                previous_obj_fun = obj_fun
                my_counter = my_counter + 1

                # check for stoppers: either the objective function converged or iterated as much as I wanted
                if (delta_obj < 1e-4) or (my_counter >= n_it):
                    time_sol_list['total'] = total_time_sol
                    # clean and delete

                    disposeDefaultEnv()

                    return obj_fun_list, time_sol_list, gap_list, x_searchers, b_target, threads
Ejemplo n.º 5
0
def test_position_searchers():
    # load graph
    graph_file = 'G7V_test.p'
    g = ext.get_graph(graph_file)
    # input for target initial vertices (belief)
    v_target = [7]
    # initial searcher vertices
    v0_searchers = [1, 2]
    horizon = 3
    # type of motion
    target_motion = 'random'
    belief_distribution = 'uniform'
    b0, M = cp.my_target_motion(g, v_target, belief_distribution,
                                target_motion)
    # searchers
    searchers = cp.create_dict_searchers(g, v0_searchers)

    # solve
    # create model
    md = Model("my_model")

    start, vertices_t, times_v = cm.get_vertices_and_steps(
        g, horizon, searchers)

    # add variables
    my_vars = mf.add_variables(
        md,
        g,
        horizon,
        start,
        vertices_t,
    )

    # add constraints (central algorithm)
    mf.add_constraints(md, g, my_vars, searchers, vertices_t, horizon, b0, M)

    mf.set_solver_parameters(md, 0.99, horizon, my_vars)

    md.update()
    # Optimize model
    md.optimize()

    x_s, b_target = mf.query_variables(md)

    # check searcher position (1)
    assert x_s.get((1, 1, 0)) == 1
    assert x_s.get((1, 3, 1)) == 1
    assert x_s.get((1, 5, 2)) == 1
    assert x_s.get((1, 6, 3)) == 1
    # check searcher position (2)
    assert x_s.get((2, 2, 0)) == 1
    assert x_s.get((2, 5, 1)) == 1
    assert x_s.get((2, 6, 2)) == 1
    assert x_s.get((2, 7, 3)) == 1

    # check target belief t = 0
    assert b_target.get((0, 0)) == 0
    assert b_target.get((1, 0)) == 0
    assert b_target.get((2, 0)) == 0
    assert b_target.get((3, 0)) == 0
    assert b_target.get((4, 0)) == 0
    assert b_target.get((5, 0)) == 0
    assert b_target.get((6, 0)) == 0
    assert b_target.get((7, 0)) == 1

    # check target belief t = 1
    assert b_target.get((0, 1)) == 0
    assert b_target.get((1, 1)) == 0
    assert b_target.get((2, 1)) == 0
    assert b_target.get((3, 1)) == 0
    assert b_target.get((4, 1)) == 0
    assert b_target.get((5, 1)) == 0
    assert b_target.get((6, 1)) == 0.5
    assert b_target.get((7, 1)) == 0.5

    # check target belief t = 2
    assert round(b_target.get((0, 2)), 3) == 0.583
    assert b_target.get((1, 2)) == 0
    assert b_target.get((2, 2)) == 0
    assert b_target.get((3, 2)) == 0
    assert b_target.get((4, 2)) == 0
    assert b_target.get((5, 2)) == 0
    assert b_target.get((6, 2)) == 0
    assert round(b_target.get((7, 2)), 3) == 0.417

    # check target belief t = 3
    assert b_target.get((0, 3)) == 1
    assert b_target.get((1, 3)) == 0
    assert b_target.get((2, 3)) == 0
    assert b_target.get((3, 3)) == 0
    assert b_target.get((4, 3)) == 0
    assert b_target.get((5, 3)) == 0
    assert b_target.get((6, 3)) == 0
    assert b_target.get((7, 3)) == 0