This is a project l did in Artificial Intelligence and l just want to share it with everyone. Its not the greatest but it might teach someone something new. So here goes the original Cinderella story

            Cinderella’s father remarried after her mother died. Her step mother hated her and her step sisters hated her. So she lived in the attic after her father died where she befriended mice and birds. Cinderella does all house choirs and cleaning. The king wanted the Prince to marry so an invitation was sent to all maidens in the kingdom.

Cinderella’s mice and birds friends made a dress for her to attend the dance ball the Prince will be holding. The two sisters were jealous so they tore the dress. A fairy god mother magically appeared and gave Cinderella a dress with a glass slippers but told her she must return from the dance ball before midnight. At the Dance ball, Cinderella and the Prince dance and the Prince fell in love with her, but Cinderella had to quickly ran away before the clock hit midnight or the fairy god mother would change her back. So she ran away from the party and lost one of her slippers on the stairs.

The Prince was looking for her but couldn’t find her. The Prince found one of Cinderella’s lost Slippers and went around the kingdom looking for the lady that it fits so he would marry her.  Cinderella’s step mother  locked her in the room so she would not meet the Prince’s coach who came looking. However, Cinderella’s birds and mice friends stole the key to the door and opened it. When the step sisters saw the door opened and the coaches were going up stairs to see if the glass slipper fits Cinderella, the sisters tripped the coaches and they fell and the slipper broke.

Surprisingly enough, Cinderella still have the other slipper in her pocket so she brought it out and she and the Prince got married and lived happily ever.

The original story is summarized in this project implementation as follows:

Cinderella was interested in the Prince so she went to the dance ball which led to their marriage.

The evidence to prove the original version is that Cinderella wasn’t home the night the night ball was held.

Alternative version 1.

Cinderella’s mother died at the hospital. The Prince and Cinderella met at the hospital, they fall in love and got married.

The evidence for this version is the hospital check-in showing that Cinderella was at the hospital.

Alternative version 2.

Cinderella won the American idol and Prince lost.  They then got married.

Evidence for this version was the Prince was seen crying.

Alternative version 3.

The Prince wants a wife  so she bought flowers for Cinderella and they got married.

The evidence for this version is that the Prince’s credit card showed he purchased flowers.

Alternative version 4.

Cinderella was interested in the Prince, she bought Prince flowers and they got married.

The evidence for this version is the fact that the Prince has flowers.

 

So here goes the code listing in Prolog, l can’t really explain all these code but it’s self explanatory. if someone need clarification, then just leave a comment.

 

 

 

:- dynamic p/2.

:- dynamic p/3.
:- op(800,fx,if).
:- op(700,xfx,then).
:- op(300,xfy,or).
:- op(200,xfy,and).

p(cinderella_interested,0.5).
p(cinderela_won_idol,0.5).
p(cinderella_mother_died,0.5).

parent(cinderella_interested,dance_ball).
parent(dance_ball,cinderella_home_danceball_night).
parent(dance_ball,prince_married_prince).

parent(cinderella_interested,cinderella_bought_flowers).
parent(cinderella_bought_flowers,prince_married_cinderella).
parent(cinderella_bought_flowers,prince_have_flowers).

parent(cinderella_mother_died,they_meet_at_hospital).
parent(they_meet_at_hospital,prince_married_cinderella).
parent(they_meet_at_hospital,checkin_shows_cinderella).

parent(cinderella_won_idol,prince_lost_idol).
parent(prince_lost_idol,prince_have_tears).
parent(prince_lost_idol,prince_married_cinderella).

parent(prince_bought_flowers,prince_card_shows_flowers).
parent(prince_bought_flowers,prince_married_cinderella).

p(cinderella_bought_flowers,[cinderella_interested],0.3).
p(cinderella_bought_flowers,[not(cinderella_interested)],0.001).
p(prince_have_flowers,[cinderella_bought_flowers],0.5).
p(prince_have_flowers,[not(cinderella_bought_flowers)],0.0003).
p(dance_ball,[cinderella_interested],0.5).
p(dance_ball,[not(cinderella_interested)],0.005).
p(cinderella_home_danceball_night,[dance_ball],0.000003).
p(cinderella_home_danceball_night,[not(dance_ball)],0.08).

p(prince_card_shows_flowers,[prince_bought_flowers],0.9).
p(prince_card_shows_flowers,[not(prince_bought_flowers)],0.001).

p(prince_lost_idol,[cinderella_won_idol],0.08).
p(prince_lost_idol,[not(cinderella_won_idol)],0.0005).
p(prince_have_tears,[prince_lost_idol],0.9).
p(prince_have_tears,[not(prince_lost_idol)],0.00001).
p(they_meet_at_hospital,[cinderella_mother_died],0.7).
p(they_meet_at_hospital,[not(cinderella_mother_died)],0.0002).
p(checkin_shows_cinderella,[they_meet_at_hospital],0.9).
p(checkin_shows_cinderella,[not(they_meet_at_hospital)],0.0001).

