def testing(trajectories, alpha, max_r):

    curr_r = alpha
    while curr_r <= max_r:
        chord_l = math.sqrt(4 * alpha * curr_r -  2 * alpha * alpha)
        sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in trajectories]
        pts = list(itertools.chain.from_iterable(sample))
        print("Grid Directional radius = {0:.4f} : {1:.4f} ".format(curr_r * 3000, len(pts) / len(trajectories)))
        curr_r *= 2

    sample = [pyscan.grid_kernel(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    pts = list(itertools.chain.from_iterable(sample))
    print("Grid : {0:.2f}".format(len(pts) / len(trajectories)))

    sample = [pyscan.halfplane_kernel(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    pts = list(itertools.chain.from_iterable(sample))
    print("Halfplane : {0:.2f}".format(len(pts) / len(trajectories)))

    sample = [pyscan.dp_compress(traj, alpha) for traj in trajectories]
    pts = list(itertools.chain.from_iterable(sample))
    print("DP : {0:.2f}".format(len(pts) / len(trajectories)))

    # sample = [pyscan.lifting_kernel(pyscan.dp_compress(traj, alpha), .01) for traj in trajectories]
    # pts = list(itertools.chain.from_iterable(sample))
    # print("Lifting : {0:.2f}".format(len(pts) / len(trajectories)))

    sample = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in trajectories]
    pts = list(itertools.chain.from_iterable(sample))
    print("Hull : {0:.2f}".format(len(pts) / len(trajectories)))

    sample = [pyscan.even_sample_error([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha, False) for traj in trajectories]
    pts = list(itertools.chain.from_iterable(sample))
    print("Even : {0:.2f}".format(len(pts) / len(trajectories)))
def testing_geometric_error(trajectories, alpha, max_r, count):
    random.shuffle(trajectories)
    trajectories = trajectories[:30]
    print("got here")
    curr_r = alpha
    while curr_r < max_r:
        chord_l = math.sqrt(4 * alpha * curr_r -  2 * alpha * alpha)
        sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in trajectories]
        for error, traj in zip(test_halfspace_error(trajectories, sample), trajectories):
            if error > alpha:
                print(error)
                print(traj)
        #print("Grid Direc Error {} {}".format(i, post_process_error(test_halfspace_error(trajectories, sample))))


    sample = [pyscan.grid_kernel(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    print("Grid : {}".format(post_process_error(test_halfspace_error(trajectories, sample))))

    sample = [pyscan.halfplane_kernel(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    print("Halfplane : {}".format( post_process_error(test_halfspace_error(trajectories, sample))))

    sample = [pyscan.dp_compress(traj, alpha) for traj in trajectories]
    print("DP Error: {}".format(post_process_error(test_halfspace_error(trajectories, sample))))

    sample = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in trajectories]
    print("Hull Error: {}".format( post_process_error(test_halfspace_error(trajectories, sample))))

    sample = [pyscan.even_sample_error(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    print("Even : {}".format(post_process_error(test_halfspace_error(trajectories, sample))))
def testing_disk_geometric_error(trajectories, alpha, max_r, count):
    random.shuffle(trajectories)
    trajectories = trajectories[:30]
    for i in np.linspace(alpha, max_r, count):
        chord_l = math.sqrt(4 * alpha * i -  2 * alpha * alpha)
        sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in trajectories]
        print("Grid Direc Error {} {}".format(50 * i, 50 * post_process_error(test_disk_error(trajectories, sample, alpha, max_r))))


    sample = [pyscan.grid_kernel(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    print("Grid : {}".format(50 * post_process_error(test_disk_error(trajectories, sample, alpha, max_r))))

    sample = [pyscan.halfplane_kernel(pyscan.dp_compress(traj, alpha), alpha) for traj in trajectories]
    print("Halfplane : {}".format(50 * post_process_error(test_disk_error(trajectories, sample, alpha, max_r))))

    sample = [pyscan.dp_compress(traj, alpha) for traj in trajectories]
    print("DP Error: {}".format(50 * post_process_error(test_disk_error(trajectories, sample, alpha, max_r))))
Ejemplo n.º 4
0
def full_coreset_example(point_count, alpha, method, min_r=None):
    if min_r is None:
        min_r = alpha

    # pts = boxed_trajectory(point_count)
    #pts = [(0.2562716971245123, 0.37042159171941874), (0.2562716971245123, 0.4148497093588573)]
    pts = [(0.28076897884325597, 0.5709315642759948), (0.2883670662212526, 0.5731789009659035),
           (0.2804742254535915, 0.5708019102361805), (0.28057247658348744, 0.5704993841433011),
           (0.2807034780899998, 0.5714501804351904), (0.2734656448549006, 0.5746699224236597),
           (0.2902993384423915, 0.5749940575231799), (0.3078862906923401, 0.5769820861334908),
           (0.30896705312110206, 0.5969488082632861),
           (0.32072443833104086, 0.5964301921040598), (0.3307133032029929, 0.5930375780625253),
           (0.34640073360844237, 0.5890183028286218), (0.35494858190868855, 0.588802212762275), (0.3610729023383628, 0.5883916416362222),
           (0.36329992794916616, 0.5732869459990769), (0.37201152813256444, 0.5787108066642039),
           (0.37682583349709176, 0.5840266222961576), (0.3765310801074273, 0.5862307409728461),
           (0.37640007860089164, 0.5878730254770267), (0.3764328289775314, 0.5880675065367327),
           (0.37649832973078756, 0.5883700326295813), (0.37649832973078756, 0.5889750848153402),
           (0.3765310801074273, 0.5894504829612848), (0.3765310801074273, 0.5894504829612848),
           (0.3765310801074273, 0.5894504829612848), (0.3766293312372999, 0.5896017460077398),
           (0.3771533372633727, 0.5939019383278819), (0.3775135914062933, 0.6018324437625461),
           (0.3605816466889298, 0.610000648270198), (0.34266719067270024, 0.6244570737083028),
           (0.3384096417108854, 0.6261857942390467),
           (0.3402764131787448, 0.6249972988741699), (0.36012314141611323, 0.6221881280117534),
           (0.3730922905613496, 0.638567755040308), (0.37581057182156274, 0.6664649826047369),
           (0.3776773432894454, 0.6949456533483143), (0.38265540053710156, 0.7206603712427262),
           (0.3997510971376172, 0.7241826393240749), (0.42909543459750465, 0.7243339023704993),
           (0.43620226632606823, 0.7361540289992835), (0.43679177310539713, 0.7557750070229177),
           (0.4369882753651657, 0.7778810208094789), (0.4371520272483178, 0.7831752274347917),
           (0.43662802122224503, 0.7839963696868667), (0.4390187987161772, 0.7933962875726462),
           (0.4388222964564087, 0.809970395660907), (0.4382982904303359, 0.8370032629599792),
           (0.4391825505993293, 0.8585690515807041), (0.4399030588851706, 0.8777146314583937),
           (0.4402305626514748, 0.8978758346478641), (0.44091832056067637, 0.9170862415454762),
           (0.43639876858583676, 0.925729844199042), (0.4386912949498963, 0.9279555718823406),
           (0.43878954607976894, 0.9269183395639188), (0.43875679570315246, 0.9269399485705596),
           (0.43875679570315246, 0.9269615575772004), (0.43872404532651277, 0.9269831665838106),
           (0.43872404532651277, 0.9270263845970922), (0.43872404532651277, 0.9269183395639188),
           (0.43878954607976894, 0.9269399485705596), (0.4388550468330484, 0.9274153467165043),
           (0.43675902272875744, 0.9279987898956222), (0.4359402633130202, 0.9259675432720297),
           (0.2562716971245123, 0.37042159171941874), (0.2562716971245123, 0.4148497093588573)]
    if method == "lifting":
        core_set_pts = pyscan.lifting_kernel(pts, math.sqrt(alpha))
    elif method == "grid":
         core_set_pts = pyscan.grid_kernel(pts, alpha)
    elif method == "even":
         core_set_pts = pyscan.even_sample_error(pts, alpha, False)
    elif method =="grid_alt":
         core_set_pts = pyscan.grid_trajectory(pts, alpha)
    elif method == "grid_direc":
        chord_l = math.sqrt(4 * alpha * min_r - 2 * alpha * alpha)
        core_set_pts = pyscan.grid_direc_kernel(pts, chord_l, alpha)
    elif method == "kernel":
        core_set_pts = pyscan.halfplane_kernel(pts, alpha)
    elif method == "graham":
        core_set_pts = pyscan.hull(pts)
    elif method == "dp":
        core_set_pts = pyscan.dp_compress(pts, alpha)
    else:
        return

    ax = plt.subplot()

    plot_tuples(ax, pts, "g")
    plot_points(ax, core_set_pts, "b")

    for pt in core_set_pts:
        if math.isinf(pt[0]) or math.isnan(pt[0]) or 0 > pt[0] or 1.0 < pt[0]:
            print(pt)
        if math.isinf(pt[1]) or math.isnan(pt[1]) or 0 > pt[1] or 1.0 < pt[1]:
            print(pt)
def testing_full_framework(
        red, blue,
        output_file,
        l_s, h_s, count,
        vparam="eps",
        eps=.01,
        alpha=.01,
        max_disk_r=None,
        min_disk_r=None,
        disc_name="disc",
        region_name="halfplane",
        sample_method="halfplane",
        fast_disk = True,
        two_level_sample=True,
        max_time=None):

    """
    How do I convert the trajectories over?
    1) Just sample evenly from the length.
    2) Choose points evenly
    3) Choose
    :param trajectories:
    :param l_s:
    :param h_s:
    :param count:
    :param vparam:
    :param eps:
    :param eps_r:
    :param r:
    :param q:
    :param disc_name:
    :param region_name:
    :param input_size:
    :return:
    """

    fieldnames = ["vparam", "disc", "region", "n", "s", "n_pts", "m_pts", "b_pts", "alpha", "time",
                  "m_disc", "m_disc_approx", "sample_method"]
    with open(output_file, 'w') as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()

        for i in np.logspace(l_s, h_s, count):

            if vparam == "eps":
                eps = i
            elif vparam == "alpha":
                alpha = i
            n = 1 / eps
            s = 1 / (2 * eps * eps)
            n = int(round(n) + .1)
            s = int(round(s) + .1)


            disc = utils.disc_to_func(disc_name)

            red_sample = pyscan.my_sample(red, s)
            blue_sample = pyscan.my_sample(blue, s)
            if two_level_sample:
                red_net = pyscan.my_sample(red, n)
                blue_net = pyscan.my_sample(blue, n)
            else:
                red_net = red_sample
                blue_net = blue_sample

            net = red_net + blue_net

            print("Running: {} {}".format(n, s))

            start_time = time.time()

            if region_name == "multiscale_disk":
                if max_disk_r is not None and alpha > max_disk_r:
                    print("Max Disk Radius is greater than alpha")
                    continue
                reg, mx = multiscale_disk(min_disk_r, max_disk_r, alpha, red_sample, blue_sample, net, disc, fast_disk)
                m_sample, b_sample, net_set = [], [], []
            else:
                if sample_method == "halfplane":
                    m_sample = [pyscan.halfplane_kernel([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha) for traj in red_sample]
                    b_sample = [pyscan.halfplane_kernel([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha) for traj in blue_sample]
                    pt_net = [pyscan.halfplane_kernel([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha) for traj in net]
                elif sample_method == "dp":
                    m_sample = [pyscan.dp_compress(traj, alpha) for traj in red_sample]
                    b_sample = [pyscan.dp_compress(traj, alpha) for traj in blue_sample]
                    pt_net = [pyscan.dp_compress(traj, alpha) for traj in net]
                elif sample_method == "hull":
                    m_sample = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in red_sample]
                    b_sample = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in blue_sample]
                    pt_net = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in net]
                elif sample_method is None:
                    #just takes the waypoints.
                    m_sample = [[pyscan.Point(pt[0], pt[1], 1.0) for pt in traj] for traj in red_sample]
                    b_sample = [[pyscan.Point(pt[0], pt[1], 1.0) for pt in traj] for traj in blue_sample]
                    pt_net = [[pyscan.Point(pt[0], pt[1], 1.0) for pt in traj] for traj in net]
                elif sample_method == "grid":
                    m_sample = [pyscan.grid_kernel(traj, alpha) for traj in red_sample]
                    b_sample = [pyscan.grid_kernel(traj, alpha) for traj in blue_sample]
                    pt_net = [pyscan.grid_kernel(traj, alpha) for traj in net]
                elif sample_method == "lifting":
                    m_sample = [pyscan.lifting_kernel(traj, alpha) for traj in red_sample]
                    b_sample = [pyscan.lifting_kernel(traj, alpha) for traj in blue_sample]
                    pt_net = [pyscan.lifting_kernel(traj, alpha) for traj in net]
                elif sample_method == "grid_direc":

                    if max_disk_r is not None and alpha > max_disk_r:
                        print("Max Disk Radius is greater than alpha")
                        continue
                    chord_l = math.sqrt(4 * alpha * max(min_disk_r, alpha) - 2 * alpha * alpha)
                    m_sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in
                                red_sample]
                    b_sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in
                                blue_sample]
                    pt_net = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in net]
                elif sample_method == "even":
                    m_sample = [pyscan.even_sample_error(traj, alpha, False) for traj in red_sample]
                    b_sample = [pyscan.even_sample_error(traj, alpha, False) for traj in blue_sample]
                    pt_net = [pyscan.even_sample_error(traj, alpha, False) for traj in net]
                else:
                    return

                if region_name == "multiscale_disk_fixed":
                    m_sample = list(pyscan.trajectories_to_labels(m_sample))
                    b_sample = list(pyscan.trajectories_to_labels(b_sample))
                    net_set = list(pyscan.trajectories_to_labels(pt_net))
                    reg, mx = multiscale_disk_fixed(min_disk_r, max_disk_r, m_sample, b_sample, net_set, disc, fast_disk)
                else:
                    m_sample = list(pyscan.trajectories_to_labels(m_sample))
                    b_sample = list(pyscan.trajectories_to_labels(b_sample))
                    net_set = list(itertools.chain.from_iterable(pt_net))
                    if region_name == "halfplane":
                        reg, mx = pyscan.max_halfplane_labeled(net_set, m_sample, b_sample, disc)
                    elif region_name == "disk":
                        reg, mx = pyscan.max_disk_labeled(net_set, m_sample, b_sample, disc)
                    elif region_name == "rectangle":
                        reg, mx = pyscan.max_rect_labeled(n, 2 * max_disk_r, m_sample, b_sample, disc)
                    elif region_name == "rectangle_scale":
                        reg, mx = pyscan.max_rect_labeled_scale(n, 2 * max_disk_r, alpha, net_set, m_sample, b_sample, disc)
                    else:
                        return

            end_time = time.time()
            actual_mx = pyscan.evaluate_range_trajectory(reg, red, blue, disc)
            row = {"vparam": vparam,
                   "disc": disc_name,
                   "region": region_name,
                   "n": n, "s": s,
                   "n_pts": len(net_set), "m_pts":len(m_sample), "b_pts":len(b_sample),
                   "alpha":alpha,
                   "time": end_time - start_time,
                   "m_disc_approx": mx,
                   "m_disc": actual_mx,
                   "sample_method": sample_method}
            writer.writerow(row)
            f.flush()
            print(row)
            if max_time is not None and end_time - start_time > max_time:
                return