Each semester, more than 400 first-year engineering students at University of California, Berkeley, enroll in Engineering 7: Introduction to Programming for Engineers. Known on campus as E7, the course teaches programming skills that students can apply throughout their studies at UC Berkeley and beyond.
For more than 15 years, E7 has been taught using MATLAB®. In addition to helping students quickly learn the basics of computer programming, MATLAB is an ideal environment for learning how to apply mathematical models and numerical methods. Many upper-level engineering courses at UC Berkeley incorporate MATLAB, and the skills students acquire in E7 are applied and reinforced throughout the engineering curricula. As one professor noted, a principal advantage of MATLAB is the ability to rapidly prototype and test ideas across a range of engineering disciplines without having to worry about low-level programming details such as memory allocation.
As the lead teaching assistant (TA) for E7, I recently worked with Professor Alex Bayen to add a fresh element to the course: a tournament in which students compete against each other by programming virtual robots in MATLAB. Unlike many assignments in introductory programming, the tournament gives students an opportunity to demonstrate their creativity and ingenuity by tackling an open-ended design problem. The tournament motivates learning by forcing the students to apply programming principles in a meaningful way, harnessing their natural competiveness, and getting them personally invested in the outcome of the tournament. The last semester the tournament was held, the instructors received the highest ratings in the history of the course, which I believe is mainly due to the success of this tournament as a teaching tool.
Preparing the Ground with Basic Programming
Each week, E7 offers two one-hour lectures given by the professor, one discussion section led by the TAs, and two two-hour lab sessions, also hosted by the TAs. Students either complete their weekly assignments during the labs, or work independently on their own computers using MATLAB and Simulink Student Version, which we strongly encourage.
Few students come to E7 knowing programming or MATLAB. To help familiarize them with the MATLAB environment, and to relate the use of MATLAB to a concept they already know well, we spend the first lab session using MATLAB as an advanced calculator.
The first programming concepts covered are variables and functions. In past years, students found the concept of variable scope difficult to grasp. Now, we introduce it early and reinforce it throughout the course. After covering variables and functions, we move on to branching statements, loops, recursion, and plotting in MATLAB.
Having students learn how to program functions early on has served a secondary purpose by facilitating automatic grading. For each assignment, the students develop a working MATLAB function that produces a result for a given a set of inputs. They submit their work to a MATLAB based automatic grading system developed at UC Berkeley. This system tests the students’ functions using some inputs that we share with the students and others that we do not. Automatic grading has reduced the TAs’ heavy workload, giving them more time to interact with students.
Most important, however, is the fact that students learn programming faster and better when using MATLAB instead of C. Because they don’t need to worry about low-level details such as pointers, memory allocation, type declarations, preprocessing, compilation, or linking, they have more time to explore actual programming concepts and practices.
Advancing to Numerical Methods
When the students are proficient in the basics of programming, they begin working on their robots, and the focus of lectures and assignments shifts to numerical methods. Using MATLAB, they complete assignments in linear algebra, least-squares regression, interpolation, root finding, and numerical differentiation and integration. For the final assignment they devise numerical solutions to ordinary differential equations.
At this stage of the course the students begin to see how MATLAB can be applied to solve problems across a wide range of engineering disciplines. At the same time, they continue to deepen their understanding of programming principles as they use MATLAB to construct their robots. They have from the fifth week to the end of the semester to program their robots, with the tournament held during the final week.
How the Robot Tournament Works
In a robot battle, two student-programmed robots compete head-to-head on a virtual map that holds fuel tanks and bombs (Figure 1). The game ends when the two robots come within five units of each other, at which point the robot with the most fuel is declared the winner. On each turn the robot must decide how far to move and in what direction. The robot loses fuel as a function of the distance traveled in a single turn. Landing on a fuel tank adds to a robot’s fuel supply, while landing on a bomb depletes it.
Programming the Virtual Robots
Each student team is required to program its robot as a MATLAB function that is called by a master MATLAB program written by the TAs. The robot has full access to the state of the game; that is, it knows the position and size of every fuel tank and bomb, as well as its own and its opponent’s position and current fuel level. Each function is called once per turn, and must return a 1x2 matrix containing its move for the next turn as delta-x and delta-y values. The fuel cost function, given in advance, is defined as delta-x2 + delta-y2 + 2. The students’ functions are not permitted to retain the previous state of the game; all decisions are based on the current game state.
Given these simple constraints, the students are free to implement any strategy they want. We provide them with MATLAB code for a robot that waits in one place, as well as four lines of code for a simple moving robot. We also give them a MATLAB P-file from one of the best teams in the previous year’s competition. The students can test their own robots against this adversary but cannot see how it was programmed.
The tournament is highly competitive, and the top teams invest considerable time, effort, and enthusiasm into it. The best robots typically have from 200 to 250 lines of MATLAB code. They include a variety of sophisticated strategies, such as anticipating the other robots’ moves and swerving around mines to entice a pursuer into hitting a mine. Many students gain so much insight into the various strategies applied that they are able to reverse-engineer the underlying algorithms for another team’s robot simply by watching it perform in a match.
A Tournament That Teaches
The tournament has advanced several learning goals for the course. Students learn how to work in groups and how to manage their time on a semester-long project. Because their robot functions are invoked by a battle program developed by TAs, the students also learn to write code that interfaces with code they cannot see and that was developed by someone else.
Most importantly, the tournament provides the opportunity and incentive for students to engage in software design. Design is fundamental to engineering, and a programming course that omits it is, in my opinion, incomplete. By building their own software based on a few requirements and constraints, students experience firsthand what it feels like to encounter and overcome open-ended engineering challenges.
Exam scores reflected their thorough understanding of programming concepts, particularly variable scope and function usage. One former student told me that he had changed his career plans to include a stronger emphasis on computer science as a result of participating in the tournament. With that kind of enthusiasm, we look forward to some of today’s undergraduates continuing the robot tournament as graduate student TAs at UC Berkeley.