Пример #1
0
def test_snk_mirror_solver():
    circle = create_circle_n_k(9, 3)
    solver = SnkMirrorSolver()
    sol = solver.solve(circle)
    circle = create_circle_n_k(12, 3)
    sol1 = solver.solve(circle)
    circle = create_circle_n_k(14, 4)
    sol2 = solver.solve(circle)
    circle = create_circle_n_k(20, 5)
    sol3 = solver.solve(circle)
    print("\\o/")
Пример #2
0
def vis_full_circle_solutions():
    from instance_generation import create_circle_n_k
    graphs = [create_circle_n_k(n, n) for n in range(4, 9)]
    from solver.cp import ConstraintDependencySolver, ConstraintDependencyLocalMinSumSolver
    from solver.mip import AngularDependencySolver, AngularDependencyLocalMinSumSolver
    sols_MSSC = []
    sols_MLSSC = []

    import pickle
    try:
        with open("Circle_sols.pk", "rb") as f:
            sols_MSSC, sols_MLSSC = pickle.load(f)
    except (EOFError, FileNotFoundError):
        solver_MSSC = AngularDependencySolver()
        solver_MLSSC = AngularDependencyLocalMinSumSolver()
        for g in tqdm.tqdm(graphs):
            sols_MSSC.append(solver_MSSC.solve(g))
            sols_MLSSC.append(solver_MLSSC.solve(g))

        with open("Circle_sols.pk", "wb") as f:
            pickle.dump((sols_MSSC, sols_MLSSC), f)
    from utils import visualize_min_sum_sol_2d
    print("Min Sum Sols:")
    for s in sols_MSSC:
        visualize_min_sum_sol_2d(s)
    return
Пример #3
0
def test_dependency_graph():
    circle = create_circle_n_k(6, 2)
    solver = GraphN2Solver()
    sweep_types = [0, -1, 0, 0, 0, 1]
    directions = [-1, -1, 1, -1, 1, 1]
    orientations = [
        PointSweep(direction, sweep_type, index)
        for direction, sweep_type, index in zip(directions, sweep_types,
                                                range(len(directions)))
    ]

    dep_graph = solver._calculate_dependecy_graph(circle, orientations)

    orig_dep_dict = {
        (0, 1): [(0, 2), (1, 5)],
        (0, 2): [(0, 4), (2, 4)],
        (1, 2): [(0, 2), (1, 3)],
        (1, 3): [(2, 3), (0, 1)],
        (2, 3): [],
        (2, 4): [(2, 3), (0, 4)],
        (3, 4): [(3, 5), (2, 4)],
        (3, 5): [(1, 3), (4, 5)],
        (4, 5): [(1, 5)],
        (0, 4): [(4, 5), (0, 5)],
        (0, 5): [],
        (1, 5): [(0, 5)]
    }
    orig_dep_graph = {edge: DependencyNode(edge) for edge in orig_dep_dict}
    for edge in orig_dep_dict:
        for dependency in orig_dep_dict[edge]:
            orig_dep_graph[edge].add_dependency(orig_dep_graph[dependency])

    assert orig_dep_graph == dep_graph
Пример #4
0
def test_get_tripel_edges():
    graph = create_circle_n_k(15, 5)
    tripel_control, abs_graph = convert_graph_to_angular_abstract_graph(
        graph, simple_graph=False, return_tripel_edges=True)
    tripel_edges = get_tripeledges_from_abs_graph(abs_graph)
    assert len(tripel_edges) == len(tripel_control)
    for edge in tripel_edges:
        assert tripel_control[edge] == tripel_edges[edge]
Пример #5
0
def test_calc_times_old_new():
    graph = create_circle_n_k(15, 10)
    order = [(i, j) for i in range(graph.vert_amount)
             for j in range(i + 1, graph.vert_amount)]
    old_times = _calculate_times_old(order, graph)
    new_times = calculate_times(order, graph)
    for old, new in zip(old_times, new_times):
        assert old == new
        assert math.isclose(old_times[old], new_times[new])
Пример #6
0
def create_graph():
    d1 = get_distance(9, 3)
    d2 = get_distance(9, 2)
    d = d1 + np.random.default_rng().random() * (d2 - d1)
    body = CelestialBody((0, 0), d, None)
    g = create_circle_n_k(9, 4)
    graph = CelestialGraph([body], g.vertices, g.edges)
    visualize_graph_2d(graph)
    for i in range(10):
        rand_g = create_random_celest_graph(9, celest_bounds=(0.4, 0.6))
        visualize_graph_2d(rand_g)
Пример #7
0
def test_generate_possible_sweeps():
    circle = create_circle_n_k(6, 2)
    solver = GraphN2Solver()
    possibilities = solver._generate_possible_start_ends(circle, 2)
    sweep = solver._calculate_orientations(circle, possibilities[0])
    sweep_types = [-1, -1, 0, 0, 0, 0]
    directions = [1, 1, -1, 1, -1, 1]
    for sweep_point, sweep_type, direction in zip(sweep, sweep_types,
                                                  directions):
        assert sweep_point.direction == direction
        assert sweep_point.type == sweep_type
Пример #8
0
def test_get_order_from_times():
    graph = create_circle_n_k(15, 10)
    order = [(i, j) for i in range(graph.vert_amount)
             for j in range(i + 1, graph.vert_amount)]
    times = calculate_times(order, graph)
    new_order = calculate_order_from_times(times, graph)
    # As both orders could actually differ without changing a thing,
    # we instead test if we get the same times again
    new_times = calculate_times(new_order, graph)
    for key in new_times:
        assert math.isclose(times[key], new_times[key])
Пример #9
0
def test_order():
    circle = create_circle_n_k(6, 2)
    solver = GraphN2Solver()
    sweep_types = [0, -1, 0, 0, 0, 1]
    directions = [-1, -1, 1, -1, 1, 1]
    orientations = [
        PointSweep(direction, sweep_type, index)
        for direction, sweep_type, index in zip(directions, sweep_types,
                                                range(len(directions)))
    ]

    dep_graph = solver._calculate_dependecy_graph(circle, orientations)
    order = solver._calculate_order(dep_graph)
Пример #10
0
def create_graph():
    d1 = get_distance(9,3)
    d2 = get_distance(9,2)
    d = d1 + np.random.default_rng().random() * (d2-d1)
    body = CelestialBody((0,0), d, None)
    g = create_circle_n_k(9,4)
    graph = CelestialGraph([body], g.vertices, g.edges)
    visualize_graph_2d(graph)
    for i in range(10):
        for j in range(3):
            rand_c = create_random_celest_graph(9+i,celest_bounds=(0.4,0.6))
            rand_g = create_random_instance(9+i, edge_chance=random.uniform(0.3, 1))
            visualize_graph_2d(rand_g, savePath=f"instance_vis/rand_e{9+i}_{j}.pdf")
            visualize_graph_2d(rand_c, savePath=f"instance_vis/cel_e{9+i}_{j}.pdf")
Пример #11
0
def solution_example_intro():
    solver1 = ALL_SOLVER["ConstraintAbsSolver"]()
    solver2 = ALL_SOLVER["ConstraintDependencySolver"]()
    solver3 = ALL_SOLVER["ConstraintDependencyLocalMinSumSolver"]()
    s52 = create_circle_n_k(5, 2)
    visualize_graph_2d(s52)
    sol1 = solver1.solve(s52)
    sol2 = solver2.solve(s52)
    sol3 = solver3.solve(s52)
    visualize_solution_2d(sol1)
    visualize_min_sum_sol_2d(sol1)
    print("makespan", sol1.makespan)
    visualize_solution_2d(sol2)
    visualize_min_sum_sol_2d(sol2)
    print("MinSum", sol2.min_sum)
    visualize_solution_2d(sol3)
    visualize_min_sum_sol_2d(sol3)
    print("LocalMinSum", sol2.local_min_sum)
    return
Пример #12
0
def _test_dep_mip_with_subtours():
    from solver.mip import AngularDependencySolver
    from instance_generation import create_circle_n_k
    solver_w = AngularDependencySolver(with_vertex_subtour_constr=True)
    solver_o = AngularDependencySolver()
    for n in range(5, 10):
        g = create_circle_n_k(n, 3)
        print("#########################################################")
        print("################ SOLVER WITH SUBTOURS ###################")
        print("#########################################################")
        s_w = solver_w.solve(g)
        print("#########################################################")
        print("################ SOLVER WITHOUT SUBTOURS ################")
        print("#########################################################")
        s_o = solver_o.solve(g)

        print("#########################################################")
        print("################ RUNTIME WITH:",s_w.runtime,"WITHOUT:", s_o.runtime,"###################")
        print("#########################################################")
        if s_w.is_optimal and s_o.is_optimal:
            assert math.isclose(s_w.min_sum, s_o.min_sum)
Пример #13
0
def test_solver():
    circles = [create_circle_n_k(n, 2) for n in range(7, 35)]
    solver = GraphN2Solver()
    sols = [solver.solve(circle) for circle in circles]
    for sol in sols:
        assert sol, "Did not find a solution for a graph"