Prolog If Then规则

时间:2015-02-14 06:00:36

标签: prolog artificial-intelligence

考虑以下规则定义的决策情况:

•如果这是美好的一天而且是夏天,那么我去海滩 •如果这是美好的一天而且是冬天,那么我会去运河划船度假村 •如果这不是一个美好的一天而且是夏天,那我就去上班 •如果这不是一个美好的一天而且是冬天,那我就去上课 •如果我去海滩,我会游泳。 •如果我去运河划船度假村,那我就去乘船 •如果我乘船或游泳,那我就玩得开心。 •如果我去上班,那我赚钱。 •如果我去上课,那我就学到了一些东西。

遵循以下情况的规则(您对每个情况的结论是什么?):

•这是美好的一天,是夏天。 •这不是一个美好的一天,而是冬天。 •这是美好的一天,是冬天。 •这不是一个美好的一天,而且是夏天。

我无法为上述问题制定规则。请有人帮助我。

1 个答案:

答案 0 :(得分:1)

您有CNL(受控自然语言)规范:规则和情境以(非常)受限制的英语子集表示。因此,任务的重点将是处理语句的“真值”......在SWI-Prolog中,我们可以写:

kb :- rules(Rs), maplist(writeln,Rs), situations(Ls), maplist(writeln,Ls).

rules(Rs) :- tokenize_atom('
    If it is a nice day and it is summer, then I go to the beach.
    If it is a nice day and it is winter, then I go to the canal boating resort.
    If it is not a nice day and it is summer, then I go to work.
    If it is not a nice day and it is winter, then I go to class.
    If I go to the beach, then I swim.
    If I go to the canal boating resort , then I go boat riding.
    If I go boat riding or I swim, then I have fun.
    If I go to work, then I make money.
    If I go to class, then I learn something. 
', L), maplist(downcase_atom,L,D), phrase(rule(Rs), D).

rule([r(C -> A)|Rs]) --> [if], condition(C), [,], [then], consequence(A), [.], rule(Rs).
rule([]) --> [].

situations(S) :- tokenize_atom('
    It is a nice day and it is summer.
    It is not a nice day and it is winter.
    It is a nice day and it is winter.
    It is not a nice day and it is summer.
',L), maplist(downcase_atom,L,D), phrase(situations(S), D).

situations([s(S)|Rs]) --> condition(S), [.], situations(Rs).
situations([]) --> [].

condition(and(A,B)) --> fact(A), [and], condition(B).
condition(or(A,B)) --> fact(A), [or], condition(B).
condition(C) --> fact(C).
consequence(C) --> fact(C).

fact(fact(true, What)) --> [it,is], what(What).
fact(fact(false, What)) --> [it,is,not], what(What).
fact(fact(true, Action)) --> [i], action(Action).

what([A,B,C]) --> [A,B,C].
what([A]) --> [A].

action(L) --> {between(1,6,N),length(L,N)},L.

我们得到了

?- kb.
r((and(fact(true,[a,nice,day]),fact(true,[summer]))->fact(true,[go,to,the,beach])))
r((and(fact(true,[a,nice,day]),fact(true,[winter]))->fact(true,[go,to,the,canal,boating,resort])))
r((and(fact(false,[a,nice,day]),fact(true,[summer]))->fact(true,[go,to,work])))
r((and(fact(false,[a,nice,day]),fact(true,[winter]))->fact(true,[go,to,class])))
r((fact(true,[go,to,the,beach])->fact(true,[swim])))
r((fact(true,[go,to,the,canal,boating,resort])->fact(true,[go,boat,riding])))
r((or(fact(true,[go,boat,riding]),fact(true,[swim]))->fact(true,[have,fun])))
r((fact(true,[go,to,work])->fact(true,[make,money])))
r((fact(true,[go,to,class])->fact(true,[learn,something])))
s(and(fact(true,[a,nice,day]),fact(true,[summer])))
s(and(fact(false,[a,nice,day]),fact(true,[winter])))
s(and(fact(true,[a,nice,day]),fact(true,[winter])))
s(and(fact(false,[a,nice,day]),fact(true,[summer])))
true 
.

现在应该更简单地处理情况,推断行动......