cinderella :- write(‘This program solves the mystery of a Prince marrying Cinderella’),nl,
write(‘Was Cinderella home the night of the Dance ball? (y or n) :’),
read(A),
store(cinderella_home_danceball_night,A,L1),

write(‘Does Prince have tears in the eyes?(y or n) :’),
read(B),
store(prince_have_tears,B,L2),

write(‘Does hospital check-in shows Cinderella was at the hospital?(y or n) :’),
read(C),
store(checkin_shows_cinderella,C,L3),

write(‘Does Prince have flowers?(y or n):’),
read(D),
store(prince_have_flowers,D,L4),

write(‘Does Prince credit card shows purchased flowers?(y or n):’),
read(E),
store(prince_card_shows_flowers,E,L5),
write(‘Beginning Probability Calculations’),nl,
prob(cinderella_interested,L1,P1),write(‘Probability of Cinderella interested given ‘),write(L1),write(‘ is ‘),write(P1),nl,
prob(cinderella_interested,L4,P4),write(‘Probability of Cinderella interested given ‘),write(L4),write(‘ is ‘),write(P4),nl,
prob(cinderella_won_idol,L2,P2),write(‘Probability of Cinderella won idol given ‘),write(L2),write(‘ is ‘), write(P2),nl,
prob(cinderella_mother_died,L3,P3),write(‘Probability of Cinderella mother died given ‘),write(L3),write(‘ is ‘),write(P3),nl,
prob(cincerella_mother_died,L5,P5),write(‘Probability of cinderella mother died given ‘),write(L5),write(‘ is ‘),write(P5),nl,

max_List([P1,P2,P3,P4,P5],Pn),write(‘The higest probability is ‘),write(Pn),nl,
merge_List(L1,L2,L3,L4,L5,L),
store_List(L),

write(‘Beginning Derivations’),nl,
assert(fact(prince_married_cinderella)),
forward.

store(cinderella_home_danceball_night, y, L1) :- append([cinderella_home_danceball_night],[],L1).
store(cinderella_home_danceball_night, n,L1) :- append([not(cinderella_home_danceball_night)],[],L1).
store(prince_card_shows_flowers, y,L5) :- append([prince_card_shows_flowers],[],L5).
store(prince_card_shows_flowers, n,L5) :- append([not(prince_card_shows_flowers)],[],L5).
store(prince_have_tears, y,L2) :- append([prince_have_tears],[],L2).
store(prince_have_tears, n,L2) :- append([not(prince_have_tears)],[],L2).
store(checkin_shows_cinderella, y,L3) :- append([checkin_shows_cinderella],[],L3).
store(checkin_shows_cinderella, n,L3) :- append([not(checkin_shows_cinderella)],[],L3).
store(prince_have_flowers, y,L4) :- append([prince_have_flowers],[],L4).
store(prince_have_flowers, n,L4) :- append([not(prince_have_flowers)],[],L4).

store_List([H]):-assert(fact(H)).
store_List([H|T]):-assert(fact(H)),store_List(T).

max_List([H], H).
max_List([H|T], M2) :- max_List(T, M), M2 is max(H, M).
merge_List([A],[B],[C],[D],[E],L):-append([A,B,C,D,E],[],L).

if prince_have_flowers then cinderella_bought_flowers.
if cinderella_bought_flowers then cinderella_interested.
if not(cinderella_home_danceball_night) then dance_ball.
if dance_ball then cinderela_interested.

if prince_card_shows_flowers then prince_bought_flowers.
if prince_bought_flowers then prince_wants_wife.
if prince_have_tears then prince_lost_idol.
if prince_lost_idol then cinderella_won_idol.

if checkin_shows_cinderella then they_meet_at_hospital.
if they_meet_at_hospital then cinderella_mother_died.

if dance_ball and prince_married_cinderella.
if prince_lost_idol then prince_married_cinderella.
if they_meet_at_hospital then prince_married_cinderella.
if prince_bought_flowers then prince_married_cinderella.
if cinderella_bought_flowers then prince_married_cinderella.
if prince_married_cinderella then ‘Prince_Married_Cinderella_and_they_live_happily_ever’.

 

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).
% Simple forward chaining in Prolog

forward :-
new_derived_fact( P), % A new fact
!,
write( ‘Derived: ‘), write( P), nl,
assert( fact( P)),
forward % Continue
;
write( ‘No more facts’). % All facts derived

new_derived_fact( Concl) :-
if Cond then Concl, % A rule
not( fact( Concl) ), % Rule’s conclusion not yet a fact
composed_fact( Cond). % Condition true?

composed_fact( Cond) :-
fact( Cond). % Simple fact

composed_fact( Cond1 and Cond2) :-
composed_fact( Cond1),
composed_fact( Cond2). % Both conjuncts true

composed_fact( Cond1 or Cond2) :-
composed_fact( Cond1)
;
composed_fact( Cond2).

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *