Java Code for an Example Help Desk
createTestFactsWorkingMemory workingMemory throws Exception {
Problem p1 = new Problem101, Problem.REFRIGERATOR; p1.problemType = Problem.NOT_RUNNING;
p1.environmentalData = Problem.CIRCUIT_BREAKER_OFF; workingMemory.insertp1;
Problem p2 = new Problem101, Problem.TV; p2.problemType = Problem.SMOKING;
workingMemory.insertp2; }
In this code we created new instances of the class P roblem and set desired at- tributes. We then use the W orkingM emory method insert to add the ojects to the
working memory collection that Drools maintains. The output when running this example is reformatted to fit the page width:
From rule ’Print all problems’: [Problem: TV
problem type: SMOKING environmental data: NONE]
From rule ’Print all problems’: [Problem: REFRIGERATOR
problem type: NOT_RUNNING environmental data: CIRCUIT_BREAKER_OFF]
Unplug appliance to prevent fire danger: [Problem: TV problem type: SMOKING
environmental data: NONE] Check for power source:
[Problem: REFRIGERATOR problem type: NOT_RUNNING
environmental data: CIRCUIT_BREAKER_OFF] The unit is not is not on and the circuit breaker
is tripped - check the circuit breaker for this room.
96
Systems
It may seem like rule-based expert systems have a lot of programming overhead; that is, it will seem excessively difficult to solve simple problems using production
systems. However, for encoding large ill-structured problems, production systems provide a convenient notation for collecting together what would otherwise be too
large a collection of unstructured data and heuristic rules Programming Expert Sys- tems in Ops5: An Introduction to Rule-Based Programming
, Brownston et al. 1985. As a programming technique, writing rule-based expert systems is not for everyone.
Some programmers find rule-based programming to be cumbersome, while others find it a good fit for solving some types of problems. I encourage the reader to have
some fun experimenting with Drools, both with the examples in this chapter, and the many examples in the Drools distribution package and documentation.
Before starting a moderate or large expert system project, there are several steps that I recommend:
• Write a detailed description of the problem to be solved. • Decide what structured data elements best describe the problem space.
• Try to break down the problem into separate modules of rules; if possible, try to develop and test these smaller modules independently, preferably one
source file per module. • Plan on writing specific rules that test parts of the system by initializing work-
ing memory for specific tests for the various modules; these tests will be very important when testing all of the modules together because tests that work
correctly for a single module may fail when all modules are loaded due to unexpected rule interactions.
Production systems model fairly accurately the stimulus-response behavior in peo- ple. The left-hand side LHS terms represent environmental data that triggers a
response or action represented by the right-hand side RHS terms in production rules. Simple stimulus-response types of production rules might be adequate for
modeling simple behaviors, but our goal in writing expert systems is to encode deep knowledge and the ability to make complex decisions in a very narrow or limited
problem domain. In order to model complex decision-making abilities, we also of- ten need to add higher-level control functionality to expert systems. This higher
level, or meta control, can be the control of which rule modules are active. We did not look at the Drools APIs for managing modules in this chapter but these APIs are
covered in the Drools documentation. Hopefully, this chapter both gave you a quick- start for experimenting with Drools and enough experience to know if a rule-based
system might be a good fit for your own development.
97
98
6 Genetic Algorithms
Genetic Algorithms GAs are computer simulations to evolve a population of chro- mosomes that contain at least some very fit individuals. Fitness is specified by a
fitness function that rates each individual in the population.
Setting up a GA simulation is fairly easy: we need to represent or encode the state of a system in a chromosome that is usually implemented as a set of bits. GA is
basically a search operation: searching for a good solution to a problem where the solution is a very fit chromosome. The programming technique of using GA is useful
for AI systems that must adapt to changing conditions because “re-programming” can be as simple as defining a new fitness function and re-running the simulation.
An advantage of GA is that the search process will not often “get stuck” in local minimum because the genetic crossover process produces radically different chro-
mosomes in new generations while occasional mutations flipping a random bit in a chromosome cause small changes. Another aspect of GA is supporting the evo-
lutionary concept of “survival of the fittest”: by using the fitness function we will preferentially “breed” chromosomes with higher fitness values.
It is interesting to compare how GAs are trained with how we train neural networks Chapter 7. We need to manually “supervise” the training process: for GAs we need
to supply a fitness function and for the two neural network models used in Chapter 7 we need to supply training data with desired sample outputs for sample inputs.