1/*
    2Program describing the Mendelian rules of inheritance of the bloodtype of 
    3people. 
    4The problem is to predict the probability of the bloodtype of a person.
    5
    6From
    7http://dtai.cs.kuleuven.be/cplve/ilp09/
    8Reference:
    9Wannes Meert, Jan Struyf, and Hendrik Blockeel. "CP-Logic theory inference with
   10contextual variable elimination and comparison to BDD based inference methods."
   11Inductive Logic Programming. Springer Berlin Heidelberg, 2010. 96-109.
   12*/
   13:- use_module(library(pitaind)).   14
   15:- if(current_predicate(use_rendering/1)).   16:- use_rendering(c3).   17:- endif.   18
   19:- pitaind.   20
   21
   22:- begin_lpad.   23:- set_pitaind(or,exc).   24
   25% mchrom(Person,C) means that the chromosome of Person inherited from his mother
   26% (or mother chromosome) has allele C. The alleles are a, b and null
   27% pchrom(Person,C) means that the chromosome of Person inherited from his father
   28% (or father chromosme) has allele C. The alleles are a, b and null
   29% bloodtype(Person,B) means that Person has bloodtype B
   30
   31% rules for determining the allele of the mother chromosome of a Person on the 
   32% basis of those of his mother 
   33mchrom(Person,a):0.90 ; mchrom(Person,b):0.05 ; mchrom(Person,null):0.05 :- mother(Mother,Person), pchrom(Mother,a   ), mchrom(Mother,a   ).
   34% if both chromosome of Person's mother have allele a, then the mother 
   35% chromosome of Person is a with probability 0.90, b with probability 0.05 and
   36% null with probability 0.05
   37mchrom(Person,a):0.49 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.02 :- mother(Mother,Person), pchrom(Mother,b   ), mchrom(Mother,a   ).
   38mchrom(Person,a):0.49 ; mchrom(Person,b):0.02 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,null), mchrom(Mother,a   ).
   39mchrom(Person,a):0.49 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.02 :- mother(Mother,Person), pchrom(Mother,a   ), mchrom(Mother,b   ).
   40mchrom(Person,a):0.05 ; mchrom(Person,b):0.90 ; mchrom(Person,null):0.05 :- mother(Mother,Person), pchrom(Mother,b   ), mchrom(Mother,b   ).
   41mchrom(Person,a):0.02 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,null), mchrom(Mother,b   ).
   42mchrom(Person,a):0.49 ; mchrom(Person,b):0.02 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,a   ), mchrom(Mother,null).
   43mchrom(Person,a):0.02 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,b   ), mchrom(Mother,null).
   44mchrom(Person,a):0.05 ; mchrom(Person,b):0.05 ; mchrom(Person,null):0.90 :- mother(Mother,Person), pchrom(Mother,null), mchrom(Mother,null).
   45                                                                                                                                           
   46% rules for determining the allele of the father chromosome of a Person on the 
   47% basis of those of his father 
   48pchrom(Person,a):0.90 ; pchrom(Person,b):0.05 ; pchrom(Person,null):0.05 :- father(Father,Person), pchrom(Father,a   ), mchrom(Father,a   ).
   49% if both chromosome of Person's father have allele a, then the father 
   50% chromosome of Person is a with probability 0.90, b with probability 0.05 and
   51% null with probability 0.05
   52pchrom(Person,a):0.49 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.02 :- father(Father,Person), pchrom(Father,b   ), mchrom(Father,a   ).
   53pchrom(Person,a):0.49 ; pchrom(Person,b):0.02 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,null), mchrom(Father,a   ).
   54pchrom(Person,a):0.49 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.02 :- father(Father,Person), pchrom(Father,a   ), mchrom(Father,b   ).
   55pchrom(Person,a):0.05 ; pchrom(Person,b):0.90 ; pchrom(Person,null):0.05 :- father(Father,Person), pchrom(Father,b   ), mchrom(Father,b   ).
   56pchrom(Person,a):0.02 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,null), mchrom(Father,b   ).
   57pchrom(Person,a):0.49 ; pchrom(Person,b):0.02 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,a   ), mchrom(Father,null).
   58pchrom(Person,a):0.02 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,b   ), mchrom(Father,null).
   59pchrom(Person,a):0.05 ; pchrom(Person,b):0.05 ; pchrom(Person,null):0.90 :- father(Father,Person), pchrom(Father,null), mchrom(Father,null).
   60                                                                                                                                            
   61                                                                                                                                            
   62% rules for determining the bloodtype of a Person on the basis of the two 
   63% alleles on his chromosomes
   64bloodtype(Person,a):0.90 ; bloodtype(Person,b):0.03 ; bloodtype(Person,ab):0.03 ; bloodtype(Person,null):0.04 :- pchrom(Person,a   ),mchrom(Person,a   ).
   65% if both chromosomes of Person have allele a, then the bloodtype of Person
   66% is a with probability 0.90, b with probability 0.03, ab with probability 0.03
   67% and null with probability 0.04
   68bloodtype(Person,a):0.03 ; bloodtype(Person,b):0.03 ; bloodtype(Person,ab):0.90 ; bloodtype(Person,null):0.04 :- pchrom(Person,b   ),mchrom(Person,a   ).
   69bloodtype(Person,a):0.90 ; bloodtype(Person,b):0.04 ; bloodtype(Person,ab):0.03 ; bloodtype(Person,null):0.03 :- pchrom(Person,null),mchrom(Person,a   ).
   70bloodtype(Person,a):0.03 ; bloodtype(Person,b):0.03 ; bloodtype(Person,ab):0.90 ; bloodtype(Person,null):0.04 :- pchrom(Person,a   ),mchrom(Person,b   ).
   71bloodtype(Person,a):0.04 ; bloodtype(Person,b):0.90 ; bloodtype(Person,ab):0.03 ; bloodtype(Person,null):0.03 :- pchrom(Person,b   ),mchrom(Person,b   ).
   72bloodtype(Person,a):0.03 ; bloodtype(Person,b):0.09 ; bloodtype(Person,ab):0.04 ; bloodtype(Person,null):0.03 :- pchrom(Person,null),mchrom(Person,b   ).
   73bloodtype(Person,a):0.90 ; bloodtype(Person,b):0.03 ; bloodtype(Person,ab):0.03 ; bloodtype(Person,null):0.04 :- pchrom(Person,a   ),mchrom(Person,null).
   74bloodtype(Person,a):0.03 ; bloodtype(Person,b):0.90 ; bloodtype(Person,ab):0.04 ; bloodtype(Person,null):0.03 :- pchrom(Person,b   ),mchrom(Person,null).
   75bloodtype(Person,a):0.03 ; bloodtype(Person,b):0.04 ; bloodtype(Person,ab):0.03 ; bloodtype(Person,null):0.90 :- pchrom(Person,null),mchrom(Person,null).
   76
   77% the alleles of the parents' chromosomes are chosen randomly
   78mchrom(p_m,a):0.3 ; mchrom(p_m,b):0.3 ; mchrom(p_m,null):0.4.
   79% the mother chromosome of p_m is a with probability 0.3, b with probability 0.3
   80% and null with probability 0.4
   81pchrom(p_m,a):0.3 ; pchrom(p_m,b):0.3 ; pchrom(p_m,null):0.4.			
   82mchrom(p_f,a):0.3 ; mchrom(p_f,b):0.3 ; mchrom(p_f,null):0.4.
   83pchrom(p_f,a):0.3 ; pchrom(p_f,b):0.3 ; pchrom(p_f,null):0.4.
   84
   85% family with 3 people
   86father(p_f, p).
   87mother(p_m, p).			
   88
   89:- end_lpad.

?- prob(bloodtype(p,a),Prob). % what is the probability that the p's bloodtype is a? % expected result 0.1957737927437685 ?- prob(bloodtype(p,b),Prob). % what is the probability that the p's bloodtype is b? % expected result 0.22385005085999995 ?- prob(bloodtype(p,ab),Prob). % what is the probability that the p's bloodtype is ab? % expected result 0.193292577 ?- prob(bloodtype(p,null),Prob). % what is the probability that the p's bloodtype is 0? % expected result 0.1375200361199999 ?- prob_bar(bloodtype(p,a),Prob). % what is the probability that the p's bloodtype is a? % expected result 0.1957737927437685 ?- prob_bar(bloodtype(p,b),Prob). % what is the probability that the p's bloodtype is b? % expected result 0.22385005085999995 ?- prob_bar(bloodtype(p,ab),Prob). % what is the probability that the p's bloodtype is ab? % expected result 0.193292577 ?- prob_bar(bloodtype(p,null),Prob). % what is the probability that the p's bloodtype is 0? % expected result 0.1375200361199999 */