GoodGJ
GoodGJ

Reputation: 339

Make some specific rules (if-then-else like) in Prolog Programming

I'am doing Prolog programming for my research.

hypothesis(Patient,cold,Score) :-
    symptom(Patient,fever) -> Score is 10; Score is 0.

hypothesis(Patient,severe_cold,Score) :-
  symptom(Patient,fever) -> Score1 is 10; Score1 is 0,
  symptom(Patient, runny_nose) -> Score2 is 20; Score2 is 0, Score is Score1 + Score2
.

I want to get score for all hypothesis. That is, if symptom(Patient,fever) is checked as ok, then I want to get score for sever_cold (Score 10) and cold (Score 10)both. For another example, if symptom(Patient, runny_nose) is checked as ok, then I want to get score for severe_cold (Score 20). But the Prolog doesn't work...

It's first time I use Prolog.. It's too confusing to handle.

**Edited****** Actually, all of the my code is as below.

%% Lines are without period(.)

diagnosis :-
readln(Line1),
readln(Line2),
readln(Line3),
readln(Line4),
readln(Line5),
readln(Line6),
readln(Line7),
readln(Line8),
readln(Line9),
readln(Line10),
write(Line1),nl,
write(Line2),nl,
write(Line3),nl,
write(Line4),nl,
write(Line5),nl,
write(Line6),nl,
write(Line7),nl,
write(Line8),nl,
write(Line9),nl,
write(Line10),nl.
%% (get_symptom(Line1,[man]) -> write('man!!!!!!!!!')),
%% (get_symptom(Line2,[woman]) -> write('woman!!!!!!!!!')).
%% if A then B else C, (A->B; C)

%% grammar
s --> np, vp.
np --> det, n.
vp --> v, np.
det --> [a].
n --> [man].
v --> [has].
n --> [woman].
n --> [fever].
n --> [runny_nose].

get_symptom(Line,[N]) :- s(Line,[]), n([N],[]).
%% FindSymptom(Line, [Symptom]) : - s(Line,[]), np(_, _, object,[a,
%% Symptom]), n(singular, [Symptom], []).

start :-
write('What is the patient''s name? '),
    readln(Patient), %% Here, this can be used for input of all symtoms
diagnosis,


hypothesis1(Patient,cold,S1),
append([cold/S1/red],[],N1),  write(S1),

write('until...'),

hypothesis2(Patient,severe_cold,S2), write(S2),
append([severe_cold/S2/red],N1,BarList),

write('until...'),


    %% write(Patient,"probably has ",Disease,"."),nl.
hypothesis(Disease),
write(Patient),
write(' probably has '),
write(Disease),
write('.'),

test_barchart(BarList).
start :-
write('Sorry, I don''t seem to be able to'),nl,
    write('diagnose the disease.'),nl.

symptom(Patient,fever) :-
get_symptom(Line1, [fever]);
get_symptom(Line2, [fever]);
get_symptom(Line3, [fever]);
get_symptom(Line4, [fever]);
get_symptom(Line5, [fever]);
get_symptom(Line6, [fever]);
get_symptom(Line7, [fever]);
get_symptom(Line8, [fever]);
get_symptom(Line9, [fever]);
get_symptom(Line10, [fever]).

symptom(Patient,runny_nose) :-
get_symptom(Line1, [runny_nose]);
get_symptom(Line2, [runny_nose]);
get_symptom(Line3, [runny_nose]);
get_symptom(Line4, [runny_nose]);
get_symptom(Line5, [runny_nose]);
get_symptom(Line6, [runny_nose]);
get_symptom(Line7, [runny_nose]);
get_symptom(Line8, [runny_nose]);
get_symptom(Line9, [runny_nose]);
get_symptom(Line10, [runny_nose]).

hypothesis1(Patient,cold,Score_Cold) :-
symptom(Patient,fever) -> Score_Cold is 100; Score_Cold is 0.

hypothesis2(Patient,severe_cold,Score_Severe) :-
(symptom(Patient,fever) -> Score1 is 50; Score1 is 0),
(symptom(Patient, runny_nose) -> Score2 is 50; Score2 is 0),
Score_Severe is Score1 + Score2.

hypothesis(Disease) :-
(hypothesis1(Patient,cold,Score1),
Score1 =:= 100 -> Disease = cold);
(hypothesis2(Patient,severe_cold,Score2),
Score2 =:= 100 -> Disease = severe_cold).

%% make graph for the result
:- use_module(library(pce)).
:- use_module(library(plot/barchart)).
:- use_module(library(autowin)).

test_barchart(BarList):-
new(W, picture),
send(W, display, new(BC, bar_chart(vertical,0,100))),
forall(member(Name/Height/Color,
          BarList),
       (   new(B, bar(Name, Height)),
           send(B, colour(Color)),
           send(BC, append, B)
       )),
send(W, open).
%% [X/100/red, y/150/green, z/80/blue, v/50/yellow]

%% append List
append([], L, L).
append([H|T], L2, [H|L3]):-
append(T, L2, L3).

Here, when I tested my program...

1 ?- start.
What is the patient's name? GJ
|: a man has a fever
|: wow
|: great
|: good
|: nice
|: nothing
|: wow
|: wow
|: wow
|: wow
100until...100until...[GJ] probably has cold.
true 

As It shows, there is only "fever" for symptom.. but all the score is 100... What happened?

Upvotes: 2

Views: 223

Answers (1)

CapelliC
CapelliC

Reputation: 60034

just add required parenthesis (beware to operators order !) and declare what you want. The sum, I guess ?

hypothesis(Patient,severe_cold,Score) :-
  (symptom(Patient,fever) -> Score1 is 10; Score1 is 0),
  (symptom(Patient, runny_nose) -> Score2 is 20; Score2 is 0),
  Score is Score1 + Score2.

edit after read the amended question, seems you're 'over engineering'.

just states separately each hypothesis, and Prolog will serve all solutions while backtracking. Then findall/3 will easily get a list of all pairs Symptom-Score, if a list is required for further processing.

Upvotes: 2

Related Questions