Adrian A. de Freitas and Troy Weingart
United States Air Force Academy
Colorado Springs, Colorado
Department of Computer and Cyber Sciences
The rocket landing simulator is a Python programming assignment that lets students simulate a rocket's trajectory as it 1) boosts from a launch pad, 2) descends towards the earth, and 3) safely lands on a boat floating in the ocean. This assignment is inspired by SpaceX's Falcon 9 rocket, which in 2018 became the first space launch vehicle ever to take off from the earth and land autonomously.
In this assignment, students develop:
This assignment was designed as a capstone for a CS1 level course, and was successfully administered to 174 non-Computer Science Freshmen students at our institution during the Spring 2020 semester.
An example student submission is shown below. Each simulation starts by having the rocket boosting from the ground on the left side of the screen. After the rocket has climbed to a predefined altitude, it turns towards the right and hands over control to the player. Initially, the student serves as the player, and controls the rocket's descent using the arrow keys (which activate the rocket's "thrusters"). In the latter phase of the project, the AI agent assumes the player role, and automatically determines which thrusters to fire in order to guarantee a safe landing.
Each time the game ends (either by the rocket landing on the barge or crashing into the ocean/ground), the simulator evaluates the player/AI's performance using criteria such as fuel consumption, softness of landing, etc., and assign him/her/it a score. The game then automatically resets itself to the same configuration and allows the player/AI to try again.
Example rocket landing AI after 1 generation. While the rocket can land on the boat, it does not yet know how to decrease its descent velocity.
Example rocket landing AI after 53 generations. Using a student-defined fitness function that scores AIs based on 1) how softly they land, and 2) how close they land to the center of the barge, the genetic algorithm tunes the AI to achieve the desired performance characteristics.
The physics model used to simulate the rocket's path is similar to that used in the classic arcade game "Asteroids." In our simulation, the rocket's position, velocity, and acceleration are tracked in both the x and y directions. As thrusters are activated (either via keyboard presses or by the AI), the game updates the acceleration value(s) in the corresponding direction(s). This in turn updates the velocity and position of the rocket. Our simulation does not take air friction into account--in other words, the rocket will continue moving in a horizontal direction at a constant speed until a horizontal acceleration is applied. Our simulation does, however, model gravity. Each frame, we apply a constant acceleration to the rocket in the DOWN direction. This acceleration counteracts the upward thrust of the rocket, and eventually allows it to descend to the Earth.
The following equations summarize how we update the acceleration, velocity, and position of the rocket at each animation frame:
# Acceleration
rocket_acceleration_x = 0.0 # This value changes depending upon which phase of the game we are in
rocket_acceleration_y = 0.0 # This value changes depending upon which phase of the game we are in
# Modifies Rocket's Y Acceleration to Take Gravity into Account
rocket_acceleration_y = rocket_acceleration_y + GRAVITY
# Velocity
rocket_velocity_x = rocket_velocity_x + rocket_acceleration_x
rocket_velocity_y = rocket_velocity_y + rocket_acceleration_y
# Position
rocket_position_x = rocket_position_x + rocket_velocity_x
rocket_position_y = rocket_position_y + rocket_velocity_y
As noted above, the exact acceleration values used to update the rocket vary depending upon which "phase" of the simulation we are in. The following figure summarizes each phase and the specific rules we used.
Note that our model assumes that (0, 0) is in the top left corner. Consequently, "negative" y accelerations and velocities corresponds to movement in the UP direction, while positive y accelerations and velocities correspond to movement in the DOWN direction.
Rocket acceleration during each phase of our simulation. Note that (0, 0) represents the top left corner of the graphics window. Consequently, all accelerations and velocities in the y direction are flipped (i.e., negative is UP, positive is DOWN)
# If the left thruster is activated, the rocket should start accelerating in the RIGHT direction
if left_thruster_activated:
rocket_acceleration_x = rocket_acceleration_x + THRUST_ACCELERATION_AMOUNT
# If the right thruster is activated, the rocket should start accelerating in the LEFT direction
if right_thruster_activated:
rocket_acceleration_x = rocket_acceleration_x - THRUST_ACCELERATION_AMOUNT
# If the bottom thruster is activated, the rocket should start accelerating in the UP direction
if up_thruster_activated:
rocket_acceleration_y = rocket_acceleration_y - THRUST_ACCELERATION_AMOUNT
Please note that the player never directly adjusts the velocity or position of the rocket--only the acceleration.
Summary | Rocket landing simulator - A 2D "game" that lets players control and land a rocket on a moving boat. Students initially control the rocket during the descent phase using the arrow keys on the keyboard. For the final turn-in, students will design and tune an agent that can intelligently land on the boat. This assignment teaches students the importance of functional decomposition using games as a motivational tool, and serves as a brief introduction to AI. |
---|---|
Audience | This assignment is intended for a CS1 level course. |
Topics | Graphics, modules/functions, data structures (e.g., tuples, lists), control structures, and artificial intelligence. |
Difficulty | This assignment is designed as a course capstone. The assignment is divided into 4 "gate checks," each taking the student roughly 1-1.5 weeks to complete (approximately 300 lines total). This time can most likely be shortened for more advanced students. |
Strengths |
|
Weaknesses |
|
Dependencies | Requires students to understand how to use functions, modules, loops, and lists. Assignment requires the pythonGraph graphics module, which is written for Python3 and distributed through pip. The assignment also requires a custom genetic algorithm library, which we provide. |
Variants |
Depending upon your needs, the assignment can be easily rescoped in the following ways:
|
To make the assignment managable for CS1 level students, we have divided the work into 4 "gate checks." Each gate check focuses on a set of functionality (animation, AI, etc.), and can be coded in approximately 40-50 lines.
To use this assignment, we recommend that instructors do the following:
The following link contains a "student friendly" description of the assignment. It contains everything the student needs to get started.
The starter pack contains all of the code templates and graphics students need in order to get started with this assignment. Additional files (i.e., the AI module) are introduced in the tutorials as required (see below).
NOTE: This link is identical to the one contained in the Assignment Writeup. It is provided here for your convenience.We have created a series of tutorials that guide students through each aspect of the assignment. In addition to instructions, these tutorials contain conceptual questions about the assignment that the student needs to correctly answer in order to proceed to the next step.
NOTE: These links are identical to the ones contained in the Assignment Writeup. They are provided here for your convenience.The following files contain working solutions for each gate check.
Here are some sample rubrics that we used to grade student submissions.