AI project 4: influence in Bayes nets

Official website project introduction
Source framework Download

1. Practice introduction

In this project, we will implement the inference algorithm of Bayesian network, especially variable elimination and perfect information value calculation. These reasoning algorithms will be able to infer the existence of invisible particles and ghosts.

After understanding the whole project code framework, you can code locally.

2. Problem coding

1,question1-Bayes Net Structure

In this part, we need to construct an empty Bayesian network according to the project description.

  • The X position determines which house is on which side of the chessboard. It's either left over from food or left by ghosts.
  • The Y position determines the vertical direction of the house. It simulates the vertical position of two houses at the same time and has one of four values: bottom top, bottom, left top and left bottom. "Top left" as its name implies: the house on the left of the chessboard is above and the house on the right of the chessboard is below.
  • The food house and haunted house specify the actual location of the two rooms. They are deterministic functions of "X position" and "Y position".
  • The observation is the measurement made by eating beans while traveling around the chessboard. Note that there are many such nodes - probably one for each chessboard position on the wall of the house. If there is no house at a given location, the corresponding observation value is none; Otherwise, it is either red or blue, and the exact distribution of colors depends on the type of house.
    The file we want to encode is the constructBayesNet function in BayesAgents.py:
There are five variables in the Bayesian network:
-Single“ x Location variable (controls the location of the house) x Location)
-Single“ y Location variable (controls the location of the house) y Location)
-Single“ food house"Variable (including house Center)
-Single "haunted house" variable (including house Center)
-Pacman A large number of "observed" variables can be measured for each cell
 We must use constants to name all location and house variables( X_POS_VAR,FOOD_HOUSE_VAR Etc.) at the top of the file.

Use the above steps to fill the“ obsVars";
use Bayes Each edge fill in the network Bayes Net (a tuple `(from, to)`);
Set each'variableDomainsDict[var]=values',among'values'It's a set pair“ var"The constants defined at the top of this file should be used again;

2,question2-Bayes Net Probabilities

  • Here, to fill the CPT, give the a priori probability of the y position variable
    You can refer to the method fillXCPT:

    It can be seen that we do not need to write our own handwritten probability. We can import the constant we have written from layout, and then call the method setProbability, because there are four kinds of location variables of y, so we need to set up 4 times. Finally, we can call setCPT ().

3,question3-Join Factors

  • Implement the joinFactors function in factorOperations.py.
  • It enters a list of Factors and returns a new factor whose probability entry is the product of the corresponding row of input Factors.
  • Factors stores a variableDomainsDict that maps each variable to a list of values it can accept (its domain).
  • Factor obtains its variableDomainsDict from the BayesNet that instantiates it. Therefore, it contains all variables of BayesNet, not just the non conditional and conditional variables used in factor.
Functions to be used:
Factor.getAllPossibleAssignmentDicts: Returns all contained in the factor assignmentDicts List of
Factor.setProbability: Set function, set probability of factor
Factor.getProbability:Obtain the probability value of the factor
Factor.unconditionedVariables: Returns a copy of an unconditional variable in a factor
Factor.conditionedVariables: Returns a copy of the condition variable in the factor
Factor.variableDomainsDict: Returns a copy of the variable field in a factor
First, start with factors Take each from the list factor,Then call factor.conditionedVariables()The function takes the conditional variable from this factor.
Then take out each conditional variable of this factor, if it is neither setsOfUnconditioned(The iteratable object of the variable, which contains the non conditional variables in this factor),Neither setsOfConditioned(Is an iteratable object of a variable, 
It contains the variables restricted by this factor)Add this variable to the setsOfConditioned Yes.
Then update the domain dictionary of this factor.
Last call Factor Constructor to generate a new Factor.
Then deal with constraints: where those lines are not equal to 1 factors Call on joinFactors. 
Finally, return a new Factor. 

3. Operation results

1,question1

Test instruction:
python autograder.py -q q1

2,question2

Run command
python autograder.py -q q2

3,question3

Run command
python autograder.py -q q2
 It may be useful to run specific tests during debugging to see that only one set of factors is printed out. For example, to run only the first test, run:
python autograder.py -t test_cases/q3/1-product-rule



Completed questions 1, 2 and 3 and passed the test.

Keywords: Machine Learning AI

Added by twomt on Tue, 12 Oct 2021 05:17:26 +0300