levy.py: 4 points
albatross.py: 4 points
In this last portion of the lab, you will be implementing one more movement class, and then using these classes to visualize the movement of animals hunting for food.
The type of movement in ContinueMove is a special kind of random movement pattern more generally called a random walk. Different types of random walks show up in many different disciplines for many different reasons, from the movement of stocks in finance to the movement of Albatross as they hunt. The latter application is the inspiration for the simulation you’ll work on here. In this part of the lab you will approximate a type of random walk (called a “Lévy Flight”) performed by albatrosses. You’ll make a new movement class (LevyMove), then you’ll get to see your class used in a simulation.
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.
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!