Animal Flight

levy.py: 4 points

albatross.py: 4 points


Background

ContinueMove instances from Part 3 make use of the random module to randomly sample their next chosen movement. Rather than keeping track of past information, instances of ContinueMove make future movmement choices that do not rely on their previous directions.

This movement strategy is a simplification of a mathematical concept called a random walk. Random walks are studied in many different disciplines and have been found to be good models for animal behavior, specifically deep sea Albatross hunting. In this part of the lab you will approximate a type of random walk (called a “Lévy Flight”) performed by albatrosses using a LevyMove class, then you’ll get to see your class used in a simulation.

Lévy Flight & LevyMove

In Part 3 you implemented the ContinueMove class, which allows for a random choice of direction and a fixed period. In other words, if period is 5 in a ContinueMove instance, the instance will always move in one direction 5 times in a row before changing direction.

To better approximate a random walk, you will now implement a new class LevyMove which employs both a random direction and a choice of random period. Specifically, the constructor for LevyMove should take two arguments: the sequence of possible directions, moves, and a value max_period that represents the maximum number of moves in one direction an instance should make.

In levy.py, implement LevyMove’s construction and next method. The next method should look pretty similar to next for ContinueMove. Note that the period should be initialized to a random integer between 1 and max_period, inclusive, and should update again after every cycle.

Now write a main method to test out the class’s behavior. Similar to how you tested ContinueMove, you are expected to make at least two instances of the LevyMove class with different sequences, two different period limits, and number of times that you call next.

Heads Up

While you’ll start testing out your LevyMove class as part of this lab below, a note that the other Move classes (RepeatMove and ContinueMove) will be useful as part of Lab 8. Stay tuned!

Reminder

As always, remember to commit and push your changes.

Albatross Simulation

Now that we have a working LevyMove class, let’s practice two important skills. First, we’ll practice reading existing code (in this case, simulation code) to be able to edit or add to it. Second, we’ll pactice making instances of our LevyMove class in a new file.

The albatross flight simulator code is found in albatross.py. There are three functions: (1) clear_frame which helps animate the frame, (2) draw_bird which draws each frame of the animation and moves the “albatross” around the picture, and (3) main which powers the simulation. Your job will be to add code to main to make sure that the simulation approximates Lévy Flight.

Read me

Note that at the top of albatross.py we initalize four different directions (UP, DOWN, RIGHT, and LEFT) - these are the only directions an albatross should move in. Their values are a list of x and y values as change in position.

In main we initialize a number of values, such as the color of the background. Where the #TODO: lines are, we need to initialize first an albatross as a LevyMove object with the maximum period (arbitrarily) set to 10. Make sure that the set of directions you pass in to create a LevyMove object aligns with the directions an albatross would move. We then also need to make sure that the albatross moves forward for each of n_steps of the simulation.

Your job will be to write two lines of code to help initialize the simulation, such that your albatross moves in an approximation of Lévy flight. You should only need to add two lines of code, one between each set of ######.

The video on the left below shows what it looks like if the albatross initalizes as a RepeatMove object first with the sequence [RIGHT, UP, UP, DOWN, LEFT, LEFT]. The second video shows a potential LevyMove flight - given LevyMove is powered by a random sequence, runs of your implementation may look different!

Both videos are slowed down to show the details of the albatross movement. Do not be concerned if your albatross flies faster than the examples!

Once you’ve implemented this, you’ll have a way to visualize all your move classes and you will have completed the lab!

Reminder

Once you’ve finished, don’t forget to commit and push your changes!