def selection_WST(data, t, tree=None):
    # find all workers in MTD
    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 = 0
    workers, dists = np.zeros(shape=(2, 0)), []

    # find workers who would perform the task
    for loc in locs:
        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):
            workers = np.concatenate(
                [workers, np.array([[loc[0]], [loc[1]]])], axis=1)
            dists.append(dist)

    # simulation
    if workers.shape[1] == 0:  # no workers
        return 0, False, None, None, None, None, None

    return len(locs), True, 0, random.choice(dists), 0, 0, 0
Beispiel #2
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
Beispiel #3
0
def selection_WST(data, t, tree=None):
    # find all workers in MTD
    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 = 0
    workers, dists = np.zeros(shape=(2, 0)), []

    # find workers who would perform the task
    for loc in locs:
        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):
            workers = np.concatenate([workers, np.array([[loc[0]], [loc[1]]])], axis=1)
            dists.append(dist)

    # simulation
    if workers.shape[1] == 0:  # no workers
            return 0, False, None, None, None, None, None

    return len(locs), True, 0, random.choice(dists), 0, 0, 0
# print np.sqrt(2*(1/0.1)**2), np.sqrt(((noisy - true) ** 2).mean())


for eps in eps_list:
    true = np.genfromtxt("../log/true_count_KF_" + str(eps) + ".log", unpack=True)
    noisy = np.genfromtxt("../log/noisy_count_KF_" + str(eps) + ".log", unpack=True)
    true_flatten = true.flatten()
    noisy_flatten = noisy.flatten()
    p = Params(1000)
    print "KF\t", "\t", np.sum(true_flatten), "\t", np.sum(noisy_flatten), "\t", np.sqrt(
        2 * (T / (eps * (1 - p.structureEps))) ** 2), "\t", np.sqrt(((noisy_flatten - true_flatten) ** 2).mean())

    true = np.genfromtxt("../log/true_count_KFPID_" + str(eps) + ".log", unpack=True)
    noisy = np.genfromtxt("../log/noisy_count_KFPID_" + str(eps) + ".log", unpack=True)
    true_flatten = true.flatten()
    noisy_flatten = noisy.flatten()
    p = Params(1000)
    print "KFPID\t", "\t", np.sum(true_flatten), "\t", np.sum(noisy_flatten), "\t", np.sqrt(
        2 * (T / (eps * (1 - p.structureEps))) ** 2), "\t", np.sqrt(((noisy_flatten - true_flatten) ** 2).mean())

x1 = time.time()
# for i in range(100):
max_dist = 100
dist = range(100)
Params.ZIPF_STEPS = 20
Params.s = 1
Params.MAR = 0.1
Params.AR_FUNCTION = "linear"
y = "\n".join(map(str, [acc_rate(max_dist, x) for x in dist]))
print y
print time.time() - x1