''' There exist predator-prey relationships among microorganisms. Both predator and prey can evolve sophisticated methods for hunting and evading respectively. This exercise is designed to have you simulate a simplified version of such behaviour: 1. A predator always moves in the direction of the closest prey 2. A prey always moves away from the closest predator Try to use the vectorization capability of NumPy as much as possible in the following problems, only using for-loops to keep track of time. ''' import numpy as np from NaNoPy import Canvas, Writer, Color screen = Canvas("Predator vs. Prey", x_size=1000, y_size=1000) pen = Writer(screen) predator = np.array([50., 50.]) prey = np.array([100., 100.]) ''' **Problem 1: One predator vs. one prey microbe** `predator` and `prey` each contain xy-coordinates. (A) Draw a red circle for the predator and a blue circle for the prey with NaNoPy. (B) Calculate the Euclidean distance between predator and prey. (C) Calculate the unit-direction from predator to prey. Hint: This is the difference between their positions normalized by the distance. (D) Move both predator and prey in this direction with speeds 2 / frame and 1 / frame respectively. (E) Repeat this cycle using a for-loop. (F) Stop the loop if the distance between predator and prey is smaller than one. ''' # your code here! ''' **Problem 2: Many predators vs. one prey microbe** Adding multiple predators means that you need to find the closest predator from the prey's perspective to decide how it moves. (A) Create an array `predators` of shape `(n_predators, 2)` to store the positions of all predators. (B) Adapt the drawing logic to draw all predators. (C) Calculate the Euclidean distance between the prey and every predator, as an array of shape `(n_predators,)`. This requires taking a sum. Use the `axis=...` keyword to specify which part of the array the sum should be taken over. (D) Find the closest predator from the perspective of the prey. (E) Calculate the direction for each predator (remember to vectorize!) (F) Adapt the code for updating the positions to accommodate multiple predators. ''' # your code here! ''' **Bonus problem (ADVANCED): Many predators vs. many prey microbes** Solving this problem using only vectorization is quite tricky. You will not be provided with detailed instructions, but here are some hints: 1. You can represent a pair of coordinates (x, y) as a complex number x + i y. This makes it easier to have every predator interact with every prey. `positions = x_array + 1j * y_array` `x_array = positions.real` `y_array = positions.imag` One advantage of this approach is that it makes it very simple to calculate the distance between two positions: `distance = np.abs(pos_2 - pos_1)` 2. `A_grid, B_grid = np.meshgrid(A, B)` produces a grid of A-values and a grid of B-values where every element of `A` is aligned with every element of `B` exactly once. A_grid and B_grid have the same shape, which makes it nice to combine them in vectorized operations. 3. You need to find the closest predator for each prey and the closest prey for each predator. They are not necessarily the same. This also means that you have to keep track of a direction for each prey and a direction for each predator. 4. You can "spice up" the problem by removing caught prey. Doing this without changing the size of the array (pure vectorization) requires masking with another array. '''