Question
use SWI-prolog only figue out step 6 give the solution % DO NOT USE THESE PROLOG FEATURES IN THIS ASSIGNMENT!! YOU % WILL NOT RECEIVE
use SWI-prolog only figue out step 6 give the solution
% DO NOT USE THESE PROLOG FEATURES IN THIS ASSIGNMENT!! YOU
% WILL NOT RECEIVE CREDIT FOR A SOLUTION CONTAINING THESE:
% "assert", ";", "->".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% What to turn in:
% 1. A paper listing of this file containing your program and a
% paper copy of SCREEN SHOTS showing the result of running
% your program. Turn this in in class. If you are on a
% 2-person team, only turn in one paper copy (with both
% names on it).
% 2. Submit a copy of your program on Canvas named hw2.txt.
% The digital copy will be used in case the grader needs to
% run your program to verify that it works, and to determine
% the submission time if late. Each student on a team should
% submit a duplicate copy on Canvas under his/her own name.
% 3. Submit a copy of your SCREEN SHOTS on Canvas as
% a .pdf, .doc, or .docx file. Turn in one document with all
% screen shots. Do not use a phone to photograph the screen.
% Use "print screen" (PrtSc) and paste into a document.
% Eact student on a team should submit a copy on Canvas
% under his/her own name.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem: Use Bratko's BN interpreter in fig15_11_mod on Canvas.
% Build a Bayesian network like the one described in
% HW3-description.docx. Then ask the user if the patient lives
% in a high pollution area, smokes, coughs, and has a positive
% x-ray. Then use the BN to compute the probability that the
% patient has asthma or cancer and tell the user as in this
% dialog:
% Does the patient live in high pollution area? (y n):
% Does the patient smoke? (y n):
% Does the patient cough? (y n):
% Is the chest x-ray positive? (y n):
% Probability of asthma:
% Probability of cancer:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Edit this file - add your solution to the steps below.
% Make your solution readable using indentation and white
% space. Do not use a line length that will wrap when printed.
% To run this file, rename it with a .pl extension if your are
% using SWI Prolog, or the proper extension for whatever Prolog
% you are using - you may use any standard Prolog interpreter.
% Directives (must be at the top of your SWI Prolog program).
% Syntax may differ in other Prolog interpreters.
:- dynamic p/2.
:- dynamic p/3.
% Step 1: Define the graph using the syntax required by
% Bratko's BN interpreter. Example: if there is an arc
% in the BN from node a to node b (a is the parent of b), say:
% parent(a, b).
% Note: Use lower case letters for nodes!!
parent(hp,a).
parent(a,co).
parent(s,ca).
parent(ca,co).
parent(ca,x).
% Step 2: Define the prior probabilities
% in the syntax for Bratko's BN interpreter.
% Example: p(b, 0.001). % prior probability of b is 0.001
% Note: Use lower case "p" in step 2 and step 3!!
p(hp,0.30).
p(s,0.20).
% Step 3: Define the conditional probability tables
% in the syntax for Bratko's BN interpreter.
% Note: the 1st argument is a node, the 2nd argument
% is a list of nodes or negated nodes, and the 3rd
% argument is the conditional probablity.
% Example syntax:
% p(sensor, [burglary, not(lightning)], 0.9).
% Note: the above in mathematical notation is:
% P(sensor | burglary & -lightening) = 0.9
p(a,[hp],0.7).
p(a,[not(hp)],0.2).
p(ca,[s],0.4).
p(ca,[not(s)],0.1).
p(co,[a,ca],0.9).
p(co,[not(a),ca],0.4).
p(co,[a,not(ca)],0.7).
p(co,[not(a),not(ca)],0.2).
p(x,[ca],0.9).
p(x,[not(ca)],0.03).
% Step 4: Follow these instructions for running your HW3
% with my modification of Bratko's BN interpreter:
% 1. Copy fig15_11_mod.pl (my mod of Bratko's BN interpreter)
% from CSC529 Canvas to your computer.
% 2. Start Prolog on your HW3 program. Before running your
% program, tell Prolog to consult fig15_11_mod.pl
% (in SWI Prolog, you would use menu commands: File
% menu -> Consult -> fig15_11_mod.pl)
% Step 5: Take screen shots showing the results of
% running the BN interpreter on these queries in
% this order:
% ?- prob(a, [hp, not(s), co, not(x)], P).
% ?- prob(a, [not(hp), s, co, x], P).
% ?- prob(a, [hp, s, co, x], P).
% ?- prob(a, [not(hp), not(s), co, not(x)], P).
% ?- prob(ca, [hp, not(s), co, not(x)], P).
% ?- prob(ca, [not(hp), s, co, x], P).
% ?- prob(ca, [hp, s, co, x], P).
% ?- prob(ca, [not(hp), not(s), co, not(x)], P).
% Step 6: Implement the dialog with the user here to
% call the BN with the values given by the user. Use
% the same questions as in the sample dialog below.
% It is not necessary to check for bad user input,
% but it should work for any valid answers!
% Sample dialogue:
% :- go.
% Does the patient live in high pollution area? (y n):
% Does the patient smoke? (y n):
% Does the patient cough? (y n):
% Is the chest x-ray positive? (y n):
% Probability of asthma: ___ (output the probability)
% Probability of cancer: ___ (output the probability)
% Give screen shots of the dialog for these user inputs:
% Pollution Smoke Cough X-ray
% y y y y
% y n y n
% n y y y
% n n y n
go :-write( 'Does the patient live in high pollution area? (y n):'),
read(choice),nl,
write(' Does the patient smoke? (y n):'),
read(choice2),nl,
write('Does the patient cough? (y n):'),
read(choice3),nl,
write('Is the chest x-ray positive? (y n):'),
read(choice4),nl.
process()
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 15.11 An interpreter for belief networks.
% Changes: changed member-> mymember, delete->mydelete,
% added parentheses to "not".
% Reasoning in belief networks
% Belief network is represented by relations:
% parent( ParentNode, Node)
% p( Node, ParentStates, Prob)
% where Prob is conditional probability of Node given
% values of parent variables ParentStates, for example:
% p( alarm, [ burglary, not earthquake], 0.99)
% p( Node, Prob)
% probability of node without parents
% prob( Event, Condition, P):
% probability of Event, given Cond, is P;
% Event is a variable, its negation, or a list
% of simple events representing their conjunction
prob( [X | Xs], Cond, P) :- !, % Probability of conjunction
prob( X, Cond, Px),
prob( Xs, [X | Cond], PRest),
P is Px * PRest.
prob( [], _, 1) :- !. % Empty conjunction
prob( X, Cond, 1) :-
mymember( X, Cond), !. % Cond implies X
prob( X, Cond, 0) :-
mymember( not(X), Cond), !. % Cond implies X is false
prob( not(X), Cond, P) :- !, % Probability of negation
prob( X, Cond, P0),
P is 1 - P0.
% Use Bayes rule if condition involves a descendant of X
prob( X, Cond0, P) :-
mydelete( Y, Cond0, Cond),
predecessor( X, Y), !, % Y is a descendant of X
prob( X, Cond, Px),
prob( Y, [X | Cond], PyGivenX),
prob( Y, Cond, Py),
P is Px * PyGivenX / Py. % Assuming Py > 0
% Cases when condition does not involve a descendant
prob( X, Cond, P) :-
p( X, P), !. % X a root cause - its probability given
prob( X, Cond, P) :- !,
findall( (CONDi,Pi), p(X,CONDi,Pi), CPlist), % Conditions on parents
sum_probs( CPlist, Cond, P).
% sum_probs( CondsProbs, Cond, WeigthedSum)
% CondsProbs is a list of conditions and corresponding probabilities,
% WeightedSum is weighted sum of probabilities of Conds given Cond
sum_probs( [], _, 0).
sum_probs( [ (COND1,P1) | CondsProbs], COND, P) :-
prob( COND1, COND, PC1),
sum_probs( CondsProbs, COND, PRest),
P is P1 * PC1 + PRest.
predecessor( X, not(Y)) :- !, % Negated variable Y
predecessor( X, Y).
predecessor( X, Y) :-
parent( X, Y).
predecessor( X, Z) :-
parent( X, Y),
predecessor( Y, Z).
mymember( X, [X | _]).
mymember( X, [_ | L]) :-
mymember( X, L).
mydelete( X, [X | L], L).
mydelete( X, [Y | L], [Y | L2]) :-
mydelete( X, L, L2).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started