Beispiel #1
0
def test_solve_single_request_scenarios_with_gurobi(request_id, substrate):
    request = create_request(request_id)
    request.profit = 1.0

    scenario = datamodel.Scenario("test",
                                  substrate, [request],
                                  objective=datamodel.Objective.MAX_PROFIT)
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.init_model_creator()
    solution = mc.compute_fractional_solution()

    assert solution is not None
    assert len(solution.request_mapping[request]) > 0
def test_create_node_mapping_constraints(request_id, substrate,
                                         import_gurobi_mock):
    MockModel = import_gurobi_mock.MockModel
    MockConstr = import_gurobi_mock.MockConstr
    MockLinExpr = import_gurobi_mock.MockLinExpr

    req = create_request(request_id)
    req.graph["root"] = example_requests[request_id]["assumed_root"]
    scenario = datamodel.Scenario("test", substrate, [req])

    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mock_model")

    if example_requests[request_id].get("ignore_bfs", False):
        mc.dag_requests[req] = req

    mc.preprocess_input()
    mc._create_sub_lp_variables()
    mc._create_node_mapping_variables()
    mc._create_node_mapping_constraints()

    def node_agg_var(i, u):
        return mc.var_aggregated_node_mapping[req][i][u]

    def node_mapping_var(i, index, u):
        return mc.var_node_mapping[req][i][u][index]

    def source_var(ij, index, u):
        return mc.edge_sub_lp[req][ij][index].var_node_flow_source[u]

    def sink_var(ij, index, v):
        return mc.edge_sub_lp[req][ij][index].var_node_flow_sink[v]

    var_type_lookup = dict(node_agg_var=node_agg_var,
                           node_mapping_var=node_mapping_var,
                           source_var=source_var,
                           sink_var=sink_var)

    expected = set()
    for constraint_data in example_requests[request_id]["constraints"][
            "node_mapping"]:
        name, expr_data = constraint_data
        name = name.format(req_name=req.name)
        expr = []
        for coeff, var_type, var_keys in expr_data:
            expr.append((coeff, var_type_lookup[var_type](*var_keys)))
        expected.add(
            MockConstr(MockLinExpr(expr), gurobipy.GRB.EQUAL, 0.0, name))

    assert set(mc.model.constrs) == expected
Beispiel #3
0
def test_solve_all_request_scenarios_with_gurobi(substrate):
    requests = [
        create_request(request_id) for request_id in filter_requests_by_tags()
    ]
    for req in requests:
        req.profit = 1.0

    scenario = datamodel.Scenario("test",
                                  substrate,
                                  requests,
                                  objective=datamodel.Objective.MAX_PROFIT)
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.init_model_creator()
    solution = mc.compute_fractional_solution()

    assert solution is not None
    for req in requests:
        assert len(solution.request_mapping[req]) > 0
Beispiel #4
0
def test_decomposition_full_request(request_id, substrate):
    req = create_request(request_id)
    req.graph["root"] = example_requests[request_id]["assumed_root"]
    scenario = datamodel.Scenario("test", substrate, [req])

    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mockmodel")

    if example_requests[request_id].get("ignore_bfs", False):
        mc.dag_requests[req] = req

    mc.preprocess_input()
    mc.create_variables()

    def node_mapping_var(i, index, u):
        return mc.var_node_mapping[req][i][u][index]

    def source_var(ij, index, u):
        return mc.edge_sub_lp[req][ij][index].var_node_flow_source[u]

    def sink_var(ij, index, v):
        return mc.edge_sub_lp[req][ij][index].var_node_flow_sink[v]

    def edge_var(ij, index, uv):
        return mc.edge_sub_lp[req][ij][index].var_edge_flow[uv]

    var_type_lookup = dict(node_mapping_var=node_mapping_var,
                           source_var=source_var,
                           sink_var=sink_var,
                           edge_var=edge_var)

    expected_mappings = set()
    expected_embedding_flow = 0.0
    for test_mapping in example_requests[request_id]["mappings"]:
        flow = test_mapping["flow"]
        expected_embedding_flow += flow
        vars = test_mapping["variables"]
        expected_mappings.add(
            (flow,
             frozenset(test_mapping["expected"]["nodes"].items()),
             frozenset((k, tuple(v)) for (k, v) in test_mapping["expected"]["edges"].items()))
        )
        for var_type, req_key, comm_index, substrate_key in vars:
            var = var_type_lookup[var_type](req_key, comm_index, substrate_key)
            var.x += flow
        mc.var_embedding_decision[req].x += flow

    frac_sol = mc.recover_fractional_solution_from_variables()

    obtained_mappings = set()
    for mapping_list in frac_sol.request_mapping.values():
        for mapping in mapping_list:
            obtained_mappings.add((
                frac_sol.mapping_flows[mapping.name],
                frozenset(mapping.mapping_nodes.items()),
                frozenset((k, tuple(v)) for (k, v) in mapping.mapping_edges.items())
            ))

    assert obtained_mappings == expected_mappings
    assert mc._used_flow_embedding_decision[req] == expected_embedding_flow

    expected_used_node_flow = {req: {}}
    for i, u_comm_index_dict in mc.var_node_mapping[req].items():
        expected_used_node_flow[req][i] = {}
        for u, comm_index_dict in u_comm_index_dict.items():
            expected_used_node_flow[req][i][u] = {}
            for comm_index, var in comm_index_dict.items():
                expected_used_node_flow[req][i][u][comm_index] = var.x
    assert mc._used_flow_node_mapping == expected_used_node_flow

    for ij in mc.dag_requests[req].edges:
        for comm_index, sub_lp in mc.edge_sub_lp[req][ij].iteritems():
            expected_used_source_flow = {
                u: var.x for u, var in sub_lp.var_node_flow_source.iteritems()
            }
            assert sub_lp._used_flow_source == expected_used_source_flow
            expected_used_sink_flow = {
                u: var.x for u, var in sub_lp.var_node_flow_sink.iteritems()
            }
            assert sub_lp._used_flow_sink == expected_used_sink_flow
            expected_used_edge_flow = {
                uv: var.x for uv, var in sub_lp.var_edge_flow.iteritems()
            }
            assert sub_lp._used_flow == expected_used_edge_flow
def test_calculate_edge_label_bags(request_id):
    request = create_request(request_id)
    labels = commutativity_model.CommutativityLabels.create_labels(request)
    expected = example_requests[request_id]["bags"]
    obtained = {i: labels.calculate_edge_label_bags(i) for i in request.nodes}
    assert obtained == expected
def test_find_node_labels(request_id):
    expected = example_requests[request_id]["labels"]
    request = create_request(request_id)
    assert commutativity_model.CommutativityLabels.create_labels(request).node_labels == expected