Artificial Intelligence
Homework 4 |
|
In this assignment, you will be implementing 3 approximate inference algorithms for a Bayesian network: direct sampling, rejection sampling, and likelihood weighting. This is a programming assignment and you may work with a partner if you choose. Click here to download the starter code. You will notice that the code is arranged in packages:
Specifying a QueryWhen you run the Inference Engine, it will ask you to type in a query. All queries should obey the following conventions:
The Textual User Interface PackageThe tui package contains three classes: InferenceEngine, Query, and Reader. The InferenceEngine is the main class that you should run. It reads in a Bayesian network from file and then continuously prompts the user to enter a query. The Query class contains all of the pieces of information specified by a query -- i.e., it stores the query variables, the evidence variables, and the value of the evidence variables. You should read through this class carefully since you will need to interact with this class when implementing the 3 approximate inference algorithms. I have purposefully made the instance variables public so that you can directly access them. Finally, the Reader class is responsible for reading in a Bayesian network from file. It contains only a single public method that takes in a filename and returns a list of nodes. You should not have to interact with this class. Bayesian Network A Bayesian network is a directed acyclic graph where each node corresponds to a random variable. The
The methods for the The The methods for the Representing Events and DistributionsHere is some terminology that we will use to simplify matters:
The BitVector classA BitVector contains an array of n boolean values. This is how we represent a configuration. For example, the configuration from above {Burglary=F, Earthquake=T} is represented by a BitVector with size 2 where the first position in the array is set to false, and the second position in the array is set to true. The BitVector class has methods to get and set each position in the array. It also contains a public static variable TRUE that you should use for variables in the Bayesian network that do not have a parent. AssignmentIteratorAssignmentIterator is an iterator (i.e. it implements the Iterator interface). This class iterates over all possible configurations. Thus, if you create a new AssignmentIterator with size n=3, each call to next() will return: 111110 101 100 011 010 001 000 Any place in your code where you need to enumerate all possible configurations, you should use this class. WeightedSetFinally, the WeightedSet class is a general-purpose class that maps configurations to numerical values. The meaning of the numerical value depends upon the usage. For example, the numerical value could be a tally (for direct sampling or rejection sampling), or it could be a weight (for likelihood weighting), or it could be a probability (for a conditional probability table). Your WeightedSet class should contain a mapping from configurations (i.e. BitVectors) to numerical values (Doubles). The constructor should populate the mapping with all possible configurations (and assign each configuration some default numerical value). You will see that the WeightedSet class has methods for adding to the map and getting from the map. It also has methods for modifying (e.g. incrementing or normalizing) the numerical values. Running Your CodeThe main() method is in the InferenceEngine class. This class takes a Bayesian network file (with extension .bn) via the command line. I have provided 2 such Bayesian network files for you however you are welcome to create your own Bayesian network files. A Bayesian network file has the following format:
Grading and SubmissionYour assignment will be graded based upon correctness (i.e. I will run each of the 3 inference algorithms on various queries), efficiency (i.e. the time/space complexity of your implementation), as well as its adherence to the Java style guide.To submit, rename your directory hw4_FirstName_LastName. Then zip your directory and upload it to Moodle. Please remember to rename your directory before you zip it. |