示例#1
0
def run_animation(all_sheep, sheep_dict, herd_a, herd_b):
    step = 0
    target = np.array([560, 560])
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    last_vector = np.zeros((n, 2), dtype=np.float32)
    radius = math.sqrt(n) * r_rep
    while True:
        herd_point_a = herd_a.position2point().copy()
        herd_point_b = herd_b.position2point().copy()
        # A-聚集 + 驱赶
        # B-驱赶
        if common.check(all_sheep, radius):
            R1.driving(herd_a, all_sheep, speed, target, app_dist)
        else:
            R1.collecting(herd_a, all_sheep, speed, app_dist, target)
        R1.driving(herd_b, all_sheep, speed, target, app_dist)

        sheepR.sheep_move(herd_point_a, herd_point_b, all_sheep, r_dist, r_rep,
                          speed, sheep_dict, last_vector)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 4000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd_a.delete()
            herd_b.delete()
            break
        step += 1
    return step
示例#2
0
def run_animation(all_sheep, sheep_dict, herd, theta, k):
    step = 0
    target = np.array([600, 600])
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    fn = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    while True:
        herd_point = herd.position2point().copy()
        if common.check_sector(all_sheep, theta, target) and common.check_dist(
                all_sheep, target, fn):
            shepherdR.driving(herd, all_sheep, speed, target, app_dist)
        else:
            shepherdR.collecting(herd, all_sheep, speed, app_dist, target)

        sheepR2.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                           sheep_dict, last_vector, k)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 2000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            break
        step += 1
    return step
示例#3
0
def run_animation(all_sheep, sheep_dict, herd):
    step = 0
    target = np.array([600, 600])
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = 50
    radius = math.sqrt(n) * r_rep
    while True:
        herd_point = herd.position2point().copy()
        if common.check(all_sheep, radius):
            shepherdR.driving(herd, all_sheep, speed, target, app_dist)
        else:
            shepherdR.collecting(herd, all_sheep, speed, app_dist)

        boid.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                        sheep_dict)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 4000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            break
        step += 1
    return step
示例#4
0
def run_animation(all_sheep, sheep_dict, herd, canvas):
    step = 0
    target = np.array([550, 550])
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    theta = math.pi/6
    fn = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    while True:
        herd_point = herd.position2point().copy()
        whichmode = ""
        if common.check_sector(all_sheep, theta, target) and common.check_dist(all_sheep, target, fn):
            shepherdRR.driving(herd, all_sheep, speed, target, app_dist)
            text = canvas.create_text(220, 20, text="driving", font=("宋体", 18))
            whichmode = "driving"
            canvas.pack()
        else:
            idx = shepherdRR.collecting(herd, all_sheep, speed, app_dist, target)
            text = canvas.create_text(220, 20, text="collecting", font=("宋体", 18))
            whichmode = "collecting"
            canvas.pack()
        step += 1
        step_txt = canvas.create_text(420, 20, text="steps: {}/2000".format(step), font=("宋体", 18))
        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed, sheep_dict, last_vector)
        tk.update()
        time.sleep(0.01)
        canvas.delete(text)
        canvas.delete(step_txt)
        if step == 100:
            print("[", end="")
            for per in all_sheep:
                print("[{}, {}], ".format(per[0], per[1]), end="")
            print("]")
            print(herd.position2point())
            print(idx)
            print(whichmode)
            break

        if common.is_all_in_target(all_sheep) or step > 4000:

            print("[", end="")
            for per in all_sheep:
                print("[{}, {}], ".format(per[0], per[1]), end="")
            print("]")
            print(herd.position2point())
            print(whichmode)
            print(idx)
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            break

    return step
示例#5
0
def run_animation(all_sheep, sheep_dict, herd, k):
    step = 0
    target = np.array([550, 550], np.float64)
    r_rep = 14
    speed = 2

    r_dist = 240
    n = len(all_sheep)
    app_dist = n + 60
    fn = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    theta = math.pi / 6
    """设定新旧行为策略"""
    herd_instance = shepherdR.Shepherd(cb.OldCBehavior(), k)
    # herd_instance = shepherdR.Shepherd(cb.NewCBehavior())

    while True:
        herd_point = herd.position2point().copy()
        if herd_instance.switch(all_sheep, target, fn, theta):
            herd_instance.driving(herd, all_sheep, speed, target, app_dist)
            text = canvas.create_text(220, 20, text="driving", font=("宋体", 18))
            canvas.pack()
        else:

            herd_instance.collecting(herd, all_sheep, speed, app_dist, target)
            text = canvas.create_text(220,
                                      20,
                                      text="collecting",
                                      font=("宋体", 18))
            canvas.pack()

        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                          sheep_dict, last_vector)

        step += 1
        step_txt = canvas.create_text(420,
                                      20,
                                      text="steps: {}/2000".format(step),
                                      font=("宋体", 18))
        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                          sheep_dict, last_vector)
        tk.update()
        canvas.delete(text)
        canvas.delete(step_txt)

        if common.is_all_in_target(all_sheep) or step > 2000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            break

    return step
