def test_force_embedding_constraint_multiple_root_mappings(
        tiny_substrate, triangle_request, monkeypatch, import_gurobi_mock):
    MockModel = import_gurobi_mock.MockModel
    MockConstr = import_gurobi_mock.MockConstr
    MockLinExpr = import_gurobi_mock.MockLinExpr

    # Extend the root node's allowed nodes
    triangle_request.node[i]["allowed_nodes"] = set("uv")
    scenario = datamodel.Scenario("test", tiny_substrate, [triangle_request])
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mock_model")

    mc.preprocess_input()
    mc.create_variables_embedding_decision()
    mc._create_node_mapping_variables()
    mc._create_force_embedding_constraint()

    assert set(mc.model.constrs) == {
        MockConstr(
            MockLinExpr([
                (-1.0, mc.var_embedding_decision[triangle_request]),
                (1.0, mc.var_aggregated_node_mapping[triangle_request][i][u]),
                (1.0, mc.var_aggregated_node_mapping[triangle_request][i][v]),
            ]), gurobipy.GRB.EQUAL, 0.0,
            "force_node_mapping_for_embedded_request_req[test_req]"),
    }
예제 #2
0
def test_only_one_request_is_embedded_due_to_capacity_limitations():
    sub = vtd.create_test_substrate("single_edge_substrate")
    req1 = vtd.create_test_request("single_edge_request",
                                   sub,
                                   allowed_nodes=dict(i1=["u1"], i2=["u2"]))
    req2 = vtd.create_test_request(
        "single_edge_request",
        sub,
    )
    req1.name = "req1"
    req2.name = "req2"
    scenario = datamodel.Scenario(
        name="test_scen",
        requests=[req1, req2],
        substrate=sub,
        objective=datamodel.Objective.MAX_PROFIT,
    )

    v = vine.OfflineViNEAlgorithm(
        scenario,
        edge_embedding_model=vine.ViNEEdgeEmbeddingModel.UNSPLITTABLE,
        lp_objective=vine.ViNELPObjective.ViNE_COSTS_DEF,
        rounding_procedure=vine.ViNERoundingProcedure.DETERMINISTIC)
    result = v.compute_integral_solution()
    solution = result.get_solution()
    m1 = solution.request_mapping[req1]
    assert m1.mapping_nodes == dict(
        i1="u1",
        i2="u2",
    )
    assert m1.mapping_edges == {("i1", "i2"): [("u1", "u2")]}
    assert solution.request_mapping[req2] is None
def test_create_node_mapping_variables(tiny_substrate, triangle_request,
                                       import_gurobi_mock):
    MockModel = import_gurobi_mock.MockModel
    scenario = datamodel.Scenario("test", tiny_substrate, [triangle_request])
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mock_model")

    mc.preprocess_input()
    mc._create_node_mapping_variables()

    ku_index = frozenset([(k, u)])
    kv_index = frozenset([(k, v)])

    assert extract_var_names(mc.var_node_mapping[triangle_request][i][u]) == {
        ku_index:
        "node_mapping_req[test_req]_vnode[i]_snode[u]_comm_index[k_u]",
        kv_index:
        "node_mapping_req[test_req]_vnode[i]_snode[u]_comm_index[k_v]",
    }
    assert extract_var_names(mc.var_node_mapping[triangle_request][j][v]) == {
        ku_index:
        "node_mapping_req[test_req]_vnode[j]_snode[v]_comm_index[k_u]",
        kv_index:
        "node_mapping_req[test_req]_vnode[j]_snode[v]_comm_index[k_v]",
    }
    assert extract_var_names(mc.var_node_mapping[triangle_request][k][u]) == {
        frozenset():
        "node_mapping_req[test_req]_vnode[k]_snode[u]_comm_index[]",
    }
    assert extract_var_names(mc.var_node_mapping[triangle_request][k][v]) == {
        frozenset():
        "node_mapping_req[test_req]_vnode[k]_snode[v]_comm_index[]",
    }
