1. Homepage
  2. Programming
  3. CS188 Introduction to Artificial Intelligence - Project 4: Inference in Bayes Nets

CS188 Introduction to Artificial Intelligence - Project 4: Inference in Bayes Nets

Engage in a Conversation
USUC BerkeleyCS188Introduction to Artificial IntelligencePythonInference in Bayes Nets

Project 4: Inference in Bayes Nets

Version 3.0. Last Updated: 03/16/2021 CourseNana.COM

Due: Friday 4/2 at 11:59pm CourseNana.COM


CourseNana.COM

Much truth is unseen. CourseNana.COM

How will Pacman become sure? CourseNana.COM

Bayes Net Inference CourseNana.COM

And now, after such a pompous poem, let's dig into our project. CourseNana.COM

Introduction

In this project, you will implement inference algorithms for Bayes Nets, specifically variable elimination and value-of-perfect-information computations. These inference algorithms will allow you to reason about the existence of invisible pellets and ghosts. CourseNana.COM

You can run the autograder for particular tests by commands of the form: CourseNana.COM

python autograder.py -t test_cases/q4/1-simple-eliminate 

The code for this project contains the following files, available as a zip archive. CourseNana.COM

Files you'll edit: CourseNana.COM

factorOperations.py CourseNana.COM

Operations on Factors (join, eliminate, normalize). CourseNana.COM

inference.py CourseNana.COM

Inference algorithms (enumeration, variable elimination, likelihood weighting). CourseNana.COM

bayesAgents.py CourseNana.COM

Pacman agents that reason under uncertainty. CourseNana.COM

Files you should read but NOT edit: CourseNana.COM

bayesNet.py CourseNana.COM

The BayesNet and Factor classes. CourseNana.COM

Files you can ignore: CourseNana.COM

graphicsDisplay.py CourseNana.COM

Graphics for Pacman CourseNana.COM

graphicsUtils.py CourseNana.COM

Support for Pacman graphics CourseNana.COM

textDisplay.py CourseNana.COM

ASCII graphics for Pacman CourseNana.COM

ghostAgents.py CourseNana.COM

Agents to control ghosts CourseNana.COM

keyboardAgents.py CourseNana.COM

Keyboard interfaces to control Pacman CourseNana.COM

layout.py CourseNana.COM

Code for reading layout files and storing their contents CourseNana.COM

autograder.py CourseNana.COM

Project autograder CourseNana.COM

testParser.py CourseNana.COM

Parses autograder test and solution files CourseNana.COM

testClasses.py CourseNana.COM

General autograding test classes CourseNana.COM

test_cases/ CourseNana.COM

Directory containing the test cases for each question CourseNana.COM

bayesNets2TestClasses.py CourseNana.COM

Project 4 specific autograding test classes CourseNana.COM

Files to Edit and Submit: You will fill in portions of factorOperations.py, inference.py, and bayesAgents.py during the assignment. Once you have completed the assignment, you will submit a token generated by submission_autograder.py. Please do not change the other files in this distribution or submit any of our original files other than this file. CourseNana.COM

Evaluation: Your code will be autograded for technical correctness. Please do not change the names of any provided functions or classes within the code, or you will wreak havoc on the autograder. However, the correctness of your implementation – not the autograder’s judgements – will be the final judge of your score. If necessary, we will review and grade assignments individually to ensure that you receive due credit for your work. CourseNana.COM

Academic Dishonesty: We will be checking your code against other submissions in the class for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. These cheat detectors are quite hard to fool, so please don’t try. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us. CourseNana.COM

Getting Help: You are not alone! If you find yourself stuck on something, contact the course staff for help. Office hours, section, and the discussion forum are there for your support; please use them. If you can’t make our office hours, let us know and we will schedule more. We want these projects to be rewarding and instructional, not frustrating and demoralizing. But, we don’t know when or how to help unless you ask. CourseNana.COM

Discussion: Please be careful not to post spoilers. CourseNana.COM


Treasure-Hunting Pacman

Pacman has entered a world of mystery. Initially, the entire map is invisible. As he explores it, he learns information about neighboring cells. The map contains two houses: a ghost house, which is probably mostly red, and a food house, which is probably mostly blue. Pacman's goal is to enter the food house while avoiding the ghost house. CourseNana.COM

Pacman will reason about which house is which based on his observations, and reason about the tradeoff between taking a chance or gathering more evidence. To enable this, you'll implement probabilistic inference using Bayes nets. CourseNana.COM

