Thread: Boids in python

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Boids in python


    Hello everyone,
    In my study of the flocking of birds, i am interested in a pythoncode for boids to do a simulation. Because i am not a programming pro it would be very kind if someone could help me with a clear and relatively easy code.

    Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    I: please find an open source boids simulation as a start.

    II: please hire someone.

    III: looks interesting.
    Originally Posted by wikipedia
    separation: steer to avoid crowding local flockmates
    alignment: steer towards the average heading of local flockmates
    cohesion: steer to move toward the average position (center of mass) of local flockmates
    A: how many dimensions is the simulation? Doesn't matter much but 1 dimension sounds stupid, and 4 or more dimensions is hard to display, and 2 dimensions is easiest.
    B: I'd propose the inputs (volume can be specified as radius) are
    i. starting volume and particle density.
    ia. volume defining "local".
    ii. particle speed. Could be a distribution in which case I suppose the slow particles would move more efficiently at the back of the flock. Easiest to assign them all the same speed.
    iii. weights (importance) for separation, alignment, and cohesion.
    iv. time step.


    These are my initial thoughts having never heard (at least not within my memory) of a boids simulation.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Wow, thanks for the quick reply!
    Unfortunately, as a simple student i can't afford to hire someone, so it's up to me and people who are so kind to help

    This is what I already have:


    WIDTH = 1000 # WIDTH OF SCREEN IN PIXELS
    HEIGHT = 500 # HEIGHT OF SCREEN IN PIXELS
    BOIDS = 20 # NUMBER OF BOIDS IN SIMULATION
    SPEED_LIMIT = 500 # FOR BOID VELOCITY
    BOID_EYESIGHT = 50 # HOW FAR A BOID CAN LOOK
    WALL = 50 # FROM SIDE IN PIXELS
    WALL_FORCE = 100 # ACCELERATION PER MOVE

    ################################################################################

    ########################
    import random
    boids = []
    for a in range (BOIDS):
    boid()
    x_pos=boid.append(random.uniform(0,WIDTH))
    y_pos=boid.append(random.uniform(0,HEIGHT)
    x_vel=boid.append(random.uniform(0,SPEED_LIMIT)
    y_vel=boid.append(random.uniform(0,SPEED_LIMIT)
    boids.append(boid)
    print boids

    #afstand
    for x_pos in range(0,WIDTH):
    for y_pos in range (0,HEIGHT):

    dist = sqrt( (x_pos - x_pos)**2 + (y_pos - y_pos)**2 )

    ########################

    ################################################################################

    import show
    show.start(boids, move)

    I am also aware of the fact that i have to implement functions, that calculate the velocity, the new neighbours, combine the rules, limit the velocity, and important is the function 'move' that updates the boid, regarded all these rules.

    For a handy link: google conrad pseudocode

    It would be amazing if you could help me out!!!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    update position comes with these rules:

    b(vel)=new(vel)
    b(pos)=b(pos)+new(vel)/200
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    nomenclature: I've used "boid" and "particle" interchangeably.

    Complex numbers can represent position and velocity. The method works because you never have to multiply complex numbers. They'll add, they'll scale by real numbers, it all works. Your distance function can just be abs .
    13 == abs(complex(5,12))

    LABEL1234:

    How do you compute local flockmates? (more efficient methods exist) The easiest thing that could work is to subtract every other position from the position under consideration. If the length (abs) of that vector is less than the vision distance the particle is in that particle's local set. These are the flockmates of the boid.

    Let's see how to compute each term contributing to the heading.

    separation: steer to avoid crowding local flockmates.
    Find the vectors to each local flockmate as before, just a subtraction. Average these vectors, then negate and normalize the result. With complex numbers you don't need to worry about components, and you don't need numpy.


    alignment: steer towards the average heading of local flockmates. The alignment contribution is the average of the directions of the flockmates. Normalize the speed per boid first.
    Code:
    >>> velocity = complex(3,4)
    >>> normalized_velocity = velocity/abs(velocity)
    >>> print(normalized_velocity)
    (0.6+0.8j)
    >>>
    cohesion: steer to move toward the average position (center of mass) of local flockmates. That's (the sum divided by tally) of local flockmates. Normalize.


    Use a weighted average to determine the new heading, and scale by the speed.

    If there's another boid this time step go to LABEL1234.


    I've ignored your wall acceleration.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    ok, that's quite understandable! But my problem is the translation from the 'english' understanding of the problem to the python code. I had already numerous syntax errors in my first part of the code :s

    And what do you mean with LABEL1234?

    You're really helpful, thanks for the effort!!!!
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    By replying to your message we see the your original indentation. Use an editor that matches parentheses and otherwise takes care of your python code. emacs.
    Code:
    import random
    boids = []
    for a in range (BOIDS):
        boid()
        x_pos=boid.append(random.uniform(0,WIDTH))
        y_pos=boid.append(random.uniform(0,HEIGHT)   ################ missing right )
        x_vel=boid.append(random.uniform(0,SPEED_LIMIT)################ missing right )
        y_vel=boid.append(random.uniform(0,SPEED_LIMIT)################ missing right )
    boids.append(boid) ################ indentation certainly wrong
    print boids    
                          
    #afstand
    for x_pos in range(0,WIDTH):
        for y_pos in range (0,HEIGHT):                  
            ################IndentationError: expected an indented block
    dist = sqrt( (x_pos - x_pos)**2 + (y_pos - y_pos)**2 )
    Last edited by b49P23TIvg; April 6th, 2013 at 08:59 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo