自治智能体之群集分离、对齐、聚集

在自治智能体的实现过程中,我们给对象注入了生命。
个体一般会和其它个体一起存在,并且相互影响。因此,我i们的目的不仅仅是模拟个体的行为,还应该把小车放入一个由许多个体组成的系统中,让它们相互影响。

复杂系统的三个主要原则:

  1. 个体之间存在小范围的联系。
  2. 个体的动作是并行的。如每个个体都应该移动,并绘制它们的外形。
  3. 系统在整体上会呈现一种自发现象。个体之间的交互会出现复杂行为和智能模式(蚁群、迁移、地震、雪花等)

群集有3个规则:

  1. 分离(又叫“躲避”) :避免与邻居发生碰撞
  2. 对齐(又叫“复制”):转向力的方向与邻居保持一致
  3. 聚集(又叫“集中”):朝着邻居的中心转向

分离算法:

 // 分离
  // 检查邻近对象并施加力的行为
  PVector separate (ArrayList<Boid> boids) {
    // 期望分离距离
    float desiredseparation = 25.0f; 
    PVector steer = new PVector(0,0,0);
    int count = 0;
    // 遍历
    for (Boid other : boids) {
      float d = PVector.dist(position,other.position);
      // 距离大于0且小于期望分离距离(距离为0为自己)
      if ((d > 0) && (d < desiredseparation)) {
        // 指向邻居的向量
        PVector diff = PVector.sub(position,other.position);
        // 向量归一化
        diff.normalize();
        // 向量的大小与距离成反比
        diff.div(d);   
        // 转向力叠加
        steer.add(diff);
        // 记录满足条件的对象数量
        count++;           
      }
    }
    //// 若满足条件的对象存在
    //if (count > 0) {
    //  steer.div((float)count);
    //}

    if (steer.mag() > 0) {
      // Reynolds: 转向力 = 期望速度 - 当前速度
      steer.normalize();
      steer.mult(maxspeed);
      steer.sub(velocity);
      steer.limit(maxforce);
    }
    return steer;
  }

对齐算法:

// 对齐
  // 检查该对象附近的对象,计算平均速度
  PVector align (ArrayList<Boid> boids) {
    float neighbordist = 50;
    PVector sum = new PVector(0,0);
    int count = 0;
    for (Boid other : boids) {
      float d = PVector.dist(position,other.position);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.velocity);
        count++;
      }
    }
    if(sum.mag() > 0) {
     //if (count > 0) {
     // sum.div((float)count);
      sum.normalize();
      sum.mult(maxspeed);
      PVector steer = PVector.sub(sum,velocity);
      steer.limit(maxforce);
      return steer;
    } else {
      return new PVector(0,0);
    }
  }

聚集算法:

 // 聚集
  // 对于附近所有对象的平均位置(即中心),计算朝向该位置的转向矢量
  PVector cohesion (ArrayList<Boid> boids) {
    float neighbordist = 50;
    PVector sum = new PVector(0,0);   
    int count = 0;
    for (Boid other : boids) {
      float d = PVector.dist(position,other.position);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.position); // Add position
        count++;
      }
    }
    
    if(sum.mag() > 0 ) {
    //if (count > 0) {
      sum.div(count);
      return seek(sum);  // Steer towards the position
    } else {
      return new PVector(0,0);
    }
  }

  // 计算朝向目标的转向力
  // 转向力 = 期望速度 - 当前速度
  PVector seek(PVector target) {
    //期望 = 目标位置 - 当前位置  
    PVector desired = PVector.sub(target,position);  
  
    desired.normalize();
    // 期望速度为最大速度
    desired.mult(maxspeed);
    // 转向力 = 期望速度 - 当前速度
    PVector steer = PVector.sub(desired,velocity);
    // 限制转向力为最大力
    steer.limit(maxforce);  
    return steer;
  }

完整代码:

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Demonstration of Craig Reynolds' "Flocking" behavior
// See: http://www.red3d.com/cwr/
// Rules: Cohesion, Separation, Alignment

// Click mouse to add boids into the system

Flock flock;

void setup() {
  size(640,360);
  flock = new Flock();
  // Add an initial set of boids into the system
  for (int i = 0; i < 200; i++) {
    Boid b = new Boid(width/2,height/2);
    flock.addBoid(b);
  }
}

void draw() {
  background(255);
  flock.run();
  
  // Instructions
  fill(0);
  text("Drag the mouse to generate new boids.",10,height-16);
}

// Add a new boid into the System
void mouseDragged() {
  flock.addBoid(new Boid(mouseX,mouseY));
}
class Boid {

  PVector position;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

  Boid(float x, float y) {
    acceleration = new PVector(0,0);
    velocity = new PVector(random(-1,1),random(-1,1));
    position = new PVector(x,y);
    r = 3.0;
    maxspeed = 3;
    maxforce = 0.05;
  }

  void run(ArrayList<Boid> boids) {
    flock(boids);
    update();
    borders();
    render();
  }

