630 likes | 911 Views
Rule-Based Systems. CIS 488/588 Bruce R. Maxim UM-Dearborn. Rule-Based Systems. Also known as “production systems” or “expert systems” Rule-based systems are one of the most successful AI paradigms Used for synthesis (construction) type systems
E N D
Rule-Based Systems CIS 488/588 Bruce R. Maxim UM-Dearborn
Rule-Based Systems • Also known as “production systems” or “expert systems” • Rule-based systems are one of the most successful AI paradigms • Used for synthesis (construction) type systems • Also used for analysis (diagnostic or classification) type systems
Rule Format Label Rn if condition1 condition2 … then action1 action2 …
Weaknesses of Expert Systems • Require a lot of detailed knowledge • Restrict knowledge domain • Not all domain knowledge fits rule format • Expert consensus must exist • Knowledge acquisition is time consuming • Truth maintenance is hard to maintain • Forgetting bad facts is hard
Generic System Components • Global Database • content of working memory (WM) • Production Rules • knowledge-base for the system • Inference Engine • rule interpreter and control subsystem
Inference Engine Rulebase new rule Conflict Resolution Execute Match new fact Factbase
Data Structure • A major concern for the rulebase is the data structure used for internal storage • If the rules are stored separately from the system in a declarative fashion they must be loaded at run-time and placed in some searchable data structure • For data driven programs efficient searching is essential and this is a function of the date structure used (lists, trees, hash tables, etc.)
Forward Chaining Procedure • Do until problem is solved or no antecedents match Collect the rules whose antecedents are found in WM. If more than one rule matches use conflict resolution strategy to eliminate all but one Do actions indicated in by rule “fired”
Synthesis Systems • Used to build things • Tend to use forward chaining • Often data driven • Often make use of breadth first search • Tend looks at all facts before proceeding
Backward Chaining Given goal g as input find the set of rules S that determine g if a set of rules does not equal empty set then loop choose rule R make R’s antecedent the new goal (ng) if new goal is unknown then backchain (ng) else apply rule R until g is solved or S is equal to empty set else consult user
Analysis System • Commonly used for diagnostic problems or classification problems • Tend to use backward chaining • Often goal driven • Often depth-first search • Tend to focus on one hypothesis (path) at a time (easier for humans)
Conflict Resolution Strategies • Physically order the rules • hard to add rules to these systems • Data ordering • arrange problem elements in priority queue • use rule dealing with highest priority elements • Specificity or Maximum Specificity • based on number of antecedents matching • choose the one with the most matches
Conflict Resolution Strategies • Recency Ordering • Data (based on order facts added to WM) • Rules (based on rule firings) • Context Limiting • partition rulebase into disjoint subsets • doing this we can have subsets and we may also have preconditions • Randomly Selection • Fire All Application Rules
Deductive Systems • Defintion • the rules in an expert system can be matched using forward or backward chaining • Sometimes it is desirable to alternate the forward and backward chaining strategies in the same system
Hybred Inference Strategy repeat • let user enter facts into factbase (WM) • select a a goal G based on current problem state • call bchain(G) to establish G Until problem is solved
Inference Net 6 mon up MM R4 risk lower discount R2 Fed expans R5 stock 6 mon down R1 decreas reserve short term R3
Answering Questions • Most expert systems users insist on being able to request an explanation of how the ES reached its results • This is often accomplished using traces of the rule matching and firing order • The rules themselves can be mapped to an “and/or” type decision tree
And/Or Tree Goal: Acquire TV Steal TV Buy TV and Earn Money Get Job
Explanations • To answer a “how” question identify the immediate sub-goals for the goal in question and report them • To answer a “why” question identify the super goals for a given goal and report them
Disadvantages • Basic rule-based systems do not: • Learn • Use multi-level reasoning • Use constraint exposing models • Look at problems from multiple perspectives • Know when to break their own rules • Make use of efficient matching strategies
Extensions • Rule-Based systems often interface with external systems • Standard declarative rules support sensors (inputs) as symbols and output (effectors) as function calls • Rules are tested against working memory using assertions (pre-conditions) to determine when they are eligible to fire (requires post processing)
VP Expert Rules !RULES BLOCK RULE 1 IF Married = Yes AND Savings = Ok AND Insurance = Yes THEN Advice = Invest BECAUSE "Rule 1 determines if married should invest"; RULE 3 IF Savings <> Ok OR Insurance = No THEN Advice = Do_Not_Invest CNF 80 BECAUSE "Rule 3 determines automatic 'not invest'";
VP Expert Control Block ! ACTIONS BLOCK ACTIONS DISPLAY "Welcome to the Investment Advisor !!“ FIND Advice DISPLAY "The best advice we have for you is to {#Advice}.“ FIND Type SORT Type DISPLAY "Your top two choices are:“ FOR X = 1 to 2 POP Type, One_type DISPLAY “Investment strategy to consider is {#One_type}.“ END;
VP Expert Statements ! STATEMENTS BLOCK ASK Married: "Are you married ?"; CHOICES Married: Yes, No; ASK Bank: "What is the size of your emergency fund ?"; ASK Investment: "Enter your confidence in at least two investments:"; CHOICES Investment: Stocks, Bonds, Money_Market, Futures; PLURAL : Investment, Type; ! Declares Investment and Type as plural variables
Knowledge Acquisition • RBS require extremely detailed domain-specific knowledge • Much of the rulebase content consists of “rules of thumb” used by human experts to solve specific problems • This takes a long time and skillful listening on the part of the knowledge engineer (knowledge acquisition bottleneck)
Expert Systems • First generation • Domain experts encoded knowledge as if..then rules in some programming language • Very brittle and hard to maintain • Second generation • Rulebase separated from program code and written in declarative form • More robust and a little easier to maintain
RBS Advantages • Simplicity • Rule syntax is based on human reasoning • Modularity • Easy to add and subtract rules • Flexibility • Easy to add and process symbols in WM • Applicability • Many problems can be formulated as rules
RBS Disadvantages • Expressiveness • Hard to predict RBS behavior from rules alone • Power • Rule behavior often restricted to syntax representation • Efficiency • Matching can be time consuming • Suitability • Rules don’t work for every problem domain
RBS Game Development – 1 • Usually one person is the programmer, knowledge engineer, and domain expert (better to be 3 separate people) • The knowledge-based system is implemented first and the rules come later in the game development cycle • Dividing the task in two (code writing and rule offering) makes it easier for one person to handle
RBS Game Development – 2 • Dividing the task provides demonstrates some of the advantages of second generation expert systems • Reusability (RBS module can be used in other programs) • Debugging (when errors occur WM contents can be captures and the offending rule easily identified and modified)
RBS Efficiency • Hard-coding and compiling may be faster than interpretation for processing linear lists of rules • Declarative rules do not need to be organized in linear lists, but are limited to simple actions (e.g. setting symbol values) • Can hardcode problem independent facts in a small database and augment this by dynamic data structures as needed
Suitable Problems • Make sure you have access to sufficient expert knowledge to capture in the rules • If your code has long strings of if statements, they could be encoded as declarative rules • RBS work bests when there a several (flexible) rule sequences can be used to reach a goal state • RBS work well to implement control strategies in deterministic, reactive environments
Brutus • First generation procedural expert system • Each action is expressed as a C++ if statement • Using steering behaviors to follow players or stop at their feet
Wall Following Behavior • If no wall is present and not already following another wall then move forward • If wall in front then turn away from it • If wall on the side and not in front then move forward to follow the wall • If no wall is present and a wall was previously being followed then turn toward the side where the last wall was
Note: context variable “following wall” used to distinguish between case 1 and 4.
RBS Considerations • It will take longer to implement than approach used for Brutus or Marvin • The RBS approach has the potential for reuse in other parts of the game (e.g. motion) • Forward chaining approach will be used • Good for behavioral simulation in reactive systems • Rule action can be procedural does not need to be declarative (like in backward chaining) • Declarative conditions can be loaded from a file
Rule Simplifications • Keep number of rules used to a minimum • Assume rules listed in priority order • Later rules assume that earlier rule conditions are already true • Allow for multiple rule actions on a match rather than repeating matches for each action on at a time
Working Memory • XML is used to initialize FEAR modules • Working memory -<memory> <Symbol name="sideWall” /> <Symbol name="following" value="false" ground="true" /> </memory>
Symbols • There are two types of symbols • Native symbols refer to sensors or effectors outside the RBS (e.g. “sideWall” ) • Internal symbols are used inside the RBS only (e.g. “following” ) • This allows functional extensions to be handled indirectly • Native symbols are set automatically before interpreter cycle starts
Rulebase -<rulebase> -<Rule> -<conditions> <Symbol name="following" value="false" /> <Symbol name="frontWall" value="false" /> <Symbol name="sideWall" value="false" /> </conditions> -<actions> <Symbol name="following" value="false" /> </actions> </Rule> <rulebase>
Interface • Must allow data to be passed to module at run-time • This mainly tells the systems the location of the native symbols and how to execute native actions • Run-time data is synchronized with static declarations
Synchronization • Variables can be registered with the module by passing pointers (first parameter) along with their names (second parameter) SetSensor(&SensorSidewall, “sideWall”); • Procedural actions are implemented as functors (classes that store functions as a means of implementing callback functions without virtual calls)
Accessor Methods • For Boolean values void Set(const string& symbol, const bool value); bool Get(const string& symbol) const; • Adding Rules on the Fly void AddCondittion(const string& symbol, const bool value); void SetAction(const string& action);
Implementation • Reuses containers from the C++ standard template library to store the RB and WM • Rules are easily edited in the XML file • Interpreter processes the rules using their positional priorities • The C++ RBS module is available at http://AiGameDev.com/