Study Notes on building an Expert System Using Prolog (2)

Source: Internet
Author: User
Tags knowledge base
Learning notes for building an Expert System Using Prolog (2) II. the inference engine using PROLOG has a built-in backward chain inference engine, which is used to implement some functions of the expert system. The rules of PROLOG indicate knowledge, and the inference engine is used to obtain answers. Each rule has a general goal and multiple sub-goals. The inference engine proves or denies each target. The reasoning result is not deterministic. The rule structure and reasoning policy are suitable for many expert systems. You only need to improve the dialog mechanism with the user to create a simple expert system. The following describes how to implement a simple "bird recognition Expert System ". (1) bird identification system (1) Rule format if premise 1, premise 2 ,... then concluded that the if part is also called the "Left half" (LHS), and the then part is also called the "right half" (RHs ). Note that the format of the prolog rule is then-if, LHS, and RHS, which are the opposite: Conclusion:-premise 1, premise 2 ,... (2) Bird (laysan_albatross):-family (albatross), color (white ). it is equivalent to: if it belongs to the Watson family, and it is a white then, it is the Watson of Shan, and it also defines the rules of the Black-foot Watson and the Swan: Bird (black_footed_albatross):-family (albatross ), color (dark ). bird (whistling_swan):-family (SWAN), voice (muffled_musical_whistle ). bird (trumpeter_swan):-family (SWAN), voice (loud_trumpeting ). rule hierarchies: Based on family (Order): Order (tubenose):-nostrils (external_tubular), live (at_sea), Bill (hooked ). order (waterfowl):-feet (webbed), Bill (flat ). family (albatross):-order (tubenose), size (large), wings (long_narrow ). family (Swan):-order (waterfowl), neck (long), color (white), flight (ponderous ). now, "section" is a rule. (3) Rules for other relationships: Bird (canada_goose):-family (goose), season (Winter), country (united_states), head (black), cheek (white ). the rule is: the Canadian black geese belong to the goose family, black heads and white necks, and the winter is in the United States. Bird (canada_goose):-family (goose), season (summer), country (Canada), head (black), cheek (white ). the rule is: the Canadian black geese belong to the goose family, black heads and white necks, and are in Canada in summer. The geographic location can be set to a multi-level rule: Country (united_states):-region (mid_west ). country (united_states):-region (south_west ). country (united_states):-region (north_west ). country (united_states):-region (mid_atlanta ). country (Canada):-province (Ontario ). country (Canada):-province (Quebec ). region (new_england):-State (x), member (x, [mascript usetts, Vermont,...]). region (south_east):-State (x), member (x, [Florida, Mississippi ,.. ...]). (2) If the user interface has a user interface, the system can obtain information as needed without forcing the user to enter the information in advance. The predicate ask provides this function. For example, ask for bird features: Eats (x):-ask (eats, X ). feet (x):-ask (feet, X ). wings (x):-ask (wings, X ). neck (x):-ask (Neck, X ). color (x):-ask (color, X ). the lite version of ask is: Ask (ATTR, Val):-write (ATTR: Val), write ('? '), Read (yes). If the user answers "yes", the predicate read is successful, otherwise it fails. Example :? -Bird (x). nostrils: external_tubular? Yes. Live: at_sea? Yes. Bill: Hooked? Yes. Size: Large? Yes. Wings: long_narrow? Yes. Color: White? Yes. x = laysan_albatross use the new predicate known/3 to remember the user's answer: Ask (A, V):-known (yes, A, v), % succeed if true !. % Stop lookingask (A, V):-known (_, A, v), % fail if false !, Fail. Ask (A, V):-write (A: V), % ask User Write ('?: '), Read (Y), % get the answer asserta (known (Y, A, v), % remember it y = Yes. % succeed or fail: Use the menu interface to write the answers on the menu, which is selected by the user. The predicate menuask is used to do this. Size (x):-menuask (size, X, [large, plump, medium, small]). flight (x):-menuask (flight, X, [ponderous, agile, flap_glide]). simple implementation of menuask: menuask (A, V, menulist):-write ('What is the value for '), write (A), write ('? '), NL, write (menulist), NL, read (x), check_val (X, A, V, menulist), asserta (known (yes, A, x )), X = v. check_val (X, A, V, menulist):-member (x, menulist ),!. Check_val (X, A, V, menulist):-write (x), write ('is not a legal value, try again. '), NL, menuask (A, V, menulist ). A simple self-built shell: top_goal (x):-bird (X ). solve:-Abolish (known, 3), define (known, 3), top_goal (x), write ('The answer is '), write (x), NL. solve:-write ('no answer found. '), NL. the built-in predicate abolish cleans up the system blackboard to start new consulting. ● Solve-start consultation; ● ask-ask the user, and remember to answer; ● menuask-ask the user to select from the menu; ● support other predicates that match the preceding three predicates. The predicates in the Knowledge Base include: ● top goal of the top_goal-knowledge base; ● rules (such as bird, order, family, and Region) established to identify and select knowledge base objects ); ● attribute rules (such as size, color, eats, and wings) must be provided by the user. ● multi-value rules-define which attributes can have multiple values. To use this shell in the prolog interpretation environment, you must load the shell and bird database into the memory :? -Consult (native). Yes? -Consult ('birds. kb'). Yes? -Solve. nostrils: external_tubular ?... This shell can be further enhanced to the highest level of command environment, called "go ". Go has three commands: • load-load the knowledge base into the memory. • Consult-consulting knowledge base. • Quit-exit the shell. Go:-greeting, repeat, write ('>'), read (x), Do (x), x = quit. greeting:-write ('this is the native Prolog shell. '), NL, write ('enter load, consult, or quit at the prompt. '), NL. do (load):-load_kb ,!. Do (consult):-solve ,!. Do (quit). Do (x):-write (x), write ('is not a legal command.'), NL, fail.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.