Example #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
    def computeError(self, Res, method="None"):
        """ Compute median absolute and relative errors """
        absErr = np.abs(Res - self.trueRes)
        idx_nonzero = np.where(self.trueRes != 0)
        absErr_nonzero = absErr[idx_nonzero]
        true_nonzero = self.trueRes[idx_nonzero]
        relErr = absErr_nonzero / true_nonzero

        # log_str_rel = "\n".join(map(str, relErr))
        # log_str_abs = "\n".join(map(str, absErr))

        if Params.IS_LOGGING:
            log_str = ""
            for i in range(len(self.query_list)):
                area = rect_area(self.query_list[i])
                query_str = str(self.query_list[i][0][0]) + "\t" + str(self.query_list[i][0][1]) + "\t" + str(
                    self.query_list[i][1][0]) + "\t" + str(self.query_list[i][1][1]) + "\t" + str(area)
                err_str = str(self.trueRes[i]) + "\t" + str(Res[i]) + "\t" + str(absErr[i]) + "\t" + str(relErr[i])
                log_str = log_str + query_str + "\t" + err_str + "\n"
            log(method, log_str)

        absErr = np.sort(absErr)
        relErr = np.sort(relErr)
        n_abs = len(absErr)
        n_rel = len(relErr)
        return absErr[int(n_abs / 2)], relErr[int(n_rel / 2)]
Example #3
0
 def testLeaf(self, curr):
     """ test whether a node should be a leaf node """
     if (curr.n_data is None or curr.n_data.shape[1] == 0) or \
         curr.area() < Params.AREA_UNIT_CELL or \
             (curr.n_count <= self.param.minPartSize) or \
                 rect_area(curr.n_box) < Params.AREA_UNIT_CELL:
         return True
     return False
Example #4
0
 def testLeaf_bdr(self, curr):
     """ test whether a node should be a leaf node """
     if (curr.n_data is None or curr.n_data.shape[1] == 0) or \
         self.cell_count >= self.param.ANALYST_COUNT or \
             (curr.n_count <= self.param.minPartSize) or \
                 rect_area(curr.n_box) < 0.01:
         return True
     return False
Example #5
0
 def testLeaf(self, curr):
     """ test whether a node should be a leaf node """
     if (curr.n_data is None or curr.n_data.shape[1] == 0) or \
         curr.area() < Params.AREA_UNIT_CELL or \
             (curr.n_count <= self.param.minPartSize) or \
                 rect_area(curr.n_box) < Params.AREA_UNIT_CELL:
         return True
     return False
Example #6
0
 def testLeaf_bdr(self, curr):
     """ test whether a node should be a leaf node """
     if (curr.n_data is None or curr.n_data.shape[1] == 0) or \
         self.cell_count >= self.param.ANALYST_COUNT or \
             (curr.n_count <= self.param.minPartSize) or \
                 rect_area(curr.n_box) < 0.01:
         return True
     return False
Example #7
0
 def testLeaf(self, curr):
     """ test whether a node should be a leaf node """
     if (curr.n_depth == Params.maxHeight) or (curr.n_data is None or curr.n_data.shape[1] == 1) or \
         curr.area() < 0.25 or \
             (curr.n_count <= self.param.K) or \
                 rect_area(curr.n_box) < 0.25:
         return True
     return False
Example #8
0
 def testLeaf(self, curr):
     """ test whether a node should be a leaf node """
     if (
         (curr.n_data is None or curr.n_data.shape[1] == 0)
         or curr.area() < 0.0004
         or (curr.n_count <= self.param.minPartSize)
         or rect_area(curr.n_box) < 0.0004
     ):
         return True
     return False
Example #9
0
def optimization(tree, fov_count, seed, param):
    np.random.seed(seed)
    fovs = read_image_data(param.dataset)

    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
