2013-03-26 86 views
0

我正在製作一個程序,通過boids模擬python中的一羣鳥。 其中一個任務是計算鄰近的boids(距離< = 50)。我試圖這樣做(見代碼),但我沒有得到好的結果。 「打印距離」給出了20倍的相同距離,所以我假設我正在計算同樣的幾個boid 20x。我需要所有的組合。 我很新的編程,所以每一個幫助,歡迎!計算python中boids的鄰居

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 
+0

此前是:http:/ /stackoverflow.com/questions/15619307/boids-in-python-calculating-distance-between-two-boids/15620200#15620200 – YXD 2013-03-26 13:49:56

回答

1

您應該將距離放入數組或合併兩個循環。因爲目前你總是使用第一對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 
+0

謝謝!我們應該在沒有數組的情況下做到這一點,合併循環意味着什麼? – user2166243 2013-03-26 13:51:17

+0

查看修改內容 – begemotv2718 2013-03-26 13:52:35

+0

我試過了,但現在出現一個錯誤:if distance [element_1] [element_2] <= 50: TypeError:'float'object has no attribute'__getitem__' – user2166243 2013-03-26 14:01:22