工作原理 :
模拟类鸟群的三大核心规则如下:
1)分离:保持类鸟个体之间的最小距离;
2)列队:让每个类鸟个体指向其局部同伴的平均移动方向;
3)内聚:让每个类鸟个体朝其局部同伴的质量中心移动。
类鸟群模拟也可以添加其他规则,如避开障碍物,或受到打扰时驱散鸟群等...
分析:
对于群体中的所有类鸟个体,做以下几件事:
• 应用三大核心规则;
• 应用所有附加规则;
• 应用所有边界条件。
• 更新类鸟个体的位置和速度。
• 绘制新的位置和速度。
设置功能:
1)鼠标点击左键:添加个体鸟
2)鼠标点击右键:驱散鸟群
3)避障
代码如下:
import sys, argparse
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy.spatial.distance import squareform, pdist, cdist
from numpy.linalg import norm
from matplotlib.colors import ListedColormap
import matplotlib.patches as patches
width, height = 640, 480
class Boids:
def __init__(self, N):
""" initialize the Boid simulation"""