I have the below code in Prolog and the problem is that when i run the code it returns for every data the same result.
?- run(Diagnosis).
Diagnosis='There is no problem with anemia'.
The KB is in another file and load it and in start predicate i load patient3 for example and it should return
Diagnosis ='Iron deficiency'
Note that the start predicate is only for check if the code works right for now! When i run this file with trace it shows that first tries the first rules.. It fails in check_RTC when it tries to go to the other rule the predicate symptoms returns symptomNeg..but why? It should take from the beginning all the data and make the checks for the every next rule, until to find the rules that doesn't fail.
:- ['kb_anemia_V4.pl'].
run(Diagnosis) :-
clause(kb_da(patient3,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]),Body),
rule(kb_da(patient3,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]),Diagnosis).
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos), %OK
check_WBC_PLT(FullBloodTest,noPancytopenia),
check_RTC(FullBloodTest,rtcUp2),
check_HBS(ElectricalHb,hbSZero),
Diagnosis = 'Spherocytcocis.'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcUp2),
check_HBS(ElectricalHb,hbsUpZero),
Diagnosis = 'Drepanocytocis.'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcUp2),
check_HBS(ElectricalHb,hbSZero) ;
check_HBS(ElectricalHb,hbsUpZero),
Diagnosis = 'No problem with Spherocytocis or Drepanocytocis.'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvLow),
checkFerritin(FullBloodTest,ferHigh),
checkElectrofHb([Symptoms,ElectricalHb],yesElectr),
Diagnosis='Thalassemia'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvLow),
checkFerritin(FullBloodTest,ferHigh),
checkElectrofHb([Symptoms,ElectricalHb],noElectr),
Diagnosis = 'There is not problem with anemia,probably hemochromatosis,because of High feritin.'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvLow),
checkFerritin(FullBloodTest,ferLow),
check_HCT_Hb_FE([FullBloodTest,PersData],hctLow),
checkRBC([FullBloodTest,PersData],rbcLow),
Diagnosis = 'Iron Deficiency'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvLow),
checkFerritin(FullBloodTest,ferLow),
check_HCT_Hb_FE([FullBloodTest,PersData],hctLow),
checkRBC([FullBloodTest,PersData],rbcHigh),
checkElectrHb([[Symptoms,ElectricalHb]],electro),
Diagnosis = 'Minor Thalassemia'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvLow),
checkFerritin(FullBloodTest,ferLow),
check_HCT_Hb_FE([FullBloodTest,PersData],hctHigh),
checkRBC([FullBloodTest,PersData],rbcHigh),
checkElectrHb([[Symptoms,ElectricalHb]],electro),
Diagnosis = 'Minor Thalassemia'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptNeg),
Diagnosis = 'There is not problem with anemia.'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvNormal),
checkFerritin(FullBloodTest,ferHigh),
checkElectrofHb([Symptoms,ElectricalHb],yesElectr),
Diagnosis='Thalassemia'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvHigh),
check_B12_RBC([PersData,FullBloodTest,SpecBloodTest],b12Low),
Diagnosis = 'B12 Anemia'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos),
check_WBC_PLT(FullBloodTest,noPancytopenia) ,
check_RTC(FullBloodTest,rtcLess2),
check_MCV(FullBloodTest,mcvHigh),
check_Folic(SpecBloodTest,folic),
Diagnosis = 'Folic Acid Anemia'.
rule(kb_da(UserId,DataCom,PersData,
[Symptoms,FullBloodTest,SpecBloodTest,ElectricalHb]), Diagnosis) :-
symptoms(Symptoms,symptPos), %OK
check_WBC_PLT(FullBloodTest,yesPancytopenia),
Diagnosis = 'Check bone marrow'.
symptoms(Symptoms,symptPos) :-
check_symptomList(Symptoms), !.
symptoms(Symptoms,symptNeg).
check_symptomList([H|T]) :-
SymptomList = ['Πονοκέφαλος', 'Αδυναμία', 'Έλλειψη αυτοσυγκέντρωσης',
'Απώλεια βάρους', 'Απώλεια όρεξης', 'Ωχρότητα',
'Επώδυνη σπλήνα', 'Ταχυκαρδία', 'Δύσπνοια','Γλωσσίτιδα','Πληγές',
'Ελλειπής διατροφή', 'πατερας_Στίγμα', 'πατερας_Μεσογειακή',
'μητερα_Στίγμα', 'μητερα_Μεσογειακή', 'Ιδιαίτερο'],
member(H,SymptomList).
check_symptomList([H|T]) :-
check_symptomList(T).
check_WBC_PLT([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT], noPancytopenia) :-
number(WBC), number(PLT),
(WBC > 4500, PLT>150)
; number(WBC), number(PLT),(WBC > 4500, PLT=<150)
; number(WBC), number(PLT),(WBC =< 4500, PLT>150).
check_WBC_PLT([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],yesPancytopenia) :-
number(WBC), number(PLT),
(WBC =< 4500, PLT=<150).
check_RTC([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],rtcLess2):-
number(RTC),RTC=<2.
check_RTC([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],rtcUp2):-
number(RTC),RTC>2.
check_MCV([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],mcvLow):-
number(MCV), MCV=<79.
check_MCV([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],mcvNormal):-
number(MCV), MCV>79,MCV<97.
check_MCV([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],mcvHigh):-
number(MCV), MCV=<97.
check_HBS([HbA1,HbA2,HbS,HbF,Schema_RBC],hbSZero):-
number(HbS), HbS=0,
Schema_RBC='Σφαίρα'.
check_HBS([HbA1,HbA2,HbS,HbF,Schema_RBC],hbSZero):-
number(HbS), HbS=0.
check_HBS([HbA1,HbA2,HbS,HbF,Schema_RBC],hbsUpZero):-
number(HbS), HbS>0,
Schema_RBC=='Δρεπάνι'.
check_HBS([HbA1,HbA2,HbS,HbF,Schema_RBC],hbsUpZero):-
number(HbS), HbS>0.
checkFerritin([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],ferLow):-
number(Ferritin),Ferritin=<20.
checkFerritin([WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],ferHigh):-
number(Ferritin),Ferritin>20.
checkElectrofHb([Symptoms, ElectricalHb],electro):-
Symptoms = ['Πονοκέφαλος', 'Αδυναμία', 'Έλλειψη αυτοσυγκέντρωσης',
'Απώλεια βάρους', 'Απώλεια όρεξης', 'Ωχρότητα',
'Επώδυνη σπλήνα', 'Ταχυκαρδία', 'Δύσπνοια','Γλωσσίτιδα','Πληγές',
'Ελλειπής διατροφή', 'πατερας_Στίγμα', 'πατερας_Μεσογειακή',
'μητερα_Στίγμα', 'μητερα_Μεσογειακή', 'Ιδιαίτερο'],
ElectricalHb = [HbA1,HbA2,HbS,HbF,Schema_RBC],
member('Ιδιαίτερο', Symptoms),
(member('πατερας_Στίγμα', Symptoms),
member('μητερα_Μεσογειακή', Symptoms))
; (member('πατερας_Μεσογειακή', Symptoms),
member('μητερα_Στίγμα', Symptoms))
; (member('πατερας_Μεσογειακή', Symptoms),
member('μητερα_Μεσογειακή', Symptoms)),
number(HbF),HbF>2,HbF<9,
number(HbA1), HbA1<5.
check_HCT_Hb_FE([[WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],[Hlikia,Filo,Egkimosini]],hctLow):-
(Filo='male',number(Hct),number(Hb),number(Fe),Hct<40,Hb<14,Fe<55
; Filo='female',number(Hct),number(Hb),number(Fe),Hct<36,Hb<10,Fe<45
; Filo='male',Egkimosini=yes,number(Hct),number(Hb),number(Fe),Hct<36,Hb<12,Fe<55).
check_HCT_Hb_FE([[WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],[Hlikia,Filo,Egkimosini]],hctHigh).
checkRBC([[WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],[Hlikia,Filo,Egkimosini]],rbcLow):-
(Filo='male',number(RBC),RBC<4.5);
(Filo='female',number(RBC),RBC<3.8).
checkRBC([[WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],[Hlikia,Filo,Egkimosini]],rbcHigh):-
(Filo='male',number(RBC),RBC>6);
(Filo='female',number(RBC),RBC>4.9).
checkElectrHb([Symptoms, ElectricalHb],electro):-
Symptoms = ['Πονοκέφαλος', 'Αδυναμία', 'Έλλειψη αυτοσυγκέντρωσης',
'Απώλεια βάρους', 'Απώλεια όρεξης', 'Ωχρότητα',
'Επώδυνη σπλήνα', 'Ταχυκαρδία', 'Δύσπνοια','Γλωσσίτιδα','Πληγές',
'Ελλειπής διατροφή', 'πατερας_Στίγμα', 'πατερας_Μεσογειακή',
'μητερα_Στίγμα', 'μητερα_Μεσογειακή', 'Ιδιαίτερο'],
ElectricalHb = [HbA1,HbA2,HbS,HbF,Schema_RBC],
member('πατερας_Στίγμα', Symptoms);
member('μητερα_Στίγμα', Symptoms);
member('μητερα_Μεσογειακή', Symptoms);
member('πατερας_Μεσογειακή', Symptoms),
number(HbF),HbF>2,HbF<9,
number(HbA1), HbA1<1.
check_B12_RBC([[Hlikia,Filo,Egkimosini],[WBC,RTC,MCV,RBC,FE,Ferritin,TIBC,Hct,Hb,PLT],[B12,Folic_acid,tSH,Kreatinin]],b12Low):-
(Filo='male',number(RBC),RBC>6);
(Filo='female',number(RBC),RBC>4.9),
number(B12), B12<130.
check_Folic([B12,Folic_acid,tSH,Kreatinin],folic):-
number(Folic_acid), Folic_acid<3.1.
The kb code file is
:- dynamic kb_da/4.
kb_da(patient1,['Ιωάννης Ιωάννου','Φλέμινγκ3 71410 Ηράκλειο','2810123456'],
[50,male,na],[['Πονοκέφαλος','Aδυναμία','Έλλειψη αυτοσυγκέντρωσης'],_,_,_]).
%Sidiropenik Anaimia
kb_da(patient2,
['Δημήτρης Παπαδημητρίου','Αρχαγγέλου 17 Κοκκινοτριμιθιά Λευκωσίας','0035797273515'],
[27,male,na], [['Πονοκέφαλος','Κούραση','Ταχυκαρδία','Δύσπνοια'],
[4800,1,75,4.3,45,18,280,33,12,125],_,_]).
kb_da(patient3,['Δέσποινα Μπασδέκη','Πανός 19 71410 Ηράκλειο',6955457845],[24,female,no],
[['Πονοκέφαλος','Έλλειψη αυτοσυγκέντρωσης','Ελλειπής διατροφή','Δύσπνοια','Ταχυκαρδία'],
[4300,1,75,3.7,41,18,250,30,10,180],_,_]).
kb_da(patient4,['Δήμητρα Περπερίδου','Καραμανώλη 5 55142 Θεσσαλονίκη',6977458521],[32,female,yes],
[['Πονοκέφαλος','Έλλειψη αυτοσυγκέντρωσης','Ελλειπής διατροφή','Δύσπνοια','Ταχυκαρδία','Ταχυκαρδία'],
[4800,1,72,3.7,50,15,280,32,10,180],_,_]).
%Stigma Mesogeiaki Anaimia
kb_da(patient5,['Manolis Manolaou','Epaminondos 24 55142 Θεσσαλονίκη',6985245125],[50,male,na],
[['Πονοκέφαλος','Δύσπνοια','Ταχυκαρδία','Ταχυκαρδία','μητερα_Στίγμα'],
[4800,1,78,7,45,15,320,39,13,125],_,_]).
kb_da(patient6,['Manolis Pavlou','Venizelou 24 59100 Veroia',6977148545],[25,male,na],
[['Πονοκέφαλος','Δύσπνοια','Ταχυκαρδία','πατερας_Μεσογειακή',],
[4800,1,78,7.8,45,15,320,39,13,155],_,_]).
kb_da(patient7,['Maria Papadopoulou','Venizelou 24 59100 Veroia',6985478452],[42,female,na],
[['Πονοκέφαλος','Δύσπνοια','Ταχυκαρδία','Ελλειπής διατροφή','μητερα_Μεσογειακή'],
[4800,1,78,7.8,45,15,320,39,13,155],_,_]).
% Mesogeiaki Anaimia
kb_da(patient8,['Μαρία Παπανικολάου','Βετσοπούλου 24 59100 Αλεξάνδρεια',6974582545],[42,female,no],
[['Πονοκέφαλος','Δύσπνοια','Αδυναμία','Ταχυκαρδία','Ελλειπής διατροφή','πατερας_Στίγμα','μητερα_Μεσογειακή','Ιδιαίτερη'],
[4800,1,85,5.5,45,22,320,39,13,155],_,
[3,hbA2,hbS,5,schema_RBC]]).
kb_da(patient9,['Γεώργιος Παπαδήμου','Παπανικολάου 58 47854 Βόλος',6974585124],[85,male,na],
[['Πονοκέφαλος','Ωχρότητα','πατερας_Μεσογειακή','πατερας_Μεσογειακή','Ιδιαίτερο'],
[4800,1,85,5.5,45,22,320,39,13,155],_,
[3,hbA2,hbS,5,schema_RBC]]).
kb_da(patient10,['Γεώργιος Καρανικόλας','Χαριλάου Τρικούπη 58 56954 Θεσσαλονίκη',6974585412],[45,male,na],
[['Πονοκέφαλος','Ωχρότητα','πατερας_Μεσογειακή','μητερα_Στίγμα','Ιδιαίτερη'],
[4400,1,75,5.5,45,22,320,39,13,125],_,
[4,hbA2,hbS,2,schema_RBC]]).
%Β12
kb_da(patient11,['Παναγιώτης Μπομπότης','Αιγαίου 3 17122 Ν.Σμύρνη Αττικης ',6974585123],[22,male,no],
[['Πονοκέφαλος','Ωχρότητα','Απώλεια βάρους','Απώλεια όρεξης'],
[4400,1,100,3.5,45,22,320,39,13,125],
[125,3.5,tSH,creatinine],_]).
kb_da(patient12,['Δήμητρα Καρανικόλα','Χαριλάου Τρικούπη 58 56954 Θεσσαλονίκη',6974585412],[24,female,na],
[['Απώλεια βάρους','Απώλεια όρεξης'],
[4800,1,100,3.7,45,22,320,39,13,170],
[125,2,tSH,creatinine],_]).
%Foliko O3i
kb_da(patient13,['Κίμωνας Ιωαννίδης','Βρυλήσια Αττικής',6984575625],[65,male,no],
[['Απώλεια βάρους','Απώλεια όρεξης'],
[4800,1,100,5.5,45,22,320,39,13,170],
[125,2,tSH,creatinine],_]).
kb_da(patient14,['Ιουλια Αναστασιάδου','Κουντουριωτη 3 54625 Θεσσαλονίκη',6958475202],[35,female,yes],
[['Απώλεια βάρους','Ωχρότητα','Απώλεια όρεξης'],
[4800,1,100,3.7,45,22,320,39,13,170],
[145,2,tSH,creatinine],_]).
%Drepanokitarwsi
kb_da(patient15,['Βασίλης Παπακωσταντίνου','Μητροπόλεως 18 74125 Ν. Ιωνία',6955447751],[35,male,no],
[['Απώλεια βάρους','Απώλεια όρεξης'],
[4800,2,100,3.7,45,22,320,39,13,170],
[hbA1,hbA2,1,hbF,'Δρεπάνι'],_]).
kb_da(patient16,['Θοδωρής Onir','Καραμανλή 45 74125 Ν.Ιωνία',6977457845],[38,male,no],
[['Απώλεια βάρους','Ωχρότητα','Απώλεια όρεξης'],
[4400,2,100,3.7,45,22,320,39,13,170],
[hbA1,hbA2,2,hbF,'Δρεπάνι'],_]).
%Σφαιροκυττάρωση
kb_da(patient17,['Όλγα Σαμανίδου','Κηφισιάς 45 74125 Κηφισιά',6985544125],[29,female,yes],
[['Πονοκέφαλος','Δύσπνοια','Αδυναμία','Απώλεια βάρους','Ωχρότητα','Απώλεια όρεξης'],
[4900,2,100,3.2,45,28,320,39,13,120],
[hbA1,hbA2,0,hbF,'Σφαίρα'],_]).
kb_da(patient18,['Σοφία Χατζηπαυλή','Γεωργίου Βλαστού 12 45785 Κώς',6974545455],[18,female,no],
[['Πονοκέφαλος','Αδυναμία','Ωχρότητα','Απώλεια όρεξης'],
[4900,2,100,3.2,45,28,320,39,13,170],
[hbA1,hbA2,0,hbF,'Σφαίρα'],_]).
%Den iparxei provlima
kb_da(patient19,['Βλάση Μπονάτσο','Παπαμιχαήλ 54 14585 Αθήνα',6985545212],[58,male,no],
[['Αδυναμία','Ωχρότητα','Απώλεια όρεξης'],
[4900,2,100,3.2,45,28,320,39,13,170],
[hbA1,hbA2,0,hbF,'Κανονικό'],_]).
kb_da(patient20,['Μαρία Παπαμιχαήλ','Κονίτσης 54 59100 Βέροια',6984525154],[42,female,no],
[['Αδυναμία','Ωχρότητα','Απώλεια όρεξης'],
[4900,2,99,3.2,45,28,320,45,13,170],
[hbA1,hbA2,2,hbF,'Κανονικό'],_]).
Here are some remarks.
1mo, always take the warnings a system generates seriously and remove them. You have all kinds of singleton variables some of which are problems and some are not. And in fact, in rule
check_HCT_Hb_FE
there isFE
andFe
.2do, it is not clear to me that using
number/1
makes much sense. Arithmetic predicates like theFe<45
are testing for a valid value anyway and produce an error in case something is wrong.3tio, there is some odd syntax (which is otherwise unrelated). Search for
person6
and,]
.4to, to localize the problem I added the following lines to your program:
In this case it was quite easy since you said that Iron deficiency is expected:
The highly generalized program already failed, so it was evident that the problem was within that goal
check_HCT_Hb_FE/2
in particular the failing goal wasHowever, the three conditions
Hct<36,Hb<10,Fe<45
are not true in this case, in fact they are instantiated to30<36,10<10,41<45
and10<10
does not hold.