CPSC 415 - Artificial Intelligence - Fall 2023

Program #6 — Probabilistic agents

Possible experience: up to +50XP (or even +65XP)

Due: Fri., Dec. 15th, midnight


In this assignment, you'll be writing a program that can compute exact and/or approximate probabilistic inference on an arbitrary Bayesian Belief Net.


Create a single Python file called yourUmwId_bbn.py with the following command-line usage:

Usage: jsmith8_bbn.py bbnFile exact|approx query

The first required command-line argument will be the name of a BBN file describing the network (see below for formatting). The second required command-line argument will be either the word "exact" or the word "approx". The third required command-line argument should be a probabilistic query in the correct syntax (see below). Your program should print a helpful error message and exit if either (a) the bbnFile is not a valid file, or (b) the file exists but is not in the correct format, or (c) some word other than "exact" or "approx" is given for the second argument, or (d) the query is not in the right syntax or contains undefined variables. It should also exit with a helpful error message if the user omits any of the required arguments.

Note: for consistency with my grader, please do require and accept the exact or approx argument in the correct position, even if you only did exact mode or only did approximate mode.

The output of the program should be a single, un-rounded floating-point number (between 0 and 1) on a single line, giving the exact or approximate probability of the requested event.

Here are some examples of using the program correctly:

$ ./jsmith8_bbn.py snowy.bbn exact "snowy"
$ ./jsmith8_bbn.py snowy.bbn exact "~late"
$ ./jsmith8_bbn.py snowy.bbn exact "hotchoc|late"
$ ./jsmith8_bbn.py snowy.bbn exact "~hotchoc|late^~snowy"

(Note that the query argument will probably need to be put in quotes when you run it, so that your shell doesn't interpret characters like "|" or "^" in an unfortunate way.)

When running in exact mode the answer must be exactly right to all available decimal places. When running in approximate mode the answer must only be in the ballpark of the correct answer (say, ±.01), but must complete and print its answer in one minute or less on Stephen's laptop.

File format

A valid .bbn file contains (possibly) multiple lines for each random variable, giving the conditional probabilities of all its values for each value of its parent(s). Here's the first example from class:

SNOWY: snowy=.1 ~snowy=.9 HOTCHOC|snowy: hotchoc=.9 ~hotchoc=.1 HOTCHOC|~snowy: hotchoc=.4 ~hotchoc=.6 LATE|snowy: late=.2 ~late=.8 LATE|~snowy: late=.1 ~late=.9

And here's the Terminator example from the class activity:

E: e=.1 ~e=.9 R|e: r=.8 ~r=.2 R|~e: r=.001 ~r=.999 T|e^r: asylum=.6 foster=.2 atlarge=.2 T|e^~r: asylum=.5 foster=.2 atlarge=.3 T|~e^r: asylum=.2 foster=.2 atlarge=.6 T|~e^~r: asylum=.3 foster=.3 atlarge=.4 N|asylum: n=.2 ~n=.8 N|foster: n=.9 ~n=.1 N|atlarge: n=.2 ~n=.8 K|asylum^n: k=.95 ~k=.05 K|asylum^~n: k=.9 ~k=.1 K|foster^n: k=.01 ~k=.99 K|foster^~n: k=.5 ~k=.5 K|atlarge^n: k=.95 ~k=.05 K|atlarge^~n: k=.9 ~k=.1

Note the following:

LATE|~snowy: late=.1 ~late=.9 HOTCHOC|~snowy: hotchoc=.4 ~hotchoc=.6 HOTCHOC|snowy: hotchoc=.9 ~hotchoc=.1 SNOWY: snowy=.1 ~snowy=.9 LATE|snowy: late=.2 ~late=.8

Query syntax

Queries can either be unconditional, like:


or conditional, like:


The only logical operators you are required to support are "not" ("~"), "and" ("^"), and "given" ("|"). You do not need to support parentheses. (Note that "~" always has the highest precedence and "|" the lowest.)


Note that I will run your program not just on the Snowy and Terminator examples, but also on those of my own concoction. Points will be awarded as follows:

Turning it in

You will turn this assignment in by attaching your properly-named Python file (your UMW ID, followed by the string _bbn.py) to an email with subject line "CPSC 415 Program #6 turnin". In the body of the email, please specify whether your program works in (a) exact mode only, (b) approximate mode only, or (c) both modes. Please do not leave me to guess.

For up to an additional +5XP, also attach a complete and properly-formatted .bbn file characterizing a Bayes Net of your own invention. It should contain a minimum of five r.v.'s, at most two of which can have no parents and at least one of which must have two or more parents. Not all of the r.v.'s can be binary: at least one must have more than two possible values. The BBN should be about something real (i.e., like the Terminator example, not like a purely random example.) The body of your email should contain a brief description of the domain, what each r.v. and value means, and a couple of useful sample queries together with their answers and interpretations. Especially witty and clever BBNs may be awarded additional XP and/or shared with the class, at my discretion.

Getting help

Come to office hours, or send me email with subject line "CPSC 415 Program #6 help!!"