Exemplo n.º 1
0
def create_scenario_from_sndlib_instance(sndlib_instance_name):
    model, graph = parse_problem(
        "./input/sndlib/problems/model.xml",
        "./input/sndlib/instances-xml/{0}/{0}.xml".format(
            sndlib_instance_name))

    substrate = sub_pkg.Substrate(sndlib_instance_name)

    node_coordinates = {}

    for node in graph.nodes():
        substrate.add_node(node)
        node_coordinates[node] = graph.node[node]["position"]
    for edge in graph.edges():
        u, v = edge
        long_u, lat_u = node_coordinates[u]
        long_v, lat_v = node_coordinates[v]
        edge_costs = None
        print(graph.graph["coordinatesType"])
        if graph.graph["coordinatesType"] == "pixel":
            edge_costs = euclid_distance(long_u, lat_u, long_v, lat_v)
        elif graph.graph["coordinatesType"] == "geographical":
            edge_costs = sub_reader_pkg.haversine(long_u, lat_u, long_v, lat_v)
        else:
            raise Exception("Unknown!")

        print("costs {} for |({},{})-({},{})|".format(edge_costs, long_u,
                                                      lat_u, long_v, lat_v))
        substrate.add_edge(u, v, cost=edge_costs)
        substrate.add_edge(v, u, cost=edge_costs)

    cum_demand = 0
    requests = []
    for demand in graph.graph["demands"]:
        id, source, target, demand, _ = demand
        print("{} {} {} {}".format(id, source, target, demand))
        requests.append(
            req_pkg.Request(source, target, capacity=demand,
                            max_deviation=0.0))
        cum_demand += demand

    mbs = {}
    for node in substrate.get_nodes():
        mbs[node] = 4 * cum_demand / len(graph.nodes())

    scen = scen_pkg.Scenario(sndlib_instance_name + "_prototype", substrate,
                             requests, mbs)

    return scen
    def construct_scenarios(self, test_scenarios_a_priori=True):
        counter = 0
        print(self.probability_for_pair)
        print(self.capacity_factor)
        print(self.suitable_substrates)
        print(self.number_of_repetitions)
        print(self.max_deviation)

        for prob, cap_factor, substrate_name, repetition in itertools.product(
                self.probability_for_pair, self.capacity_factor,
                self.suitable_substrates.names,
                range(self.number_of_repetitions)):

            #print self.substrate_filter
            if self.substrate_filter is not None and substrate_name not in self.substrate_filter:
                continue
            substrate = self.suitable_substrates.substrates[substrate_name]

            while True:

                #print substrate_name
                pairs = []

                handled_nodes = []
                for u in substrate.nodes:
                    handled_nodes.append(u)
                    for v in substrate.nodes:
                        if v in handled_nodes:
                            continue
                        if random.random() <= prob:
                            pairs.append((u, v))
                            #req = req_pkg.Request(u, v, random.uniform(md_lb, md_ub), capacity=1)
                            #requests.append(req)

                successful_generation = False

                for deviation in self.max_deviation:

                    if counter > 0 and counter % 100 == 0:
                        if self.substrate_filter is not None:
                            print(
                                f"Having created {counter} of {len(self.max_deviation) * len(self.capacity_factor) * len(self.probability_for_pair)*len(self.substrate_filter)*self.number_of_repetitions} many scenarios"
                            )
                        else:
                            print(
                                f"Having created {counter} of {len(self.max_deviation) * len(self.capacity_factor) * len(self.probability_for_pair)*len(self.suitable_substrates.names)*self.number_of_repetitions} many scenarios"
                            )

                    number_of_nodes = substrate.get_number_of_nodes()

                    capacity = math.ceil((number_of_nodes - 1) * 2 * prob +
                                         (number_of_nodes * number_of_nodes -
                                          2 * number_of_nodes - 1) / 2 * prob *
                                         cap_factor)

                    requests = []

                    md_lb, md_ub = deviation, deviation
                    for (u, v) in pairs:
                        req = req_pkg.Request(u,
                                              v,
                                              random.uniform(md_lb, md_ub),
                                              capacity=1)
                        requests.append(req)

                    middleboxes = {}
                    for u in substrate.nodes:
                        middleboxes[u] = capacity

                    scenario = scen_pkg.Scenario(counter, substrate, requests,
                                                 middleboxes)

                    if deviation == self.max_deviation[0]:
                        mip = std_mip_pkg.ExactDeploymentMIP(scenario)
                        result = mip.run()
                        if result is not None:
                            successful_generation = True

                    self.scenario_keys.append((prob, deviation, cap_factor,
                                               substrate_name, repetition))
                    self.scenarios[(prob, deviation, cap_factor,
                                    substrate_name, repetition)] = scenario

                    counter += 1
                if successful_generation:
                    break
    def construct_scenarios(self, test_scenarios_a_priori=True):
        counter = 0
        print(self.probability_for_pair)
        print(self.capacity_factor)
        print(self.number_of_repetitions)
        print(self.max_deviation)


        prototypical_scenarios = {}
        for substrate_name in self.substrate_filter:
            scenario = sndlib_pkg.create_scenario_from_sndlib_instance(substrate_name)
            prototypical_scenarios[substrate_name] = scenario

        for prob, cap_factor, substrate_name, repetition in itertools.product(self.probability_for_pair,
                                                                self.capacity_factor,
                                                                self.substrate_filter,
                                                                range(self.number_of_repetitions)):

            prototypical_scenario = prototypical_scenarios[substrate_name]
            substrate = prototypical_scenario.substrate

            print(f"\n\n\n\n\nstarting to generate {prob} {cap_factor} {substrate_name} {repetition}\n\n\n\n\n")

            while True:

                #print substrate_name
                pairs = []

                handled_nodes = []
                for req in prototypical_scenario.requests:
                    if random.random() <= prob:
                        pairs.append((req.tail, req.head, req.capacity))


                print(pairs)

                successful_generation = False

                for deviation in self.max_deviation:

                    if counter > 0 and counter % 100 == 0:
                        if self.substrate_filter is not None:
                            print(f"Having created {counter} of {len(self.max_deviation) * len(self.capacity_factor) * len(self.probability_for_pair)*len(self.substrate_filter)*self.number_of_repetitions} many scenarios")
                        else:
                            print(f"Having created {counter} of {len(self.max_deviation) * len(self.capacity_factor) * len(self.probability_for_pair)*len(self.suitable_substrates.names)*self.number_of_repetitions} many scenarios")


                    number_of_nodes = substrate.get_number_of_nodes()

                    requests = []

                    cum_capacity = 0.0
                    md_lb, md_ub = deviation, deviation
                    for (u,v, cap) in pairs:
                        req = req_pkg.Request(u, v, random.uniform(md_lb, md_ub), capacity=cap)
                        requests.append(req)

                        cum_capacity += cap

                    capacity = 4 * cum_capacity / number_of_nodes


                    middleboxes = {}
                    for u in substrate.nodes:
                        middleboxes[u] = capacity

                    scenario =  scen_pkg.Scenario(counter, substrate, requests, middleboxes)

                    #util_pkg.prettyPrint(scenario)

                    if deviation == self.max_deviation[0]:
                        mip = mip_pkg.ExactDeploymentMIP_diff_weights(scenario, mip_gap=1.0)
                        result = mip.run()
                        if result is not None:
                            successful_generation = True
                        else:
                            break


                    self.scenario_keys.append((prob, deviation, cap_factor, substrate_name, repetition))
                    self.scenarios[(prob,deviation,cap_factor,substrate_name,repetition)] = scenario

                    counter += 1

                if successful_generation:
                    break