Ejemplo n.º 1
0
def geocast_knn(data, t):
    # find all workers in MTD

    # find all workers in the query
    MTD_RECT = np.array([[t[0] - Params.ONE_KM * Params.MTD, t[1] - Params.ONE_KM * Params.MTD],
                         [t[0] + Params.ONE_KM * Params.MTD, t[1] + Params.ONE_KM * Params.MTD]])
    locs = rect_query_points(data, MTD_RECT).transpose()
    locs = sorted(locs, key=lambda loc: distance(loc[0], loc[1], t[0], t[1]))

    u, dist, found = 0, 0, False
    workers = np.zeros(shape=(2, 0))
    for loc in locs:
        workers = np.concatenate([workers, np.array([[loc[0]], [loc[1]]])], axis=1)
        _dist = distance(loc[0], loc[1], t[0], t[1])
        u_c = acc_rate(Params.MTD, _dist)
        u = 1 - (1 - u) * (1 - u_c)
        if is_performed(u_c):
            if not found:
                found = True
                dist = _dist
        if u >= Params.U:
            break

    # simulation
    isPerformed, worker, dist_fcfs = performed_tasks(workers, Params.MTD, t, True)
    hops_count, coverage, hops_count2 = hops_expansion(t, workers.transpose(), Params.NETWORK_DIAMETER)

    if isPerformed:  # the task is performed
        return workers.shape[1], True, dist, dist_fcfs, hops_count, coverage, hops_count2

    return workers.shape[1], False, None, None, hops_count, coverage, hops_count2
Ejemplo n.º 2
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)