  void applyForce(PVector force) {
    // We could add mass here if we want A = F / M
    acceleration.add(force);
  }

  // We accumulate a new acceleration each time based on three rules
  void flock(ArrayList<Boid> boids) {
    PVector sep = separate(boids);   // Separation
    PVector ali = align(boids);      // Alignment
    PVector coh = cohesion(boids);   // Cohesion
    // Arbitrarily weight these forces
    sep.mult(1.5);
    ali.mult(1.0);
    coh.mult(1.0);
    // Add the force vectors to acceleration
    applyForce(sep);
    applyForce(ali);
    applyForce(coh);
  }

  // Method to update position
  void update() {
    // Update velocity
    velocity.add(acceleration);
    // Limit speed
    velocity.limit(maxspeed);
    position.add(velocity);
    // Reset accelertion to 0 each cycle
    acceleration.mult(0);
  }
  
  // 分离
  // 检查邻近对象并施加力的行为
  PVector separate (ArrayList<Boid> boids) {
    // 期望分离距离
    float desiredseparation = 25.0f; 
    PVector steer = new PVector(0,0,0);
    int count = 0;
    // 遍历
    for (Boid other : boids) {
      float d = PVector.dist(position,other.position);
      // 距离大于0且小于期望分离距离(距离为0为自己)
      if ((d > 0) && (d < desiredseparation)) {
        // 指向邻居的向量
        PVector diff = PVector.sub(position,other.position);
        // 向量归一化
        diff.normalize();
        // 向量的大小与距离成反比
        diff.div(d);   
        // 转向力叠加
        steer.add(diff);
        // 记录满足条件的对象数量
        count++;           
      }
    }
    //// 若满足条件的对象存在
    //if (count > 0) {
    //  steer.div((float)count);
    //}

    if (steer.mag() > 0) {
      // Reynolds: 转向力 = 期望速度 - 当前速度
      steer.normalize();
      steer.mult(maxspeed);
      steer.sub(velocity);
      steer.limit(maxforce);
    }
    return steer;
  }

  // 对齐
  // 检查该对象附近的对象,计算平均速度
  PVector align (ArrayList<Boid> boids) {
    float neighbordist = 50;
    PVector sum = new PVector(0,0);
    int count = 0;
    for (Boid other : boids) {
      float d = PVector.dist(position,other.position);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.velocity);
        count++;
      }
    }
    if(sum.mag() > 0) {
     //if (count > 0) {
     // sum.div((float)count);
      sum.normalize();
      sum.mult(maxspeed);
      PVector steer = PVector.sub(sum,velocity);
      steer.limit(maxforce);
      return steer;
    } else {
      return new PVector(0,0);
    }
  }

  // 聚集
  // 对于附近所有对象的平均位置(即中心),计算朝向该位置的转向矢量
  PVector cohesion (ArrayList<Boid> boids) {
    float neighbordist = 50;
    PVector sum = new PVector(0,0);   
    int count = 0;
    for (Boid other : boids) {
      float d = PVector.dist(position,other.position);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.position); // Add position
        count++;
      }
    }
    
    if(sum.mag() > 0 ) {
    //if (count > 0) {
      sum.div(count);
      return seek(sum);  // Steer towards the position
    } else {
      return new PVector(0,0);
    }
  }

  // 计算朝向目标的转向力
  // 转向力 = 期望速度 - 当前速度
  PVector seek(PVector target) {
    //期望 = 目标位置 - 当前位置  
    PVector desired = PVector.sub(target,position);  
  
    desired.normalize();
    // 期望速度为最大速度
    desired.mult(maxspeed);
    // 转向力 = 期望速度 - 当前速度
    PVector steer = PVector.sub(desired,velocity);
    // 限制转向力为最大力
    steer.limit(maxforce);  
    return steer;
  }
  
  void render() {
    // Draw a triangle rotated in the direction of velocity
    float theta = velocity.heading2D() + radians(90);
    fill(175);
    stroke(0);
    pushMatrix();
    translate(position.x,position.y);
    rotate(theta);
    beginShape(TRIANGLES);
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);
    endShape();
    popMatrix();
  }

  // Wraparound
  void borders() {
    if (position.x < -r) position.x = width+r;
    if (position.y < -r) position.y = height+r;
    if (position.x > width+r) position.x = -r;
    if (position.y > height+r) position.y = -r;
  }


}
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Flock class
// Does very little, simply manages the ArrayList of all the boids

class Flock {
  ArrayList<Boid> boids; // An ArrayList for all the boids

  Flock() {
    boids = new ArrayList<Boid>(); // Initialize the ArrayList
  }

  void run() {
    for (Boid b : boids) {
      b.run(boids);  // Passing the entire list of boids to each boid individually
    }
  }

  void addBoid(Boid b) {
    boids.add(b);
  }

}
发布了28 篇原创文章 · 获赞 0 · 访问量 294

猜你喜欢

转载自blog.csdn.net/weixin_37964410/article/details/103665904