To play for yourself, run: CourseNana.COM

python hunters.py -p KeyboardAgent -r 

Bayes Nets and Factors

First, take a look at bayesNet.py to see the classes you'll be working with - BayesNet and Factor. You can also run this file to see an example BayesNet and associated Factors: python bayesNet.py CourseNana.COM

You should look at the printStarterBayesNet function - there are helpful comments that can make your life much easier later on. CourseNana.COM

The Bayes Net created in this function is shown below: CourseNana.COM

(Raining --> Traffic <-- Ballgame) CourseNana.COM

A summary of the terminology is given below: CourseNana.COM

  • Bayes Net: This is a representation of a probabilistic model as a directed acyclic graph and a set of conditional probability tables, one for each variable, as shown in lecture. The Traffic Bayes Net above is an example.
  • Factor: This stores a table of probabilities, although the sum of the entries in the table is not necessarily 1. A factor is of the general form P(X1,...Xm,y1,...,yn|Z1,...,Zp,w1,...,wq). Recall that lower case variables have already been assigned. For each possible assignment of values to the and variables, the factor stores a single number. The variables are said to be conditioned while the variables are unconditioned.
  • Conditional Probability Table (CPT): This is a factor satisfying two properties:
    1. Its entries must sum to 1 for each assignment of the conditional variables
    2. There is exactly one unconditioned variable

The Traffic Bayes Net stores the following CPTs: P(Raining), P(Ballgame), P(Traffic|Ballgame,Raining). CourseNana.COM


Question 1 (3 points): Bayes Net Structure