示例#6
0
def run_animation(all_sheep, sheep_dict, herd):
    step = 0
    target = np.array([550, 550], np.float64)
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    fn = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    theta = math.pi / 6
    """设定新旧行为策略"""
    herd_instance = shepherdR.Shepherd(cb.OldCBehavior())
    # herd_instance = shepherdR.Shepherd(cb.NewCBehavior())

    while True:
        herd_point = herd.position2point().copy()
        if herd_instance.switch(all_sheep, target, fn, theta):
            herd_instance.driving(herd, all_sheep, speed, target, app_dist)
        else:

            herd_instance.collecting(herd, all_sheep, speed, app_dist, target)

        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                          sheep_dict, last_vector)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 4000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            break
        step += 1
    return step
示例#7
0
def run_animation(all_sheep, sheep_dict, herd):
    step = 0
    target = np.array([600, 600])
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    theta = math.pi / 4.5
    fn = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    # 处理轨迹
    UU = []
    VV = []
    XX = []
    YY = []
    Px = {}
    Py = {}
    for i in range(n):
        Px['coor' + str(i)] = []
        Py['coor' + str(i)] = []

    for i in range(n):
        Px['coor' + str(i)].append(all_sheep[i][0])
        Py['coor' + str(i)].append(all_sheep[i][1])
    dist_center = 0
    dist_shepherd = 0
    pre_mean = np.array([np.mean(all_sheep[:, 0]), np.mean(all_sheep[:, 1])])
    pre_herd = herd.position2point()

    while True:
        herd_point = herd.position2point().copy()
        if common.check_sector(all_sheep, theta, target) and common.check_dist(
                all_sheep, target, fn):
            shepherdR.driving(herd, all_sheep, speed, target, app_dist)
        else:
            shepherdR.collecting(herd, all_sheep, speed, app_dist, target)

        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                          sheep_dict, last_vector)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 4000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            print("dispersion: ", common.calculate_dispersion(all_sheep))
            break
        # 处理轨迹
        global_mean = np.array(
            [np.mean(all_sheep[:, 0]),
             np.mean(all_sheep[:, 1])])
        dist_center += la.norm(pre_mean - global_mean)
        pre_mean = global_mean
        dist_shepherd += la.norm(pre_herd - herd.position2point())
        pre_herd = herd.position2point()
        for i in range(n):
            Px['coor' + str(i)].append(all_sheep[i][0])
            Py['coor' + str(i)].append(all_sheep[i][1])
        XX.append(herd.position2point()[0])
        YY.append(herd.position2point()[1])
        UU.append(global_mean[0])
        VV.append(global_mean[1])
        step += 1
    xx = np.array(XX)
    yy = np.array(YY)

    common.print_list(xx)
    common.print_list(yy)

    return step
def run_animation(all_sheep, sheep_dict, herd):
    step = 0
    target = np.array([600, 600], np.float64)
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    radius = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    # 处理轨迹
    UU = []
    VV = []
    XX = []
    YY = []
    Px = {}
    Py = {}
    for i in range(n):
        Px['coor' + str(i)] = []
        Py['coor' + str(i)] = []

    for i in range(n):
        Px['coor' + str(i)].append(all_sheep[i][0])
        Py['coor' + str(i)].append(all_sheep[i][1])
    dist_center = 0
    dist_shepherd = 0
    pre_mean = np.array([np.mean(all_sheep[:, 0]), np.mean(all_sheep[:, 1])])
    pre_herd = herd.position2point()
    while True:
        herd_point = herd.position2point().copy()
        if common.check(all_sheep, radius):
            shepherdR.driving(herd, all_sheep, speed, target, app_dist)
        else:
            shepherdR.collecting(herd, all_sheep, speed, app_dist)

        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                          sheep_dict, last_vector)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 4000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            print("dispersion: ", common.calculate_dispersion(all_sheep))
            break

        # 处理轨迹
        global_mean = np.array(
            [np.mean(all_sheep[:, 0]),
             np.mean(all_sheep[:, 1])])
        dist_center += la.norm(pre_mean - global_mean)
        pre_mean = global_mean
        dist_shepherd += la.norm(pre_herd - herd.position2point())
        pre_herd = herd.position2point()

        for i in range(n):
            Px['coor' + str(i)].append(all_sheep[i][0])
            Py['coor' + str(i)].append(all_sheep[i][1])
        XX.append(herd.position2point()[0])
        YY.append(herd.position2point()[1])
        UU.append(global_mean[0])
        VV.append(global_mean[1])
        step += 1

    # 处理轨迹
    fig, ax = plt.subplots()

    xx = np.array(XX)
    yy = np.array(YY)
    uu = np.array(UU)
    vv = np.array(VV)

    for i in range(n):
        plt.plot(np.array(Px['coor' + str(i)]),
                 600 - np.array(Py['coor' + str(i)]),
                 linewidth=0.1,
                 c='silver')

    plt.plot(xx, 600 - yy, c='darkkhaki', label='shepherd', linewidth=2)
    plt.plot(uu,
             600 - vv,
             '-.',
             c='darkcyan',
             label='sheep center',
             linewidth=2)
    plt.legend()

    y = np.arange(0, 600, 100)
    plt.xticks(y)
    plt.yticks(y)
    plt.xlabel("X Position")
    plt.ylabel("Y Position")
    plt.show()
    fig.savefig('E:\\我的坚果云\\latex\\doubleDistSum\\pics\\SPPL_trial60.pdf',
                dpi=600,
                format='pdf')

    ## 输出距离
    print("dist_center:", dist_center)
    print("dist_shepherd:", dist_shepherd)
    return step
