以下是我的决策树的Prolog程序,它根据输入的名称给出输出油或电信或计算机(该人的投资选项:高油风险,电信 - 中等风险,计算机稳定风险), marital_status,收入和抵押贷款。但是我收到了错误! moderate_risk(X): - ask_marital_status(X,Y),Y =已婚,ask_income(X,I), I =< 50000,ask_mortgage(X,Z),Z =< 50000,!。
moderate_risk(X):-ask_marital_status(X,M),M=married,ask_income(X,I), I>50000,!.
moderate_risk(X):-ask_marital_status(X,M), M=single, ask_income(X,I), I=<35000,!.
stable_risk(X):-ask_marital_status(X,M), M=married, ask_income(X,I), I=<50000,
ask_mortgage(X,Z), Z>50000,!.
stable_risk(X):-ask_marital_status(X,M), M=single, ask_income(X,I), I>35000,
ask_age(X,A),A>50,!.
high_risk(X):-ask_marital_status(X,M), M=single, ask_income(X,I), I>35000,
ask_age(X,A),A=<50,!.
invest(X,oil):-stable_risk(X),!.
invest(X,telecommunications):-moderate_risk(X),!.
invest(X,computers):-high_risk(X),!.
main(X,Z):-var(X), write('what is your name?'),read(X), invest(X,Z),!.
main(X,Z):-invest(X,Z),!.
ask_marital_status(X,Y):-marital_status(X,Y).
ask_marital_status(X,Y):-not(marital_status(X,Y)), write('what is your marital
status:married or single?'), read(Y), asserta(marital_status(X,Y)).
ask_income(X,Y):-income(X,Y).
ask_income(X,Y):-not(income(X,Y)),write('what is your annual income?'), read(Y),
asserta(income(X,Y)).
ask_mortgage(X,Z):-mortgage(X,Z).
ask_mortgage(X,Z):-not(mortgage(X,Z)),write('what is your remaining mortgage?'),
read(Z), asserta(mortgage(X,Z)).
ask_age(X,A):-not(age(X,A)).
ask_age(X,A):-not(age(X,A)), write('what is your age?'), read(A), asserta(age(X,A)).
答案 0 :(得分:1)
这是我尝试编译时得到的结果:
Warning: The predicates below are not defined. If these are defined Warning: at runtime using assert/1, use :- dynamic Name/Arity. Warning: Warning: age/2, which is referenced by Warning: /home/bvassile/Documents/code/prolog/marital.pl:80:16: 1-st clause of ask_age/2 Warning: /home/bvassile/Documents/code/prolog/marital.pl:81:19: 2-nd clause of ask_age/2 Warning: income/2, which is referenced by Warning: /home/bvassile/Documents/code/prolog/marital.pl:70:19: 1-st clause of ask_income/2 Warning: /home/bvassile/Documents/code/prolog/marital.pl:71:22: 2-nd clause of ask_income/2 Warning: marital_status/2, which is referenced by Warning: /home/bvassile/Documents/code/prolog/marital.pl:64:4: 1-st clause of ask_marital_status/2 Warning: /home/bvassile/Documents/code/prolog/marital.pl:66:8: 2-nd clause of ask_marital_status/2 Warning: mortgage/2, which is referenced by Warning: /home/bvassile/Documents/code/prolog/marital.pl:75:21: 1-st clause of ask_mortgage/2 Warning: /home/bvassile/Documents/code/prolog/marital.pl:76:24: 2-nd clause of ask_mortgage/2 true.
因此,我按照警告信息的前两行中的建议,将以下内容添加到文件顶部:
:- dynamic age/2, income/2, marital_status/2, mortgage/2.
我还纠正了ask_age/2
定义中的错误。使用此程序(具有更正的格式):
:- dynamic age/2, income/2, marital_status/2, mortgage/2.
moderate_risk(X) :-
ask_marital_status(X,Y),
Y=married,
ask_income(X,I), I=<50000,
ask_mortgage(X,Z), Z=<50000,
!.
moderate_risk(X) :-
ask_marital_status(X,M),
M=married,
ask_income(X,I), I>50000,
!.
moderate_risk(X) :-
ask_marital_status(X,M),
M=single,
ask_income(X,I), I=<35000,
!.
stable_risk(X) :-
ask_marital_status(X,M),
M=married,
ask_income(X,I), I=<50000,
ask_mortgage(X,Z), Z>50000,
!.
stable_risk(X) :-
ask_marital_status(X,M),
M=single,
ask_income(X,I), I>35000,
ask_age(X,A), A>50,
!.
high_risk(X) :-
ask_marital_status(X,M),
M=single,
ask_income(X,I), I>35000,
ask_age(X,A),A=<50,
!.
invest(X,oil) :-
stable_risk(X),
!.
invest(X,telecommunications) :-
moderate_risk(X),
!.
invest(X,computers) :-
high_risk(X),
!.
main(X,Z) :-
var(X),
write('what is your name?'), read(X),
invest(X,Z),
!.
main(X,Z) :-
invest(X,Z),
!.
ask_marital_status(X,Y) :-
marital_status(X,Y).
ask_marital_status(X,Y) :-
not(marital_status(X,Y)),
write('what is your marital status:married or single?'), read(Y),
asserta(marital_status(X,Y)).
ask_income(X,Y) :- income(X,Y).
ask_income(X,Y) :- \+ income(X,Y),
write('what is your annual income?'), read(Y),
asserta(income(X,Y)).
ask_mortgage(X,Z) :- mortgage(X,Z).
ask_mortgage(X,Z) :- \+ mortgage(X,Z),
write('what is your remaining mortgage?'), read(Z),
asserta(mortgage(X,Z)).
ask_age(X,A) :- age(X,A).
ask_age(X,A) :- \+ age(X,A),
write('what is your age?'), read(A),
asserta(age(X,A)).
你会注意到你有一些愚蠢的削减。其中大多数可能是不必要的。特别是在子句末尾的剪辑总是永远不会存在。您可以尝试删除子句末尾的所有剪切,看看这是否会改变程序的行为。
我现在可以使用?- main(boris, Z).
加载和运行代码。
答案 1 :(得分:0)
正如SWI-Prolog告诉你的那样,marital_status/2
没有定义。你可以参考:
ask_marital_status(X,Y) :- marital_status(X,Y).
但你从未在任何地方定义它。