使用prolog的专家系统,有一个永远不会满足的预测

时间:2016-12-05 16:20:02

标签: prolog

xpositive(symbol,symbol).
xnegative(symbol,symbol).
run:-animal_is(X), !,write("\nYour animal may be a"),write(X),nl, nl, clear_facts.
run:-write("\nUnable to determine what"),clear_facts.
positive(X,Y):-write("\npositive(X,Y)0\n"),xpositive(X, Y), ! .
positive(X,Y):-write("\npositive(X,Y)1\n"),not(xnegative(X, Y)),ask(X, Y).
negative(X,Y):-write("\nnegative(X,Y)0\n"),xnegative(X, Y), !.
negative(X,Y):-write("\nnegative(X,Y)1\n"),not(xpositive(X, Y)),ask(X, Y).
ask(X,Y):-write("\nask \n"),write(X),write(" it "),write(Y),readln(R),write("\n0000000000000\n"),new_rules(X,Y,R).
new_rules(X, Y, y):-write("\n01111111111110\n"),assert(xpositive(X, Y)).
new_rules(X, Y, n):-write("\n02222222222220\n"),assert(xnegative(X, Y)), fail.
clear_facts:-retract(xpositive(_,_)) , fail.
clear_facts:-retract(xnegative(_,_)), fail.
clear_facts:-write("\nExit\n").
it_is(mammal):-write("\nit_is(mammal)0\n"),positive( has, hair).
it_is(mammal):-write("\nit_is(mammal)1\n"),positive( does, give_milk).
it_is(bird):-positive( has, feathers).
it_is(bird):-positive( does, fly),positive(does, lay_eggs).
it_is(carnivore):-positive(does, eat_meat).
it_is(carnivore):-positive(has, pointed_teeth),positive(has, claws),positive(has, forward_eyes).
it_is(ungulate):-it_is(mammal),positive( has, hooves).
it_is(ungulate):-it_is( mammal),positive( does, chew_cud).
animal_is(cheetah):-it_is( mammal) , it_is( carnivore),positive( has, tawny_color),positive(has, black_spots).
animal_is( tiger) :-it_is( mammal),it_is( carnivore) , positive( has, tawny_color) , positive( has,black_stripes).
animal_is(giraffe):- it_is(ungulate) ,positive(has, long_neck) , positive(has, long_legs),positive(has, dark-_spots) .

以上是我的prolog代码(从互联网上下载),这些行:

ask(X,Y):-write("\nask \n"),write(X),write(" it "),write(Y),readln(R),write("\n0000000000000\n"),new_rules(X,Y,R).

new_rules(X, Y, y):-write("\n01111111111110\n"),assert(xpositive(X, Y)).

new_rules(X, Y, n):-write("\n02222222222220\n"),assert(xnegative(X, Y)), fail.

表示询问用户问题,然后将新事实断言到系统中。但预测" new_rules"不能满足因为" 011111110"或" 02222220"无法打印到屏幕上,因此系统无法使用答案!!我只是不知道为什么!!非常感谢你的所有建议!!!!!!

1 个答案:

答案 0 :(得分:0)

我不完全确定您的程序应该对所有0,1和2做什么,所以在回答您的建议请求时,这是SWI-Prolog中程序的工作版本(它还有助于指定您正在使用的prolog版本。)

-RdR(PS。如果你喜欢这个答案,不要忘记upvote)

:- dynamic xpositive/2, xnegative/2.

run:- write("\nAnalyse your animal...\n"), 
      clear_facts,
      animal_is(X),
      write("\nYour animal may be a "),
      writeln(X),nl.
run:- write("\nUnable to determine more\n").

clear_facts:-retractall(xpositive(_,_)), retractall(xnegative(_,_)).


positive(X,Y):-xpositive(X, Y), ! .
positive(X,Y):-not(xnegative(X, Y)),ask(X, Y).
negative(X,Y):-xnegative(X, Y), !.
negative(X,Y):-not(xpositive(X, Y)),ask(X, Y).

ask(X,Y):-
  format("~n~w it ~w (y/n)? ",[X,Y]),
  get_single_char(Ch),
  char_code(C,Ch),
  new_rule(X,Y,C).

new_rule(X, Y, 'y'):-assert(xpositive(X, Y)),!.
new_rule(X, Y, 'n'):-assert(xnegative(X, Y)),!,fail.


it_is(mammal):-positive( has, hair).
it_is(mammal):-positive( does, give_milk).
it_is(bird):-positive( has, feathers).
it_is(bird):-positive( does, fly),positive(does, lay_eggs).
it_is(carnivore):-positive(does, eat_meat).
it_is(carnivore):-positive(has, pointed_teeth),
    positive(has, claws),positive(has, forward_eyes).
it_is(ungulate):-it_is(mammal),positive( has, hooves).
it_is(ungulate):-it_is( mammal),positive( does, chew_cud).

animal_is(cheetah):-it_is( mammal) , it_is( carnivore),
    positive( has, tawny_color),positive(has, black_spots).
animal_is( tiger) :-it_is( mammal),it_is( carnivore) , 
    positive( has, tawny_color) , positive( has,black_stripes).
animal_is(giraffe):- it_is(ungulate) ,positive(has, long_neck),
    positive(has, long_legs),positive(has, dark_spots).