示例#9
0
def run_animation(all_sheep, sheep_dict, herd):
    step = 0
    target = np.array([600, 600])
    r_dist = 250
    r_rep = 14
    speed = 2
    n = len(all_sheep)
    app_dist = n + 50
    theta = math.pi / 4.5
    fn = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    # 处理轨迹
    UU = []
    VV = []
    XX = []
    YY = []
    Px = {}
    Py = {}
    for i in range(n):
        Px['coor' + str(i)] = []
        Py['coor' + str(i)] = []

    for i in range(n):
        Px['coor' + str(i)].append(all_sheep[i][0])
        Py['coor' + str(i)].append(all_sheep[i][1])
    dist_center = 0
    dist_shepherd = 0
    pre_mean = np.array([np.mean(all_sheep[:, 0]), np.mean(all_sheep[:, 1])])
    pre_herd = herd.position2point()

    while True:
        herd_point = herd.position2point().copy()
        if common.check_sector(all_sheep, theta, target) and common.check_dist(
                all_sheep, target, fn):
            shepherdR.driving(herd, all_sheep, speed, target, app_dist)
        else:
            shepherdR.collecting(herd, all_sheep, speed, app_dist, target)

        sheepR.sheep_move(herd_point, all_sheep, r_dist, r_rep, speed,
                          sheep_dict, last_vector)

        tk.update()
        time.sleep(0.01)

        if common.is_all_in_target(all_sheep) or step > 4000:
            for per_sheep in sheep_dict.values():
                per_sheep.delete()
            herd.delete()
            print("dispersion: ", common.calculate_dispersion(all_sheep))
            break
        # 处理轨迹
        global_mean = np.array(
            [np.mean(all_sheep[:, 0]),
             np.mean(all_sheep[:, 1])])
        dist_center += la.norm(pre_mean - global_mean)
        pre_mean = global_mean
        dist_shepherd += la.norm(pre_herd - herd.position2point())
        pre_herd = herd.position2point()
        for i in range(n):
            Px['coor' + str(i)].append(all_sheep[i][0])
            Py['coor' + str(i)].append(all_sheep[i][1])
        XX.append(herd.position2point()[0])
        YY.append(herd.position2point()[1])
        UU.append(global_mean[0])
        VV.append(global_mean[1])
        step += 1

    # 处理轨迹
    fig, ax = plt.subplots()

    xx = np.array(XX)
    yy = np.array(YY)
    uu = np.array(UU)
    vv = np.array(VV)

    for i in range(n):
        plt.plot(np.array(Px['coor' + str(i)]),
                 600 - np.array(Py['coor' + str(i)]),
                 linewidth=0.1,
                 c="silver",
                 zorder=1)

    plt.plot(xx,
             600 - yy,
             c='darkkhaki',
             label='shepherd',
             linewidth=2,
             zorder=2)
    # plt.plot(uu, 600 - vv, '-.', c='darkcyan', label='sheep center', linewidth=2)
    y = np.arange(0, 700, 100)
    plt.xlabel("X Position")
    plt.ylabel("Y Position")

    n = 50
    X = list()
    for i in range(n - 1):
        np.random.seed(i)
        x = np.random.randint(300, 400)
        y = np.random.randint(200, 350)
        X.append([x, y])

    # outlier sheep
    Y = np.array([50, 550])

    X = np.array(X)
    plt.scatter(X[:, 0], X[:, 1], label="normal sheep", zorder=3)
    plt.scatter(Y[0], Y[1], label="outlier sheep", zorder=3)

    range_x = np.arange(0, 700, 100)
    plt.xticks(range_x)
    plt.yticks(range_x)
    # plt.grid()
    plt.legend()
    plt.show()
    fig.savefig(
        'E:\\我的坚果云\\latex\\doubleDistSum\\pics\\outlier_a_mdaf_trajectory.pdf',
        dpi=600,
        format='pdf')
    ## 输出距离
    print("dist_center:", dist_center)
    print("dist_shepherd:", dist_shepherd)
    return step