예제 #4
0
    def solve_vne_lp_relax(self, fixed_node_mappings_dict=None):
        single_req_scenario = dm.Scenario(
            name="vine_scenario_{}".format(self._current_request.name),
            substrate=self.residual_capacity_substrate,
            requests=[self._current_request],
            objective=dm.Objective.MIN_COST,
        )

        sub_mc = FractionalClassicMCFModel(
            single_req_scenario,
            lp_objective=self.lp_objective,
            gurobi_settings=self.gurobi_settings,
            logger=self.logger,
            optimization_callback=None
        )
        sub_mc._disable_temporal_information_output = True
        sub_mc.init_model_creator()
        sub_mc.model.setParam("LogFile", "")
        if fixed_node_mappings_dict is not None:
            sub_mc.create_constraints_fix_node_mappings(self._current_request, fixed_node_mappings_dict)
        lp_variable_assignment = sub_mc.compute_fractional_solution()
        #necessary as otherwise too many gurobi models are created
        del sub_mc.model
        del sub_mc
        return lp_variable_assignment
def test_create_constraints_track_node_loads(monkeypatch, substrate,
                                             triangle_request,
                                             import_gurobi_mock):
    MockConstr = import_gurobi_mock.MockConstr
    MockLinExpr = import_gurobi_mock.MockLinExpr
    MockModel = import_gurobi_mock.MockModel

    # add a request node with new type
    l = "l"
    t1, t2 = "t1", "t2"
    triangle_request.add_node(l, 0.75, t2, [v])
    triangle_request.add_edge(l, i, 2.0, None)

    substrate.node[v]["supported_types"].append(t2)
    substrate.node[v]["capacity"][t2] = 100.0
    substrate.node[v]["cost"][t2] = 0.25
    substrate.types.add(t2)

    scenario = datamodel.Scenario("test", substrate, [triangle_request])
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mock_model")
    mc.preprocess_input()
    mc.create_variables_request_load()
    mc._create_node_mapping_variables()
    mc._create_constraints_track_node_loads()

    ku_index = frozenset([("k", "u")])
    kv_index = frozenset([("k", "v")])

    assert set(mc.model.constrs) == {
        MockConstr(MockLinExpr([
            (-1.0, mc.var_request_load[triangle_request][t2, v]),
            (0.75, mc.var_aggregated_node_mapping[triangle_request][l][v])
        ]),
                   GRB.EQUAL,
                   0.0,
                   name="track_node_load_req[test_req]_type[t2]_snode[v]"),
        MockConstr(MockLinExpr([
            (-1.0, mc.var_request_load[triangle_request][t1, v]),
            (1.0, mc.var_aggregated_node_mapping[triangle_request][j][v]),
            (1.0, mc.var_aggregated_node_mapping[triangle_request][k][v]),
        ]),
                   GRB.EQUAL,
                   0.0,
                   name="track_node_load_req[test_req]_type[t1]_snode[v]"),
        MockConstr(MockLinExpr([
            (-1.0, mc.var_request_load[triangle_request][t1, u]),
            (1.0, mc.var_aggregated_node_mapping[triangle_request][i][u]),
            (1.0, mc.var_aggregated_node_mapping[triangle_request][k][u]),
        ]),
                   GRB.EQUAL,
                   0.0,
                   name="track_node_load_req[test_req]_type[t1]_snode[u]"),
        MockConstr(MockLinExpr([
            (-1.0, mc.var_request_load[triangle_request][t1, w]),
        ]),
                   GRB.EQUAL,
                   0.0,
                   name="track_node_load_req[test_req]_type[t1]_snode[w]"),
    }
def test_extract_edge_mapping_reversed_edge(substrate, triangle_request):
    triangle_request.graph["root"] = j

    scenario = datamodel.Scenario("test", substrate, [triangle_request])

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

    ku_index = frozenset([(k, u)])
    kv_index = frozenset([(k, v)])

    ji_sub_lp = list(mc.edge_sub_lp[triangle_request][("j", "i")].values())[0]
    ji_sub_lp.var_node_flow_source[v].x = 0.5
    ji_sub_lp.var_edge_flow[wu].x = 0.5
    ji_sub_lp.var_edge_flow[vw].x = 0.5
    ji_sub_lp.var_node_flow_sink[u].x = 0.5

    partial_mapping = solutions.Mapping("test_mapping", triangle_request,
                                        substrate, True)
    partial_mapping.map_node(j, v)

    assert ji_sub_lp.extract_edge_mapping(partial_mapping) == ([uw,
                                                                wv], u, 0.5)
