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(pita)).   14
   15:- if(current_predicate(use_rendering/1)).   16:- use_rendering(c3).   17:- endif.   18
   19:- pita.   20
   21:- begin_lpad.   22
   23% mchrom(Person,C) means that the chromosome of Person inherited from his mother
   24% (or mother chromosome) has allele C. The alleles are a, b and null
   25% pchrom(Person,C) means that the chromosome of Person inherited from his father
   26% (or father chromosme) has allele C. The alleles are a, b and null
   27% bloodtype(Person,B) means that Person has bloodtype B
   28
   29% rules for determining the allele of the mother chromosome of a Person on the 
   30% basis of those of his mother 
   31mchrom(Person,a):0.90 ; mchrom(Person,b):0.05 ; mchrom(Person,null):0.05 :- mother(Mother,Person), pchrom(Mother,a   ), mchrom(Mother,a   ).
   32% if both chromosome of Person's mother have allele a, then the mother 
   33% chromosome of Person is a with probability 0.90, b with probability 0.05 and
   34% null with probability 0.05
   35mchrom(Person,a):0.49 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.02 :- mother(Mother,Person), pchrom(Mother,b   ), mchrom(Mother,a   ).
   36mchrom(Person,a):0.49 ; mchrom(Person,b):0.02 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,null), mchrom(Mother,a   ).
   37mchrom(Person,a):0.49 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.02 :- mother(Mother,Person), pchrom(Mother,a   ), mchrom(Mother,b   ).
   38mchrom(Person,a):0.05 ; mchrom(Person,b):0.90 ; mchrom(Person,null):0.05 :- mother(Mother,Person), pchrom(Mother,b   ), mchrom(Mother,b   ).
   39mchrom(Person,a):0.02 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,null), mchrom(Mother,b   ).
   40mchrom(Person,a):0.49 ; mchrom(Person,b):0.02 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,a   ), mchrom(Mother,null).
   41mchrom(Person,a):0.02 ; mchrom(Person,b):0.49 ; mchrom(Person,null):0.49 :- mother(Mother,Person), pchrom(Mother,b   ), mchrom(Mother,null).
   42mchrom(Person,a):0.05 ; mchrom(Person,b):0.05 ; mchrom(Person,null):0.90 :- mother(Mother,Person), pchrom(Mother,null), mchrom(Mother,null).
   43                                                                                                                                           
   44% rules for determining the allele of the father chromosome of a Person on the 
   45% basis of those of his father 
   46pchrom(Person,a):0.90 ; pchrom(Person,b):0.05 ; pchrom(Person,null):0.05 :- father(Father,Person), pchrom(Father,a   ), mchrom(Father,a   ).
   47% if both chromosome of Person's father have allele a, then the father 
   48% chromosome of Person is a with probability 0.90, b with probability 0.05 and
   49% null with probability 0.05
   50pchrom(Person,a):0.49 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.02 :- father(Father,Person), pchrom(Father,b   ), mchrom(Father,a   ).
   51pchrom(Person,a):0.49 ; pchrom(Person,b):0.02 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,null), mchrom(Father,a   ).
   52pchrom(Person,a):0.49 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.02 :- father(Father,Person), pchrom(Father,a   ), mchrom(Father,b   ).
   53pchrom(Person,a):0.05 ; pchrom(Person,b):0.90 ; pchrom(Person,null):0.05 :- father(Father,Person), pchrom(Father,b   ), mchrom(Father,b   ).
   54pchrom(Person,a):0.02 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,null), mchrom(Father,b   ).
   55pchrom(Person,a):0.49 ; pchrom(Person,b):0.02 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,a   ), mchrom(Father,null).
   56pchrom(Person,a):0.02 ; pchrom(Person,b):0.49 ; pchrom(Person,null):0.49 :- father(Father,Person), pchrom(Father,b   ), mchrom(Father,null).
   57pchrom(Person,a):0.05 ; pchrom(Person,b):0.05 ; pchrom(Person,null):0.90 :- father(Father,Person), pchrom(Father,null), mchrom(Father,null).
   58                                                                                                                                            
   59                                                                                                                                            
   60% rules for determining the bloodtype of a Person on the basis of the two 
   61% alleles on his chromosomes
   62bloodtype(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   ).
   63% if both chromosomes of Person have allele a, then the bloodtype of Person
   64% is a with probability 0.90, b with probability 0.03, ab with probability 0.03
   65% and null with probability 0.04
   66bloodtype(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   ).
   67bloodtype(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   ).
   68bloodtype(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   ).
   69bloodtype(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   ).
   70bloodtype(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   ).
   71bloodtype(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).
   72bloodtype(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).
   73bloodtype(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).
   74
   75% the alleles of the parents' chromosomes are chosen randomly
   76mchrom(p_m,a):0.3 ; mchrom(p_m,b):0.3 ; mchrom(p_m,null):0.4.
   77% the mother chromosome of p_m is a with probability 0.3, b with probability 0.3
   78% and null with probability 0.4
   79pchrom(p_m,a):0.3 ; pchrom(p_m,b):0.3 ; pchrom(p_m,null):0.4.			
   80mchrom(p_f,a):0.3 ; mchrom(p_f,b):0.3 ; mchrom(p_f,null):0.4.
   81pchrom(p_f,a):0.3 ; pchrom(p_f,b):0.3 ; pchrom(p_f,null):0.4.
   82
   83% family with 3 people
   84father(p_f, p).
   85mother(p_m, p).			
   86
   87:- end_lpad.

?- prob(bloodtype(p,a),Prob). % what is the probability that the p's bloodtype is a? % expected result 0.3186942939999999 ?- prob(bloodtype(p,b),Prob). % what is the probability that the p's bloodtype is b? % expected result 0.2239874943000002 ?- prob(bloodtype(p,ab),Prob). % what is the probability that the p's bloodtype is ab? % expected result 0.19329257700000035 ?- prob(bloodtype(p,null),Prob). % what is the probability that the p's bloodtype is 0? % expected result 0.16751706690000012 ?- prob(bloodtype(p,a),Prob),bar(Prob,C). % what is the probability that the p's bloodtype is a? % expected result 0.3186942939999999 ?- prob(bloodtype(p,b),Prob),bar(Prob,C). % what is the probability that the p's bloodtype is b? % expected result 0.2239874943000002 ?- prob(bloodtype(p,ab),Prob),bar(Prob,C). % what is the probability that the p's bloodtype is ab? % expected result 0.19329257700000035 ?- prob(bloodtype(p,null),Prob),bar(Prob,C). % what is the probability that the p's bloodtype is 0? % expected result 0.16751706690000012 */