0

我在序言中有以下事实。

p(cold,[flu,high_body_temp,headache,dizzy],0.3],
p(cold,[flu,not high_body_temp,headache,dizzy],0.2],
p(cold,[flu,high_body_temp,not headache,dizzy],0.4],
p(cold,[flu,high_body_temp,headache,not dizzy],0.1],
p(cold,[not flu,high_body_temp,headache,dizzy],0.3],
p(cold,[flu,not high_body_temp,headache,not dizzy],0.3],


p(diarrhea,[headache,not stomachache,dizzy,vomit],0.5),
p(diarrhea,[headache,stomachache,not dizzy,vomit],0.4),
p(diarrhea,[headache,stomachache,dizzy,not vomit],0.2),
p(diarrhea,[not headache,stomachache,dizzy,vomit],0.1),
p(diarrhea,[headache,not stomachache,not dizzy,not vomit],0.1),

以及在运行时生成的列表,例如:

[flu,headache]

我们应该从包含 [flu,headache] 的“真实”元素的两个事实中得到答案,而“不”意味着该元素不存在:

p(cold,[flu,not high_body_temp,headache,not dizzy],0.3],
p(diarrhea,[headache,not stomachache,not dizzy,not vomit],0.1),

答案应该是:

cold = 0.3
diarrhea = 0.1

我如何在 prolog 中编写代码来完成这项工作?请帮助。我完全被困住了。TQ。

4

1 回答 1

0

一旦纠正了语法错误,似乎是一个非常简单的匹配:

:- op(100, fx, not).

p(cold,[flu,high_body_temp,headache,dizzy],0.3).
p(cold,[flu,not high_body_temp,headache,dizzy],0.2).
p(cold,[flu,high_body_temp,not headache,dizzy],0.4).
p(cold,[flu,high_body_temp,headache,not dizzy],0.1).
p(cold,[not flu,high_body_temp,headache,dizzy],0.3).
p(cold,[flu,not high_body_temp,headache,not dizzy],0.3).


p(diarrhea,[headache,not stomachache,dizzy,vomit],0.5).
p(diarrhea,[headache,stomachache,not dizzy,vomit],0.4).
p(diarrhea,[headache,stomachache,dizzy,not vomit],0.2).
p(diarrhea,[not headache,stomachache,dizzy,vomit],0.1).
p(diarrhea,[headache,not stomachache,not dizzy,not vomit],0.1).

answer(L, A, N) :-
    p(A, S, N), maplist(match(L), S).
match(L, not S) :-
    \+ memberchk(S, L).
match(L, S) :-
    memberchk(S, L).

产量

10 ?- answer([flu,headache],A,N).
A = cold,
N = 0.3 ;
A = diarrhea,
N = 0.1 ;
false.
于 2013-03-11T08:28:22.777 回答