def test_reduce_flow_in_modelcreator(substrate, triangle_request, import_gurobi_mock):
    MockModel = import_gurobi_mock.MockModel
    scenario = datamodel.Scenario("test", substrate, [triangle_request])
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mock_model")
    mc.preprocess_input()
    mc.create_variables_embedding_decision()
    mc._create_node_mapping_variables()
    mc._create_sub_lp_variables()

    ku_index = frozenset([(k, u)])

    mapping = solutions.Mapping("foo", triangle_request, substrate, True)
    mapping.mapping_nodes = {i: u, j: v, k: u}
    mapping.mapping_edges = {ij: [uv], jk: [vw, wu], ik: []}

    mc.reduce_flow(mapping, 0.25)
    assert mc._used_flow_embedding_decision[triangle_request] == 0.25

    assert mc._used_flow_node_mapping[triangle_request][i][u][ku_index] == 0.25
    assert mc._used_flow_node_mapping[triangle_request][j][v][ku_index] == 0.25
    assert mc._used_flow_node_mapping[triangle_request][k][u][frozenset()] == 0.25
    for ij_, uv_list in list(mapping.mapping_edges.items()):
        i_, j_ = ij_
        sub_lp = mc.edge_sub_lp[triangle_request][ij_][ku_index]
        assert sub_lp._used_flow_source[mapping.mapping_nodes[i_]] == 0.25
        assert sub_lp._used_flow_sink[mapping.mapping_nodes[j_]] == 0.25
        for uv_ in uv_list:
            u_, v_ = uv_
            assert sub_lp._used_flow[uv_] == 0.25
예제 #8
0
def test_gurobi_with_insufficient_node_resources_cannot_embed_all(
        substrate, triangle_request):
    triangle_request_copy = copy.deepcopy(triangle_request)
    triangle_request_copy.name = "test_req_copy"
    requests = [triangle_request, triangle_request_copy]
    for req in requests:
        req.profit = 1.0

    triangle_request.node[i]["demand"] = 1.0
    triangle_request_copy.node[i]["demand"] = 1.0
    triangle_request.node[k]["allowed_nodes"] = {"w"}
    triangle_request_copy.node[k]["allowed_nodes"] = {"w"}
    substrate.node[u]["capacity"]["t1"] = 1.5

    scenario = datamodel.Scenario("test",
                                  substrate,
                                  requests,
                                  objective=datamodel.Objective.MAX_PROFIT)
    mc = commutativity_model.CommutativityModelCreator(
        scenario,
        gurobi_settings=modelcreator.GurobiSettings(threads=2, timelimit=60))
    mc.init_model_creator()
    solution = mc.compute_fractional_solution()

    assert solution is not None
    assert len(solution.request_mapping[triangle_request]) > 0
    assert len(solution.request_mapping[triangle_request_copy]) > 0

    flow_sum = sum(solution.mapping_flows[m.name] for req in requests
                   for m in solution.request_mapping[req])
    assert flow_sum == 1.5
예제 #9
0
def test_extract_edge_mapping_multiple_start_nodes(substrate, triangle_request):
    triangle_request.node[i]["allowed_nodes"] = [u, v]
    scenario = datamodel.Scenario("test", substrate, [triangle_request])

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

    ku_index = frozenset([(k, u)])
    ij_sub_lp = mc.edge_sub_lp[triangle_request][ij][ku_index]
    ij_sub_lp.var_node_flow_source[u].x = 0.3
    ij_sub_lp.var_node_flow_source[v].x = 0.7

    ij_sub_lp.var_edge_flow[uv].x = 0.3

    ij_sub_lp.var_node_flow_sink[v].x = 1.0

    partial_mapping = solutions.Mapping("test_mapping", triangle_request, substrate, True)
    partial_mapping.map_node(i, u)

    assert ij_sub_lp.extract_edge_mapping(partial_mapping) == ([uv], v, 0.3)

    partial_mapping = solutions.Mapping("test_mapping", triangle_request, substrate, True)
    partial_mapping.map_node(i, v)

    assert ij_sub_lp.extract_edge_mapping(partial_mapping) == ([], v, 0.7)
