我在prolog中有以下事实。
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]
我们应该从两个事实得到答案包含[流感,头痛]的'真实'元素,而'不'意味着元素不存在:
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
答案 0 :(得分: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.