【问题标题】:Prolog If Then rulesProlog If Then 规则
【发布时间】:2015-02-14 06:00:36
【问题描述】:

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

• 如果天气好,现在是夏天,那我就去海滩 • 如果天气好,而且是冬天,那我就去运河划船胜地 • 如果天气不好而且是夏天,那我就去上班 • 如果天气不好而且是冬天,那我去上课 • 如果我去海滩,我会游泳。 • 如果我去运河划船度假村,我会去划船 • 如果我去划船或游泳,那么我会很开心。 • 如果我去工作,我就会赚钱。 • 如果我去上课,那么我会学到一些东西。

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

• 这是美好的一天,现在是夏天。 • 天气不好,现在是冬天。 • 这是美好的一天,现在是冬天。 • 这不是一个好日子,而且是夏天。

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

【问题讨论】:

  • 1.创建您定义的事实。 2. 根据这些事实创建函数 3. 实现这些函数,您就完成了。
  • @gauravdave predicates 将被写入,而不是 functions

标签: prolog artificial-intelligence


【解决方案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 
.

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

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多