예제 #10
0
def test_requests_are_processed_in_profit_order():
    sub = vtd.create_test_substrate("single_edge_substrate")
    req1 = vtd.create_test_request("single_edge_request",
                                   sub,
                                   allowed_nodes=dict(i1=["u1"], i2=["u2"]))
    req2 = vtd.create_test_request("single_edge_request",
                                   sub,
                                   allowed_nodes=dict(i1=["u1"], i2=["u2"]))
    req2.profit = req1.profit + 1

    scenario = datamodel.Scenario(
        name="test_scen",
        requests=[req1, req2],
        substrate=sub,
        objective=datamodel.Objective.MAX_PROFIT,
    )

    v = vine.OfflineViNEAlgorithm(
        scenario,
        edge_embedding_model=vine.ViNEEdgeEmbeddingModel.UNSPLITTABLE,
        lp_objective=vine.ViNELPObjective.ViNE_COSTS_DEF,
        rounding_procedure=vine.ViNERoundingProcedure.DETERMINISTIC)
    result = v.compute_integral_solution()
    solution = result.get_solution()
    m = solution.request_mapping[req2]

    # Although req2 is second in the request list, it must be processed first due to its higher profit.
    assert m.mapping_nodes == dict(
        i1="u1",
        i2="u2",
    )
    assert m.mapping_edges == {("i1", "i2"): [("u1", "u2")]}
    assert solution.request_mapping[req1] is None
    def test_mc_ecg_does_not_ignore_unprofitable_requests_when_using_min_cost_objective(self):
        profit_req = copy.deepcopy(self.request)
        no_profit_req = copy.deepcopy(self.request)
        profit_req.name = "profit_req"
        no_profit_req.name = "no_profit_req"
        profit_req.profit = 1000.0
        no_profit_req.profit = 0.0
        scenario = datamodel.Scenario("test_scenario", self.substrate, [profit_req, no_profit_req],
                                      objective=datamodel.Objective.MIN_COST)
        mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
        assert no_profit_req in mc_ecg.requests
        assert no_profit_req in mc_ecg.all_requests
        assert profit_req in mc_ecg.requests

        mc_ecg.init_model_creator()

        # Check that the unprofitable request is in the Gurobi Model:
        assert no_profit_req in mc_ecg.extended_graphs
        assert no_profit_req in mc_ecg.var_node_flow
        assert no_profit_req in mc_ecg.var_edge_flow
        assert no_profit_req in mc_ecg.var_request_load
        assert no_profit_req in mc_ecg.var_embedding_decision
        assert no_profit_req in mc_ecg.var_request_load

        fs = mc_ecg.compute_fractional_solution()
        # Check that the unprofitable request *is* in the fractional solution
        assert isinstance(fs, solutions.FractionalScenarioSolution)
        assert profit_req in fs.request_mapping
        assert no_profit_req in fs.request_mapping
        assert sum([fs.mapping_flows[m.name] for m in fs.request_mapping[profit_req]]) == 1
        assert sum([fs.mapping_flows[m.name] for m in fs.request_mapping[no_profit_req]]) == 1
        for i in profit_req.nodes:
            assert all(i in mapping.mapping_nodes for mapping in fs.request_mapping[profit_req])
        for ij in profit_req.edges:
            assert all(ij in mapping.mapping_edges for mapping in fs.request_mapping[profit_req])
