Beispiel #1
0
def run_animation(all_sheep, sheep_dict, herd, app_dist):
    step = 0
    target = np.array([550, 550], np.float64)
    r_rep = 14
    speed = 2
    r_dist = 240
    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)
            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
Beispiel #2
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