def algorithm(inputfilename):
    routers_list = finding_most_valid_routers(inputfilename)
    l = lambda x: (x[0], x[2], x[3], x[4], x[5], x[6])
    rows, radius, connecting_to_backbone_cost, router_cost, budget, initial_backbone = l(
        reading_input_data(inputfilename))

    placed_routers = list()
    while budget - router_cost > (
            rows * connecting_to_backbone_cost) / 2 and len(routers_list) > 0:
        current = choice(routers_list)
        while True:
            val = find_better_neighbors(routers_list, current,
                                        initial_backbone, inputfilename)
            if current != val:
                current = val
            else:
                break

        routers_list = [
            i for i in routers_list
            if not (abs(i[0][0] - current[0][0]) <= radius
                    and abs(i[0][1] - current[0][1]) <= radius)
        ]
        placed_routers.append(current)
        budget -= current[2] * connecting_to_backbone_cost + router_cost

    return placed_routers
예제 #2
0
def greedy_algorithm(inputfile):
    l = lambda x: (x[3], x[4], x[5])
    connecting_to_backbone_cost, router_cost, budget = l(
        reading_input_data(inputfile))
    routers_list = finding_most_valid_routers2(inputfile)
    print(1)
    sorted_routers_list = sorted(routers_list, key=lambda x: x[1] / x[2])
    dots_s = set()
    n = 0
    i = len(sorted_routers_list) - 1
    sorted_routers_listt = sorted_routers_list[:]
    while n < budget and i != 0:
        if n + sorted_routers_listt[i][
                2] * connecting_to_backbone_cost + router_cost < budget:
            sorted_routers_listt = sorted(sorted_routers_list,
                                          key=lambda x: len(x[3] - dots_s))
            dots_s = dots_s.union(sorted_routers_listt[i][3])
            dots_s.add((sorted_routers_listt[i][0][0],
                        sorted_routers_listt[i][0][1], "r"))
            n += sorted_routers_listt[i][
                2] * connecting_to_backbone_cost + router_cost
        else:
            i -= 1
    r_list = []
    dots_s = list(dots_s)
    for i in range(len(dots_s)):
        if len(dots_s[i]) == 3:
            r_list.append((dots_s[i][0], dots_s[i][1]))
    return len(dots_s), dots_s, len(r_list), r_list