예제 #12
0
def test_extract_edge_mapping_only_use_exit_with_remaining_node_sink_flow(substrate, triangle_request):
    triangle_request.node[j]["allowed_nodes"] = [v, w]
    scenario = datamodel.Scenario("test", substrate, [triangle_request])

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

    ku_index = frozenset([(k, u)])
    ij_sub_lp = mc.edge_sub_lp[triangle_request][ij][ku_index]
    ij_sub_lp.var_node_flow_source[u].x = 1.0

    # This contains 3 mappings with values 0.5, 0.25 and 0.25
    ij_sub_lp.var_edge_flow[uw].x = 0.5
    ij_sub_lp.var_edge_flow[uv].x = 0.5
    ij_sub_lp.var_edge_flow[wv].x = 0.25

    ij_sub_lp.var_node_flow_sink[w].x = 0.25
    ij_sub_lp.var_node_flow_sink[v].x = 0.75

    partial_mapping = solutions.Mapping("test_mapping", triangle_request, substrate, True)
    partial_mapping.map_node(i, u)

    ij_sub_lp._used_flow_source[u] += 0.5
    ij_sub_lp._used_flow[uv] += 0.5
    ij_sub_lp._used_flow_sink[v] += 0.5
    assert ij_sub_lp.extract_edge_mapping(partial_mapping) == ([uw], w, 0.25)

    ij_sub_lp._used_flow_source[u] += 0.25
    ij_sub_lp._used_flow[uw] += 0.25
    ij_sub_lp._used_flow_sink[w] += 0.25
    assert ij_sub_lp.extract_edge_mapping(partial_mapping) == ([uw, wv], v, 0.25)
예제 #13
0
def test_gurobi_with_insufficient_node_resources_invalid_substrate_node(
        substrate, triangle_request):
    # triangle_request_copy will be embeddable, while triangle_request will have no valid mapping for node i
    triangle_request_copy = copy.deepcopy(triangle_request)
    triangle_request_copy.name = "test_req_copy"
    requests = [triangle_request, triangle_request_copy]
    for req in requests:
        req.profit = 1.0

    triangle_request.node[i]["demand"] = 1.0
    triangle_request_copy.node[i]["allowed_nodes"] = {"v"}
    substrate.node[u]["capacity"]["t1"] = 0.5

    scenario = datamodel.Scenario("test",
                                  substrate,
                                  requests,
                                  objective=datamodel.Objective.MAX_PROFIT)
    mc = commutativity_model.CommutativityModelCreator(
        scenario,
        gurobi_settings=modelcreator.GurobiSettings(threads=2, timelimit=60))
    mc.init_model_creator()
    solution = mc.compute_fractional_solution()

    assert solution is not None
    assert triangle_request not in solution.request_mapping
    assert len(solution.request_mapping[triangle_request_copy]) > 0
 def test_can_handle_request_whose_node_demand_exceeds_all_substrate_capacities(self):
     self.request.node["p"]["demand"] = 10**10
     scenario = datamodel.Scenario("test_scenario", self.substrate, [self.request],
                                   objective=datamodel.Objective.MAX_PROFIT)
     mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
     mc_ecg.init_model_creator()
     mc_ecg.model.optimize()
     assert mc_ecg.model.getAttr(GRB.Attr.ObjVal) == pytest.approx(0.0)
 def test_can_handle_request_whose_edge_demand_exceeds_some_substrate_capacities(self):
     self.request.edge[("m", "p")]["demand"] = 10.0  # m is allowed on u, v and p on v
     self.substrate.edge["w", "u"]["capacity"] = 1.0
     scenario = datamodel.Scenario("test_scenario", self.substrate, [self.request],
                                   objective=datamodel.Objective.MAX_PROFIT)
     mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
     mc_ecg.init_model_creator()
     mc_ecg.model.optimize()
     assert mc_ecg.model.getAttr(GRB.Attr.ObjVal) == pytest.approx(1000.0)
예제 #16
0
 def setup(self):
     self.substrate = datamodel.Substrate("sub1")
     self.request = datamodel.Request("req1")
     self.scenario = datamodel.Scenario("Sen1", self.substrate,
                                        [self.request])
     self.mapping = solutions.Mapping("map1", self.request, self.substrate,
                                      True)
     self.scenariosolution = solutions.IntegralScenarioSolution(
         "Solution1", self.scenario)
