-1

I have two lists, positions_rabbits_x (with all the x coordinates) and positions_rabbits_y (with all y coords). After my program runs 200 sec.my "rabbits" are reproducing, however when I call this function my program crashes. (it doesn't print an error, just overloads because it adds too many "rabbits" my computer so it doesn't respond anymore) Many thanks!!

def reproducing_rabbits():
    global positions_rabbits_x, positions_rabbits_y
    global rabbits_angles


    angle = 2 * math.pi * random.random()
    index = 0

    for rabx in range(len(positions_rabbits_x)-1,-1,-1):
        for rabx2 in range(len(positions_rabbits_y)):    
            distance = math.sqrt((positions_rabbits_x[rabx] - positions_rabbits_x[rabx2])**2 + (positions_rabbits_y[rabx] - positions_rabbits_y[rabx2])**2) 
            if distance < 1:
                index += 1
                if index == 1:
                    positions_rabbits_x.append(random.randint(1, 100))
                    positions_rabbits_y.append(random.randint(1,100))
                    rabbits_angles.append(angle)
                    index = 0
                else:
                    pass
    return




2 Answers2

0

With the suggested changes made in the comments your code runs.

from math import pi
from random import random, randint

def reproducing_rabbits(positions_rabbits_x, positions_rabbits_y):

    angle = 2 * pi * random()
    rabbits_angles = []

    index = 0

    for rabx in range(len(positions_rabbits_x)-1,-1,-1):
        for rabx2 in range(len(positions_rabbits_x)):  
            # change or to and since it seems this should be based upon
            # Manhattan distance (meaning if rabbit positions in x and y
            # are within 1
            if positions_rabbits_x[rabx] - positions_rabbits_x[rabx2] < 1 and positions_rabbits_y[rabx] - positions_rabbits_y[rabx2] < 1:
                index += 1
                if index == 1:
                    positions_rabbits_x.append(randint(1, 100))
                    positions_rabbits_y.append(randint(1,100))
                    rabbits_angles.append(angle)
                    index = 0
                else:
                    index= 0
                    continue

    return positions_rabbits_x, positions_rabbits_y, rabbits_angles

Test

positions_rabbits_x = list(range(4))
positions_rabbits_y = list(range(4))

positions_rabbits_x, positions_rabbits_y, rabbits_angles = reproducing_rabbits(positions_rabbits_x, positions_rabbits_y)
print(f'Positions x {positions_rabbits_x}')
print(f'Positions y {positions_rabbits_y}')
print(f'Rabbits Angles {rabbits_angles}')

Output

Positions x [0, 1, 2, 3, 67, 73, 29, 69, 99, 7, 11, 55, 18, 49, 81, 36, 79, 82, 87, 79, 16, 11, 62, 2, 49, 91, 15, 27, 47, 22]
Positions y [0, 1, 2, 3, 24, 85, 9, 19, 46, 34, 56, 25, 84, 24, 56, 86, 73, 71, 14, 68, 13, 62, 25, 83, 17, 90, 4, 41, 26, 20]
Rabbits Angles [6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789, 6.142160513085789]
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • Thank @DarrylG! It works but it still overloads, I think they reproduce too fast which causes the program to overload and stop responding.. – Elise Olthof Mar 21 '20 at 12:00
  • Thank you sooo much, It works and doesn't overload anymore, I will change my habbits on globals – Elise Olthof Mar 21 '20 at 12:39
0

I would suggest a change in how you iterate over the rabbit coordinates.

There is a lot of superfluous code in the loop too which I removed. index was not needed.

def reproducing_rabbits(rabbits_x, rabbits_y, rabbits_angles):

    for n, (x1, y1) in enumerate(zip(rabbits_x, rabbits_y)):
        for m, (x2, y2) in enumerate(zip(rabbits_x, rabbits_y)):  
            if math.sqrt((x1-x2)**2 + (y1-y2)**2) < 1 and n != m: # No self-reproduction.
                rabbits_x.append(randint(1, 100))
                rabbits_y.append(randint(1, 100))
                rabbits_angles.append(2*pi*random())

    return rabbits_x, rabbits_y, rabbits_angles
Guimoute
  • 4,407
  • 3
  • 12
  • 28
  • Thanks! It works! However, it still crashes after it is called because the rabbits reproduce too much so the program can't handle it anymore.. – Elise Olthof Mar 21 '20 at 12:03
  • IT WORKS! next to the function itself, I didn't use the right statement calling it! very stupid, anyway thanks a LOT. This was bugging me for a long time it finally works. – Elise Olthof Mar 21 '20 at 12:38
  • @EliseOlthof I assume the algorithm supposed to converge, but you always add a safety net like `if loops_count > 10000: break` just in case. – Guimoute Mar 21 '20 at 14:35