예제 #3
0
def dumm_algorithm(inputfilename):
    routers_list = finding_most_valid_routers(inputfilename)
    l = lambda x: (x[3], x[4], x[5])
    connecting_to_backbone_cost, router_cost, budget = l(
        reading_input_data(inputfilename))

    total_cost = 0
    score_list_ = list()

    for len_ in range(1, budget // router_cost + 1):
        for subset in combinations(routers_list, len_):
            for i in subset:
                total_cost += (i[2] * connecting_to_backbone_cost +
                               router_cost)

            if total_cost < budget:
                total_connected_cells = sum([i[2] for i in subset])
                ZAGLUSHKA_CELLS = [[i for i in range(2)]
                                   for j in range(total_connected_cells)]
                coordinates_of_routers = [i[0] for i in subset]

                creating_outputfile(total_connected_cells, ZAGLUSHKA_CELLS,
                                    len(subset), coordinates_of_routers)

                val = count_points("../txt/algoroutput.txt", inputfilename)

                score_list_.append(val)

            total_cost = 0

    return max(score_list_)
예제 #4
0
def count_points(outputfilename, inputfilename):
    """
    :param outputfilename:The name of our resulted file
    :param inputfilename:The name of our input data file
    :return: Our algorithms score
    """
    l = lambda x: (x[2], x[3], x[4], x[5], x[7])
    radius, connecting_to_backbone_cost, router_cost, budget, cells_list = l(
        reading_input_data(inputfilename))
    l = lambda x: (x[1], x[2], x[3])
    routers_list, connected_cells, placed_routers = l(
        reading_output_data(outputfilename))
    placed = placing_connected_cells(routers_list, radius, cells_list)[1]

    return 1000 * placed + (budget -
                            (connected_cells * connecting_to_backbone_cost +
                             placed_routers * router_cost))
예제 #5
0
def text_connectedcells_visualization(outputfilename, inputfilename):
    """
    :param outputfilename: Name of an output file
    :param inputfilename: Name of an output file
    :return: Returns the view of connected to network cells, which are denoted as "w"
    """
    placed = 0
    router_list = reading_output_data(outputfilename)[1]
    l = lambda x: (x[2], x[7])
    radius, cells_list = l(reading_input_data(inputfilename))

    print("Routers are placed in {} position".format(router_list))

    cells_list, placed = placing_connected_cells(router_list,radius,cells_list)

    for i in cells_list:
        print("".join(i))
예제 #6
0
def dynamic_algorithm(inputfile):
    l = lambda x: (x[3], x[4], x[5])
    connecting_to_backbone_cost, router_cost, budget = l(
        reading_input_data(inputfile))
    routers_list = finding_most_valid_routers2(inputfile)
    dict_ = dict()
    sorted_routers_list = sorted(routers_list, key=lambda x: x[1] / x[2])
    dots_s = set()
    dots_list = []

    def recur(budget, i, dots_set):
        sorted_routers_listt = sorted(sorted_routers_list,
                                      key=lambda x: len(x[3] - dots_set))
        dots = dots_set.copy()
        if budget in dict_:
            return dict_[budget]
        if i == 0 or budget == 0:
            dots_list.append(dots)
            return 0
        elif sorted_routers_listt[i][
                2] * connecting_to_backbone_cost + router_cost > budget:
            dict_[budget] = recur(budget, i - 1, dots)
            return dict_[budget]
        else:
            d = dots.union(sorted_routers_listt[i][3])
            d.add((sorted_routers_listt[i][0][0],
                   sorted_routers_listt[i][0][1], "r"))
            val = sorted_routers_listt[i][
                2] * connecting_to_backbone_cost + router_cost
            g = sorted_routers_listt[i][1] + recur(budget - val, i - 1, d)
            gg = recur(budget, i - 1, dots)
            if g > gg:
                dict_[budget] = g
            else:
                dict_[budget] = gg
            return dict_[budget]

    asd = recur(budget, len(routers_list) - 1, dots_s)
    d_set = list(max(dots_list))
    r_list = []
    for i in range(len(d_set)):
        if len(d_set[i]) == 3:
            r_list.append((d_set[i][0], d_set[i][1]))
    return len(d_set), d_set, len(r_list), r_list
예제 #7
0
def text_result_visualization(outputfilename, inputfilename):
    """
    :param outputfilename: Name of an input file
    :param inputfilename: Name of an output file
    :return: Text representation of already changed data
    """
    l = lambda x: (x[0], x[1])
    connected_cells_list, routers_list = l(reading_output_data(outputfilename))
    l = lambda x: (x[7], x[6])
    cells_list, initial_backbone = l(reading_input_data(inputfilename))

    for i in connected_cells_list:
        cells_list[i[0]][i[1]] = "c"
    for i in routers_list:
        cells_list[i[0]][i[1]] = "r"
    cells_list[initial_backbone[0]][initial_backbone[1]] = "b"

    for i in cells_list:
        print("".join(i))

    return cells_list
예제 #8
0
def finding_most_valid_routers2(inputfilename):
    """
    :param inputfilename: The name of our input data file
    :return: List of possible placed routers with coordinates as a first param, coverage as second and distance to
    backbone as third
    """
    def calc_dist(backbone, router):
        return max(abs(backbone[0] - router[0]), abs(backbone[1] - router[1]))

    rows, cols, radius, connecting_to_backbone_cost, router_cost, budget,\
                                            initial_backbone, cells_list = reading_input_data(inputfilename)

    routers_list = list()
    i = 0

    for X in range(rows):
        for Y in range(cols):
            distance = calc_dist(initial_backbone, (X, Y))
            if distance != 0 and cells_list[X][
                    Y] == ".":  #Adding routers that are not backbone pos and in . pos
                routers_list.append([(X, Y), 0, distance, set()])
                #Finding the coverage of our router
                for x in range(-radius, radius + 1):
                    for y in range(-radius, radius + 1):
                        try:
                            if x == 0 and y == 0:
                                continue
                            if cells_list[X + x][
                                    Y +
                                    y] == "." and X + x >= 0 and Y + y >= 0 and nowalls(
                                        cells_list, X, Y, X + x, Y + y):
                                routers_list[i][1] += 1
                                routers_list[i][3].add((X + x, Y + y))
                        except IndexError:
                            pass
                i += 1

    return routers_list
예제 #9
0
def text_input_visualization(inputfilename):
    """
    :param inputfilename: Name of an input file
    :return: Text representation of input data
    """
    rows, cols, radius, connecting_to_backbone_cost, router_cost, budget, initial_backbone, cells_list = reading_input_data(
        inputfilename)
    print("""{0} rows, {1} columns, router range radius is {2}
backbone costs {3}, router costs {4}, budget is {5}
the initial cell connected to backbone is {6} """ \
          .format(rows, cols, radius, connecting_to_backbone_cost, router_cost, budget, initial_backbone))
    for i in cells_list:
        print("".join(i))

    return cells_list