예제 #17
0
def test_tw_formulation_precomputed_scenario(request_id):
    # build test scenario
    req = create_test_request(request_id)
    sub = create_test_substrate_topology_zoo()
    scenario = datamodel.Scenario(name="test_scen",
                                  substrate=sub,
                                  requests=[req])

    tw_mc = treewidth_model._TreewidthModelCreator(scenario)
    tw_mc.init_model_creator()
 def test_can_handle_request_whose_node_demand_exceeds_some_substrate_capacities(self):
     self.request.node["q"]["demand"] = 10.0  # q is allowed on u, w
     self.substrate.node["w"]["capacity"]["universal"] = 1.0  # q no longer fits on w
     self.substrate.node["u"]["capacity"]["universal"] = 100.0  # q still fits on u
     scenario = datamodel.Scenario("test_scenario", self.substrate, [self.request],
                                   objective=datamodel.Objective.MAX_PROFIT)
     mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
     mc_ecg.init_model_creator()
     mc_ecg.model.optimize()
     assert mc_ecg.model.getAttr(GRB.Attr.ObjVal) == pytest.approx(1000.0)
 def test_example_get_load_based_on_integral_solution(self):
     scenario = datamodel.Scenario("test_scenario", self.substrate, [self.request],
                                   objective=datamodel.Objective.MIN_COST)
     mc_mip = mip.ClassicMCFModel(scenario)
     mc_mip.init_model_creator()
     sol = mc_mip.compute_integral_solution().solution
     mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(sol.scenario)
     mc_ecg.init_model_creator()
     mc_ecg.fix_mapping_variables_according_to_integral_solution(sol)
     mc_ecg.model.optimize()
     assert mc_mip.model.getAttr(GRB.Attr.ObjVal) == pytest.approx(mc_ecg.model.getAttr(GRB.Attr.ObjVal))