Implement the constructBayesNet function in BayesAgents.py. It constructs an empty Bayes net with the structure described below. (We'll specify the actual factors in the next question.) CourseNana.COM

The treasure hunting world is generated according to the following Bayes net: CourseNana.COM

Bayes net diagram CourseNana.COM

Don't worry if this looks complicated! We'll take it step by step. As described in the code for constructBayesNet, we build the empty structure by listing all of the variables, their values, and the edges between them. This figure shows the variables and the edges, but what about their values? CourseNana.COM

  • X positions determine which house goes on which side of the board. It is either food-left or ghost-left.
  • Y positions determine how the houses are vertically oriented. It models the vertical positions of both houses simultaneously, and has one of four values: both-top, both-bottom, left-top, and left-bottom. "left-top" is as the name suggests: the house on the left side of the board is on top, and the house on the right side of the board is on the bottom.
  • Food house and ghost house specify the actual positions of the two houses. They are both deterministic functions of "X positions" and "Y positions".
  • The observations are measurements that Pacman makes while traveling around the board. Note that there are many of these nodes---one for every board position that might be the wall of a house. If there is no house in a given location, the corresponding observation is none; otherwise it is either red or blue, with the precise distribution of colors depending on the kind of house.

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q1

Question 2 (1 point): Bayes Net Probabilities

Implement the fillYCPT function in bayesAgents.py. These take the Bayes net you constructed in the previous problem, and specify the factors governing the Y position variables. (We've already filled in the X position, house, and observation factors for you.) CourseNana.COM

Here's the structure of the Bayes net again: CourseNana.COM

CourseNana.COM

For an example of how to construct factors, look at the implementation of the factor for X positions in fillXCPT. CourseNana.COM

The Y positions are given by values BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL. These variables, and their associated probabilities PROB_BOTH_TOP, PROB_BOTH_BOTTOM, PROB_ONLY_LEFT_TOP, PROB_ONLY_LEFT_BOTTOM, are provided by constants at the top of the file. CourseNana.COM

If you're interested, you can look at the computation for house positions. All you need to remember is that each house can be in one of four positions: top-left, top-right, bottom-left, or bottom-right. CourseNana.COM

Hint: There are only four entries in the Y position factor, so you can specify each of those by hand. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q2 

Question 3 (5 points): Join Factors

Implement the joinFactors function in factorOperations.py. It takes in a list of Factors and returns a new Factor whose probability entries are the product of the corresponding rows of the input Factors. CourseNana.COM

joinFactors can be used as the product rule, for example, if we have a factor of the form P(X|Y) and another factor of the form P(Y), then joining these factors will yield P(X,Y). So, joinFactors allows us to incorporate probabilities for conditioned variables (in this case, Y). However, you should not assume that joinFactors is called on probability tables -- it is possible to call joinFactors on Factorswhose rows do not sum to 1. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q3 

It may be useful to run specific tests during debugging, to see only one set of factors print out. For example, to only run the first test, run: CourseNana.COM

python autograder.py -t test_cases/q3/1-product-rule

Hints and Observations: CourseNana.COM

  • Your joinFactors should return a new Factor.
  • Here are some examples of what joinFactors can do:
    • joinFactors(P(X|Y), P(Y)) = P(X,Y)
    • joinFactors(P(V,W|X,Y,Z), P(X,Y|Z)) = P(V,W,X,Y|Z)
    • joinFactors(P(X|Y,Z), P(Y)) = P(X,Y|Z)
    • joinFactors(P(V|W), P(X|Y), P(Z)) = P(V,X,Z|W,Y)
  • For a general joinFactors operation, which variables are unconditioned in the returned Factor? Which variables are conditioned?
  • Factors store a variableDomainsDict, which maps each variable to a list of values that it can take on (its domain). A Factor gets its variableDomainsDict from the BayesNet from which it was instantiated. As a result, it contains all the variables of the BayesNet, not only the unconditioned and conditioned variables used in the Factor. For this problem, you may assume that all the input Factors have come from the same BayesNet, and so their variableDomainsDicts are all the same.

Question 4 (4 points): Eliminate

Implement the eliminate function in factorOperations.py. It takes a Factor and a variable to eliminate and returns a new Factor that does not contain that variable. This corresponds to summing all of the entries in the Factor which only differ in the value of the variable being eliminated. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q4 

It may be useful to run specific tests during debugging, to see only one set of factors print out. For example, to only run the first test, run: CourseNana.COM

python autograder.py -t test_cases/q4/1-simple-eliminate

Hints and Observations: CourseNana.COM

  • Your eliminate should return a new Factor.
  • eliminate can be used to marginalize variables from probability tables. For example:
    • eliminate(P(X,Y|Z), Y) = P(X|Z)
    • eliminate(P(X,Y|Z), X) = P(Y|Z)
  • For a general eliminate operation, which variables are unconditioned in the returned Factor? Which variables are conditioned?
  • Remember that Factors store the variableDomainsDict of the original BayesNet, and not only the unconditioned and conditioned variables that they use. As a result, the returned Factor should have the same variableDomainsDict as the input Factor.

Question 5 (4 points): Normalize

Implement the normalize function in factorOperations.py. It takes a Factor as input and normalizes it, that is, it scales all of the entries in the Factor such that the sum of the entries in the Factor is 1. If the sum of probabilities in the input factor is 0, you should return None. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q5 

It may be useful to run specific tests during debugging, to see only one set of factors print out. For example, to only run the first test, run: CourseNana.COM

python autograder.py -t test_cases/q5/1-preNormalized

Hints and Observations: CourseNana.COM

  • Your normalize should return a new Factor.
  • normalize does not affect probability distributions (since probability distributions must already sum to 1).
  • For a general normalize operation, which variables are unconditioned in the returned Factor? Which variables are conditioned? Make sure to read the docstring of normalize for more instructions. In particular, pay attention to the treatment of unconditioned variables with exactly one entry in their domain.
  • Remember that Factors store the variableDomainsDict of the original BayesNet, and not only the unconditioned and conditioned variables that they use. As a result, the returned Factor should have the same variableDomainsDict as the input Factor.

Question 6 (4 points): Variable Elimination

Implement the inferenceByVariableElimination function in inference.py. It answers a probabilistic query, which is represented using a BayesNet, a list of query variables, and the evidence. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q6 

It may be useful to run specific tests during debugging, to see only one set of factors print out. For example, to only run the first test, run: CourseNana.COM

python autograder.py -t test_cases/q6/1-disconnected-eliminate

Hints and Observations: CourseNana.COM

  • The algorithm should iterate over hidden variables in elimination order, performing joining over and eliminating that variable, until the only the query and evidence variables remain.
  • The sum of the probabilities in your output factor should sum to 1 (so that it is a true conditional probability, conditioned on the evidence).
  • Look at the inferenceByEnumeration function in inference.py for an example on how to use the desired functions. (Reminder: Inference by enumeration first joins over all the variables and then eliminates all the hidden variables. In contrast, variable elimination interleaves join and eliminate by iterating over all the hidden variables and perform a join and eliminate on a single hidden variable before moving on to the next hidden variable.)
  • You will need to take care of the special case where a factor you have joined only has one unconditioned variable (the docstring specifies what to do in greater detail).

Question 7 (1 point): Marginal Inference

Inside bayesAgents.py, use the inference.inferenceByVariableElimination function you just wrote to complete the function getMostLikelyFoodHousePosition. This function should compute the marginal distribution over positions of the food house, then return the most likely position. The return value should be a dictionary containing a single key-value pair, {FOOD_HOUSE_VAR: best_house_val}, where best_house_val is the most likely position from HOUSE_VALS. This is used by Bayesian Pacman, who wanders around randomly collecting information for a fixed number of timesteps, then heads directly to the house most likely to contain food. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q7 

Hint: You may find Factor.getProbability(...) and Factor.getAllPossibleAssignmentDicts(...) to be useful. CourseNana.COM


Question 8 (3 points): Value of Perfect Information

Bayesian Pacman spends a lot of time wandering around randomly, even when further exploration doesn't provide any additional value. Can we do something smarter? CourseNana.COM

We'll evaluate VPI Pacman in a more restricted setting: everything in the world has been observed, except for the colors of one of the houses' walls. VPI Pacman has three choices: CourseNana.COM

  1. immediately enter the already-explored house
  2. immediately enter the hidden house
  3. explore the outside of the hidden house, and then make a decision about where to go

Part (a)

First look at computeEnterValues. This function computes the expected value of entering the top left and top right houses. Again, you can run the inference function you already wrote, on the bayes net self.BayesNet to do all the heavy lifting here. First compute p(foodHouse = topLeft and ghostHouse = topRight | evidence) and p(foodHouse = topRight and ghostHouse = topLeft | evidence). Then use these two probabilities to compute expected rewards for entering the top left or top right houses. Use WON_GAME_REWARD and GHOST_COLLISION_REWARD as the reward for entering the foodHouse and ghostHouse, respectively. CourseNana.COM

Part (b)

Next look at computeExploreValue. This function computes the expected value of exploring all of the hidden cells, and then making a decision. We've provided a helper method, getExplorationProbsAndOutcomes, which returns a list of future observations Pacman might make, and the probability of each. To calculate the value of the extra information Pacman will gain, you can use the following formula: CourseNana.COM

E[value of exploration]=p(evidence)maxactionsE[value of action|evidence] CourseNana.COM

Note that E[value of action|evidence] is exactly the quantity computed by computeEnterVals, so to compute the value of exploration, you can call computeEnterValues again with the hypothetical evidence provided by getExplorationProbsAndOutcomes. CourseNana.COM

Grading: To test and debug your code, run CourseNana.COM

python autograder.py -q q8 

Hint: CourseNana.COM

After exploring, Pacman will again need to compute the expected value of entering the left and right houses. Fortunately, you've already written a function to do this! Your solution to computeExploreValue can rely on your solution to computeEnterValues to determine the value of future observations. CourseNana.COM


CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
US代写,UC Berkeley代写,CS188代写,Introduction to Artificial Intelligence代写,Python代写,Inference in Bayes Nets代写,US代编,UC Berkeley代编,CS188代编,Introduction to Artificial Intelligence代编,Python代编,Inference in Bayes Nets代编,US代考,UC Berkeley代考,CS188代考,Introduction to Artificial Intelligence代考,Python代考,Inference in Bayes Nets代考,UShelp,UC Berkeleyhelp,CS188help,Introduction to Artificial Intelligencehelp,Pythonhelp,Inference in Bayes Netshelp,US作业代写,UC Berkeley作业代写,CS188作业代写,Introduction to Artificial Intelligence作业代写,Python作业代写,Inference in Bayes Nets作业代写,US编程代写,UC Berkeley编程代写,CS188编程代写,Introduction to Artificial Intelligence编程代写,Python编程代写,Inference in Bayes Nets编程代写,USprogramming help,UC Berkeleyprogramming help,CS188programming help,Introduction to Artificial Intelligenceprogramming help,Pythonprogramming help,Inference in Bayes Netsprogramming help,USassignment help,UC Berkeleyassignment help,CS188assignment help,Introduction to Artificial Intelligenceassignment help,Pythonassignment help,Inference in Bayes Netsassignment help,USsolution,UC Berkeleysolution,CS188solution,Introduction to Artificial Intelligencesolution,Pythonsolution,Inference in Bayes Netssolution,