Rocket Landing Simulator (with Genetic Algorithm Tuning)

Adrian A. de Freitas and Troy Weingart
United States Air Force Academy
Colorado Springs, Colorado
Department of Computer and Cyber Sciences

Contents

Overview

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.

Video Demonstration

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.

Physics Model

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)

Meta Information

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
  • Motivational - While this assignment is not a "game" strictly speaking, it shares many aspects of games (e.g., graphics, animation) that appeal to a wide range of students. Furthermore, this assignment serves as an excellent introduction to AI, and shows students how they can produce impressive programs using the skills they developed in class.
  • Summative Assessment Tool - The assignment serves as a good summative assessment of students' programming skills. Students have to demonstrate their mastery of functional decomposition, variable scope, functions, and loops in order to successfully complete this assignment.
  • Encourages Creativity - This assignment gives students numerous opportunities to express their creativity. In addition to adding custom graphics/sounds, students were also required to design and implement their own AI. At the end of the course, we held a competition to see which AIs could consistently land the rocket on the boat while expending the least fuel and landing the most gently. Even students that struggled in the class enjoyed this aspect of the assignment, and many of their AIs ended up outperforming our higher-ranking students.
Weaknesses
  • Custom Graphics and AI Modules - The assignment, as written, requires students to learn a graphics library (pythonGraph) and a custom AI module developed by us. While these modules and their documentation are provided in subsequent sections, students may need some additional instruction before they can start on the assignment.
  • Lengthy - This assignment was designed to serve as a final project for a CS1 level course, and is considerably more complex (300 lines) than a typical programming assignment. See below for potential ways to shorten the assignment.
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:
  • Manual Simulator Only - Only require students to build the rocket simulator (Gate Checks 1-3). In this version of the assignment, students develop the graphics for the simulation, and manually fly the rocket using the arrow keys.
  • Artificial Intelligence Only - Give students a working simulator (Gate Check 3's Solution), and have them write the rules-based agent that can land the rocket on the boat. If desired, students can also use the genetic algorithm library (or create their own) to successfully tune the rocket's performance characteristics.

How to Use this Assignment

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:

  1. Have students install the pythonGraph module. They need this to render the graphics.
  2. Give students the Assignment Writeup. This page contains:
  3. Have students turn in each gate check in sequence.

Resources

Assignment Writeup

The following link contains a "student friendly" description of the assignment. It contains everything the student needs to get started.


Student Starter Pack

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.

Gate Check Tutorials

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.

Working Code Examples (For Instructors)

The following files contain working solutions for each gate check.


Rubrics (For Instructors)

Here are some sample rubrics that we used to grade student submissions.