Spring 2009»CSCI 470 Homework 2

CSCI 470 Homework 2

Assigned Date: Tuesday, Feb. 10, 2009
Due Date: Thursday, Feb. 19
Due Time: 1:40pm

Last modified on April 07, 2009, at 11:37 AM (see updates)


This assignment focuses on:

  • modeling AI agents and environments; and
  • reading and writing elegant Python code.

This is a pair-programming assignment (i.e., you may work with a partner). You may discuss the assignment only with your partner or the instructor.


The attached code, agentsModel.py, is adapted from code provided with Russell and Norvig, "AI - A Modern Approach". It implement Agents and Environments (chapter 2). It defines the following classes:

  • Object -- a physical object that can exist in an environment.
    • Agent -- an abstract agent with one required slot, .program, which holds a function that takes one argument, the percept, and returns an action. There is an optional slot, .performance, which is a number giving the performance measure of the agent in its environment. There are several agent subclasses, such as:
      • RandomAgent -- an agent that chooses an action at random, ignoring all percepts; and
      • TableDrivenAgent -- an agent that selects an action based on the percept sequence "seen" so far.
    • Dirt -- models dirt in environments.
    • Wall -- models a wall in environments
    • Environment -- a model of a physical environment which contains objects (e.g., walls, dirt, etc.) and runs simulations of agents perceiving and acting. An environment keeps a list of .objects and .agents. Each agent may be given a .performance slot, initialized to 0. Each object has a .location slot.

Additional classes and functions are provided.


  1. Read agentsModel.py, and understand how it models various AI agents and environments. (This code uses utils.py. Save them both in the same directory.)
  2. Modify agentsModel.py to output a printable representation of an environment when it is first initialized and after an action is performed by an agent.
    • There are many ways to accomplish this task. Find the most elegant, while keeping with the coding style of the provided code. (In other words, first read the code, understand how it works, and then consider how you will update it to perform the required task.)
    • Do not change the names/parameters of any of the provided classes/functions.
    • Do not remove any provided classes or objects.
    • Feel free to extend the provided _docex string.
    • Identify changes in the code by surrounding them with '###################' (i.e., one the line above, and on the line below the change). Also, explain why the change (don't reiterate the code, instead explain the bigger picture). Feel free to number your changes (in the comments, e.g., "change #1: ...") and refer to them in other comments (e.g., see "change #x").
  3. Consider a larger version of the vacuum environment and a vacuum agent. What is a good strategy to employ? What if there are two agents competing? What if the agents are collaborating?
    • Answer these questions in the opening comments of your code.
    • Implement a vacuum agent for this environment.
    • Run a simulation with two agent instances.
    • When you run the simulation within IDLE, capture the complete session, i.e., copy it and save it in a text file, called simulationRun.txt. Feel free to add comments (e.g., see this sample from the notes wiki), but do not modify anything else (e.g., do not change the output generated by your program).

Also see Peter Norvig's docex.py -- a framework for running unit tests and examples, written in docstrings.


All programs that you complete in your career as a student and as a professional developer should be fully documented. Follow the Golden Rule of Style: "A program should be as easy for a human being to read and understand as it is for a computer to execute." [1] Obviously, you should comment any variable, obscure statement, block of code, method, and class you create. Your comments should express why something is being done, as opposed to how the how is shown by the code.

In this course, you should always include opening comments as follows:

#   Author:     <Your Name>
#   Email:      <Your email address>
#   Class:      CSCI 470, Section 1
#   Assignment: HMWK2
#   Due Date:   <The assignment's due date>
#   Certification of Authenticity <include one of the following>:     
#      I certify that this lab is entirely my own work.
#      I certify that this lab is my own work, but I received
#      some assistance from:  <Name(s)>
#   Purpose: <Provide a simple, yet complete description of the task being
#         performed by this program. It may be several sentences long.>
#   Input: <Provide a simple, yet complete description of the input required
#               by this program.>
#   Output: <Provide a simple, yet complete description of the output generated
#          by this program.>


  1. Submit the updated code on WebCT by the due date.
  2. Also submit simulationRun.txt - the IDLE session of your simulation.


Your grade will be based on how well you followed the above instructions, and the depth/quality of your work.

Relevant Quote

"Any amount of work can be done in any amount of time... only the quality varies." ~Joao Meidanis


  1. Cooper, D. and Clancy, M. (1985) "Oh! Pascal", 2nd ed., W.W. Norton & Company, New York, p. 42.