Week 4 Project - Adversarial Search and Games - CSMM
Week 4 Project - Adversarial Search and Games - CSMM
4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
INSTRUCTIONS
In this assignment you will create an agent to intelligently play the 2048puzzle game, using
more advanced techniques to probe the search space than the simple methods used in the
previous assignment. If you have not played the game before, you may do so
at gabrielecirulli.github.io/2048 to get a sense of how the game works. You will implement an
adversarial search algorithm that plays the game intelligently, perhaps much more so than
playing by hand.
Please read all sections of the instructions carefully. In particular, please pay close attention to
the recent change in submission and grading policy in the final section, titled "You only get
three submissions":
I. Introduction
II. Algorithm Review
III. Using The Skeleton Code
IV. What You Need To Submit
V. Important Information
VI. Before You Submit
I. Introduction
An instance of the 2048puzzle game is played on a 4×4 grid, with numbered tiles that slide in all
four directions when a player moves them. Every turn, a new tile will randomly appear in an
empty spot on the board, with a value of either 2 or 4. Per the input direction given by the
player, all tiles on the grid slide as far as possible in that direction, until they either (1) collide
with another tile, or (2) collide with the edge of the grid. If two tiles of the same number collide
while moving, they will merge into a single tile, valued at the sum of the two original tiles that
collided. The resulting tile cannot merge with another tile again in the same move.
In the first assignment, you had ample experience with the process of abstracting ideas and
designing functions, classes, and data structures. The goal was to get familiar with how objects,
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 1/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
states, nodes, functions, and implicit or explicit search trees are implemented and interact in
practice. This time, the focus is strictly on the groundlevel details of the algorithms. You will be
provided with all the skeleton code necessary to get started, so that you can focus solely on
optimizing your algorithm.
With typical board games like chess, the two players in the game (i.e. the "Computer AI" and the
"Player") take similar actions in their turn, and have similar objectives to achieve in the game. In
the 2048puzzle game, the setup is inherently asymmetric; that is, the computer and player take
drastically different actions in their turns. Specifically, the computer is responsible for placing
random tiles of 2 or 4 on the board, while the player is responsible for moving the pieces.
However, adversarial search can be applied to this game just the same.
II. Algorithm Review
Before you begin, review the lecture slides on adversarial search. Is this a zerosum game? What
is the minimax principle? In the 2048puzzle game, the computer AI is technically not
"adversarial". In particular, all it does is spawn random tiles of 2 and 4 each turn, with a
designated probability of either a 2 or a 4; it certainly does not specifically spawn tiles at the
most inopportune locations to foil the player's progress. However, we will create a "Player AI" to
play as if the computer is completely adversarial. In particular, we will employ
the minimax algorithm in this assignment.
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 2/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
[1] As we saw in the case of a simple game of tictactoe, it is useful to employ the minimax
algorithm, which assumes that the opponent is a perfect "minimizing" agent. In practice,
however, we may encounter asubpar opponent that makes silly moves. When this happens, the
algorithm's assumption deviates from the actual opponent's behavior. In this case, it still leads to
the desired outcome of never losing. However, if the deviation goes the other way (e.g. suppose
we employ a "maximax" algorithm that assumes that the opponent wants us to win), then the
outcome would certainly be different.
To let you focus on the details of the algorithm, a skeleton code is provided to help you get
started, and to allow you to test your algorithm on your own. The skeleton code includes the
following files. Note that you will only be working in one of them, and the rest of them are read
only:
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 3/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
Readonly: BaseAI.py. This is the base class for any AI component. All AIs inherit from
this module, and implement thegetMove() function, which takes a Grid object as
parameter and returns a move (there are different "moves" for different AIs).
Writable: PlayerAI.py. You will create this file, and this is where you will be doing
your work. This should inherit from BaseAI. The getMove() function, which you will
need to implement, returns a number that indicates the player’s action. In particular, 0
stands for "Up", 1 stands for "Down", 2 stands for "Left", and 3 stands for "Right". You
need to create this file and make it as intelligent as possible. You may include other files in
your submission, but they will have to be included through this file.
.
To test your code, execute the game manager like so:
$ python GameManager.py
The progress of the game will be displayed on your terminal screen, with one snapshot printed
after each move that the Computer AI or Player AI makes. The Player AI is allowed 0.1
seconds to come up with each move. The process continues until the game is over; that is, until
no further legal moves can be made. At the end of the game, themaximum tile value on the
board is printed.
IMPORTANT: Do not modify the files that are specified as readonly. When your submission is
graded, the grader will first automaticallyoverwrite all readonly files in the directory before
executing your code. This is to ensure that all students are using the same gameplay mechanism
and computer opponent, and that you cannot "work around" the skeleton program and
manually output a high score.
Your job in this assignment is to write PlayerAI.py, which intelligently plays the 2048puzzle
game. Here is a snippet of starter code to allow you to observe how the game looks when it is
played out. In the following "naive" Player AI. The getMove() function simply selects a next
move in random out of the available moves:
from random import randint
from BaseAI import BaseAI
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 4/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
class PlayerAI(BaseAI):
def getMove(self, grid):
moves = grid.getAvailableMoves()
return moves[randint(0, len(moves) 1)] if moves else None
Of course, that is indeed a very naive way to play the 2048puzzle game. If you submit this as
your finished product, you will likely receive a grade of zero. You should implement your Player
AI with the following points in mind:
Use heuristic functions. What is the maximum height of the game tree? Unlike
elementary games like tictactoe, in this game it is highly impracticable to search the
entire depth of the theoretical game tree. To be able to cut off your search at any point,
you must employ heuristic functions to allow you to assign approximate values to nodes
in the tree. Remember, the time limit allowed for each move is 0.1 seconds, so you will a
systematic way to cut off your search before time runs out.
Assign heuristic weights. You will likely want to include more than one heuristic
function. In that case, you will need to assign weights associated with each individual
heuristic. Deciding on an appropriate set of weights will take careful reasoning, along
with careful experimentation. If you feel adventurous, you can also simply write an
optimization metaalgorithm to iterate over the space of weight vectors, until you arrive at
results that you are happy enough with.
V. Important Information
Please read the following information carefully. Before you post a clarifying question on the
discussion board, make sure that your question is not already answered in the following
sections.
1. Note on Python 3
.
Each file in the skeleton code actually comes in two flavors: [filename].py (written in
Python 2) and [filename]_3.py (written in Python 3). If you prefer to develop in Python
3, you will be using the latter version of each file in the skeleton code provided. In addition,
you will have to name your player AI file PlayerAI_3.py as well, so that the grader will
be alerted to use the correct version of Python during grading. For grading purposes, please
only submit one of the following, but not both:
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 5/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
If you submit both versions, the grader will only grade one of them, which probably not
what you would want. To test your algorithm in Python 3, execute the game manager like
so:
$ python3 GameManager_3.py
2. Basic Requirements
You must use adversarial search in your PlayerAI (minimax with alphabeta pruning).
You must provide your move within the time limit of 0.1 seconds.
Your grade will depend on the maximum tile values your program usually gets to.
3. Grading Submissions
Submissions for which the average maximum tile value fallshalfway between 1024 and
2048 will receive full credit. As an approximation, you want to strive for half of your
games to achieve 2048.
4. Evaluation Functions
Think very carefully about your heuristic valuations; these will likely make or break your
player. Consider both qualitative and quantitative measures, such as:
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 6/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
Hints? Here is a basic AI written in JavaScript. You are supposed to do better! More hints?
Here is an interesting StackOverflowdiscussion, which may be helpful. There are lots of
great ideas everywhere, but remember that you are required to start with the minimax
algorithm and alphabeta pruning in this assignment.
In the game manager, you will see that we enforce the time limit strictly. That is,
if the getMove() function takes longer than 0.1 seconds to return a move, the game
will immediately terminate, and the maximum tile value will be assessed prematurely. The
same goes for when your submission is graded in bulk testing. You are responsible for
making sure that the getMove() function does not exceed the time limit. Once a violation is
detected in the game manager, that game is immediately over.
Think about how to enforce this rule strictly on your end. In particular, please do not rely on
a depthlimit as a basis for limiting the running time. While your algorithm may manage to
abide by the time limit via a depth limit on your own computer, there is no guarantee that it
will on other machines, in particular on the grading machine. You assignments will all be
executed on the grading platform, which evens the efficiency playing field for all
submissions, and holds everyone accountable for the same time limit.
Finally, there are multiple tools out there to help you clock time. However, make sure you
are relying on CPU time, instead of wall time, as your clocking mechanism. If you take a
look atGameManager.py, you will see that we are usingtime.clock() on our end to
enforce the limit. If you want to be safe, then use exactly that for your own implementation
side limit as well.
6. Skeleton Efficiency
As the astute student might observe, the skeleton code provided may not be the most
efficient. In particular, the operations: move(),getAvailableCells(), insertTile(),
and clone() are by no means the most efficient implementations available for achieving
the desired functionality. However, the purpose of the assignment is to gain practice in
adversarial search and heuristic evaluation, and not to optimize object representation with
bitarrays and lowlevel operations.
Everyone will be using the same skeleton code. If you wish, you are free to write your own
helper methods in a separate file (remember, Grid.py is readonly). However, this is by no
means necessary; in fact, students have written player AIs that have beaten the game
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 7/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
handily by employing the given methods. You should focus your efforts on developing a
smart algorithm first. If you have additional time, or as an extra boost (or last resort...), you
are certainly encouraged to improve these methods by writing your own efficient ones.
VI. Before You Submit
Make sure your code executes without fail on Vocareum. In particular, make sure you name
your file correctly according to the instructions specified above, especially regarding different
Python versions.
Make sure your player achieves a satisfactory score on Vocareum. Your submission will be
graded on the platform, where you may be allocated more or less processing power than your
personal computer.
You only get three submissions. Due to the change in policies regarding the submission
deadline for graded course material, new assignments are now graded upon submission. In
particular, by hitting the "SUBMIT" button on Vocareum, you are committing your work as
the final product, and no further changes in your code will be considered. Depending on the
grading load of the platform and the effectiveness of your code, it may take up to a day for
your work to be graded completely.
We are allowing several attempts at submitting your final work product. This is intended to
accommodate for silly mistakes or accidents, or if you suddenly discover a novel way to
improve your original algorithm and average scores after your initial submission. However,
please test your code extensively before you submit...!
USE OF VOCAREUM
This assignment uses Vocareum for submission and grading. Vocareum comes equipped with an
editing environment that you may use to do your development work. You are NOT required to
use the editor. In particular, you are free to choose your favorite editor / IDE to do your
development work on. When you are done with your work, you can simply upload your files
onto Vocareum for submission and grading.
However, your assignments will be graded on the platform, so you MUST make sure that your
code executes without error on the platform. In particular, do not use any additional thirdparty
libraries and packages. We do not guarantee that they will work on the platform, even if they
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 8/9
2/27/2017 Week 4 Project: Adversarial Search and Games | Week 4 Project: Adversarial Search and Games | CSMM.101x Courseware | edX
work on your personal computer. For the purposes of this project, everything that comes with
the standard Python library should be more than sufficient.
https://courses.edx.org/courses/coursev1:ColumbiaX+CSMM.101x+1T2017/courseware/b640307843b6433d9d8ace7c0a0eaefc/6366aea9fddd43b8b7db9c... 9/9