コード例 #1
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
    radius = math.sqrt(n) * r_rep
    last_vector = np.zeros((n, 2), dtype=np.float32)
    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()
            return common.calculate_dispersion(all_sheep)
            break
        step += 1
    return step
コード例 #2
0
                                        y + Ra / 2, colors[1])
    X = np.array(X)
    shepherd = Agent(560, 560, 560 + Ra, 560 + Ra, 'red')
    shepherd_point = shepherd.position2point()
    step = 0
    global_mean = np.array([np.mean(X[:, 0]), np.mean(X[:, 1])])
    """自适应模式切换"""
    while True:
        if check(X, global_mean):
            X, global_mean, shepherd_point = driving(shepherd_point, target, X,
                                                     global_mean)
        else:
            X, global_mean, shepherd_point, = collecting(
                shepherd_point, X, global_mean)
        if all_sheeps_in(X) or step > 4000:
            disp = common.calculate_dispersion(X)
            print("dispersion: ", disp)
            Y.append(disp)
            break
        tk.update()
        time.sleep(0.01)
        step += 1
    for i in range(N):  # 每一轮都要先删除 再重建 删除羊
        agent['sheep' + str(i)].delete()
    shepherd.delete()  # 删除牧羊犬

# 打印数据
Y = np.array(Y)
print("[", end="")
for y in Y:
    print(",", end="")
コード例 #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 = 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([300, 300], 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()
    target_radius = 75
    while True:
        herd_point = herd.position2point().copy()
        if common.check(all_sheep, radius):
            shepherdR.driving(herd, all_sheep, speed, target, app_dist)
            text = canvas.create_text(220, 20, text="driving", font=("宋体", 18))
            canvas.pack()
        else:
            shepherdR.collecting(herd, all_sheep, speed, app_dist)
            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))
        tk.update()
        time.sleep(0.01)
        canvas.delete(text)
        canvas.delete(step_txt)

        if common.is_all_in_center(all_sheep, target,
                                   target_radius) 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])

    # 处理轨迹
    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
コード例 #5
0
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
    theta = cmath.pi / 6
    fn = cmath.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")

    plt.plot(xx, 600 - yy, c='darkkhaki', label='shepherd', linewidth=3)
    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\\MDAF_trial60.pdf',
                dpi=600,
                format='pdf')
    ## 输出距离
    print("dist_center:", dist_center)
    print("dist_shepherd:", dist_shepherd)
    return step