Example #10
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
Example #11
0
    def get(self, geocast_id):
        """
        Override the standard GET
        """
        global all_data, eps
        parts = geocast_id.split('/')
        dataset = parts[0]
        print parts
        t = map(float, parts[1].split(','))
        print dataset, t
        # if task not in region
        if not is_rect_cover(all_data[dataset][1], t):
            self.write(json.dumps({"error": "invalid task"}))
            return

        q, q_log = geocast(all_data[dataset][0], t, float(eps))
        no_workers, workers, Cells, no_hops, coverage, no_hops2 = post_geocast(t, q, q_log)
        performed, worker, dist = performed_tasks(workers, Params.MTD, t, True)
        x_min, y_min, x_max, y_max = [], [], [], []
        worker_counts, utilities, distances, compactnesses, areas = [], [], [], [], []
        if worker is not None:
            worker = worker.tolist()

        corner_points = Set([])
        for cell in Cells:
            x_min.append(cell[0].n_box[0][0])
            y_min.append(cell[0].n_box[0][1])
            x_max.append(cell[0].n_box[1][0])
            y_max.append(cell[0].n_box[1][1])
            worker_counts.append(cell[0].n_count)
            utilities.append([cell[2][1], cell[2][2]])
            compactnesses.append(cell[2][3])
            distances.append(float("%.3f" % distance_to_rect(t[0], t[1], cell[0].n_box)))
            distances.append(float("%.3f" % distance_to_rect(t[0], t[1], cell[0].n_box)))
            areas.append(float("%.3f" % rect_area(cell[0].n_box)))
            corner_points = corner_points | rect_vertex_set(cell[0].n_box)

        points = list(corner_points)
        x = make_circle(points)
        if x is not None:
            cx, cy, r = x
            print cx, cy, r
        else:
            cx, cy, r = 0, 0, 0
        no_hops2 = math.ceil(no_hops2)
        if performed:
            self.write(
                json.dumps({"is_performed": performed,
                            "notified_workers": {"no_workers": no_workers,
                                                 "x_coords": workers[0].tolist(),
                                                 "y_coords": workers[1].tolist()},
                            "geocast_query": {"no_cell": len(Cells),
                                              "compactness": q_log[-1][3],
                                              "x_min_coords": x_min,
                                              "y_min_coords": y_min,
                                              "x_max_coords": x_max,
                                              "y_max_coords": y_max,
                                              "worker_counts": worker_counts,
                                              "utilities": utilities,
                                              "compactnesses": compactnesses,
                                              "distances": distances,
                                              "areas": areas},
                            "spatial_task": {"location": t},
                            "volunteer_worker": {"location": worker,
                                                 "distance": dist},
                            "hop_count": no_hops2,
                            "bounding_circle": [cx, cy, r]},
                           sort_keys=False)
            )
        else:
            self.write(
                json.dumps({"is_performed": False,
                            "notified_workers": {"no_workers": no_workers,
                                                 "x_coords": workers[0].tolist(),
                                                 "y_coords": workers[1].tolist()},
                            "geocast_query": {"no_cell": len(Cells),
                                              "x_min_coords": x_min,
                                              "y_min_coords": y_min,
                                              "x_max_coords": x_max,
                                              "y_max_coords": y_max,
                                              "worker_counts": worker_counts,
                                              "utilities": utilities,
                                              "compactnesses": compactnesses,
                                              "distances": distances,
                                              "areas": areas},
                            "spatial_task": {"location": t},
                            "volunteer_worker": {"location": worker,
                                                 "distance": dist},
                            "hop_count": no_hops2,
                            "bounding_circle": [cx, cy, r]},
                           sort_keys=False)
            )


        # logging
        if Params.GEOCAST_LOG:
            info = GeocastInfo(int(performed), t, Cells)
            log_str = str(info.logging()) + "\n"
            geocast_log("geocast_server", log_str, eps)
Example #12
0
    def to_str(self):
        return str(self.id) + "\n" + "\n".join(fov.to_str() for fov in self.fovs)


from figures import SIZE, GRAY, BLUE

COLOR = {True: "#6699cc", False: "#ff3333"}

if False:
    fov1 = FOV(34.03331, -118.26935, 270, 60, 0.250)
    fov2 = FOV(34.03215, -118.26937, 255, 60, 0.250)
    fov_list = [fov2, fov1]
    video = Video(fov_list, 10)

    print fov1.mbr()
    print rect_area(fov2.mbr()), fov2.area(), video.area()

    fig = pyplot.figure(1, figsize=SIZE, dpi=90)

    ax = fig.add_subplot(122)
    patch2b = PolygonPatch(video.c_union(), fc=BLUE, ec=BLUE, alpha=0.5, zorder=2)
    ax.add_patch(patch2b)

    ax.set_title("union")
    xrange = [34.03, 34.04]
    yrange = [-118.28, -118.26]
    ax.set_xlim(*xrange)
    ax.set_ylim(*yrange)
    ax.set_aspect(1)

    pyplot.show()