예제 #20
0
def test_commutativity_with_gurobi(triangle_request, substrate):
    triangle_request.profit = 10000
    triangle_request.add_node("l", 0.75, "t1", [v])
    triangle_request.add_edge("l", i, 0.5, None)

    scenario = datamodel.Scenario("test",
                                  substrate, [triangle_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
예제 #21
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 get_test_scenario(test_data_dict):
    substrate = create_test_substrate(
        test_data_dict["substrate_id"],
        **test_data_dict.get("substrate_kwargs", {}))
    request = create_test_request(test_data_dict["request_id"], substrate,
                                  **test_data_dict.get("request_kwargs", {}))
    return datamodel.Scenario(
        name="test_scen",
        requests=[request],
        substrate=substrate,
        objective=test_data_dict.get("objective",
                                     datamodel.Objective.MAX_PROFIT),
    )
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
 def test_can_get_fractional_solution(self):
     scenario = datamodel.Scenario("test_scenario", self.substrate, [self.request],
                                   objective=datamodel.Objective.MIN_COST)
     mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
     mc_ecg.init_model_creator()
     fs = mc_ecg.compute_fractional_solution()
     assert isinstance(fs, solutions.FractionalScenarioSolution)
     for req in scenario.requests:
         assert req in fs.request_mapping
         # ensure that all nodes & edges are in each mapping
         for i in req.nodes:
             assert all(i in mapping.mapping_nodes for mapping in fs.request_mapping[req])
         for ij in req.edges:
             assert all(ij in mapping.mapping_edges for mapping in fs.request_mapping[req])
예제 #25
0
def get_test_scenario(number_of_requests=1,
                      request_size=2,
                      substrate_size=3,
                      request_demand=1.0,
                      substrate_capacity=10.0):
    sub = get_test_substrate(substrate_size, capacity=substrate_capacity)
    requests = []
    for i in range(1, 1 + number_of_requests):
        name = "test_req_{}".format(i)
        req = get_clique_request(request_size,
                                 name=name,
                                 demand=request_demand)
        requests.append(req)
    return datamodel.Scenario("test_scenario", sub, requests)
예제 #26
0
    def test_fractual_model(self):

        self.scenario = datamodel.Scenario("Sen1", self.substrate,
                                           [self.request])

        modelcreator = mc.ModelCreatorDecomp(self.scenario)
        modelcreator.init_model_creator()
        solution = modelcreator.compute_fractional_solution()
        assert self.request in solution.request_mapping
        print("profile:  time pre = {}, optimization = {} , post = {}".format(
            modelcreator.time_preprocess, modelcreator.time_optimization,
            modelcreator.time_postprocessing))
        if solution:
            if solution.validate_solution():
                print(solution)
예제 #27
0
def test_fractional_solution_random_request_with_gurobi(seed, substrate):
    random.seed(seed)
    req = generate_random_request_graph(10, 0.25)
    requests = [req]
    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
    assert len(solution.request_mapping[req]) > 0
    def test_can_handle_request_whose_edge_demand_exceeds_all_substrate_capacities(self):
        self.request.edge[("m", "p")]["demand"] = 10**10
        scenario = datamodel.Scenario("test_scenario", self.substrate, [self.request],
                                      objective=datamodel.Objective.MAX_PROFIT)
        mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
        mc_ecg.init_model_creator()
        sol = mc_ecg.compute_fractional_solution()
        assert mc_ecg.model.getAttr(GRB.Attr.ObjVal) == pytest.approx(1000.0)  # Can be solved by colocation

        # forbid colocation => should become infeasible
        self.request.set_allowed_nodes("m", {"u"})
        self.request.set_allowed_nodes("p", {"v"})
        mc_ecg = modelcreator_ecg_decomposition.ModelCreatorCactusDecomposition(scenario)
        mc_ecg.init_model_creator()
        sol = mc_ecg.compute_fractional_solution()
        assert mc_ecg.model.getAttr(GRB.Attr.ObjVal) == pytest.approx(0.0)  # Cannot be solved by colocation
예제 #29
0
def test_extract_edge_mapping_colocated_nodes(substrate, triangle_request):
    scenario = datamodel.Scenario("test", substrate, [triangle_request])

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

    ku_index = frozenset([(k, u)])
    ik_sub_lp = mc.edge_sub_lp[triangle_request][ik][ku_index]
    ik_sub_lp.var_node_flow_source[u].x = 0.5
    ik_sub_lp.var_node_flow_sink[u].x = 0.5

    partial_mapping = solutions.Mapping("test_mapping", triangle_request, substrate, True)
    partial_mapping.map_node(i, u)

    assert ik_sub_lp.extract_edge_mapping(partial_mapping) == ([], u, 0.5)
def test_reduce_flow_in_modelcreator_with_reversed_edges(
        substrate, triangle_request, import_gurobi_mock):
    MockModel = import_gurobi_mock.MockModel
    triangle_request.graph["root"] = j
    scenario = datamodel.Scenario("test", substrate, [triangle_request])
    mc = commutativity_model.CommutativityModelCreator(scenario)
    mc.model = MockModel("mock_model")
    mc.preprocess_input()
    mc.create_variables_embedding_decision()
    mc._create_node_mapping_variables()
    mc._create_sub_lp_variables()

    mapping = solutions.Mapping("foo", triangle_request, substrate, True)
    mapping.mapping_nodes = {i: u, j: v, k: u}
    mapping.mapping_edges = {ij: [uv], jk: [vw, wu], ik: []}

    mc.reduce_flow(mapping, 0.25)
    assert mc._used_flow_embedding_decision[triangle_request] == 0.25

    dag_request = mc.dag_requests[triangle_request]
    cycle_end = [
        i_ for i_ in triangle_request.nodes
        if len(dag_request.get_in_neighbors(i_)) == 2
    ][0]
    comm_index = frozenset([(cycle_end, mapping.mapping_nodes[cycle_end])])

    for i_, u_ in mapping.mapping_nodes.items():
        if i_ == cycle_end:
            assert mc._used_flow_node_mapping[triangle_request][i_][u_][
                frozenset()] == 0.25
        else:
            assert mc._used_flow_node_mapping[triangle_request][i_][u_][
                comm_index] == 0.25
    for ij_, uv_list in mapping.mapping_edges.items():
        # We only need ij_ in the orientation of the BFS request
        if ij_ not in dag_request.edges:
            ij_ = ij_[1], ij_[0]
        i_, j_ = ij_
        sub_lp = mc.edge_sub_lp[triangle_request][ij_][comm_index]
        assert sub_lp._used_flow_source[mapping.mapping_nodes[i_]] == 0.25
        assert sub_lp._used_flow_sink[mapping.mapping_nodes[j_]] == 0.25
        for uv_ in uv_list:
            u_, v_ = uv_
            if sub_lp.is_reversed_edge:
                uv_ = v_, u_
            assert sub_lp._used_flow[uv_] == 0.25