Ejemplo n.º 1
0
def optimization(tree, fov_count, seed, param):
    np.random.seed(seed)
    fovs_file = re.sub(r"\.dat$", "", param.dataset) + ".txt"
    videos = read_data(fovs_file)

    fovs = []
    for v in videos:
        leaf_node = tree.leafCover(v.location())
        if leaf_node:
            v.size = np.random.zipf(param.ZIPFIAN_SKEW)
            if v.size > 20:
                v.size = 20
            v.fov_count = int(v.size)
            fovs = fovs + v.get_fovs()
        # else:
        #     print "not a leaf node", v.location()

    universe = Set([i for i in range(param.GRID_SIZE * param.GRID_SIZE)])
    all_sets = {}
    for i in range(len(fovs)):
        all_sets[i] = fovs[i].cellids(param)

    weights = {}
    count = 0
    last_weight = 0
    for item in universe:
        coord = cell_coord(item, param)
        # print coord
        leaf_node = tree.leafCover(coord)
        if leaf_node is not None:
            compute_urgency(leaf_node)
            boundary = np.array([[param.x_min, param.y_min], [param.x_max, param.y_max]])
            coverage_ratio = min(
                1, rect_area(boundary) / (param.GRID_SIZE * param.GRID_SIZE * rect_area(leaf_node.n_box))
            )
            weights[item] = leaf_node.urgency * coverage_ratio
            last_weight = weights[item]
        else:
            weights[item] = last_weight
            count = count + 1

    # print count
    # print universe
    # print all_sets
    # print fov_count
    # print weights

    start = time.time()

    covered_sets, covered_items, covered_weight = max_cover(universe, all_sets, fov_count, weights)
    print "time ", time.time() - start

    print covered_weight
    return covered_weight
Ejemplo n.º 2
0
def optimization(tree, bandwidth, seed, param):
    np.random.seed(seed)
    fovs_file = re.sub(r'\.dat$', '', param.dataset) + ".txt"
    videos = read_data(fovs_file)

    # update video value
    for v in videos:
        leaf_node = tree.leafCover(v.location())
        if leaf_node:
            v.size = np.random.zipf(param.ZIPFIAN_SKEW)
            if v.size > 20:
                v.size = 20
            v.fov_count = int(v.size)
            # size = int(np.random.uniform(1,10))

            # compute urgency of leaf node
            compute_urgency(leaf_node)
            ratio = min(1, 1000000 * v.area() / rect_area(leaf_node.n_box) / 6.25)
            # print ratio
            v.value = leaf_node.urgency * ratio
            # print v.value
        # else:
        #     print "not a leaf node", v.location()

    weights = [v.size for v in videos]
    values  = [v.value for v in videos]
    # print weights
    # print values
    total_value = zeroOneKnapsack(values,weights,bandwidth)

    print "\n if my knapsack can hold %d bandwidth, i can get %f profit." % (bandwidth,total_value[0])
    print "\tby taking item(s): ",
    for i in range(len(total_value[1])):
        if (total_value[1][i] != 0):
            print i+1,

    return total_value