0

Boid を使用して Python で鳥の群れをシミュレートするプログラムを作成しています。タスクの 1 つは、隣接するボイド (距離 <= 50) をカウントすることです。このようにしようとしましたが (コードを参照)、良い結果が得られません。「印刷距離」は同じ距離の 20 倍を与えるので、同じ 2 組のボイドを 20 倍に数えていると仮定します。ただし、すべての組み合わせが必要です。私はプログラミングにかなり慣れていないので、どんな助けも大歓迎です!

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
from math import sqrt

X = 0
Y = 1
VX = 2
VY = 3

boids = []

for i in range(BOIDS):
    b_pos_x = random.uniform(0,WIDTH)
    b_pos_y = random.uniform(0,HEIGHT)
    b_vel_x = random.uniform(-100,100)
    b_vel_y = random.uniform(-100,100)
    b = [b_pos_x, b_pos_y, b_vel_x, b_vel_y]

    boids.append(b)

# definition of functions:

def calculate_distance(a,b):                       # calculates distance between two boids
    distance = sqrt((a[X] - b[X])**2 + (a[Y] - b[Y])**2)
    return distance

def value_velocity(v):                             # calculates velocity of boids
    velocity = sqrt(v[VX]**2+v[VY]**2)
    return velocity

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

for element_1 in range(len(boids)):             
    for element_2 in range(len(boids)):
        if element_1 != element_2:                    # for two different boids:
            distance = calculate_distance(boids[element_1],boids[element_2]) 
                 # calculate distance between boids

    velocity = value_velocity(boids[element_1])             # calculate velocity of boids

neighbors = 0                      # start neighbor counter

for element_1 in range(len(boids)):
    for element_2 in range(len(boids)):
        if element_1 != element_2:          # for diferent boids
            if distance <= 50:              # with a distance of <=50
                neighbors += 1              # add +1 to neighbor counter
    print distance
4

1 に答える 1

1

距離を配列にするか、2 つのループをマージする必要があります。現在、for ループの最初のペアで計算された距離の最後の値を常に使用するためです。

したがって、たとえば次のようにコードを変更できます。

distance=[[calculate_distance(boids[i],boids[j]) for j in range(len(boids))] for i in range(len(boids))]

for element_1 in range(len(boids)):
    for element_2 in range(len(boids)):
        if element_1 != element_2:          # for diferent boids
            if distance[element_1][element_2] <= 50:              # with a distance of <=50
                neighbors += 1
                print distance[element_1][element_2]

最初のループですべての隣接計算を行うこともできます。

neighbors =0
for element_1 in range(len(boids)):             
    for element_2 in range(len(boids)):
        if element_1 != element_2:                    # for two different boids:
            distance = calculate_distance(boids[element_1],boids[element_2]) 
            if distance <=50:
               neighbors += 1
于 2013-03-26T13:40:29.567 に答える